-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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 update() does not accept TypedDict with compatible subset keys #6462
Comments
This is a pretty subtle issue. The However, the same reasoning could be applied to Not sure what should be done. If we want to be safe, we could require that the argument to An explicit cast to |
Hi, I get the same mypy error with the following code: from mypy_extensions import TypedDict
class Movie(TypedDict):
name: str
year: int
m1 = Movie(name='Blade Runner', year=1982)
m2 = Movie(name='Star Wars', year=1977)
m1.update(m2)
Is this something related to the described issue? Thanks! |
Yes, I think this is an aspect of the same problem. In this case however it looks to me as a bug, mypy is overly strict in your case, since we know |
Hello, the bug in #6462 (comment) just bit me and is blocking my adoption of Cheers, |
I've tried two ways of doing this. One is to iterate over the keys and assign (doesn't work), but this version seems to be the only thing that mypy is happy with from mypy_extensions import TypedDict
class Movie(TypedDict):
name: str
year: int
m1 = Movie(name='Blade Runner', year=1982)
m2 = Movie(name='Star Wars', year=1977)
m1['name'] = m2['name']
m1['year'] = m2['year'] |
It doesn't even let you update a class TestType(TypedDict):
prop: int
def doThing(foo: TestType):
foo.update(foo) # (parameter) foo: TestType
# Argument 1 to "update" of "TypedDict" has incompatible type "TestType"; expected "TypedDict({'prop'?: int})" |
Method for updating the dictionary is ignored, as the update method of typedict have some problems (python/mypy#6462).
Method for updating the dictionary is ignored, as the update method of typedict have some problems (python/mypy#6462).
Method for updating the dictionary is ignored, as the update method of typedict have some problems (python/mypy#6462).
Method for updating the dictionary is ignored, as the update method of typedict have some problems (python/mypy#6462).
Fixes #9408 Fixes #4122 Fixes #6462 Supersedes #13353 This PR enables two similar technically unsafe behaviors for TypedDicts, as @JukkaL explained in #6462 (comment) allowing an "incomplete" TypedDict as an argument to `.update()` is technically unsafe (and a similar argument applies to `**` syntax in TypedDict literals). These are however very common patterns (judging from number of duplicates to above issues), so I think we should support them. Here is what I propose: * Always support cases that are safe (like passing the type itself to `update`) * Allow popular but technically unsafe cases _by default_ * Have a new flag (as part of `--strict`) to fall back to current behavior Note that unfortunately we can't use just a custom new error code, since we need to conditionally tweak some types in a plugin. Btw there are couple TODOs I add here: * First is for unsafe behavior for repeated TypedDict keys. This is not new, I just noticed it when working on this * Second is for tricky corner case involving multiple `**` items where we may have false-negatives in strict mode. Note that I don't test all the possible combinations here (since the phase space is huge), but I think I am testing all main ingredients (and I will be glad to add more if needed): * All syntax variants for TypedDicts creation are handled * Various shadowing/overrides scenarios * Required vs non-required keys handling * Union types (both as item and target types) * Inference for generic TypedDicts * New strictness flag More than half of the tests I took from the original PR #13353
Expect this to pass type checking but got this error:
The text was updated successfully, but these errors were encountered: