X Tutup
The Wayback Machine - https://web.archive.org/web/20210421213309/https://github.com/python/mypy/pull/10326
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypedDict incompatible type message more understandable #10326

Merged
merged 6 commits into from Apr 21, 2021

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 Apr 15, 2021
@Axeinator Axeinator marked this pull request as ready for review Apr 15, 2021
@Axeinator
Copy link
Contributor Author

@Axeinator Axeinator commented Apr 19, 2021

Hi, can this PR be reviewed? Thanks!

@TH3CHARLie TH3CHARLie requested review from JukkaL and TH3CHARLie Apr 20, 2021
@@ -730,7 +730,7 @@ p['x'] = 1
from mypy_extensions import TypedDict
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"

This comment has been minimized.

@TH3CHARLie

TH3CHARLie Apr 20, 2021
Collaborator

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 (#10253)
@Axeinator Axeinator requested a review from TH3CHARLie Apr 20, 2021
Axeinator added 2 commits Apr 20, 2021
Copy link
Collaborator

@TH3CHARLie TH3CHARLie left a comment

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

@TH3CHARLie
Copy link
Collaborator

@TH3CHARLie TH3CHARLie commented Apr 21, 2021

OK, let's merge this

@TH3CHARLie TH3CHARLie merged commit 09bf250 into python:master Apr 21, 2021
7 checks passed
7 checks passed
Comment
Details
Run (0)
Details
build (windows-py37-32)
Details
Run (1)
Details
build (windows-py37-64)
Details
Run (2)
Details
Travis CI - Pull Request Build Passed
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

2 participants
X Tutup