-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
TypedDict doesn't respect subtypes or promoted types. #2610
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
TypedDicts appear to have explicitly decided not to accept subtypes on fields, but this behavior is counter intuitive. This made it so TypedDicts didn't respect `Any` and caused problems with what should have been ducktype compatible. This also brings TypedDicts more in line with other container types and with how fields on classes behave. ```python from typing import Dict def foo() -> Dict[float, object]: return { 1: 32 } ``` This fixes python#2610
It took me some time to understand the bug report too... It seems that the failure (with (I wouldn't call that duck typing (mypy makes special allowances for |
@gvanrossum Sorry for the obtuse bug report...it took me a bit to figure out what was going on. I changed the example to not rely on the |
#2621 (comment) describes an alternative way to fix this. |
TypedDicts appear to have explicitly decided not to accept subtypes on fields, but this behavior is counter intuitive. This made it so TypedDicts didn't respect `Any` and caused problems with what should have been ducktype compatible. This also brings TypedDicts more in line with other container types and with how fields on classes behave. ```python from typing import Dict def foo() -> Dict[float, object]: return { 1: 32 } ``` This fixes python#2610
TypedDicts appear to have explicitly decided not to accept subtypes on fields, but this behavior is counter intuitive. This made it so TypedDicts didn't respect `Any` and caused problems with what should have been ducktype compatible. This also brings TypedDicts more in line with other container types and with how fields on classes behave. ```python from typing import Dict def foo() -> Dict[float, object]: return { 1: 32 } ``` This fixes python#2610
TypedDicts appear to have explicitly decided not to accept subtypes on fields, but this behavior is counter intuitive. This made it so TypedDicts didn't respect `Any` and caused problems with what should have been ducktype compatible. This also brings TypedDicts more in line with other container types and with how fields on classes behave. ```python from typing import Dict def foo() -> Dict[float, object]: return { 1: 32 } ``` This fixes python#2610
Uh oh!
There was an error while loading. Please reload this page.
Repro:
test_typed_dict_return_value.py
As an aside, the error message above is fairly hard to decode. It took me a few hours to realize what was going on.
The problem also exists with
Any
types:The text was updated successfully, but these errors were encountered: