Skip to content

Commit ef0b763

Browse files
authored
Enable strict optional for more test files (8) (#15606)
Followup to #15586
1 parent 91e4ce4 commit ef0b763

File tree

6 files changed

+202
-163
lines changed

6 files changed

+202
-163
lines changed

mypy/test/testcheck.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,6 @@
5252
# TODO: Enable strict optional in test cases by default. Remove files here, once test cases are updated
5353
no_strict_optional_files = {
5454
"check-modules.test",
55-
"check-namedtuple.test",
56-
"check-overloading.test",
57-
"check-plugin-attrs.test",
58-
"check-statements.test",
59-
"check-tuples.test",
6055
"check-unions.test",
6156
"check-varargs.test",
6257
}

test-data/unit/check-namedtuple.test

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from collections import namedtuple
33

44
X = namedtuple('X', 'x y')
5-
x = None # type: X
5+
x: X
66
a, b = x
77
b = x[0]
88
a = x[1]
@@ -14,7 +14,7 @@ x[2] # E: Tuple index out of range
1414
from collections import namedtuple
1515

1616
X = namedtuple('X', ('x', 'y'))
17-
x = None # type: X
17+
x: X
1818
a, b = x
1919
b = x[0]
2020
a = x[1]
@@ -32,7 +32,7 @@ X = namedtuple('X', 'x, _y, _z') # E: "namedtuple()" field names cannot start w
3232
from collections import namedtuple
3333

3434
X = namedtuple('X', 'x y')
35-
x = None # type: X
35+
x: X
3636
x.x
3737
x.y
3838
x.z # E: "X" has no attribute "z"
@@ -63,13 +63,13 @@ class A(NamedTuple):
6363
from collections import namedtuple
6464

6565
X = namedtuple('X', 'x y')
66-
x = None # type: X
66+
x: X
6767
x.x = 5 # E: Property "x" defined in "X" is read-only
6868
x.y = 5 # E: Property "y" defined in "X" is read-only
6969
x.z = 5 # E: "X" has no attribute "z"
7070

7171
class A(X): pass
72-
a = None # type: A
72+
a: A
7373
a.x = 5 # E: Property "x" defined in "X" is read-only
7474
a.y = 5 # E: Property "y" defined in "X" is read-only
7575
-- a.z = 5 # not supported yet
@@ -292,7 +292,7 @@ A = NamedTuple('A', [('a', int), ('b', str)])
292292
class B(A): pass
293293
a = A(1, '')
294294
b = B(1, '')
295-
t = None # type: Tuple[int, str]
295+
t: Tuple[int, str]
296296
if int():
297297
b = a # E: Incompatible types in assignment (expression has type "A", variable has type "B")
298298
if int():
@@ -357,7 +357,7 @@ C(2).b
357357
from collections import namedtuple
358358

359359
X = namedtuple('X', ['x', 'y'])
360-
x = None # type: X
360+
x: X
361361
reveal_type(x._asdict()) # N: Revealed type is "builtins.dict[builtins.str, Any]"
362362

363363
[builtins fixtures/dict.pyi]
@@ -366,7 +366,7 @@ reveal_type(x._asdict()) # N: Revealed type is "builtins.dict[builtins.str, Any
366366
from collections import namedtuple
367367

368368
X = namedtuple('X', ['x', 'y'])
369-
x = None # type: X
369+
x: X
370370
reveal_type(x._replace()) # N: Revealed type is "Tuple[Any, Any, fallback=__main__.X]"
371371
x._replace(y=5)
372372
x._replace(x=3)
@@ -391,7 +391,7 @@ X._replace(x=1, y=2) # E: Missing positional argument "_self" in call to "_repl
391391
from typing import NamedTuple
392392

393393
X = NamedTuple('X', [('x', int), ('y', str)])
394-
x = None # type: X
394+
x: X
395395
reveal_type(x._replace()) # N: Revealed type is "Tuple[builtins.int, builtins.str, fallback=__main__.X]"
396396
x._replace(x=5)
397397
x._replace(y=5) # E: Argument "y" to "_replace" of "X" has incompatible type "int"; expected "str"
@@ -405,7 +405,7 @@ reveal_type(X._make([5, 'a'])) # N: Revealed type is "Tuple[builtins.int, built
405405
X._make('a b') # E: Argument 1 to "_make" of "X" has incompatible type "str"; expected "Iterable[Any]"
406406

407407
-- # FIX: not a proper class method
408-
-- x = None # type: X
408+
-- x: X
409409
-- reveal_type(x._make([5, 'a'])) # N: Revealed type is "Tuple[builtins.int, builtins.str, fallback=__main__.X]"
410410
-- x._make('a b') # E: Argument 1 to "_make" of "X" has incompatible type "str"; expected Iterable[Any]
411411

@@ -423,7 +423,7 @@ from typing import NamedTuple
423423

424424
X = NamedTuple('X', [('x', int), ('y', str)])
425425
reveal_type(X._source) # N: Revealed type is "builtins.str"
426-
x = None # type: X
426+
x: X
427427
reveal_type(x._source) # N: Revealed type is "builtins.str"
428428
[builtins fixtures/tuple.pyi]
429429

@@ -459,7 +459,7 @@ from typing import NamedTuple
459459

460460
X = NamedTuple('X', [('x', int), ('y', str)])
461461
reveal_type(X._field_types) # N: Revealed type is "builtins.dict[builtins.str, Any]"
462-
x = None # type: X
462+
x: X
463463
reveal_type(x._field_types) # N: Revealed type is "builtins.dict[builtins.str, Any]"
464464

465465
[builtins fixtures/dict.pyi]
@@ -472,7 +472,7 @@ def f(x: A) -> None: pass
472472

473473
class B(NamedTuple('B', []), A): pass
474474
f(B())
475-
x = None # type: A
475+
x: A
476476
if int():
477477
x = B()
478478

@@ -482,7 +482,7 @@ def g(x: C) -> None: pass
482482
class D(NamedTuple('D', []), A): pass
483483

484484
g(D()) # E: Argument 1 to "g" has incompatible type "D"; expected "C"
485-
y = None # type: C
485+
y: C
486486
if int():
487487
y = D() # E: Incompatible types in assignment (expression has type "D", variable has type "C")
488488
[builtins fixtures/tuple.pyi]
@@ -499,9 +499,9 @@ class A(NamedTuple('A', [('x', str)])):
499499
class B(A):
500500
pass
501501

502-
a = None # type: A
502+
a: A
503503
a = A('').member()
504-
b = None # type: B
504+
b: B
505505
b = B('').member()
506506
a = B('')
507507
a = B('').member()
@@ -511,14 +511,14 @@ a = B('').member()
511511
from typing import NamedTuple, TypeVar
512512
A = NamedTuple('A', [('x', str)])
513513
reveal_type(A('hello')._replace(x='')) # N: Revealed type is "Tuple[builtins.str, fallback=__main__.A]"
514-
a = None # type: A
514+
a: A
515515
a = A('hello')._replace(x='')
516516

517517
class B(A):
518518
pass
519519

520520
reveal_type(B('hello')._replace(x='')) # N: Revealed type is "Tuple[builtins.str, fallback=__main__.B]"
521-
b = None # type: B
521+
b: B
522522
b = B('hello')._replace(x='')
523523
[builtins fixtures/tuple.pyi]
524524

test-data/unit/check-overloading.test

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,8 @@ class C: pass
450450
from foo import *
451451
[file foo.pyi]
452452
from typing import overload
453-
a, b = None, None # type: (A, B)
453+
a: A
454+
b: B
454455
if int():
455456
b = f(a) # E: Incompatible types in assignment (expression has type "A", variable has type "B")
456457
if int():
@@ -492,7 +493,8 @@ class C: pass
492493
from foo import *
493494
[file foo.pyi]
494495
from typing import overload
495-
a, b = None, None # type: (A, B)
496+
a: A
497+
b: B
496498
if int():
497499
b = a.f(a) # E: Incompatible types in assignment (expression has type "A", variable has type "B")
498500
if int():
@@ -514,7 +516,8 @@ class B: pass
514516
from foo import *
515517
[file foo.pyi]
516518
from typing import overload
517-
a, b = None, None # type: (A, B)
519+
a: A
520+
b: B
518521
if int():
519522
a = f(a)
520523
if int():
@@ -549,7 +552,10 @@ from foo import *
549552
[file foo.pyi]
550553
from typing import overload, TypeVar, Generic
551554
t = TypeVar('t')
552-
ab, ac, b, c = None, None, None, None # type: (A[B], A[C], B, C)
555+
ab: A[B]
556+
ac: A[C]
557+
b: B
558+
c: C
553559
if int():
554560
b = f(ab)
555561
c = f(ac)
@@ -569,7 +575,8 @@ class C: pass
569575
from foo import *
570576
[file foo.pyi]
571577
from typing import overload
572-
a, b = None, None # type: (A, B)
578+
a: A
579+
b: B
573580
a = A(a)
574581
a = A(b)
575582
a = A(object()) # E: No overload variant of "A" matches argument type "object" \
@@ -589,8 +596,8 @@ class B: pass
589596
from foo import *
590597
[file foo.pyi]
591598
from typing import overload, Callable
592-
o = None # type: object
593-
a = None # type: A
599+
o: object
600+
a: A
594601

595602
if int():
596603
a = f # E: Incompatible types in assignment (expression has type overloaded function, variable has type "A")
@@ -607,7 +614,8 @@ class A: pass
607614
from foo import *
608615
[file foo.pyi]
609616
from typing import overload
610-
t, a = None, None # type: (type, A)
617+
t: type
618+
a: A
611619

612620
if int():
613621
a = A # E: Incompatible types in assignment (expression has type "Type[A]", variable has type "A")
@@ -625,7 +633,8 @@ class B: pass
625633
from foo import *
626634
[file foo.pyi]
627635
from typing import overload
628-
a, b = None, None # type: int, str
636+
a: int
637+
b: str
629638
if int():
630639
a = A()[a]
631640
if int():
@@ -647,7 +656,9 @@ from foo import *
647656
[file foo.pyi]
648657
from typing import TypeVar, Generic, overload
649658
t = TypeVar('t')
650-
a, b, c = None, None, None # type: (A, B, C[A])
659+
a: A
660+
b: B
661+
c: C[A]
651662
if int():
652663
a = c[a]
653664
b = c[a] # E: Incompatible types in assignment (expression has type "A", variable has type "B")
@@ -761,7 +772,8 @@ from typing import overload
761772
def f(t: type) -> 'A': pass
762773
@overload
763774
def f(t: 'A') -> 'B': pass
764-
a, b = None, None # type: (A, B)
775+
a: A
776+
b: B
765777
if int():
766778
a = f(A)
767779
if int():
@@ -1215,6 +1227,7 @@ f(*(1, '', 1))() # E: No overload variant of "f" matches argument type "Tuple[in
12151227
[builtins fixtures/tuple.pyi]
12161228

12171229
[case testPreferExactSignatureMatchInOverload]
1230+
# flags: --no-strict-optional
12181231
from foo import *
12191232
[file foo.pyi]
12201233
from typing import overload, List

test-data/unit/check-plugin-attrs.test

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,7 @@ class A(Generic[T]):
672672
[builtins fixtures/classmethod.pyi]
673673

674674
[case testAttrsForwardReference]
675+
# flags: --no-strict-optional
675676
import attr
676677
@attr.s(auto_attribs=True)
677678
class A:
@@ -687,6 +688,7 @@ A(B(None))
687688
[builtins fixtures/list.pyi]
688689

689690
[case testAttrsForwardReferenceInClass]
691+
# flags: --no-strict-optional
690692
import attr
691693
@attr.s(auto_attribs=True)
692694
class A:
@@ -1593,6 +1595,7 @@ def f(t: TA) -> None:
15931595
[builtins fixtures/plugin_attrs.pyi]
15941596

15951597
[case testNonattrsFields]
1598+
# flags: --no-strict-optional
15961599
from typing import Any, cast, Type
15971600
from attrs import fields
15981601

0 commit comments

Comments
 (0)