diff --git a/mypy/test/testcheck.py b/mypy/test/testcheck.py index a77da090f740..20dfbb77f3e0 100644 --- a/mypy/test/testcheck.py +++ b/mypy/test/testcheck.py @@ -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 @@ -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] diff --git a/test-data/unit/check-modules.test b/test-data/unit/check-modules.test index 8a30237843a5..fc3daff64fbd 100644 --- a/test-data/unit/check-modules.test +++ b/test-data/unit/check-modules.test @@ -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(): @@ -585,6 +586,7 @@ x = 1+0 [case testConditionalImportAndAssign] +# flags: --no-strict-optional try: from m import x except: @@ -676,6 +678,7 @@ class B(A): ... [case testImportVariableAndAssignNone] +# flags: --no-strict-optional try: from m import x except: @@ -684,6 +687,7 @@ except: x = 1 [case testImportFunctionAndAssignNone] +# flags: --no-strict-optional try: from m import f except: @@ -709,6 +713,7 @@ except: def f(): pass [case testAssignToFuncDefViaGlobalDecl2] +# flags: --no-strict-optional import typing from m import f def g() -> None: @@ -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]") @@ -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]") @@ -738,6 +745,7 @@ def f(): pass [out] [case testConditionalImportAndAssignNoneToModule] +# flags: --no-strict-optional if object(): import m else: @@ -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" @@ -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] @@ -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] @@ -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]() @@ -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] diff --git a/test-data/unit/check-unions.test b/test-data/unit/check-unions.test index 65d5c1abc7e8..28d83aa54ccc 100644 --- a/test-data/unit/check-unions.test +++ b/test-data/unit/check-unions.test @@ -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 @@ -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() @@ -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]" @@ -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 @@ -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 @@ -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 @@ -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]" @@ -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]" @@ -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" @@ -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): diff --git a/test-data/unit/check-varargs.test b/test-data/unit/check-varargs.test index 92b9f7f04f26..4da9e0e5033e 100644 --- a/test-data/unit/check-varargs.test +++ b/test-data/unit/check-varargs.test @@ -8,8 +8,8 @@ [case testVarArgsWithinFunction] from typing import Tuple def f( *b: 'B') -> None: - ab = None # type: Tuple[B, ...] - ac = None # type: Tuple[C, ...] + ab: Tuple[B, ...] + ac: Tuple[C, ...] if int(): b = ac # E: Incompatible types in assignment (expression has type "Tuple[C, ...]", variable has type "Tuple[B, ...]") ac = b # E: Incompatible types in assignment (expression has type "Tuple[B, ...]", variable has type "Tuple[C, ...]") @@ -46,9 +46,9 @@ class A: pass class B(A): pass class C: pass -a = None # type: A -b = None # type: B -c = None # type: C +a: A +b: B +c: C f(c) # E: Argument 1 to "f" has incompatible type "C"; expected "A" f(a, b, c) # E: Argument 3 to "f" has incompatible type "C"; expected "A" @@ -67,9 +67,9 @@ class A: pass class B(A): pass class C: pass -a = None # type: A -b = None # type: B -c = None # type: C +a: A +b: B +c: C f(a) # E: Argument 1 to "f" has incompatible type "A"; expected "C" f(c, c) # E: Argument 2 to "f" has incompatible type "C"; expected "A" @@ -88,9 +88,9 @@ class A: pass class B(A): pass class C: pass -a = None # type: A -b = None # type: B -c = None # type: C +a: A +b: B +c: C f(a) # E: Argument 1 to "f" has incompatible type "A"; expected "Optional[C]" f(c, c) # E: Argument 2 to "f" has incompatible type "C"; expected "A" @@ -103,8 +103,8 @@ f(c, b, b, a, b) [case testCallVarargsFunctionWithIterable] from typing import Iterable -it1 = None # type: Iterable[int] -it2 = None # type: Iterable[str] +it1: Iterable[int] +it2: Iterable[str] def f(*x: int) -> None: pass f(*it1) f(*it2) # E: Argument 1 to "f" has incompatible type "*Iterable[str]"; expected "int" @@ -127,7 +127,7 @@ reveal_type(f(*x, *y)) # N: Revealed type is "Tuple[builtins.int, builtins.str, [case testCallVarargsFunctionWithIterableAndPositional] from typing import Iterable -it1 = None # type: Iterable[int] +it1: Iterable[int] def f(*x: int) -> None: pass f(*it1, 1, 2) f(*it1, 1, *it1, 2) @@ -161,10 +161,10 @@ class A: pass class B(A): pass class C: pass -a = None # type: A -b = None # type: B -c = None # type: C -o = None # type: object +a: A +b: B +c: C +o: object if int(): a = f(o) # E: Incompatible types in assignment (expression has type "object", variable has type "A") @@ -188,6 +188,7 @@ if int(): [builtins fixtures/list.pyi] [case testTypeInferenceWithCalleeVarArgsAndDefaultArgs] +# flags: --no-strict-optional from typing import TypeVar T = TypeVar('T') a = None # type: A @@ -229,10 +230,10 @@ def f(a: 'A', b: 'B') -> None: class A: pass class B: pass -aa = None # type: List[A] -ab = None # type: List[B] -a = None # type: A -b = None # type: B +aa: List[A] +ab: List[B] +a: A +b: B f(*aa) # E: Argument 1 to "f" has incompatible type "*List[A]"; expected "B" f(a, *ab) # Ok @@ -248,10 +249,10 @@ class B: pass class C: pass class CC(C): pass -a = None # type: A -b = None # type: B -c = None # type: C -cc = None # type: CC +a: A +b: B +c: C +cc: CC f(*(a, b, b)) # E: Argument 1 to "f" has incompatible type "*Tuple[A, B, B]"; expected "C" f(*(b, b, c)) # E: Argument 1 to "f" has incompatible type "*Tuple[B, B, C]"; expected "A" @@ -267,7 +268,7 @@ f(a, *(b, cc)) [builtins fixtures/tuple.pyi] [case testInvalidVarArg] - +# flags: --no-strict-optional def f(a: 'A') -> None: pass @@ -293,7 +294,10 @@ def g(a: 'A', *b: 'A') -> None: pass class A: pass class B: pass -aa, ab, a, b = None, None, None, None # type: (List[A], List[B], A, B) +aa: List[A] +ab: List[B] +a: A +b: B f(*aa) # E: Argument 1 to "f" has incompatible type "*List[A]"; expected "B" f(a, *aa) # E: Argument 2 to "f" has incompatible type "*List[A]"; expected "B" f(b, *ab) # E: Argument 1 to "f" has incompatible type "B"; expected "A" @@ -315,7 +319,10 @@ class B: pass class C: pass class CC(C): pass -a, b, c, cc = None, None, None, None # type: (A, B, C, CC) +a: A +b: B +c: C +cc: CC f(*(b, b, b)) # E: Argument 1 to "f" has incompatible type "*Tuple[B, B, B]"; expected "A" f(*(a, a, b)) # E: Argument 1 to "f" has incompatible type "*Tuple[A, A, B]"; expected "B" @@ -342,7 +349,8 @@ def f(a: 'A') -> None: pass def g(a: 'A', *b: 'A') -> None: pass class A: pass -d, a = None, None # type: (Any, A) +d: Any +a: A f(a, a, *d) # E: Too many arguments for "f" f(a, *d) # Ok f(*d) # Ok @@ -351,6 +359,7 @@ g(*d) g(a, *d) g(a, a, *d) [builtins fixtures/list.pyi] + [case testListVarArgsAndSubtyping] from typing import List def f( *a: 'A') -> None: @@ -362,8 +371,8 @@ def g( *a: 'B') -> None: class A: pass class B(A): pass -aa = None # type: List[A] -ab = None # type: List[B] +aa: List[A] +ab: List[B] g(*aa) # E: Argument 1 to "g" has incompatible type "*List[A]"; expected "B" f(*aa) @@ -449,6 +458,7 @@ foo(*()) [case testIntersectionTypesAndVarArgs] +# flags: --no-strict-optional from foo import * [file foo.pyi] from typing import overload @@ -518,7 +528,9 @@ def f(a: S, *b: T) -> Tuple[S, T]: class A: pass class B: pass -a, b, aa = None, None, None # type: (A, B, List[A]) +a: A +b: B +aa: List[A] if int(): a, b = f(*aa) # E: Argument 1 to "f" has incompatible type "*List[A]"; expected "B" @@ -553,7 +565,8 @@ def f(a: S, b: T) -> Tuple[S, T]: pass class A: pass class B: pass -a, b = None, None # type: (A, B) +a: A +b: B if int(): a, a = f(*(a, b)) # E: Argument 1 to "f" has incompatible type "*Tuple[A, B]"; expected "A" @@ -575,10 +588,11 @@ if int(): from typing import List, TypeVar, Generic, Tuple T = TypeVar('T') S = TypeVar('S') -a, b = None, None # type: (A, B) -ao = None # type: List[object] -aa = None # type: List[A] -ab = None # type: List[B] +a: A +b: B +ao: List[object] +aa: List[A] +ab: List[B] class G(Generic[T]): def f(self, *a: S) -> Tuple[List[S], List[T]]: @@ -649,7 +663,7 @@ f(1, '') # E: Argument 2 to "f" has incompatible type "str"; expected "int" [case testVarArgsFunctionSubtyping] from typing import Callable -x = None # type: Callable[[int], None] +x: Callable[[int], None] def f(*x: int) -> None: pass def g(*x: str) -> None: pass x = f