Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit39d35cd

Browse files
authored
Fix new style union syntax in type aliases (#14008)
Fix Python 3.10 `|` union syntax in type aliases, when one ofthe operands is a type alias or a type with an overloaded `__init__`.We can now infer `typing._SpecialForm` for type aliases in a runtime context.Also create a bunch of minimal test-only stubs for stdlib modules to fix some test failures caused by the missing `typing._SpecialForm`in the default test stubs. This is generally what we want in any case, since using typeshed stubs with minimal builtins/typing stubs canresult in unpredictable behavior and slow tests.Fixes#12368.Fixes#12005.Fixes#11426.
1 parentd2a3e66 commit39d35cd

21 files changed

+186
-27
lines changed

‎mypy/checker.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6048,11 +6048,11 @@ def lookup_qualified(self, name: str) -> SymbolTableNode:
60486048
last=parts[-1]
60496049
iflastinn.names:
60506050
returnn.names[last]
6051-
eliflen(parts)==2andparts[0]=="builtins":
6052-
fullname="builtins."+last
6051+
eliflen(parts)==2andparts[0]in ("builtins","typing"):
6052+
fullname=".".join(parts)
60536053
iffullnameinSUGGESTED_TEST_FIXTURES:
6054-
suggestion=", e.g. add '[builtins fixtures/{}]' to your test".format(
6055-
SUGGESTED_TEST_FIXTURES[fullname]
6054+
suggestion=", e.g. add '[{} fixtures/{}]' to your test".format(
6055+
parts[0],SUGGESTED_TEST_FIXTURES[fullname]
60566056
)
60576057
else:
60586058
suggestion=""

‎mypy/checkexpr.py‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3870,9 +3870,8 @@ class LongName(Generic[T]): ...
38703870
else:
38713871
ifalias_definition:
38723872
returnAnyType(TypeOfAny.special_form)
3873-
# This type is invalid in most runtime contexts, give it an 'object' type.
3874-
# TODO: Use typing._SpecialForm instead?
3875-
returnself.named_type("builtins.object")
3873+
# The _SpecialForm type can be used in some runtime contexts (e.g. it may have __or__).
3874+
returnself.named_type("typing._SpecialForm")
38763875

38773876
defapply_type_arguments_to_callable(
38783877
self,tp:Type,args:Sequence[Type],ctx:Context
@@ -4742,7 +4741,7 @@ def has_member(self, typ: Type, member: str) -> bool:
47424741
typ=typ.fallback
47434742
ifisinstance(typ,Instance):
47444743
returntyp.type.has_readable_member(member)
4745-
ifisinstance(typ,CallableType)andtyp.is_type_obj():
4744+
ifisinstance(typ,FunctionLike)andtyp.is_type_obj():
47464745
returntyp.fallback.type.has_readable_member(member)
47474746
elifisinstance(typ,AnyType):
47484747
returnTrue

‎mypy/messages.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@
132132
"builtins.isinstance":"isinstancelist.pyi",
133133
"builtins.property":"property.pyi",
134134
"builtins.classmethod":"classmethod.pyi",
135+
"typing._SpecialForm":"typing-medium.pyi",
135136
}
136137

137138

@@ -2253,6 +2254,9 @@ def format_literal_value(typ: LiteralType) -> str:
22532254
ifitype.extra_attrsanditype.extra_attrs.mod_nameandmodule_names:
22542255
returnf"{base_str}{itype.extra_attrs.mod_name}"
22552256
returnbase_str
2257+
ifitype.type.fullname=="typing._SpecialForm":
2258+
# This is not a real type but used for some typing-related constructs.
2259+
return"<typing special form>"
22562260
ifverbosity>=2or (fullnamesanditype.type.fullnameinfullnames):
22572261
base_str=itype.type.fullname
22582262
else:

‎mypyc/test-data/run-async.test‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ async def g() -> int:
1313
async def f() -> int:
1414
return await g()
1515

16+
[file asyncio/__init__.pyi]
17+
async def sleep(t: float) -> None: ...
18+
1619
[typing fixtures/typing-full.pyi]
1720

1821
[file driver.py]

‎test-data/unit/check-ctypes.test‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ a[3] = b"bytes" # E: No overload variant of "__setitem__" of "Array" matches ar
2020
for x in a:
2121
reveal_type(x) # N: Revealed type is "builtins.int"
2222
[builtins fixtures/floatdict.pyi]
23+
[typing fixtures/typing-medium.pyi]
2324

2425
[case testCtypesArrayCustomElementType]
2526
import ctypes
@@ -52,6 +53,7 @@ myu: Union[ctypes.Array[ctypes.c_int], List[str]]
5253
for myi in myu:
5354
reveal_type(myi) # N: Revealed type is "Union[builtins.int, builtins.str]"
5455
[builtins fixtures/floatdict.pyi]
56+
[typing fixtures/typing-medium.pyi]
5557

5658
[case testCtypesArrayUnionElementType]
5759
import ctypes
@@ -76,6 +78,7 @@ mya[3] = b"bytes" # E: No overload variant of "__setitem__" of "Array" matches
7678
for myx in mya:
7779
reveal_type(myx) # N: Revealed type is "Union[__main__.MyCInt, builtins.int]"
7880
[builtins fixtures/floatdict.pyi]
81+
[typing fixtures/typing-medium.pyi]
7982

8083
[case testCtypesCharArrayAttrs]
8184
import ctypes
@@ -84,13 +87,15 @@ ca = (ctypes.c_char * 4)(b'a', b'b', b'c', b'\x00')
8487
reveal_type(ca.value) # N: Revealed type is "builtins.bytes"
8588
reveal_type(ca.raw) # N: Revealed type is "builtins.bytes"
8689
[builtins fixtures/floatdict.pyi]
90+
[typing fixtures/typing-medium.pyi]
8791

8892
[case testCtypesCharPArrayDoesNotCrash]
8993
import ctypes
9094

9195
# The following line used to crash with "Could not find builtin symbol 'NoneType'"
9296
ca = (ctypes.c_char_p * 0)()
9397
[builtins fixtures/floatdict.pyi]
98+
[typing fixtures/typing-medium.pyi]
9499

95100
[case testCtypesWcharArrayAttrs]
96101
import ctypes
@@ -99,6 +104,7 @@ wca = (ctypes.c_wchar * 4)('a', 'b', 'c', '\x00')
99104
reveal_type(wca.value) # N: Revealed type is "builtins.str"
100105
wca.raw # E: Array attribute "raw" is only available with element type "c_char", not "c_wchar"
101106
[builtins fixtures/floatdict.pyi]
107+
[typing fixtures/typing-medium.pyi]
102108

103109
[case testCtypesCharUnionArrayAttrs]
104110
import ctypes
@@ -108,6 +114,7 @@ cua: ctypes.Array[Union[ctypes.c_char, ctypes.c_wchar]]
108114
reveal_type(cua.value) # N: Revealed type is "Union[builtins.bytes, builtins.str]"
109115
cua.raw # E: Array attribute "raw" is only available with element type "c_char", not "Union[c_char, c_wchar]"
110116
[builtins fixtures/floatdict.pyi]
117+
[typing fixtures/typing-medium.pyi]
111118

112119
[case testCtypesAnyUnionArrayAttrs]
113120
import ctypes
@@ -117,6 +124,7 @@ caa: ctypes.Array[Union[ctypes.c_char, Any]]
117124
reveal_type(caa.value) # N: Revealed type is "Union[builtins.bytes, Any]"
118125
reveal_type(caa.raw) # N: Revealed type is "builtins.bytes"
119126
[builtins fixtures/floatdict.pyi]
127+
[typing fixtures/typing-medium.pyi]
120128

121129
[case testCtypesOtherUnionArrayAttrs]
122130
import ctypes
@@ -126,6 +134,7 @@ cua: ctypes.Array[Union[ctypes.c_char, ctypes.c_int]]
126134
cua.value # E: Array attribute "value" is only available with element type "c_char" or "c_wchar", not "Union[c_char, c_int]"
127135
cua.raw # E: Array attribute "raw" is only available with element type "c_char", not "Union[c_char, c_int]"
128136
[builtins fixtures/floatdict.pyi]
137+
[typing fixtures/typing-medium.pyi]
129138

130139
[case testCtypesAnyArrayAttrs]
131140
import ctypes
@@ -134,6 +143,7 @@ aa: ctypes.Array[Any]
134143
reveal_type(aa.value) # N: Revealed type is "Any"
135144
reveal_type(aa.raw) # N: Revealed type is "builtins.bytes"
136145
[builtins fixtures/floatdict.pyi]
146+
[typing fixtures/typing-medium.pyi]
137147

138148
[case testCtypesOtherArrayAttrs]
139149
import ctypes
@@ -142,6 +152,7 @@ oa = (ctypes.c_int * 4)(1, 2, 3, 4)
142152
oa.value # E: Array attribute "value" is only available with element type "c_char" or "c_wchar", not "c_int"
143153
oa.raw # E: Array attribute "raw" is only available with element type "c_char", not "c_int"
144154
[builtins fixtures/floatdict.pyi]
155+
[typing fixtures/typing-medium.pyi]
145156

146157
[case testCtypesArrayConstructorStarargs]
147158
import ctypes
@@ -154,6 +165,7 @@ reveal_type(intarr4(*int_values)) # N: Revealed type is "ctypes.Array[ctypes.c_
154165
reveal_type(intarr4(*c_int_values)) # N: Revealed type is "ctypes.Array[ctypes.c_int]"
155166
reveal_type(intarr6(1, ctypes.c_int(2), *int_values)) # N: Revealed type is "ctypes.Array[ctypes.c_int]"
156167
reveal_type(intarr6(1, ctypes.c_int(2), *c_int_values)) # N: Revealed type is "ctypes.Array[ctypes.c_int]"
168+
[typing fixtures/typing-medium.pyi]
157169

158170
float_values = [1.0, 2.0, 3.0, 4.0]
159171
intarr4(*float_values) # E: Array constructor argument 1 of type "List[float]" is not convertible to the array element type "Iterable[c_int]"
@@ -167,3 +179,4 @@ x = {"a": 1, "b": 2}
167179
intarr4(**x)
168180

169181
[builtins fixtures/floatdict.pyi]
182+
[typing fixtures/typing-medium.pyi]

‎test-data/unit/check-dataclasses.test‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -633,8 +633,9 @@ class Two:
633633

634634
c = Two()
635635
x = c.S
636-
reveal_type(x) # N: Revealed type is "builtins.object"
636+
reveal_type(x) # N: Revealed type is "typing._SpecialForm"
637637
[builtins fixtures/dataclasses.pyi]
638+
[typing fixtures/typing-medium.pyi]
638639

639640
[case testDataclassOrdering]
640641
# flags: --python-version 3.7

‎test-data/unit/check-generics.test‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,20 +1049,20 @@ CA = Callable[[T], int]
10491049
TA = Tuple[T, int]
10501050
UA = Union[T, int]
10511051

1052-
cs = CA + 1 # E: Unsupported left operand type for + ("object")
1052+
cs = CA + 1 # E: Unsupported left operand type for + ("<typing special form>")
10531053
reveal_type(cs) # N: Revealed type is "Any"
10541054

1055-
ts = TA() # E: "object" not callable
1055+
ts = TA() # E: "<typing special form>" not callable
10561056
reveal_type(ts) # N: Revealed type is "Any"
10571057

1058-
us = UA.x # E: "object" has no attribute "x"
1058+
us = UA.x # E: "<typing special form>" has no attribute "x"
10591059
reveal_type(us) # N: Revealed type is "Any"
10601060

10611061
xx = CA[str] + 1 # E: Type application is only supported for generic classes
10621062
yy = TA[str]() # E: Type application is only supported for generic classes
10631063
zz = UA[str].x # E: Type application is only supported for generic classes
10641064
[builtins fixtures/tuple.pyi]
1065-
1065+
[typing fixtures/typing-medium.pyi]
10661066
[out]
10671067

10681068
[case testGenericTypeAliasesTypeVarBinding]

‎test-data/unit/check-literal.test‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1484,16 +1484,17 @@ Alias = Literal[3]
14841484

14851485
isinstance(3, Literal[3]) # E: Cannot use isinstance() with Literal type
14861486
isinstance(3, Alias) # E: Cannot use isinstance() with Literal type \
1487-
# E: Argument 2 to "isinstance" has incompatible type "object"; expected "Union[type, Tuple[Any, ...]]"
1487+
# E: Argument 2 to "isinstance" has incompatible type "<typing special form>"; expected "Union[type, Tuple[Any, ...]]"
14881488
isinstance(3, Renamed[3]) # E: Cannot use isinstance() with Literal type
14891489
isinstance(3, indirect.Literal[3]) # E: Cannot use isinstance() with Literal type
14901490

14911491
issubclass(int, Literal[3]) # E: Cannot use issubclass() with Literal type
14921492
issubclass(int, Alias) # E: Cannot use issubclass() with Literal type \
1493-
# E: Argument 2 to "issubclass" has incompatible type "object"; expected "Union[type, Tuple[Any, ...]]"
1493+
# E: Argument 2 to "issubclass" has incompatible type "<typing special form>"; expected "Union[type, Tuple[Any, ...]]"
14941494
issubclass(int, Renamed[3]) # E: Cannot use issubclass() with Literal type
14951495
issubclass(int, indirect.Literal[3]) # E: Cannot use issubclass() with Literal type
14961496
[builtins fixtures/isinstancelist.pyi]
1497+
[typing fixtures/typing-medium.pyi]
14971498
[out]
14981499

14991500
[case testLiteralErrorsWhenSubclassed]

‎test-data/unit/check-python310.test‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1788,3 +1788,19 @@ def f6(a: object) -> None:
17881788
case _ if y is not None: # E: Name "y" may be undefined
17891789
pass
17901790
[builtins fixtures/tuple.pyi]
1791+
1792+
[case testTypeAliasWithNewUnionSyntaxAndNoneLeftOperand]
1793+
from typing import overload
1794+
class C:
1795+
@overload
1796+
def __init__(self) -> None: pass
1797+
@overload
1798+
def __init__(self, x: int) -> None: pass
1799+
def __init__(self, x=0):
1800+
pass
1801+
1802+
class D: pass
1803+
1804+
X = None | C
1805+
Y = None | D
1806+
[builtins fixtures/type.pyi]

‎test-data/unit/check-type-aliases.test‎

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -821,28 +821,28 @@ c = Child()
821821

822822
reveal_type(NormalImplicit) # N: Revealed type is "def () -> __main__.Foo"
823823
reveal_type(NormalExplicit) # N: Revealed type is "def () -> __main__.Foo"
824-
reveal_type(SpecialImplicit) # N: Revealed type is "builtins.object"
825-
reveal_type(SpecialExplicit) # N: Revealed type is "builtins.object"
824+
reveal_type(SpecialImplicit) # N: Revealed type is "typing._SpecialForm"
825+
reveal_type(SpecialExplicit) # N: Revealed type is "typing._SpecialForm"
826826

827827
reveal_type(Parent.NormalImplicit) # N: Revealed type is "def () -> __main__.Foo"
828828
reveal_type(Parent.NormalExplicit) # N: Revealed type is "def () -> __main__.Foo"
829-
reveal_type(Parent.SpecialImplicit) # N: Revealed type is "builtins.object"
830-
reveal_type(Parent.SpecialExplicit) # N: Revealed type is "builtins.object"
829+
reveal_type(Parent.SpecialImplicit) # N: Revealed type is "typing._SpecialForm"
830+
reveal_type(Parent.SpecialExplicit) # N: Revealed type is "typing._SpecialForm"
831831

832832
reveal_type(Child.NormalImplicit) # N: Revealed type is "def () -> __main__.Foo"
833833
reveal_type(Child.NormalExplicit) # N: Revealed type is "def () -> __main__.Foo"
834-
reveal_type(Child.SpecialImplicit) # N: Revealed type is "builtins.object"
835-
reveal_type(Child.SpecialExplicit) # N: Revealed type is "builtins.object"
834+
reveal_type(Child.SpecialImplicit) # N: Revealed type is "typing._SpecialForm"
835+
reveal_type(Child.SpecialExplicit) # N: Revealed type is "typing._SpecialForm"
836836

837837
reveal_type(p.NormalImplicit) # N: Revealed type is "def () -> __main__.Foo"
838838
reveal_type(p.NormalExplicit) # N: Revealed type is "def () -> __main__.Foo"
839-
reveal_type(p.SpecialImplicit) # N: Revealed type is "builtins.object"
840-
reveal_type(p.SpecialExplicit) # N: Revealed type is "builtins.object"
839+
reveal_type(p.SpecialImplicit) # N: Revealed type is "typing._SpecialForm"
840+
reveal_type(p.SpecialExplicit) # N: Revealed type is "typing._SpecialForm"
841841

842842
reveal_type(c.NormalImplicit) # N: Revealed type is "def () -> __main__.Foo"
843843
reveal_type(p.NormalExplicit) # N: Revealed type is "def () -> __main__.Foo"
844-
reveal_type(c.SpecialImplicit) # N: Revealed type is "builtins.object"
845-
reveal_type(c.SpecialExplicit) # N: Revealed type is "builtins.object"
844+
reveal_type(c.SpecialImplicit) # N: Revealed type is "typing._SpecialForm"
845+
reveal_type(c.SpecialExplicit) # N: Revealed type is "typing._SpecialForm"
846846

847847
# Use type aliases in a type alias context in a plausible way
848848

@@ -895,6 +895,7 @@ reveal_type(weird_child_2) # N: Revealed type is "def () -> Any"
895895
reveal_type(weird_child_3) # N: Revealed type is "def () -> Any"
896896
reveal_type(weird_child_4) # N: Revealed type is "def () -> Any"
897897
[builtins fixtures/tuple.pyi]
898+
[typing fixtures/typing-medium.pyi]
898899

899900
[case testMalformedTypeAliasRuntimeReassignments]
900901
from typing import Union
@@ -927,8 +928,8 @@ SpecialExplicit = 4 # E: Cannot assign multiple types to name "SpecialExplicit"
927928

928929
Parent.NormalImplicit = 4 # E: Incompatible types in assignment (expression has type "int", variable has type "Type[Foo]")
929930
Parent.NormalExplicit = 4 # E: Incompatible types in assignment (expression has type "int", variable has type "Type[Foo]")
930-
Parent.SpecialImplicit = 4
931-
Parent.SpecialExplicit = 4
931+
Parent.SpecialImplicit = 4 # E: Incompatible types in assignment (expression has type "int", variable has type "<typing special form>")
932+
Parent.SpecialExplicit = 4 # E: Incompatible types in assignment (expression has type "int", variable has type "<typing special form>")
932933

933934
Child.NormalImplicit = 4 # E: Incompatible types in assignment (expression has type "int", variable has type "Type[Foo]")
934935
Child.NormalExplicit = 4 # E: Incompatible types in assignment (expression has type "int", variable has type "Type[Foo]")
@@ -945,3 +946,4 @@ c.NormalExplicit = 4 # E: Incompatible types in assignment (expression has type
945946
c.SpecialImplicit = 4
946947
c.SpecialExplicit = 4
947948
[builtins fixtures/tuple.pyi]
949+
[typing fixtures/typing-medium.pyi]

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp