Skip to content

Commit 6cd8c00

Browse files
authored
Enable strict optional for more test files (3) (#15597)
1 parent d106c84 commit 6cd8c00

File tree

4 files changed

+30
-36
lines changed

4 files changed

+30
-36
lines changed

mypy/test/helpers.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import shutil
88
import sys
99
import time
10-
from typing import Any, Callable, Collection, Iterable, Iterator, Pattern
10+
from typing import Any, Callable, Iterable, Iterator, Pattern
1111

1212
# Exporting Suite as alias to TestCase for backwards compatibility
1313
# TODO: avoid aliasing - import and subclass TestCase directly
@@ -317,10 +317,7 @@ def assert_type(typ: type, value: object) -> None:
317317

318318

319319
def parse_options(
320-
program_text: str,
321-
testcase: DataDrivenTestCase,
322-
incremental_step: int,
323-
no_strict_optional_files: Collection[str] = (),
320+
program_text: str, testcase: DataDrivenTestCase, incremental_step: int
324321
) -> Options:
325322
"""Parse comments like '# flags: --foo' in a test case."""
326323
options = Options()
@@ -342,10 +339,6 @@ def parse_options(
342339
else:
343340
flag_list = []
344341
options = Options()
345-
# TODO: Enable strict optional in test cases by default (requires *many* test case changes)
346-
if os.path.basename(testcase.file) in no_strict_optional_files:
347-
options.strict_optional = False
348-
349342
options.error_summary = False
350343
options.hide_error_codes = True
351344
options.force_uppercase_builtins = True

mypy/test/testfinegrained.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,6 @@
4545
# Set to True to perform (somewhat expensive) checks for duplicate AST nodes after merge
4646
CHECK_CONSISTENCY = False
4747

48-
# TODO: Enable strict optional in test cases by default. Remove files here, once test cases are updated
49-
no_strict_optional_files = {"fine-grained.test", "fine-grained-suggest.test"}
50-
5148

5249
class FineGrainedSuite(DataSuite):
5350
files = find_test_files(
@@ -143,9 +140,7 @@ def run_case(self, testcase: DataDrivenTestCase) -> None:
143140

144141
def get_options(self, source: str, testcase: DataDrivenTestCase, build_cache: bool) -> Options:
145142
# This handles things like '# flags: --foo'.
146-
options = parse_options(
147-
source, testcase, incremental_step=1, no_strict_optional_files=no_strict_optional_files
148-
)
143+
options = parse_options(source, testcase, incremental_step=1)
149144
options.incremental = True
150145
options.use_builtins_fixtures = True
151146
options.show_traceback = True

test-data/unit/fine-grained-suggest.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1090,7 +1090,7 @@ optional2(10)
10901090
optional2('test')
10911091

10921092
def optional3(x: Optional[List[Any]]):
1093-
assert not x
1093+
assert x
10941094
return x[0]
10951095

10961096
optional3(test)

test-data/unit/fine-grained.test

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ main:3: error: "A" has no attribute "x"
191191
[case testVariableTypeBecomesInvalid]
192192
import m
193193
def f() -> None:
194-
a = None # type: m.A
194+
a: m.A
195195
[file m.py]
196196
class A: pass
197197
[file m.py.2]
@@ -1724,15 +1724,15 @@ f = 1
17241724
main:1: error: Module "a" has no attribute "f"
17251725

17261726
[case testDecoratedMethodRefresh]
1727-
from typing import Iterator, Callable, List
1727+
from typing import Iterator, Callable, List, Optional
17281728
from a import f
17291729
import a
17301730

1731-
def dec(f: Callable[['A'], Iterator[int]]) -> Callable[[int], int]: pass
1731+
def dec(f: Callable[['A'], Optional[Iterator[int]]]) -> Callable[[int], int]: pass
17321732

17331733
class A:
17341734
@dec
1735-
def f(self) -> Iterator[int]:
1735+
def f(self) -> Optional[Iterator[int]]:
17361736
self.x = a.g() # type: int
17371737
return None
17381738
[builtins fixtures/list.pyi]
@@ -4626,6 +4626,7 @@ class User:
46264626
==
46274627

46284628
[case testNoStrictOptionalModule]
4629+
# flags: --no-strict-optional
46294630
import a
46304631
a.y = a.x
46314632
[file a.py]
@@ -4643,9 +4644,10 @@ y: int
46434644
[out]
46444645
==
46454646
==
4646-
main:2: error: Incompatible types in assignment (expression has type "Optional[str]", variable has type "int")
4647+
main:3: error: Incompatible types in assignment (expression has type "Optional[str]", variable has type "int")
46474648

46484649
[case testNoStrictOptionalFunction]
4650+
# flags: --no-strict-optional
46494651
import a
46504652
from typing import Optional
46514653
def f() -> None:
@@ -4666,9 +4668,10 @@ def g(x: str) -> None:
46664668
[out]
46674669
==
46684670
==
4669-
main:5: error: Argument 1 to "g" has incompatible type "Optional[int]"; expected "str"
4671+
main:6: error: Argument 1 to "g" has incompatible type "Optional[int]"; expected "str"
46704672

46714673
[case testNoStrictOptionalMethod]
4674+
# flags: --no-strict-optional
46724675
import a
46734676
from typing import Optional
46744677
class C:
@@ -4693,7 +4696,7 @@ class B:
46934696
[out]
46944697
==
46954698
==
4696-
main:6: error: Argument 1 to "g" of "B" has incompatible type "Optional[int]"; expected "str"
4699+
main:7: error: Argument 1 to "g" of "B" has incompatible type "Optional[int]"; expected "str"
46974700

46984701
[case testStrictOptionalModule]
46994702
# flags: --strict-optional
@@ -5276,10 +5279,11 @@ class I(metaclass=ABCMeta):
52765279
@abstractmethod
52775280
def f(self) -> None: pass
52785281
[file b.py]
5282+
from typing import Optional
52795283
from z import I
52805284
class Foo(I):
52815285
pass
5282-
def x() -> Foo: return None
5286+
def x() -> Optional[Foo]: return None
52835287
[file z.py.2]
52845288
from abc import abstractmethod, ABCMeta
52855289
class I(metaclass=ABCMeta):
@@ -5301,10 +5305,11 @@ class I(metaclass=ABCMeta):
53015305
@abstractmethod
53025306
def f(self) -> None: pass
53035307
[file b.py]
5308+
from typing import Optional
53045309
from a import I
53055310
class Foo(I):
53065311
pass
5307-
def x() -> Foo: return None
5312+
def x() -> Optional[Foo]: return None
53085313
[file a.py.2]
53095314
from abc import abstractmethod, ABCMeta
53105315
class I(metaclass=ABCMeta):
@@ -7769,7 +7774,8 @@ from typing import List
77697774
import b
77707775
class A(b.B):
77717776
def meth(self) -> None:
7772-
self.x, *self.y = None, None # type: str, List[str]
7777+
self.x: str
7778+
self.y: List[str]
77737779
[file b.py]
77747780
from typing import List
77757781
class B:
@@ -7781,7 +7787,7 @@ class B:
77817787
[builtins fixtures/list.pyi]
77827788
[out]
77837789
==
7784-
main:5: error: Incompatible types in assignment (expression has type "List[str]", base class "B" defined the type as "List[int]")
7790+
main:6: error: Incompatible types in assignment (expression has type "List[str]", base class "B" defined the type as "List[int]")
77857791

77867792
[case testLiskovFineVariableCleanDefInMethodNested-only_when_nocache]
77877793
from b import B
@@ -9109,27 +9115,27 @@ import a
91099115
[file a.py]
91109116
# mypy: no-warn-no-return
91119117

9112-
from typing import List
9113-
def foo() -> List:
9118+
from typing import List, Optional
9119+
def foo() -> Optional[List]:
91149120
20
91159121

91169122
[file a.py.2]
91179123
# mypy: disallow-any-generics, no-warn-no-return
91189124

9119-
from typing import List
9120-
def foo() -> List:
9125+
from typing import List, Optional
9126+
def foo() -> Optional[List]:
91219127
20
91229128

91239129
[file a.py.3]
91249130
# mypy: no-warn-no-return
91259131

9126-
from typing import List
9127-
def foo() -> List:
9132+
from typing import List, Optional
9133+
def foo() -> Optional[List]:
91289134
20
91299135

91309136
[file a.py.4]
9131-
from typing import List
9132-
def foo() -> List:
9137+
from typing import List, Optional
9138+
def foo() -> Optional[List]:
91339139
20
91349140
[out]
91359141
==
@@ -9867,7 +9873,7 @@ x = 0 # Arbitrary change to trigger reprocessing
98679873
[builtins fixtures/dict.pyi]
98689874
[out]
98699875
==
9870-
a.py:5: note: Revealed type is "def (x: builtins.int) -> builtins.str"
9876+
a.py:5: note: Revealed type is "def (x: builtins.int) -> Union[builtins.str, None]"
98719877

98729878
[case testTypeVarTupleCached]
98739879
import a

0 commit comments

Comments
 (0)