-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Fix crashes in synthetic types #3322
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
Changes from all commits
e52f9c5
554c6fa
590d1da
ff786ca
e2f4237
3e0ef98
310efe4
66c0c69
ce514d1
3a14a53
e30e994
57ca8f1
32159cc
70e3f63
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3137,6 +3137,93 @@ class M(type): | |
class A(metaclass=M): pass | ||
reveal_type(type(A).x) # E: Revealed type is 'builtins.int' | ||
|
||
-- Synthetic types crashes | ||
-- ----------------------- | ||
|
||
[case testCrashInvalidArgsSyntheticClassSyntax] | ||
from typing import List, NamedTuple | ||
from mypy_extensions import TypedDict | ||
class TD(TypedDict): | ||
x: List[int, str] # E: "list" expects 1 type argument, but 2 given | ||
class NM(NamedTuple): | ||
x: List[int, str] # E: "list" expects 1 type argument, but 2 given | ||
|
||
# These two should never crash, reveals are in the next test | ||
TD({'x': []}) | ||
NM(x=[]) | ||
[builtins fixtures/dict.pyi] | ||
[out] | ||
|
||
[case testCrashInvalidArgsSyntheticClassSyntaxReveals] | ||
from typing import List, NamedTuple | ||
from mypy_extensions import TypedDict | ||
class TD(TypedDict): | ||
x: List[int, str] # E: "list" expects 1 type argument, but 2 given | ||
class NM(NamedTuple): | ||
x: List[int, str] # E: "list" expects 1 type argument, but 2 given | ||
|
||
x: TD | ||
x1 = TD({'x': []}) | ||
y: NM | ||
y1 = NM(x=[]) | ||
reveal_type(x) # E: Revealed type is 'TypedDict(x=builtins.list[Any], _fallback=__main__.TD)' | ||
reveal_type(x1) # E: Revealed type is 'TypedDict(x=builtins.list[Any], _fallback=typing.Mapping[builtins.str, builtins.list[Any]])' | ||
reveal_type(y) # E: Revealed type is 'Tuple[builtins.list[Any], fallback=__main__.NM]' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No reveal for y1? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added it. |
||
reveal_type(y1) # E: Revealed type is 'Tuple[builtins.list[Any], fallback=__main__.NM]' | ||
[builtins fixtures/dict.pyi] | ||
[out] | ||
|
||
[case testCrashInvalidArgsSyntheticFunctionSyntax] | ||
from typing import List, NewType, NamedTuple | ||
from mypy_extensions import 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 | ||
|
||
# These three should not crash | ||
TD({'x': []}) | ||
NM(x=[]) | ||
NT([]) | ||
[builtins fixtures/dict.pyi] | ||
[out] | ||
|
||
-- The two tests below will not crash after | ||
-- https://github.com/python/mypy/issues/3319 is fixed | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remember to remove the |
||
[case testCrashForwardSyntheticClassSyntax-skip] | ||
from typing import NamedTuple | ||
from mypy_extensions import TypedDict | ||
class A1(NamedTuple): | ||
b: 'B' | ||
x: int | ||
class A2(TypedDict): | ||
b: 'B' | ||
x: int | ||
class B: | ||
pass | ||
x: A1 | ||
y: A2 | ||
reveal_type(x.b) # E: Revealed type is '__main__.B' | ||
reveal_type(y['b']) # E: Revealed type is '__main__.B' | ||
[builtins fixtures/dict.pyi] | ||
[out] | ||
|
||
[case testCrashForwardSyntheticFunctionSyntax-skip] | ||
from typing import NamedTuple | ||
from mypy_extensions import TypedDict | ||
A1 = NamedTuple('A1', [('b', 'B'), ('x', int)]) | ||
A2 = TypedDict('A2', {'b': 'B', 'x': int}) | ||
class B: | ||
pass | ||
x: A1 | ||
y: A2 | ||
reveal_type(x.b) # E: Revealed type is '__main__.B' | ||
reveal_type(y['b']) # E: Revealed type is '__main__.B' | ||
[builtins fixtures/dict.pyi] | ||
[out] | ||
|
||
-- Special support for six | ||
-- ----------------------- | ||
|
||
[case testSixWithMetaclass] | ||
import six | ||
class M(type): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this need an update to serialize() (perhaps in the comment)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we don't need to serialize this (since it is needed only for semantic analysis). I updated the comment.