KEMBAR78
TypedDict incompatible type message more understandable by Axeinator · Pull Request #10326 · python/mypy · GitHub
Skip to content

Conversation

@Axeinator
Copy link
Contributor

@Axeinator Axeinator commented Apr 15, 2021

Description

Added a section to the incompatible_argument function in mypy.messages that is executed when trying to add an incompatible type to a TypedDict. Required adding some imports from mypy.nodes to allow for the isinstance to work.

Fixes #10253

Test Plan

Verified changes with the sample code provided in the issue as well as some other sample TypedDict implementations, below.

Foo = TypedDict('Foo', {'a': int})
blah: Foo = {'a': 4}
blah['a'] = ["r", 3]

Foo = TypedDict('Foo', {'a': str})
blah: Foo = {'a': 'lol'}
blah['a'] = 1

Also modified the testCannotSetItemOfTypedDictWithIncompatibleValueType test to check for the new error message.

@Axeinator Axeinator changed the title TypedDict incompatible type message more understandable (#10253) TypedDict incompatible type message more understandable Apr 15, 2021
@Axeinator Axeinator marked this pull request as draft April 15, 2021 06:36
@Axeinator Axeinator marked this pull request as ready for review April 15, 2021 13:58
@Axeinator
Copy link
Contributor Author

Hi, can this PR be reviewed? Thanks!

@TH3CHARLie TH3CHARLie requested review from JukkaL and TH3CHARLie April 20, 2021 02:00
TaggedPoint = TypedDict('TaggedPoint', {'type': str, 'x': int, 'y': int})
p = TaggedPoint(type='2d', x=42, y=1337)
p['x'] = 'y' # E: Argument 2 has incompatible type "str"; expected "int"
p['x'] = 'y' # E: Argument "x" has incompatible type "str"; expected "int"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the message purposed in the original issue 'Value of "foo" has incompatible type "str"; expected "int"' is more clear since x is not an argument.

Error message for TypedDict more consistent with what was in original issue (python#10253)
@Axeinator Axeinator requested a review from TH3CHARLie April 20, 2021 02:11
Copy link
Collaborator

@TH3CHARLie TH3CHARLie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, let's wait @JukkaL for a final review since error messages are pervasive

@TH3CHARLie
Copy link
Collaborator

OK, let's merge this

@TH3CHARLie TH3CHARLie merged commit 09bf250 into python:master Apr 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bad error message for wrong type of typeddict setitem

2 participants