Skip to content

Commit

Permalink
Update TypedDict imports in tests (#18528)
Browse files Browse the repository at this point in the history
`mypy_extensions.TypedDict` has been redundant for a while now. With the
next mypy_extensions release, it will raise a `DeprecationWarning` when
imported.

Replace existing imports in tests with `typing.TypedDict`.
  • Loading branch information
cdce8p authored Jan 25, 2025
1 parent 1b24bf7 commit 0c60548
Show file tree
Hide file tree
Showing 21 changed files with 412 additions and 349 deletions.
45 changes: 24 additions & 21 deletions test-data/unit/check-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -5184,11 +5184,12 @@ def test() -> None:
[builtins fixtures/tuple.pyi]

[case testCrashOnSelfRecursiveTypedDictVar]
from mypy_extensions import TypedDict
from typing import TypedDict

A = TypedDict('A', {'a': 'A'}) # type: ignore
a: A
[builtins fixtures/isinstancelist.pyi]
[typing fixtures/typing-typeddict.pyi]

[case testCrashInJoinOfSelfRecursiveNamedTuples]

Expand All @@ -5205,7 +5206,7 @@ lst = [n, m]
[builtins fixtures/isinstancelist.pyi]

[case testCorrectJoinOfSelfRecursiveTypedDicts]
from mypy_extensions import TypedDict
from typing import TypedDict

def test() -> None:
class N(TypedDict):
Expand All @@ -5220,6 +5221,7 @@ def test() -> None:
lst = [n, m]
reveal_type(lst[0]['x']) # N: Revealed type is "Any"
[builtins fixtures/isinstancelist.pyi]
[typing fixtures/typing-typeddict.pyi]

[case testCrashInForwardRefToNamedTupleWithIsinstance]
from typing import Dict, NamedTuple
Expand All @@ -5236,8 +5238,7 @@ def parse_ast(name_dict: NameDict) -> None:
[typing fixtures/typing-medium.pyi]

[case testCrashInForwardRefToTypedDictWithIsinstance]
from mypy_extensions import TypedDict
from typing import Dict
from typing import Dict, TypedDict

NameDict = Dict[str, 'NameInfo']
class NameInfo(TypedDict):
Expand All @@ -5248,7 +5249,7 @@ def parse_ast(name_dict: NameDict) -> None:
pass
reveal_type(name_dict['']['ast']) # N: Revealed type is "builtins.bool"
[builtins fixtures/isinstancelist.pyi]
[typing fixtures/typing-medium.pyi]
[typing fixtures/typing-typeddict.pyi]

[case testCorrectIsinstanceInForwardRefToNewType]
from typing import Dict, NewType
Expand Down Expand Up @@ -5313,13 +5314,13 @@ x = NT(N(1))

[case testNewTypeFromForwardTypedDict]

from typing import NewType, Tuple
from mypy_extensions import TypedDict
from typing import NewType, Tuple, TypedDict

NT = NewType('NT', 'N') # E: Argument 2 to NewType(...) must be subclassable (got "N")
class N(TypedDict):
x: int
[builtins fixtures/dict.pyi]
[typing fixtures/typing-typeddict.pyi]
[out]

[case testCorrectAttributeInForwardRefToNamedTuple]
Expand All @@ -5335,7 +5336,7 @@ class Process(NamedTuple):
[out]

[case testCorrectItemTypeInForwardRefToTypedDict]
from mypy_extensions import TypedDict
from typing import TypedDict
proc: Process
reveal_type(proc['state']) # N: Revealed type is "builtins.int"

Expand All @@ -5344,6 +5345,7 @@ def get_state(proc: 'Process') -> int:
class Process(TypedDict):
state: int
[builtins fixtures/isinstancelist.pyi]
[typing fixtures/typing-typeddict.pyi]
[out]

[case testCorrectDoubleForwardNamedTuple]
Expand All @@ -5362,7 +5364,7 @@ reveal_type(x.one.attr) # N: Revealed type is "builtins.str"
[out]

[case testCrashOnDoubleForwardTypedDict]
from mypy_extensions import TypedDict
from typing import TypedDict

x: A
class A(TypedDict):
Expand All @@ -5373,6 +5375,7 @@ class B(TypedDict):

reveal_type(x['one']['attr']) # N: Revealed type is "builtins.str"
[builtins fixtures/isinstancelist.pyi]
[typing fixtures/typing-typeddict.pyi]
[out]

[case testCrashOnForwardUnionOfNamedTuples]
Expand All @@ -5392,8 +5395,7 @@ def foo(node: Node) -> int:
[out]

[case testCrashOnForwardUnionOfTypedDicts]
from mypy_extensions import TypedDict
from typing import Union
from typing import TypedDict, Union

NodeType = Union['Foo', 'Bar']
class Foo(TypedDict):
Expand All @@ -5405,6 +5407,7 @@ def foo(node: NodeType) -> int:
x = node
return x['x']
[builtins fixtures/isinstancelist.pyi]
[typing fixtures/typing-typeddict.pyi]
[out]

[case testSupportForwardUnionOfNewTypes]
Expand Down Expand Up @@ -5471,8 +5474,7 @@ def f(x: ForwardUnion) -> None:
[out]

[case testCrashInvalidArgsSyntheticClassSyntax]
from typing import List, NamedTuple
from mypy_extensions import TypedDict
from typing import List, NamedTuple, TypedDict
class TD(TypedDict):
x: List[int, str] # E: "list" expects 1 type argument, but 2 given
class NM(NamedTuple):
Expand All @@ -5482,11 +5484,11 @@ class NM(NamedTuple):
TD({'x': []})
NM(x=[])
[builtins fixtures/dict.pyi]
[typing fixtures/typing-typeddict.pyi]
[out]

[case testCrashInvalidArgsSyntheticClassSyntaxReveals]
from typing import List, NamedTuple
from mypy_extensions import TypedDict
from typing import List, NamedTuple, TypedDict
class TD(TypedDict):
x: List[int, str] # E: "list" expects 1 type argument, but 2 given
class NM(NamedTuple):
Expand All @@ -5501,11 +5503,11 @@ reveal_type(x1) # N: Revealed type is "TypedDict('__main__.TD', {'x': builtins.l
reveal_type(y) # N: Revealed type is "Tuple[builtins.list[Any], fallback=__main__.NM]"
reveal_type(y1) # N: Revealed type is "Tuple[builtins.list[Any], fallback=__main__.NM]"
[builtins fixtures/dict.pyi]
[typing fixtures/typing-typeddict.pyi]
[out]

[case testCrashInvalidArgsSyntheticFunctionSyntax]
from typing import List, NewType, NamedTuple
from mypy_extensions import TypedDict
from typing import List, NewType, NamedTuple, TypedDict
TD = TypedDict('TD', {'x': List[int, str]}) # E: "list" expects 1 type argument, but 2 given
NM = NamedTuple('NM', [('x', List[int, str])]) # E: "list" expects 1 type argument, but 2 given
NT = NewType('NT', List[int, str]) # E: "list" expects 1 type argument, but 2 given
Expand All @@ -5515,11 +5517,11 @@ TD({'x': []})
NM(x=[])
NT([])
[builtins fixtures/dict.pyi]
[typing fixtures/typing-typeddict.pyi]
[out]

[case testCrashForwardSyntheticClassSyntax]
from typing import NamedTuple
from mypy_extensions import TypedDict
from typing import NamedTuple, TypedDict
class A1(NamedTuple):
b: 'B'
x: int
Expand All @@ -5533,11 +5535,11 @@ y: A2
reveal_type(x.b) # N: Revealed type is "__main__.B"
reveal_type(y['b']) # N: Revealed type is "__main__.B"
[builtins fixtures/dict.pyi]
[typing fixtures/typing-typeddict.pyi]
[out]

[case testCrashForwardSyntheticFunctionSyntax]
from typing import NamedTuple
from mypy_extensions import TypedDict
from typing import NamedTuple, TypedDict
A1 = NamedTuple('A1', [('b', 'B'), ('x', int)])
A2 = TypedDict('A2', {'b': 'B', 'x': int})
class B:
Expand All @@ -5547,6 +5549,7 @@ y: A2
reveal_type(x.b) # N: Revealed type is "__main__.B"
reveal_type(y['b']) # N: Revealed type is "__main__.B"
[builtins fixtures/dict.pyi]
[typing fixtures/typing-typeddict.pyi]
[out]

-- Special support for six
Expand Down
4 changes: 2 additions & 2 deletions test-data/unit/check-custom-plugin.test
Original file line number Diff line number Diff line change
Expand Up @@ -579,8 +579,7 @@ plugins=<ROOT>/test-data/unit/plugins/method_sig_hook.py

[case testMethodSignatureHookNamesFullyQualified]
# flags: --config-file tmp/mypy.ini
from mypy_extensions import TypedDict
from typing import NamedTuple
from typing import NamedTuple, TypedDict

class FullyQualifiedTestClass:
@classmethod
Expand All @@ -601,6 +600,7 @@ reveal_type(FullyQualifiedTestNamedTuple('')._asdict()) # N: Revealed type is "b
\[mypy]
plugins=<ROOT>/test-data/unit/plugins/fully_qualified_test_hook.py
[builtins fixtures/classmethod.pyi]
[typing fixtures/typing-typeddict.pyi]

[case testDynamicClassPlugin]
# flags: --config-file tmp/mypy.ini
Expand Down
23 changes: 12 additions & 11 deletions test-data/unit/check-flags.test
Original file line number Diff line number Diff line change
Expand Up @@ -1082,25 +1082,25 @@ main:6: error: A type on this line becomes "Any" due to an unfollowed import

[case testDisallowUnimportedAnyTypedDictSimple]
# flags: --ignore-missing-imports --disallow-any-unimported
from mypy_extensions import TypedDict
from typing import TypedDict
from x import Unchecked

M = TypedDict('M', {'x': str, 'y': Unchecked}) # E: Type of a TypedDict key becomes "Any" due to an unfollowed import

def f(m: M) -> M: pass # no error
[builtins fixtures/dict.pyi]
[typing fixtures/typing-typeddict.pyi]

[case testDisallowUnimportedAnyTypedDictGeneric]
# flags: --ignore-missing-imports --disallow-any-unimported

from mypy_extensions import TypedDict
from typing import List
from typing import List, TypedDict
from x import Unchecked

M = TypedDict('M', {'x': str, 'y': List[Unchecked]}) # E: Type of a TypedDict key becomes "List[Any]" due to an unfollowed import

def f(m: M) -> M: pass # no error
[builtins fixtures/dict.pyi]
[typing fixtures/typing-typeddict.pyi]

[case testDisallowAnyDecoratedUnannotatedDecorator]
# flags: --disallow-any-decorated
Expand Down Expand Up @@ -1337,13 +1337,14 @@ def k(s: E) -> None: pass

[case testDisallowAnyExprTypedDict]
# flags: --disallow-any-expr
from mypy_extensions import TypedDict
from typing import TypedDict

Movie = TypedDict('Movie', {'name': str, 'year': int})

def g(m: Movie) -> Movie:
return m
[builtins fixtures/dict.pyi]
[typing fixtures/typing-typeddict.pyi]

[case testDisallowIncompleteDefs]
# flags: --disallow-incomplete-defs
Expand Down Expand Up @@ -1483,8 +1484,7 @@ n: N

[case testCheckDisallowAnyGenericsTypedDict]
# flags: --disallow-any-generics
from typing import Dict, Any, Optional
from mypy_extensions import TypedDict
from typing import Dict, Any, Optional, TypedDict

VarsDict = Dict[str, Any]
HostsDict = Dict[str, Optional[VarsDict]]
Expand All @@ -1497,6 +1497,7 @@ GroupDataDict = TypedDict(

GroupsDict = Dict[str, GroupDataDict] # type: ignore
[builtins fixtures/dict.pyi]
[typing fixtures/typing-typeddict.pyi]


[case testCheckDisallowAnyGenericsStubOnly]
Expand Down Expand Up @@ -1929,22 +1930,22 @@ Bar = NewType('Bar', List[Any]) # E: Explicit "Any" is not allowed [explicit-a

[case testDisallowAnyExplicitTypedDictSimple]
# flags: --disallow-any-explicit --show-error-codes
from mypy_extensions import TypedDict
from typing import Any
from typing import Any, TypedDict

M = TypedDict('M', {'x': str, 'y': Any}) # E: Explicit "Any" is not allowed [explicit-any]
M(x='x', y=2) # no error
def f(m: M) -> None: pass # no error
[builtins fixtures/dict.pyi]
[typing fixtures/typing-typeddict.pyi]

[case testDisallowAnyExplicitTypedDictGeneric]
# flags: --disallow-any-explicit --show-error-codes
from mypy_extensions import TypedDict
from typing import Any, List
from typing import Any, List, TypedDict

M = TypedDict('M', {'x': str, 'y': List[Any]}) # E: Explicit "Any" is not allowed [explicit-any]
N = TypedDict('N', {'x': str, 'y': List}) # no error
[builtins fixtures/dict.pyi]
[typing fixtures/typing-typeddict.pyi]

[case testDisallowAnyGenericsTupleNoTypeParams]
# flags: --disallow-any-generics
Expand Down
Loading

0 comments on commit 0c60548

Please sign in to comment.