-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Possible false positive calling dict.update() method with TypedDict hierarchy #9335
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
Comments
I'm having a similar issue, regarding updating a child type instance with data from a parent type instance. I'm not entirely sure it should work and I'm a little confused. PEP 589 says this on inheritance:
What I understand from this is that given two TypedDict types So I tested this: from typing import TypedDict
class BaseRequired(TypedDict):
x: int
class BaseOptional(TypedDict, total=False):
x: int
class DerivedRequired_BaseRequired(BaseRequired):
y: int
class DerivedRequired_BaseOptional(BaseOptional):
y: int
class DerivedOptional_BaseRequired(BaseRequired, total=False):
y: int
class DerivedOptional_BaseOptional(BaseOptional, total=False):
y: int
base_required: BaseRequired = {"x": 1}
base_optional: BaseOptional = {"x": 1}
derived_required__base_required: DerivedRequired_BaseRequired = {"x": 1, "y": 2}
derived_required__base_optional: DerivedRequired_BaseOptional = {"x": 1, "y": 2}
derived_optional__base_required: DerivedOptional_BaseRequired = {"x": 1, "y": 2}
derived_optional__base_optional: DerivedOptional_BaseOptional = {"x": 1, "y": 2}
derived_required__base_required.update(base_required)
derived_required__base_optional.update(base_required)
derived_optional__base_required.update(base_required)
derived_optional__base_optional.update(base_required)
derived_required__base_required.update(base_optional)
derived_required__base_optional.update(base_optional)
derived_optional__base_required.update(base_optional)
derived_optional__base_optional.update(base_optional)
class SmallRequired(TypedDict):
x: int
class SmallOptional(TypedDict, total=False):
x: int
class BigRequired(TypedDict):
x: int
y: int
class BigOptional(TypedDict, total=False):
x: int
y: int
small_required: SmallRequired = {"x": 1}
small_optional: SmallOptional = {"x": 1}
big_required: BigRequired = {"x": 1, "y": 2}
big_optional: BigOptional = {"x": 1, "y": 2}
big_required.update(small_required)
big_required.update(small_optional)
big_optional.update(small_required)
big_optional.update(small_optional) All calls to
|
I have also a similar issue with a dict .update()
baseModel.py:21: error: Dict entry 0 has incompatible type "str": "str"; expected "str": "int" |
Closing as a duplicate of #6462 |
Python version: 3.8.2
mypy version: 0.782
mypy flags: --strict (same result without this flag)
I've encountered what looks like a false positive when calling
.update()
on TypedDicts that have some required and some optional fields. I have a hierarchy of TypedDicts with some required fields in the base class and some optional fields in the derived class. Here is a minimal snippet that reproduces the error:I expected this to type check with no errors. However, mypy emits the following about the last line:
It seems like mypy is expecting a TypedDict with the same fields (but all optional) as Derived. If I change the last line to
update_me.update(update_me)
, the same error occurs. If I addtotal=False
toBase
's signature, the error goes away, but then I no longer have any required fields.Also interesting to note:
reveal_type(update_me.update)
prints the signature I would expect:And if I inline the argument, i.e.
update_me.update({'id': 2})
, the error goes away. However, my use case issuch that doing so is not a viable solution.
Thank you! Please let me know if there's anything I've missed.
The text was updated successfully, but these errors were encountered: