Skip to content

Commit d4865b2

Browse files
authored
Enable strict optional for more test files (2) (#15596)
1 parent 8f66718 commit d4865b2

14 files changed

+97
-81
lines changed

mypy/test/testcheck.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,33 +55,20 @@
5555
"check-bound.test",
5656
"check-dynamic-typing.test",
5757
"check-expressions.test",
58-
"check-formatting.test",
5958
"check-functions.test",
6059
"check-generic-subtyping.test",
6160
"check-generics.test",
62-
"check-incremental.test",
6361
"check-inference-context.test",
6462
"check-inference.test",
65-
"check-inline-config.test",
6663
"check-isinstance.test",
6764
"check-kwargs.test",
68-
"check-lists.test",
6965
"check-literal.test",
7066
"check-modules.test",
7167
"check-namedtuple.test",
72-
"check-newsemanal.test",
7368
"check-overloading.test",
7469
"check-plugin-attrs.test",
75-
"check-protocols.test",
76-
"check-selftype.test",
77-
"check-serialize.test",
7870
"check-statements.test",
79-
"check-super.test",
8071
"check-tuples.test",
81-
"check-type-aliases.test",
82-
"check-type-checks.test",
83-
"check-typeddict.test",
84-
"check-typevar-values.test",
8572
"check-unions.test",
8673
"check-varargs.test",
8774
}

test-data/unit/check-formatting.test

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
[case testStringInterpolationType]
66
from typing import Tuple
7-
i, f, s, t = None, None, None, None # type: (int, float, str, Tuple[int])
7+
i: int
8+
f: float
9+
s: str
10+
t: Tuple[int]
811
'%d' % i
912
'%f' % f
1013
'%s' % s
@@ -21,7 +24,9 @@ i, f, s, t = None, None, None, None # type: (int, float, str, Tuple[int])
2124

2225
[case testStringInterpolationSAcceptsAnyType]
2326
from typing import Any
24-
i, o, s = None, None, None # type: (int, object, str)
27+
i: int
28+
o: object
29+
s: str
2530
'%s %s %s' % (i, o, s)
2631
[builtins fixtures/primitives.pyi]
2732

@@ -139,8 +144,10 @@ class BytesThing:
139144
def __getitem__(self, __key: bytes) -> str:
140145
...
141146

142-
a = None # type: Any
143-
ds, do, di = None, None, None # type: Dict[str, int], Dict[object, int], Dict[int, int]
147+
a: Any
148+
ds: Dict[str, int]
149+
do: Dict[object, int]
150+
di: Dict[int, int]
144151
'%(a)' % 1 # E: Format requires a mapping (expression has type "int", expected type for mapping is "SupportsKeysAndGetItem[str, Any]")
145152
'%()d' % a
146153
'%()d' % ds

test-data/unit/check-incremental.test

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1734,12 +1734,12 @@ class R: pass
17341734
[file r/s.py]
17351735
from . import m
17361736
R = m.R
1737-
a = None # type: R
1737+
a: R
17381738

17391739
[file r/s.py.2]
17401740
from . import m
17411741
R = m.R
1742-
a = None # type: R
1742+
a: R
17431743

17441744
[case testIncrementalBaseClassAttributeConflict]
17451745
class A: pass
@@ -2603,7 +2603,7 @@ def output() -> IntNode[str]:
26032603
return Node(1, 'x')
26042604
x = output() # type: IntNode
26052605

2606-
y = None # type: IntNode
2606+
y: IntNode
26072607
y.x = 1
26082608
y.y = 1
26092609
y.y = 'x'
@@ -2625,7 +2625,7 @@ def output() -> IntNode[str]:
26252625
return Node(1, 'x')
26262626
x = output() # type: IntNode
26272627

2628-
y = None # type: IntNode
2628+
y: IntNode
26292629
y.x = 1
26302630
y.y = 1
26312631
y.y = 'x'

test-data/unit/check-inline-config.test

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
# mypy: disallow-any-generics, no-warn-no-return
66

7-
from typing import List
8-
def foo() -> List: # E: Missing type parameters for generic type "List"
7+
from typing import List, Optional
8+
def foo() -> Optional[List]: # E: Missing type parameters for generic type "List"
99
20
1010

1111
[builtins fixtures/list.pyi]
@@ -15,8 +15,8 @@ def foo() -> List: # E: Missing type parameters for generic type "List"
1515
# mypy: disallow-any-generics
1616
# mypy: no-warn-no-return
1717

18-
from typing import List
19-
def foo() -> List: # E: Missing type parameters for generic type "List"
18+
from typing import List, Optional
19+
def foo() -> Optional[List]: # E: Missing type parameters for generic type "List"
2020
20
2121

2222
[builtins fixtures/list.pyi]
@@ -25,8 +25,8 @@ def foo() -> List: # E: Missing type parameters for generic type "List"
2525

2626
# mypy: disallow-any-generics=true, warn-no-return=0
2727

28-
from typing import List
29-
def foo() -> List: # E: Missing type parameters for generic type "List"
28+
from typing import List, Optional
29+
def foo() -> Optional[List]: # E: Missing type parameters for generic type "List"
3030
20
3131

3232
[builtins fixtures/list.pyi]
@@ -36,8 +36,8 @@ def foo() -> List: # E: Missing type parameters for generic type "List"
3636

3737
# mypy: disallow-any-generics = true, warn-no-return = 0
3838

39-
from typing import List
40-
def foo() -> List: # E: Missing type parameters for generic type "List"
39+
from typing import List, Optional
40+
def foo() -> Optional[List]: # E: Missing type parameters for generic type "List"
4141
20
4242

4343
[builtins fixtures/list.pyi]
@@ -84,20 +84,20 @@ import a
8484
[file a.py]
8585
# mypy: disallow-any-generics, no-warn-no-return
8686

87-
from typing import List
88-
def foo() -> List:
87+
from typing import List, Optional
88+
def foo() -> Optional[List]:
8989
20
9090

9191
[file a.py.2]
9292
# mypy: no-warn-no-return
9393

94-
from typing import List
95-
def foo() -> List:
94+
from typing import List, Optional
95+
def foo() -> Optional[List]:
9696
20
9797

9898
[file a.py.3]
99-
from typing import List
100-
def foo() -> List:
99+
from typing import List, Optional
100+
def foo() -> Optional[List]:
101101
20
102102
[out]
103103
tmp/a.py:4: error: Missing type parameters for generic type "List"
@@ -131,8 +131,9 @@ tmp/a.py:4: error: Missing type parameters for generic type "List"
131131
import a, b
132132
[file a.py]
133133
# mypy: no-warn-no-return
134+
from typing import Optional
134135

135-
def foo() -> int:
136+
def foo() -> Optional[int]:
136137
20
137138

138139
[file b.py]

test-data/unit/check-lists.test

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33

44
[case testNestedListAssignment]
55
from typing import List
6-
a1, b1, c1 = None, None, None # type: (A, B, C)
7-
a2, b2, c2 = None, None, None # type: (A, B, C)
6+
a1: A
7+
a2: A
8+
b1: B
9+
b2: B
10+
c1: C
11+
c2: C
812

913
if int():
1014
a1, [b1, c1] = a2, [b2, c2]
@@ -21,7 +25,9 @@ class C: pass
2125

2226
[case testNestedListAssignmentToTuple]
2327
from typing import List
24-
a, b, c = None, None, None # type: (A, B, C)
28+
a: A
29+
b: B
30+
c: C
2531

2632
a, b = [a, b]
2733
a, b = [a] # E: Need more than 1 value to unpack (2 expected)
@@ -35,7 +41,9 @@ class C: pass
3541

3642
[case testListAssignmentFromTuple]
3743
from typing import List
38-
a, b, c = None, None, None # type: (A, B, C)
44+
a: A
45+
b: B
46+
c: C
3947
t = a, b
4048

4149
if int():
@@ -55,7 +63,9 @@ class C: pass
5563

5664
[case testListAssignmentUnequalAmountToUnpack]
5765
from typing import List
58-
a, b, c = None, None, None # type: (A, B, C)
66+
a: A
67+
b: B
68+
c: C
5969

6070
def f() -> None: # needed because test parser tries to parse [a, b] as section header
6171
[a, b] = [a, b]

test-data/unit/check-newsemanal.test

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2149,6 +2149,7 @@ x: C
21492149
class C:
21502150
def frob(self, foos: Dict[Any, Foos]) -> None:
21512151
foo = foos.get(1)
2152+
assert foo
21522153
dict(foo)
21532154
[builtins fixtures/dict.pyi]
21542155

test-data/unit/check-protocols.test

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,9 @@ class MyHashable(Protocol):
319319
class C:
320320
__my_hash__ = None
321321

322-
var: MyHashable = C() # E: Incompatible types in assignment (expression has type "C", variable has type "MyHashable")
322+
var: MyHashable = C() # E: Incompatible types in assignment (expression has type "C", variable has type "MyHashable") \
323+
# N: Following member(s) of "C" have conflicts: \
324+
# N: __my_hash__: expected "Callable[[], int]", got "None"
323325

324326
[case testNoneDisablesProtocolSubclassingWithStrictOptional]
325327
# flags: --strict-optional
@@ -1263,13 +1265,13 @@ if int():
12631265
[builtins fixtures/classmethod.pyi]
12641266

12651267
[case testOverloadedMethodsInProtocols]
1266-
from typing import overload, Protocol, Union
1268+
from typing import overload, Protocol, Union, Optional
12671269

12681270
class P(Protocol):
12691271
@overload
1270-
def f(self, x: int) -> int: pass
1272+
def f(self, x: int) -> Optional[int]: pass
12711273
@overload
1272-
def f(self, x: str) -> str: pass
1274+
def f(self, x: str) -> Optional[str]: pass
12731275

