Skip to content

Commit 5296439

Browse files
committed
Create mypy.typing module for experimental typing additions. Move TypedDict there.
1 parent c7a6aec commit 5296439

File tree

3 files changed

+26
-14
lines changed

3 files changed

+26
-14
lines changed

mypy/semanal.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1812,7 +1812,7 @@ def check_typeddict(self, node: Node, var_name: str = None) -> TypeInfo:
18121812
return None
18131813
callee = call.callee
18141814
fullname = callee.fullname
1815-
if fullname != 'typing.TypedDict':
1815+
if fullname != 'mypy.typing.TypedDict':
18161816
return None
18171817
items, types, ok = self.parse_typeddict_args(call, fullname)
18181818
if not ok:

mypy/typing.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""The "mypy.typing" module defines experimental extensions to the standard
2+
"typing" module that is supported by the mypy typechecker.
3+
"""
4+
5+
from typing import cast, Dict, Type, TypeVar
6+
7+
8+
_T = TypeVar('_T')
9+
10+
11+
def TypedDict(typename: str, fields: Dict[str, Type[_T]]) -> Type[dict]:
12+
"""TypedDict creates a dictionary type that expects all of its
13+
instances to have a certain common set of keys, with each key
14+
associated with a value of a consistent type. This expectation
15+
is not checked at runtime but is only enforced by typecheckers.
16+
"""
17+
def new_dict(*args, **kwargs):
18+
return dict(*args, **kwargs)
19+
20+
new_dict.__name__ = typename # type: ignore # https://github.com/python/mypy/issues/708
21+
new_dict.__supertype__ = dict # type: ignore # https://github.com/python/mypy/issues/708
22+
return cast(Type[dict], new_dict)

test-data/unit/semanal-typeddict.test

+3-13
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
-- Semantic analysis of typed dicts
22

33
[case testCanDefineTypedDictType]
4-
# TODO: Support TypedDict in typing.py and not just typing.pyi
5-
from typing import TYPE_CHECKING
6-
if TYPE_CHECKING:
7-
from typing import TypedDict
8-
else:
9-
TypedDict = lambda name, fields: dict
4+
from mypy.typing import TypedDict
105
Point = TypedDict('Point', {'x': int, 'y': int})
116
[out]
127
...
@@ -15,12 +10,7 @@ Point = TypedDict('Point', {'x': int, 'y': int})
1510

1611
-- TODO: Support: testCanAnnotateDictLiteralAsTypedDict
1712
[case testCannotYetAnnotateDictLiteralAsTypedDict]
18-
# TODO: Support TypedDict in typing.py and not just typing.pyi
19-
from typing import TYPE_CHECKING
20-
if TYPE_CHECKING:
21-
from typing import TypedDict
22-
else:
23-
TypedDict = lambda name, fields: dict
13+
from mypy.typing import TypedDict
2414
Point = TypedDict('Point', {'x': int, 'y': int})
25-
point = dict(x=42, y=1337) # type: Point # E: Incompatible types in assignment (expression has type Dict[str, int], variable has type "Point")
15+
point = dict(x=42, y=1337) # type: Point # E: Incompatible types in assignment (expression has type Dict[str, int], variable has type "Point")
2616

0 commit comments

Comments
 (0)