Skip to content

Commit

Permalink
Enable strict optional for more test files (9) (#15607)
Browse files Browse the repository at this point in the history
Followup to #15586

This will be the last one 🚀
  • Loading branch information
cdce8p committed Jul 7, 2023
1 parent ef0b763 commit d106c84
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 76 deletions.
12 changes: 1 addition & 11 deletions mypy/test/testcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,6 @@
typecheck_files.remove("check-modules-case.test")


# TODO: Enable strict optional in test cases by default. Remove files here, once test cases are updated
no_strict_optional_files = {
"check-modules.test",
"check-unions.test",
"check-varargs.test",
}


class TypeCheckSuite(DataSuite):
files = typecheck_files

Expand Down Expand Up @@ -129,9 +121,7 @@ def run_case_once(
perform_file_operations(operations)

# Parse options after moving files (in case mypy.ini is being moved).
options = parse_options(
original_program_text, testcase, incremental_step, no_strict_optional_files
)
options = parse_options(original_program_text, testcase, incremental_step)
options.use_builtins_fixtures = True
if not testcase.name.endswith("_no_incomplete"):
options.enable_incomplete_feature = [TYPE_VAR_TUPLE, UNPACK]
Expand Down
21 changes: 15 additions & 6 deletions test-data/unit/check-modules.test
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ x = object()
[case testChainedAssignmentAndImports]
import m

i, s = None, None # type: (int, str)
i: int
s: str
if int():
i = m.x
if int():
Expand Down Expand Up @@ -585,6 +586,7 @@ x = 1+0


[case testConditionalImportAndAssign]
# flags: --no-strict-optional
try:
from m import x
except:
Expand Down Expand Up @@ -676,6 +678,7 @@ class B(A): ...


[case testImportVariableAndAssignNone]
# flags: --no-strict-optional
try:
from m import x
except:
Expand All @@ -684,6 +687,7 @@ except:
x = 1

[case testImportFunctionAndAssignNone]
# flags: --no-strict-optional
try:
from m import f
except:
Expand All @@ -709,6 +713,7 @@ except:
def f(): pass

[case testAssignToFuncDefViaGlobalDecl2]
# flags: --no-strict-optional
import typing
from m import f
def g() -> None:
Expand All @@ -721,6 +726,7 @@ def f(): pass
[out]

[case testAssignToFuncDefViaNestedModules]
# flags: --no-strict-optional
import m.n
m.n.f = None
m.n.f = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "Callable[[], Any]")
Expand All @@ -730,6 +736,7 @@ def f(): pass
[out]

[case testAssignToFuncDefViaModule]
# flags: --no-strict-optional
import m
m.f = None
m.f = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "Callable[[], Any]")
Expand All @@ -738,6 +745,7 @@ def f(): pass
[out]

[case testConditionalImportAndAssignNoneToModule]
# flags: --no-strict-optional
if object():
import m
else:
Expand All @@ -758,6 +766,7 @@ else:
[out]

[case testImportAndAssignToModule]
# flags: --no-strict-optional
import m
m = None
m.f(1) # E: Argument 1 to "f" has incompatible type "int"; expected "str"
Expand Down Expand Up @@ -930,8 +939,8 @@ tmp/a/b/__init__.py:3: error: Name "a" is not defined

[case testSubmoduleMixingLocalAndQualifiedNames]
from a.b import MyClass
val1 = None # type: a.b.MyClass # E: Name "a" is not defined
val2 = None # type: MyClass
val1: a.b.MyClass # E: Name "a" is not defined
val2: MyClass

[file a/__init__.py]
[file a/b.py]
Expand Down Expand Up @@ -1501,7 +1510,7 @@ import bar
from foo import *
def bar(y: AnyAlias) -> None: pass

l = None # type: ListAlias[int]
l: ListAlias[int]
reveal_type(l)

[file foo.py]
Expand Down Expand Up @@ -1532,7 +1541,7 @@ Row = Dict[str, int]

[case testImportStarAliasGeneric]
from y import *
notes = None # type: G[X]
notes: G[X]
another = G[X]()
second = XT[str]()
last = XT[G]()
Expand Down Expand Up @@ -1561,7 +1570,7 @@ from typing import Any
def bar(x: Any, y: AnyCallable) -> Any:
return 'foo'

cb = None # type: AnyCallable
cb: AnyCallable
reveal_type(cb) # N: Revealed type is "def (*Any, **Any) -> Any"

