Skip to content

Enable additional TypedDict tests #3657

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

Merged
merged 1 commit into from
Jul 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
70 changes: 14 additions & 56 deletions test-data/unit/check-typeddict.test
Original file line number Diff line number Diff line change
Expand Up @@ -336,21 +336,6 @@ D = TypedDict('D', {'x': List[int]})
reveal_type(D(x=[])) # E: Revealed type is 'TypedDict('__main__.D', {'x': builtins.list[builtins.int]})'
[builtins fixtures/dict.pyi]

-- TODO: Fix mypy stubs so that the following passes in the test suite
--[case testCanConvertTypedDictToAnySuperclassOfMapping]
--from mypy_extensions import TypedDict
--from typing import Sized, Iterable, Container
--Point = TypedDict('Point', {'x': int, 'y': int})
--def as_sized(p: Point) -> Sized:
-- return p
--def as_iterable(p: Point) -> Iterable[str]:
-- return p
--def as_container(p: Point) -> Container[str]:
-- return p
--def as_object(p: Point) -> object:
-- return p
--[builtins fixtures/dict.pyi]

[case testCannotConvertTypedDictToDictOrMutableMapping]
from mypy_extensions import TypedDict
from typing import Dict, MutableMapping
Expand Down Expand Up @@ -439,18 +424,18 @@ reveal_type(joined1) # E: Revealed type is 'builtins.list[typing.Mapping*[built
reveal_type(joined2) # E: Revealed type is 'builtins.list[typing.Mapping*[builtins.str, builtins.int]]'
[builtins fixtures/dict.pyi]

-- TODO: Fix mypy stubs so that the following passes in the test suite
--[case testJoinOfTypedDictWithCompatibleMappingSupertypeIsSupertype]
--from mypy_extensions import TypedDict
--from typing import Sized
--Cell = TypedDict('Cell', {'value': int})
--left = Cell(value=42)
--right = {'score': 999} # type: Sized
--joined1 = [left, right]
--joined2 = [right, left]
--reveal_type(joined1) # E: Revealed type is 'builtins.list[typing.Sized*]'
--reveal_type(joined2) # E: Revealed type is 'builtins.list[typing.Sized*]'
--[builtins fixtures/dict.pyi]
[case testJoinOfTypedDictWithCompatibleMappingSupertypeIsSupertype]
from mypy_extensions import TypedDict
from typing import Sized
Cell = TypedDict('Cell', {'value': int})
left = Cell(value=42)
right = {'score': 999} # type: Sized
joined1 = [left, right]
joined2 = [right, left]
reveal_type(joined1) # E: Revealed type is 'builtins.list[typing.Sized*]'
reveal_type(joined2) # E: Revealed type is 'builtins.list[typing.Sized*]'
[builtins fixtures/dict.pyi]
[typing fixtures/typing-full.pyi]

[case testJoinOfTypedDictWithIncompatibleMappingIsObject]
from mypy_extensions import TypedDict
Expand Down Expand Up @@ -602,26 +587,6 @@ reveal_type(f(a)) # E: Revealed type is 'builtins.str*'
-- TODO: Figure out some way to trigger the ConstraintBuilderVisitor.visit_typeddict_type() path.


-- Methods

-- TODO: iter() does not accept TypedDictType as an argument type. Figure out why.
--[case testCanCallMappingMethodsOnTypedDict]
--from mypy_extensions import TypedDict
--Cell = TypedDict('Cell', {'value': int})
--c = Cell(value=42)
--c['value']
--iter(c)
--len(c)
--'value' in c
--c.keys()
--c.items()
--c.values()
--c.get('value')
--c == c
--c != c
--[builtins fixtures/dict.pyi]


-- Special Method: __getitem__

[case testCanGetItemOfTypedDictWithValidStringLiteralKey]
Expand Down Expand Up @@ -707,14 +672,6 @@ def set_coordinate(p: TaggedPoint, key: str, value: int) -> None:
[builtins fixtures/dict.pyi]


-- Special Method: get

-- TODO: Implement support for these cases:
--[case testGetOfTypedDictWithValidStringLiteralKeyReturnsPreciseType]
--[case testGetOfTypedDictWithInvalidStringLiteralKeyIsError]
--[case testGetOfTypedDictWithNonLiteralKeyReturnsImpreciseType]


-- isinstance

[case testTypedDictAndInstance]
Expand All @@ -726,7 +683,8 @@ if isinstance(d, D): # E: Cannot use isinstance() with a TypedDict type
[builtins fixtures/isinstancelist.pyi]


-- scoping
-- Scoping

[case testTypedDictInClassNamespace]
# https://github.com/python/mypy/pull/2553#issuecomment-266474341
from mypy_extensions import TypedDict
Expand Down
3 changes: 2 additions & 1 deletion test-data/unit/fixtures/typing-full.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,13 @@ class Sequence(Iterable[T], Generic[T]):
@abstractmethod
def __getitem__(self, n: Any) -> T: pass

class Mapping(Iterable[T], Generic[T, U]):
class Mapping(Iterable[T], Sized, Generic[T, U]):
@overload
def get(self, k: T) -> Optional[U]: ...
@overload
def get(self, k: T, default: Union[U, V]) -> Union[U, V]: ...
def values(self) -> Iterable[U]: pass # Approximate return type
def __len__(self) -> int: ...

class MutableMapping(Mapping[T, U]): pass

Expand Down
38 changes: 38 additions & 0 deletions test-data/unit/pythoneval.test
Original file line number Diff line number Diff line change
Expand Up @@ -1356,3 +1356,41 @@ _testTypedDictGet.py:8: error: Revealed type is 'builtins.str'
_testTypedDictGet.py:9: error: TypedDict "D" has no key 'z'
_testTypedDictGet.py:10: error: No overload variant of "get" of "Mapping" matches argument types []
_testTypedDictGet.py:12: error: Revealed type is 'builtins.object*'

[case testTypedDictMappingMethods]
from mypy_extensions import TypedDict
Cell = TypedDict('Cell', {'value': int})
c = Cell(value=42)
for x in c:
reveal_type(x)
reveal_type(iter(c))
reveal_type(len(c))
reveal_type('value' in c)
reveal_type(c.keys())
reveal_type(c.items())
reveal_type(c.values())
c == c
c != c
[out]
_testTypedDictMappingMethods.py:5: error: Revealed type is 'builtins.str*'
_testTypedDictMappingMethods.py:6: error: Revealed type is 'typing.Iterator[builtins.str*]'
_testTypedDictMappingMethods.py:7: error: Revealed type is 'builtins.int'
_testTypedDictMappingMethods.py:8: error: Revealed type is 'builtins.bool'
_testTypedDictMappingMethods.py:9: error: Revealed type is 'typing.AbstractSet[builtins.str*]'
_testTypedDictMappingMethods.py:10: error: Revealed type is 'typing.AbstractSet[Tuple[builtins.str*, builtins.int*]]'
_testTypedDictMappingMethods.py:11: error: Revealed type is 'typing.ValuesView[builtins.int*]'

[case testCanConvertTypedDictToAnySuperclassOfMapping]
from mypy_extensions import TypedDict
from typing import Sized, Iterable, Container

Point = TypedDict('Point', {'x': int, 'y': int})

p: Point
s: Sized = p
it: Iterable[str] = p
c: Container[str] = p
o: object = p
it2: Iterable[int] = p
[out]
_testCanConvertTypedDictToAnySuperclassOfMapping.py:11: error: Incompatible types in assignment (expression has type "Point", variable has type Iterable[int])