Skip to content

Commit d3afe68

Browse files
JukkaLilevkivskyi
authored andcommitted
Enable additional TypedDict tests (#3657)
1 parent 5928551 commit d3afe68

File tree

3 files changed

+54
-57
lines changed

3 files changed

+54
-57
lines changed

test-data/unit/check-typeddict.test

Lines changed: 14 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -336,21 +336,6 @@ D = TypedDict('D', {'x': List[int]})
336336
reveal_type(D(x=[])) # E: Revealed type is 'TypedDict('__main__.D', {'x': builtins.list[builtins.int]})'
337337
[builtins fixtures/dict.pyi]
338338

339-
-- TODO: Fix mypy stubs so that the following passes in the test suite
340-
--[case testCanConvertTypedDictToAnySuperclassOfMapping]
341-
--from mypy_extensions import TypedDict
342-
--from typing import Sized, Iterable, Container
343-
--Point = TypedDict('Point', {'x': int, 'y': int})
344-
--def as_sized(p: Point) -> Sized:
345-
-- return p
346-
--def as_iterable(p: Point) -> Iterable[str]:
347-
-- return p
348-
--def as_container(p: Point) -> Container[str]:
349-
-- return p
350-
--def as_object(p: Point) -> object:
351-
-- return p
352-
--[builtins fixtures/dict.pyi]
353-
354339
[case testCannotConvertTypedDictToDictOrMutableMapping]
355340
from mypy_extensions import TypedDict
356341
from typing import Dict, MutableMapping
@@ -439,18 +424,18 @@ reveal_type(joined1) # E: Revealed type is 'builtins.list[typing.Mapping*[built
439424
reveal_type(joined2) # E: Revealed type is 'builtins.list[typing.Mapping*[builtins.str, builtins.int]]'
440425
[builtins fixtures/dict.pyi]
441426

442-
-- TODO: Fix mypy stubs so that the following passes in the test suite
443-
--[case testJoinOfTypedDictWithCompatibleMappingSupertypeIsSupertype]
444-
--from mypy_extensions import TypedDict
445-
--from typing import Sized
446-
--Cell = TypedDict('Cell', {'value': int})
447-
--left = Cell(value=42)
448-
--right = {'score': 999} # type: Sized
449-
--joined1 = [left, right]
450-
--joined2 = [right, left]
451-
--reveal_type(joined1) # E: Revealed type is 'builtins.list[typing.Sized*]'
452-
--reveal_type(joined2) # E: Revealed type is 'builtins.list[typing.Sized*]'
453-
--[builtins fixtures/dict.pyi]
427+
[case testJoinOfTypedDictWithCompatibleMappingSupertypeIsSupertype]
428+
from mypy_extensions import TypedDict
429+
from typing import Sized
430+
Cell = TypedDict('Cell', {'value': int})
431+
left = Cell(value=42)
432+
right = {'score': 999} # type: Sized
433+
joined1 = [left, right]
434+
joined2 = [right, left]
435+
reveal_type(joined1) # E: Revealed type is 'builtins.list[typing.Sized*]'
436+
reveal_type(joined2) # E: Revealed type is 'builtins.list[typing.Sized*]'
437+
[builtins fixtures/dict.pyi]
438+
[typing fixtures/typing-full.pyi]
454439

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

604589

605-
-- Methods
606-
607-
-- TODO: iter() does not accept TypedDictType as an argument type. Figure out why.
608-
--[case testCanCallMappingMethodsOnTypedDict]
609-
--from mypy_extensions import TypedDict
610-
--Cell = TypedDict('Cell', {'value': int})
611-
--c = Cell(value=42)
612-
--c['value']
613-
--iter(c)
614-
--len(c)
615-
--'value' in c
616-
--c.keys()
617-
--c.items()
618-
--c.values()
619-
--c.get('value')
620-
--c == c
621-
--c != c
622-
--[builtins fixtures/dict.pyi]
623-
624-
625590
-- Special Method: __getitem__
626591

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

709674

710-
-- Special Method: get
711-
712-
-- TODO: Implement support for these cases:
713-
--[case testGetOfTypedDictWithValidStringLiteralKeyReturnsPreciseType]
714-
--[case testGetOfTypedDictWithInvalidStringLiteralKeyIsError]
715-
--[case testGetOfTypedDictWithNonLiteralKeyReturnsImpreciseType]
716-
717-
718675
-- isinstance
719676

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

728685

729-
-- scoping
686+
-- Scoping
687+
730688
[case testTypedDictInClassNamespace]
731689
# https://github.com/python/mypy/pull/2553#issuecomment-266474341
732690
from mypy_extensions import TypedDict

test-data/unit/fixtures/typing-full.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,13 @@ class Sequence(Iterable[T], Generic[T]):
103103
@abstractmethod
104104
def __getitem__(self, n: Any) -> T: pass
105105

106-
class Mapping(Iterable[T], Generic[T, U]):
106+
class Mapping(Iterable[T], Sized, Generic[T, U]):
107107
@overload
108108
def get(self, k: T) -> Optional[U]: ...
109109
@overload
110110
def get(self, k: T, default: Union[U, V]) -> Union[U, V]: ...
111111
def values(self) -> Iterable[U]: pass # Approximate return type
112+
def __len__(self) -> int: ...
112113

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

test-data/unit/pythoneval.test

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1356,3 +1356,41 @@ _testTypedDictGet.py:8: error: Revealed type is 'builtins.str'
13561356
_testTypedDictGet.py:9: error: TypedDict "D" has no key 'z'
13571357
_testTypedDictGet.py:10: error: No overload variant of "get" of "Mapping" matches argument types []
13581358
_testTypedDictGet.py:12: error: Revealed type is 'builtins.object*'
1359+
1360+
[case testTypedDictMappingMethods]
1361+
from mypy_extensions import TypedDict
1362+
Cell = TypedDict('Cell', {'value': int})
1363+
c = Cell(value=42)
1364+
for x in c:
1365+
reveal_type(x)
1366+
reveal_type(iter(c))
1367+
reveal_type(len(c))
1368+
reveal_type('value' in c)
1369+
reveal_type(c.keys())
1370+
reveal_type(c.items())
1371+
reveal_type(c.values())
1372+
c == c
1373+
c != c
1374+
[out]
1375+
_testTypedDictMappingMethods.py:5: error: Revealed type is 'builtins.str*'
1376+
_testTypedDictMappingMethods.py:6: error: Revealed type is 'typing.Iterator[builtins.str*]'
1377+
_testTypedDictMappingMethods.py:7: error: Revealed type is 'builtins.int'
1378+
_testTypedDictMappingMethods.py:8: error: Revealed type is 'builtins.bool'
1379+
_testTypedDictMappingMethods.py:9: error: Revealed type is 'typing.AbstractSet[builtins.str*]'
1380+
_testTypedDictMappingMethods.py:10: error: Revealed type is 'typing.AbstractSet[Tuple[builtins.str*, builtins.int*]]'
1381+
_testTypedDictMappingMethods.py:11: error: Revealed type is 'typing.ValuesView[builtins.int*]'
1382+
1383+
[case testCanConvertTypedDictToAnySuperclassOfMapping]
1384+
from mypy_extensions import TypedDict
1385+
from typing import Sized, Iterable, Container
1386+
1387+
Point = TypedDict('Point', {'x': int, 'y': int})
1388+
1389+
p: Point
1390+
s: Sized = p
1391+
it: Iterable[str] = p
1392+
c: Container[str] = p
1393+
o: object = p
1394+
it2: Iterable[int] = p
1395+
[out]
1396+
_testCanConvertTypedDictToAnySuperclassOfMapping.py:11: error: Incompatible types in assignment (expression has type "Point", variable has type Iterable[int])

0 commit comments

Comments
 (0)