8000 Set TypedDicts to always be their declared type as opposed to the type inferred when instantiated by rowillia · Pull Request #2621 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Set TypedDicts to always be their declared type as opposed to the type inferred when instantiated #2621

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Allow fields on a TypedDict to be subtypes of their declared types.
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 #2610
  • Loading branch information
Roy Williams committed Jan 6, 2017
commit 465bf009f30584c7206bdca1d5a9e8b3e82d050d
2 changes: 1 addition & 1 deletion mypy/subtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ def visit_typeddict_type(self, left: TypedDictType) -> bool:
if not left.names_are_wider_than(right):
return False
for (_, l, r) in left.zip(right):
if not is_equivalent(l, r, self.check_type_parameter):
if not is_subtype(l, r, self.check_type_parameter):
return False
# (NOTE: Fallbacks don't matter.)
return True
Expand Down
39 changes: 31 additions & 8 deletions test-data/unit/check-typeddict.test
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,6 @@ def convert(op: ObjectPoint) -> Point:
return op # E: Incompatible return value type (got "ObjectPoint", expected "Point")
[builtins fixtures/dict.pyi]

[case testCannotConvertTypedDictToSimilarTypedDictWithWiderItemTypes]
from mypy_extensions import TypedDict
Point = TypedDict('Point', {'x': int, 'y': int})
ObjectPoint = TypedDict('ObjectPoint', {'x': object, 'y': object})
def convert(p: Point) -> ObjectPoint:
return p # E: Incompatible return value type (got "Point", expected "ObjectPoint")
[builtins fixtures/dict.pyi]

[case testCannotConvertTypedDictToSimilarTypedDictWithIncompatibleItemTypes]
from mypy_extensions import TypedDict
Point = TypedDict('Point', {'x': int, 'y': int})
Expand Down Expand Up @@ -136,6 +128,37 @@ def as_mapping(p: Point) -> Mapping[str, str]:
return p # E: Incompatible return value type (got "Point", expected Mapping[str, str])
[builtins fixtures/dict.pyi]

[case testTypedDictAcceptsIntForFloatDuckTypes]
from mypy_extensions import TypedDict
from typing import Any, Mapping
Point = TypedDict('Point', {'x': float, 'y': float})
def create_point() -> Point:
return Point(x=1, y=2)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you also do something like reveal_type(Point(x=1, y=2)) in a test case?

[builtins fixtures/dict.pyi]

[case testTypedDictDoesNotAcceptsFloatForInt]
from mypy_extensions import TypedDict
from typing import Any, Mapping
Point = TypedDict('Point', {'x': int, 'y': int})
def create_point() -> Point:
return Point(x=1.2, y=2.5)
[out]
main:5: error: Incompatible return value type (got "TypedDict(x=float, y=float)", expected "Point")
main:5: error: Incompatible types (expression has type "float", TypedDict item "x" has type "int")
main:5: error: Incompatible types (expression has type "float", TypedDict item "y" has type "int")
[builtins fixtures/dict.pyi]

[case testTypedDictAcceptsAnyType]
from mypy_extensions import TypedDict
from typing import Any, Mapping
Point = TypedDict('Point', {'x': float, 'y': float})
def create_point(something: Any) -> Point:
return Point({
'x': something.x,
'y': something.y
})
[builtins fixtures/dict.pyi]

-- TODO: Fix mypy stubs so that the following passes in the test suite
--[case testCanConvertTypedDictToAnySuperclassOfMapping]
--from mypy_extensions import TypedDict
Expand Down
0