12741276
class C:
12751277
def f(self, x: Union[int, str]) -> None:
@@ -1286,9 +1288,9 @@ main:18: error: Incompatible types in assignment (expression has type "D", varia
12861288
main:18: note: Following member(s) of "D" have conflicts:
12871289
main:18: note: Expected:
12881290
main:18: note: @overload
1289-
main:18: note: def f(self, x: int) -> int
1291+
main:18: note: def f(self, x: int) -> Optional[int]
12901292
main:18: note: @overload
1291-
main:18: note: def f(self, x: str) -> str
1293+
main:18: note: def f(self, x: str) -> Optional[str]
12921294
main:18: note: Got:
12931295
main:18: note: def f(self, x: int) -> None
12941296

@@ -1441,6 +1443,7 @@ def g(x: P, y: P2) -> None: pass
14411443
reveal_type(f(g)) # N: Revealed type is "__main__.P2"
14421444

14431445
[case testMeetOfIncompatibleProtocols]
1446+
# flags: --no-strict-optional
14441447
from typing import Protocol, Callable, TypeVar
14451448

14461449
class P(Protocol):
@@ -1634,6 +1637,7 @@ f(Alias) # E: Only concrete class can be given where "Type[P]" is expected
16341637
f(GoodAlias)
16351638

16361639
[case testInstantiationProtocolInTypeForVariables]
1640+
# flags: --no-strict-optional
16371641
from typing import Type, Protocol
16381642

16391643
class P(Protocol):
@@ -2397,6 +2401,7 @@ x: P = None
23972401
[out]
23982402

23992403
[case testNoneSubtypeOfAllProtocolsWithoutStrictOptional]
2404+
# flags: --no-strict-optional
24002405
from typing import Protocol
24012406
class P(Protocol):
24022407
attr: int

test-data/unit/check-selftype.test

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -897,7 +897,8 @@ class Base(Protocol):
897897

898898
class TweakFunc:
899899
def func(self: Base) -> int:
900-
return reveal_type(super().func()) # N: Revealed type is "builtins.int"
900+
return reveal_type(super().func()) # E: Call to abstract method "func" of "Base" with trivial body via super() is unsafe \
901+
# N: Revealed type is "builtins.int"
901902

902903
class Good:
903904
def func(self) -> int: ...
@@ -1269,14 +1270,14 @@ class AClass:
12691270
...
12701271

12711272
def foo(x: Type[AClass]) -> None:
1272-
reveal_type(x.delete) # N: Revealed type is "Overload(def (id: builtins.int, id2: builtins.int) -> builtins.int, def (id: __main__.AClass, id2: None =) -> builtins.int)"
1273+
reveal_type(x.delete) # N: Revealed type is "Overload(def (id: builtins.int, id2: builtins.int) -> Union[builtins.int, None], def (id: __main__.AClass, id2: None =) -> Union[builtins.int, None])"
12731274
y = x()
1274-
reveal_type(y.delete) # N: Revealed type is "Overload(def (id: builtins.int, id2: builtins.int) -> builtins.int, def (id: __main__.AClass, id2: None =) -> builtins.int)"
1275+
reveal_type(y.delete) # N: Revealed type is "Overload(def (id: builtins.int, id2: builtins.int) -> Union[builtins.int, None], def (id: __main__.AClass, id2: None =) -> Union[builtins.int, None])"
12751276
y.delete(10, 20)
12761277
y.delete(y)
12771278

12781279
def bar(x: AClass) -> None:
1279-
reveal_type(x.delete) # N: Revealed type is "Overload(def (id: builtins.int, id2: builtins.int) -> builtins.int, def (id: __main__.AClass, id2: None =) -> builtins.int)"
1280+
reveal_type(x.delete) # N: Revealed type is "Overload(def (id: builtins.int, id2: builtins.int) -> Union[builtins.int, None], def (id: __main__.AClass, id2: None =) -> Union[builtins.int, None])"
12801281
x.delete(10, 20)
12811282
[builtins fixtures/classmethod.pyi]
12821283

test-data/unit/check-serialize.test

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1275,6 +1275,7 @@ main:2: error: Too many arguments for "f"
12751275
main:2: error: Too many arguments for "f"
12761276

12771277
[case testSerializeDummyType]
1278+
# flags: --no-strict-optional
12781279
import a
12791280
[file a.py]
12801281
import b

test-data/unit/check-super.test

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ class B:
1111
def f(self) -> 'B': pass
1212
class A(B):
1313
def f(self) -> 'A':
14-
a, b = None, None # type: (A, B)
14+
a: A
15+
b: B
1516
if int():
1617
a = super().f() # E: Incompatible types in assignment (expression has type "B", variable has type "A")
1718
a = super().g() # E: "g" undefined in superclass
@@ -26,7 +27,8 @@ class B:
2627
def f(self, y: 'A') -> None: pass
2728
class A(B):
2829
def f(self, y: Any) -> None:
29-
a, b = None, None # type: (A, B)
30+
a: A
31+
b: B
3032
super().f(b) # E: Argument 1 to "f" of "B" has incompatible type "B"; expected "A"
3133
super().f(a)
3234
self.f(b)
@@ -35,6 +37,7 @@ class A(B):
3537
[out]
3638

3739
[case testAccessingSuperInit]
40+
# flags: --no-strict-optional
3841
import typing
3942
class B:
4043
def __init__(self, x: A) -> None: pass
@@ -90,7 +93,7 @@ class B(A):
9093
def __new__(cls, x: int, y: str = '') -> 'B':
9194
super().__new__(cls, 1)
9295
super().__new__(cls, 1, '') # E: Too many arguments for "__new__" of "A"
93-
return None
96+
return cls(1)
9497
B('') # E: Argument 1 to "B" has incompatible type "str"; expected "int"
9598
B(1)
9699
B(1, 'x')

0 commit comments

Comments
 (0)