[file foo.py]
Expand Down
44 changes: 24 additions & 20 deletions test-data/unit/check-unions.test
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ class B: y = 2
class C: pass
class D: pass

u = None # type: Union[A, C, D]
v = None # type: Union[C, D]
w = None # type: Union[A, B]
x = None # type: Union[A, C]
y = None # type: int
z = None # type: str
u: Union[A, C, D]
v: Union[C, D]
w: Union[A, B]
x: Union[A, C]
y: int
z: str

if int():
y = w.y
Expand Down Expand Up @@ -89,9 +89,9 @@ class B:
class C:
def foo(self) -> str: pass

x = None # type: Union[A, B]
y = None # type: Union[A, C]
i = None # type: int
x: Union[A, B]
y: Union[A, C]
i: int

x.foo()
y.foo()
Expand All @@ -103,7 +103,7 @@ if int():

[case testUnionIndexing]
from typing import Union, List
x = None # type: Union[List[int], str]
x: Union[List[int], str]
x[2]
x[2] + 1 # E: Unsupported operand types for + ("str" and "int") \
# N: Left operand is of type "Union[int, str]"
Expand Down Expand Up @@ -132,6 +132,7 @@ def f(x: Union[int, str]) -> int: pass
def f(x: type) -> str: pass

[case testUnionWithNoneItem]
# flags: --no-strict-optional
from typing import Union
def f() -> Union[int, None]: pass
x = 1
Expand Down Expand Up @@ -221,6 +222,7 @@ else:
# N: def g(x: Union[int, str]) -> None

[case testUnionSimplificationSpecialCases]
# flags: --no-strict-optional
from typing import Any, TypeVar, Union

class C(Any): pass
Expand Down Expand Up @@ -266,9 +268,10 @@ class M(Generic[V]):

def f(x: M[C]) -> None:
y = x.get(None)
reveal_type(y) # N: Revealed type is "__main__.C"
reveal_type(y) # N: Revealed type is "Union[__main__.C, None]"

[case testUnionSimplificationSpecialCases2]
# flags: --no-strict-optional
from typing import Any, TypeVar, Union

class C(Any): pass
Expand Down Expand Up @@ -317,10 +320,10 @@ S = TypeVar('S')
R = TypeVar('R')
def u(x: T, y: S, z: R) -> Union[R, S, T]: pass

a = None # type: Any
a: Any

reveal_type(u(1, 1, 1)) # N: Revealed type is "builtins.int"
reveal_type(u(C(), C(), None)) # N: Revealed type is "__main__.C"
reveal_type(u(C(), C(), None)) # N: Revealed type is "Union[None, __main__.C]"
reveal_type(u(a, a, 1)) # N: Revealed type is "Union[builtins.int, Any]"
reveal_type(u(a, C(), a)) # N: Revealed type is "Union[Any, __main__.C]"
reveal_type(u('', 1, 1)) # N: Revealed type is "Union[builtins.int, builtins.str]"
Expand Down Expand Up @@ -370,9 +373,9 @@ T = TypeVar('T')
S = TypeVar('S')
def u(x: T, y: S) -> Union[S, T]: pass

t_o = None # type: Type[object]
t_s = None # type: Type[str]
t_a = None # type: Type[Any]
t_o: Type[object]
t_s: Type[str]
t_a: Type[Any]

# Two identical items
reveal_type(u(t_o, t_o)) # N: Revealed type is "Type[builtins.object]"
Expand All @@ -397,10 +400,10 @@ T = TypeVar('T')
S = TypeVar('S')
def u(x: T, y: S) -> Union[S, T]: pass

t_o = None # type: Type[object]
t_s = None # type: Type[str]
t_a = None # type: Type[Any]
t = None # type: type
t_o: Type[object]
t_s: Type[str]
t_a: Type[Any]
t: type

# Union with object
reveal_type(u(t_o, object())) # N: Revealed type is "builtins.object"
Expand Down Expand Up @@ -1010,6 +1013,7 @@ MYTYPE = List[Union[str, "MYTYPE"]] # E: Cannot resolve name "MYTYPE" (possible
[builtins fixtures/list.pyi]

[case testNonStrictOptional]
# flags: --no-strict-optional
from typing import Optional, List

def union_test1(x):
Expand Down
Loading

0 comments on commit d106c84

Please sign in to comment.