From 91e4ce496fd6032c8670d1d3ce350930ab4f3aa4 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Fri, 7 Jul 2023 05:05:14 +0200 Subject: [PATCH] Enable strict optional for more test files (7) (#15604) --- mypy/test/testcheck.py | 5 - test-data/unit/check-inference-context.test | 176 ++++++++++---------- test-data/unit/check-inference.test | 111 ++++++------ test-data/unit/check-isinstance.test | 23 +-- test-data/unit/check-kwargs.test | 37 ++-- test-data/unit/check-literal.test | 18 +- 6 files changed, 199 insertions(+), 171 deletions(-) diff --git a/mypy/test/testcheck.py b/mypy/test/testcheck.py index f15e5afabf0e..c91bd915096f 100644 --- a/mypy/test/testcheck.py +++ b/mypy/test/testcheck.py @@ -51,11 +51,6 @@ # TODO: Enable strict optional in test cases by default. Remove files here, once test cases are updated no_strict_optional_files = { - "check-inference-context.test", - "check-inference.test", - "check-isinstance.test", - "check-kwargs.test", - "check-literal.test", "check-modules.test", "check-namedtuple.test", "check-overloading.test", diff --git a/test-data/unit/check-inference-context.test b/test-data/unit/check-inference-context.test index 625ab091a6a9..13cfec46eeab 100644 --- a/test-data/unit/check-inference-context.test +++ b/test-data/unit/check-inference-context.test @@ -13,9 +13,9 @@ def f() -> 'A[T]': pass class A(Generic[T]): pass class B: pass -ab = None # type: A[B] -ao = None # type: A[object] -b = None # type: B +ab: A[B] +ao: A[object] +b: B if int(): ao = f() @@ -28,9 +28,9 @@ from typing import TypeVar, Generic T = TypeVar('T') class A(Generic[T]): pass class B: pass -ab = None # type: A[B] -ao = None # type: A[object] -b = None # type: B +ab: A[B] +ao: A[object] +b: B if int(): ao = A() @@ -48,11 +48,11 @@ class A(Generic[T]): pass class B: pass class C: pass -b = None # type: B -c = None # type: C -ab = None # type: A[B] -ao = None # type: A[object] -ac = None # type: A[C] +b: B +c: C +ab: A[B] +ao: A[object] +ac: A[C] if int(): ac = f(b) # E: Argument 1 to "f" has incompatible type "B"; expected "C" @@ -77,10 +77,10 @@ if int(): from typing import TypeVar, Generic T = TypeVar('T') def g() -> None: - ao = None # type: A[object] - ab = None # type: A[B] - o = None # type: object - b = None # type: B + ao: A[object] + ab: A[B] + o: object + b: B x = f(o) if int(): @@ -111,9 +111,9 @@ class A(Generic[T]): pass from typing import TypeVar, Generic T = TypeVar('T') def g() -> None: - ao = None # type: A[object] - ab = None # type: A[B] - b = None # type: B + ao: A[object] + ab: A[B] + b: B x, y = f(b), f(b) if int(): ao = x # E: Incompatible types in assignment (expression has type "A[B]", variable has type "A[object]") @@ -130,9 +130,9 @@ class B: pass from typing import TypeVar, List, Generic T = TypeVar('T') def h() -> None: - ao = None # type: A[object] - ab = None # type: A[B] - b = None # type: B + ao: A[object] + ab: A[B] + b: B x, y = g(f(b)) if int(): ao = x # E: Incompatible types in assignment (expression has type "A[B]", variable has type "A[object]") @@ -162,10 +162,10 @@ def f(a: T) -> 'Tuple[A[T], A[T]]': pass class A(Generic[T]): pass class B: pass -b = None # type: B -o = None # type: object -ab = None # type: A[B] -ao = None # type: A[object] +b: B +o: object +ab: A[B] +ao: A[object] if int(): ab, ao = f(b) # E: Incompatible types in assignment (expression has type "A[B]", variable has type "A[object]") @@ -192,10 +192,10 @@ def h(a: S, b: T) -> 'Tuple[A[S], A[S], A[T], A[T]]': pass class A(Generic[T]): pass class B: pass -b = None # type: B -o = None # type: object -ab = None # type: A[B] -ao = None # type: A[object] +b: B +o: object +ab: A[B] +ao: A[object] if int(): ao, ao, ab = f(b, b) # E: Incompatible types in assignment (expression has type "A[B]", variable has type "A[object]") @@ -229,12 +229,12 @@ class A(Generic[T]): pass class B: pass class C(B): pass -ac = None # type: A[C] -ab = None # type: A[B] -ao = None # type: A[object] -b = None # type: B -c = None # type: C -o = None # type: object +ac: A[C] +ab: A[B] +ao: A[object] +b: B +c: C +o: object if int(): ab = f(b, o) # E: Argument 2 to "f" has incompatible type "object"; expected "B" @@ -266,11 +266,11 @@ def f(a: T) -> 'A[T]': pass class A(Generic[T]): pass class B: pass -aab = None # type: A[A[B]] -aao = None # type: A[A[object]] -ao = None # type: A[object] -b = None # type: B -o = None # type: object +aab: A[A[B]] +aao: A[A[object]] +ao: A[object] +b: B +o: object if int(): aab = f(f(o)) # E: Argument 1 to "f" has incompatible type "object"; expected "B" @@ -279,6 +279,7 @@ if int(): aab = f(f(b)) aao = f(f(b)) ao = f(f(b)) + [case testNestedGenericFunctionCall2] from typing import TypeVar, Generic T = TypeVar('T') @@ -289,10 +290,10 @@ def g(a: T) -> 'A[T]': pass class A(Generic[T]): pass class B: pass -ab = None # type: A[B] -ao = None # type: A[object] -b = None # type: B -o = None # type: object +ab: A[B] +ao: A[object] +b: B +o: object if int(): ab = f(g(o)) # E: Argument 1 to "g" has incompatible type "object"; expected "B" @@ -300,6 +301,7 @@ if int(): if int(): ab = f(g(b)) ao = f(g(b)) + [case testNestedGenericFunctionCall3] from typing import TypeVar, Generic T = TypeVar('T') @@ -310,10 +312,10 @@ def g(a: T) -> 'A[T]': pass class A(Generic[T]): pass class B: pass -ab = None # type: A[B] -ao = None # type: A[object] -b = None # type: B -o = None # type: object +ab: A[B] +ao: A[object] +b: B +o: object if int(): ab = f(g(o), g(b)) # E: Argument 1 to "g" has incompatible type "object"; expected "B" @@ -334,9 +336,9 @@ if int(): [case testMethodCallWithContextInference] from typing import TypeVar, Generic T = TypeVar('T') -o = None # type: object -b = None # type: B -c = None # type: C +o: object +b: B +c: C def f(a: T) -> 'A[T]': pass class A(Generic[T]): @@ -344,9 +346,9 @@ class A(Generic[T]): class B: pass class C(B): pass -ao = None # type: A[object] -ab = None # type: A[B] -ac = None # type: A[C] +ao: A[object] +ab: A[B] +ac: A[C] ab.g(f(o)) # E: Argument 1 to "f" has incompatible type "object"; expected "B" if int(): @@ -365,9 +367,9 @@ ab.g(f(c)) [case testEmptyListExpression] from typing import List -aa = None # type: List[A] -ao = None # type: List[object] -a = None # type: A +aa: List[A] +ao: List[object] +a: A def f(): a, aa, ao # Prevent redefinition a = [] # E: Incompatible types in assignment (expression has type "List[]", variable has type "A") @@ -379,15 +381,15 @@ class A: pass [builtins fixtures/list.pyi] [case testSingleItemListExpressions] -from typing import List -aa = None # type: List[A] -ab = None # type: List[B] -ao = None # type: List[object] -a = None # type: A -b = None # type: B +from typing import List, Optional +aa: List[Optional[A]] +ab: List[B] +ao: List[object] +a: A +b: B def f(): aa, ab, ao # Prevent redefinition -aa = [b] # E: List item 0 has incompatible type "B"; expected "A" +aa = [b] # E: List item 0 has incompatible type "B"; expected "Optional[A]" ab = [a] # E: List item 0 has incompatible type "A"; expected "B" aa = [a] @@ -402,11 +404,11 @@ class B: pass [case testMultiItemListExpressions] from typing import List -aa = None # type: List[A] -ab = None # type: List[B] -ao = None # type: List[object] -a = None # type: A -b = None # type: B +aa: List[A] +ab: List[B] +ao: List[object] +a: A +b: B def f(): ab, aa, ao # Prevent redefinition ab = [b, a] # E: List item 1 has incompatible type "A"; expected "B" @@ -433,6 +435,7 @@ class B: pass [out] [case testNestedListExpressions] +# flags: --no-strict-optional from typing import List aao = None # type: List[List[object]] aab = None # type: List[List[B]] @@ -596,17 +599,17 @@ y = C() # type: Any [case testInferLambdaArgumentTypeUsingContext] from typing import Callable -f = None # type: Callable[[B], A] +f: Callable[[B], A] if int(): f = lambda x: x.o f = lambda x: x.x # E: "B" has no attribute "x" class A: pass class B: - o = None # type: A + o: A [case testInferLambdaReturnTypeUsingContext] from typing import List, Callable -f = None # type: Callable[[], List[A]] +f: Callable[[], List[A]] if int(): f = lambda: [] f = lambda: [B()] # E: List item 0 has incompatible type "B"; expected "A" @@ -680,6 +683,7 @@ def foo(arg: Callable[..., T]) -> None: pass foo(lambda: 1) [case testLambdaNoneInContext] +# flags: --no-strict-optional from typing import Callable def f(x: Callable[[], None]) -> None: pass def g(x: Callable[[], int]) -> None: pass @@ -687,13 +691,13 @@ f(lambda: None) g(lambda: None) [case testIsinstanceInInferredLambda] -from typing import TypeVar, Callable +from typing import TypeVar, Callable, Optional T = TypeVar('T') S = TypeVar('S') class A: pass class B(A): pass class C(A): pass -def f(func: Callable[[T], S], *z: T, r: S = None) -> S: pass +def f(func: Callable[[T], S], *z: T, r: Optional[S] = None) -> S: pass f(lambda x: 0 if isinstance(x, B) else 1) # E: Cannot infer type argument 1 of "f" f(lambda x: 0 if isinstance(x, B) else 1, A())() # E: "int" not callable f(lambda x: x if isinstance(x, B) else B(), A(), r=B())() # E: "B" not callable @@ -739,7 +743,9 @@ from typing import List class A: pass class B: pass class C(B): pass -a, b, c = None, None, None # type: (List[A], List[B], List[C]) +a: List[A] +b: List[B] +c: List[C] if int(): a = a or [] if int(): @@ -762,7 +768,7 @@ from typing import List, TypeVar t = TypeVar('t') s = TypeVar('s') # Some type variables can be inferred using context, but not all of them. -a = None # type: List[A] +a: List[A] def f(a: s, b: t) -> List[s]: pass class A: pass class B: pass @@ -780,7 +786,7 @@ def f(a: s, b: t) -> List[s]: pass class A: pass class B: pass # Like testSomeTypeVarsInferredFromContext, but tvars in different order. -a = None # type: List[A] +a: List[A] if int(): a = f(A(), B()) if int(): @@ -800,8 +806,8 @@ map( [case testChainedAssignmentInferenceContexts] from typing import List -i = None # type: List[int] -s = None # type: List[str] +i: List[int] +s: List[str] if int(): i = i = [] if int(): @@ -821,10 +827,10 @@ a.x = [''] # E: List item 0 has incompatible type "str"; expected "int" [case testListMultiplyInContext] from typing import List -a = None # type: List[int] +a: List[int] if int(): - a = [None] * 3 - a = [''] * 3 # E: List item 0 has incompatible type "str"; expected "int" + a = [None] * 3 # E: List item 0 has incompatible type "None"; expected "int" + a = [''] * 3 # E: List item 0 has incompatible type "str"; expected "int" [builtins fixtures/list.pyi] [case testUnionTypeContext] @@ -847,7 +853,7 @@ g(f(a)) [case testStar2Context] from typing import Any, Dict, Tuple, Iterable -def f1(iterable: Iterable[Tuple[str, Any]] = None) -> None: +def f1(iterable: Iterable[Tuple[str, Any]] = ()) -> None: f2(**dict(iterable)) def f2(iterable: Iterable[Tuple[str, Any]], **kw: Any) -> None: pass @@ -913,7 +919,7 @@ T = TypeVar('T') def f(x: Union[T, List[int]]) -> Union[T, List[int]]: pass reveal_type(f(1)) # N: Revealed type is "Union[builtins.int, builtins.list[builtins.int]]" reveal_type(f([])) # N: Revealed type is "builtins.list[builtins.int]" -reveal_type(f(None)) # N: Revealed type is "builtins.list[builtins.int]" +reveal_type(f(None)) # N: Revealed type is "Union[None, builtins.list[builtins.int]]" [builtins fixtures/list.pyi] [case testUnionWithGenericTypeItemContextAndStrictOptional] @@ -941,7 +947,7 @@ c = C[List[int]]() reveal_type(c.f('')) # N: Revealed type is "Union[builtins.list[builtins.int], builtins.str]" reveal_type(c.f([1])) # N: Revealed type is "builtins.list[builtins.int]" reveal_type(c.f([])) # N: Revealed type is "builtins.list[builtins.int]" -reveal_type(c.f(None)) # N: Revealed type is "builtins.list[builtins.int]" +reveal_type(c.f(None)) # N: Revealed type is "Union[builtins.list[builtins.int], None]" [builtins fixtures/list.pyi] [case testGenericMethodCalledInGenericContext] diff --git a/test-data/unit/check-inference.test b/test-data/unit/check-inference.test index 166e173e7301..ee13cb3830fc 100644 --- a/test-data/unit/check-inference.test +++ b/test-data/unit/check-inference.test @@ -54,7 +54,7 @@ class B: pass [case testInferringLvarTypeFromGvar] -g = None # type: B +g: B def f() -> None: a = g @@ -78,7 +78,7 @@ def g(): pass [case testInferringExplicitDynamicTypeForLvar] from typing import Any -g = None # type: Any +g: Any def f(a: Any) -> None: b = g @@ -95,8 +95,8 @@ def f(a: Any) -> None: def f() -> None: a = A(), B() - aa = None # type: A - bb = None # type: B + aa: A + bb: B if int(): bb = a[0] # E: Incompatible types in assignment (expression has type "A", variable has type "B") aa = a[1] # E: Incompatible types in assignment (expression has type "B", variable has type "A") @@ -122,8 +122,8 @@ class A: pass from typing import TypeVar, Generic T = TypeVar('T') class A(Generic[T]): pass -a_i = None # type: A[int] -a_s = None # type: A[str] +a_i: A[int] +a_s: A[str] def f() -> None: a_int = A() # type: A[int] @@ -182,7 +182,7 @@ class B: pass [case testInferringLvarTypesInTupleAssignment] from typing import Tuple def f() -> None: - t = None # type: Tuple[A, B] + t: Tuple[A, B] a, b = t if int(): a = b # E: Incompatible types in assignment (expression has type "B", variable has type "A") @@ -200,7 +200,7 @@ class B: pass [case testInferringLvarTypesInNestedTupleAssignment1] from typing import Tuple def f() -> None: - t = None # type: Tuple[A, B] + t: Tuple[A, B] a1, (a, b) = A(), t if int(): a = b # E: Incompatible types in assignment (expression has type "B", variable has type "A") @@ -386,7 +386,7 @@ reveal_type(C) # N: Revealed type is "__main__.Foo" [case testInferringLvarTypesInMultiDefWithInvalidTuple] from typing import Tuple -t = None # type: Tuple[object, object, object] +t: Tuple[object, object, object] def f() -> None: a, b = t # Fail @@ -543,9 +543,9 @@ if int(): [case testInferSimpleGenericFunction] from typing import Tuple, TypeVar T = TypeVar('T') -a = None # type: A -b = None # type: B -c = None # type: Tuple[A, object] +a: A +b: B +c: Tuple[A, object] def id(a: T) -> T: pass @@ -569,8 +569,8 @@ from typing import TypeVar T = TypeVar('T') def f() -> None: a = id - b = None # type: int - c = None # type: str + b: int + c: str if int(): b = a(c) # E: Incompatible types in assignment (expression has type "str", variable has type "int") b = a(b) @@ -584,7 +584,7 @@ def id(x: T) -> T: from typing import TypeVar T = TypeVar('T') class A: pass -a = None # type: A +a: A def ff() -> None: x = f() # E: Need type annotation for "x" @@ -605,8 +605,8 @@ class A: pass class B(A): pass T = TypeVar('T') -a = None # type: A -b = None # type: B +a: A +b: B def f(a: T, b: T) -> T: pass @@ -629,10 +629,11 @@ def f(a: T, b: S) -> Tuple[T, S]: pass class A: pass class B: pass -a, b = None, None # type: (A, B) -taa = None # type: Tuple[A, A] -tab = None # type: Tuple[A, B] -tba = None # type: Tuple[B, A] +a: A +b: B +taa: Tuple[A, A] +tab: Tuple[A, B] +tba: Tuple[B, A] if int(): taa = f(a, b) # E: Argument 2 to "f" has incompatible type "B"; expected "A" @@ -651,9 +652,9 @@ if int(): [case testConstraintSolvingWithSimpleGenerics] from typing import TypeVar, Generic T = TypeVar('T') -ao = None # type: A[object] -ab = None # type: A[B] -ac = None # type: A[C] +ao: A[object] +ab: A[B] +ac: A[C] def f(a: 'A[T]') -> 'A[T]': pass @@ -683,8 +684,8 @@ if int(): [case testConstraintSolvingFailureWithSimpleGenerics] from typing import TypeVar, Generic T = TypeVar('T') -ao = None # type: A[object] -ab = None # type: A[B] +ao: A[object] +ab: A[B] def f(a: 'A[T]', b: 'A[T]') -> None: pass @@ -696,7 +697,9 @@ f(ao, ab) # E: Cannot infer type argument 1 of "f" f(ab, ao) # E: Cannot infer type argument 1 of "f" f(ao, ao) f(ab, ab) + [case testTypeInferenceWithCalleeDefaultArgs] +# flags: --no-strict-optional from typing import TypeVar T = TypeVar('T') a = None # type: A @@ -809,7 +812,9 @@ class C(A, I, J): pass def f(a: T, b: T) -> T: pass T = TypeVar('T') -a, i, j = None, None, None # type: (A, I, J) +a: A +i: I +j: J a = f(B(), C()) @@ -846,7 +851,7 @@ from typing import Callable, Type, TypeVar T = TypeVar('T') def f(x: Callable[..., T]) -> T: return x() class A: pass -x = None # type: Type[A] +x: Type[A] y = f(x) reveal_type(y) # N: Revealed type is "__main__.A" @@ -1031,7 +1036,8 @@ class A: pass class B: pass def d_ab() -> Dict[A, B]: return {} def d_aa() -> Dict[A, A]: return {} -a, b = None, None # type: (A, B) +a: A +b: B d = {a:b} if int(): d = d_ab() @@ -1041,7 +1047,8 @@ if int(): [case testSetLiteral] from typing import Any, Set -a, x = None, None # type: (int, Any) +a: int +x: Any def s_i() -> Set[int]: return set() def s_s() -> Set[str]: return set() s = {a} @@ -1113,7 +1120,8 @@ list_2 = [f, h] [case testInferenceOfFor1] -a, b = None, None # type: (A, B) +a: A +b: B class A: pass class B: pass @@ -1132,7 +1140,9 @@ class A: pass class B: pass class C: pass -a, b, c = None, None, None # type: (A, B, C) +a: A +b: B +c: C for x, (y, z) in [(A(), (B(), C()))]: b = x # E: Incompatible types in assignment (expression has type "A", variable has type "B") c = y # E: Incompatible types in assignment (expression has type "B", variable has type "C") @@ -1152,7 +1162,8 @@ for xxx, yyy in [(None, None)]: class A: pass class B: pass -a, b = None, None # type: (A, B) +a: A +b: B for x, y in [[A()]]: b = x # E: Incompatible types in assignment (expression has type "A", variable has type "B") @@ -1234,7 +1245,7 @@ class B: pass [case testMultipleAssignmentWithPartialDefinition] -a = None # type: A +a: A if int(): x, a = a, a if int(): @@ -1246,7 +1257,7 @@ if int(): class A: pass [case testMultipleAssignmentWithPartialDefinition2] -a = None # type: A +a: A if int(): a, x = [a, a] if int(): @@ -1260,7 +1271,7 @@ class A: pass [case testMultipleAssignmentWithPartialDefinition3] from typing import Any, cast -a = None # type: A +a: A if int(): x, a = cast(Any, a) if int(): @@ -1275,7 +1286,7 @@ class A: pass class A: pass class B: pass -if A: +if int(): a = A() if int(): a = A() @@ -1380,6 +1391,7 @@ main:4: error: Cannot infer type argument 1 of "f" main:4: error: Argument 2 to "f" has incompatible type "int"; expected "str" [case testInferLambdaNone] +# flags: --no-strict-optional from typing import Callable def f(x: Callable[[], None]) -> None: pass def g(x: Callable[[], int]) -> None: pass @@ -1442,8 +1454,8 @@ def g(cond: bool) -> Any: [case testOrOperationWithGenericOperands] from typing import List -a = None # type: List[A] -o = None # type: List[object] +a: List[A] +o: List[object] a2 = a or [] if int(): a = a2 @@ -1464,7 +1476,7 @@ x.y # E: "object" has no attribute "y" [case testAccessDataAttributeBeforeItsTypeIsAvailable] -a = None # type: A +a: A a.x.y # E: Cannot determine type of "x" class A: def __init__(self) -> None: @@ -1481,7 +1493,7 @@ from typing import List, _promote class A: pass @_promote(A) class B: pass -a = None # type: List[A] +a: List[A] x1 = [A(), B()] x2 = [B(), A()] x3 = [B(), B()] @@ -1504,7 +1516,7 @@ class A: pass class B: pass @_promote(B) class C: pass -a = None # type: List[A] +a: List[A] x1 = [A(), C()] x2 = [C(), A()] x3 = [B(), C()] @@ -1915,6 +1927,7 @@ reveal_type(dd) # N: Revealed type is "builtins.dict[builtins.str, builtins.int [builtins fixtures/dict.pyi] [case testInferFromEmptyDictWhenUsingInSpecialCase] +# flags: --no-strict-optional d = None if 'x' in d: # E: "None" has no attribute "__iter__" (not iterable) pass @@ -1937,6 +1950,7 @@ reveal_type(a) # N: Revealed type is "builtins.list[builtins.int]" [builtins fixtures/list.pyi] [case testInferSetTypeFromInplaceOr] +# flags: --no-strict-optional a = set() a |= {'x'} reveal_type(a) # N: Revealed type is "builtins.set[builtins.str]" @@ -1953,7 +1967,8 @@ def f() -> None: x = None else: x = 1 - x() # E: "int" not callable + x() # E: "int" not callable \ + # E: "None" not callable [out] [case testLocalVariablePartiallyTwiceInitializedToNone] @@ -1964,7 +1979,8 @@ def f() -> None: x = None else: x = 1 - x() # E: "int" not callable + x() # E: "int" not callable \ + # E: "None" not callable [out] [case testLvarInitializedToNoneWithoutType] @@ -1978,7 +1994,8 @@ def f() -> None: x = None if object(): x = 1 -x() # E: "int" not callable +x() # E: "int" not callable \ + # E: "None" not callable [case testPartiallyInitializedToNoneAndThenToPartialList] x = None @@ -2339,8 +2356,8 @@ reveal_type(A.g) # N: Revealed type is "def (x: builtins.str)" [case testUnificationRedundantUnion] from typing import Union -a = None # type: Union[int, str] -b = None # type: Union[str, tuple] +a: Union[int, str] +b: Union[str, tuple] def f(): pass def g(x: Union[int, str]): pass c = a if f() else b diff --git a/test-data/unit/check-isinstance.test b/test-data/unit/check-isinstance.test index 2d010b8ba38d..3403e726d8b5 100644 --- a/test-data/unit/check-isinstance.test +++ b/test-data/unit/check-isinstance.test @@ -10,7 +10,7 @@ y = x [case testJoinAny] from typing import List, Any -x = None # type: List[Any] +x: List[Any] def foo() -> List[int]: pass def bar() -> List[str]: pass @@ -51,9 +51,9 @@ def f(x: Union[int, str, List]) -> None: [case testClassAttributeInitialization] class A: - x = None # type: int + x: int def __init__(self) -> None: - self.y = None # type: int + self.y: int z = self.x w = self.y @@ -71,7 +71,7 @@ def foo(x: Union[str, int]): y + [1] # E: List item 0 has incompatible type "int"; expected "str" z = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "str") -x = None # type: int +x: int y = [x] [builtins fixtures/isinstancelist.pyi] @@ -124,7 +124,7 @@ x = 'a' [case testUnionMultiAssignment] from typing import Union -x = None # type: Union[int, str] +x: Union[int, str] if int(): x = 1 x = 'a' @@ -488,7 +488,7 @@ x.y # OK: x is known to be a B [case testIsInstanceBasic] from typing import Union -x = None # type: Union[int, str] +x: Union[int, str] if isinstance(x, str): x = x + 1 # E: Unsupported operand types for + ("str" and "int") x = x + 'a' @@ -499,7 +499,7 @@ else: [case testIsInstanceIndexing] from typing import Union -x = None # type: Union[int, str] +x: Union[int, str] j = [x] if isinstance(j[0], str): j[0] = j[0] + 'a' @@ -671,7 +671,7 @@ foo() from typing import Union def foo() -> None: - x = None # type: Union[int, str] + x: Union[int, str] if isinstance(x, int): for z in [1,2]: break @@ -686,7 +686,7 @@ foo() [case testIsInstanceThreeUnion] from typing import Union, List -x = None # type: Union[int, str, List[int]] +x: Union[int, str, List[int]] while bool(): if isinstance(x, int): @@ -706,7 +706,7 @@ x + [1] # E: Unsupported operand types for + ("int" and "List[int] [case testIsInstanceThreeUnion2] from typing import Union, List -x = None # type: Union[int, str, List[int]] +x: Union[int, str, List[int]] while bool(): if isinstance(x, int): x + 1 @@ -725,7 +725,7 @@ x + [1] # E: Unsupported operand types for + ("int" and "List[int] from typing import Union, List while bool(): - x = None # type: Union[int, str, List[int]] + x: Union[int, str, List[int]] def f(): x # Prevent redefinition x = 1 if isinstance(x, int): @@ -1787,6 +1787,7 @@ issubclass(x, (int, Iterable[int])) # E: Parameterized generics cannot be used [typing fixtures/typing-full.pyi] [case testIssubclassWithMetaclasses] +# flags: --no-strict-optional class FooMetaclass(type): ... class Foo(metaclass=FooMetaclass): ... class Bar: ... diff --git a/test-data/unit/check-kwargs.test b/test-data/unit/check-kwargs.test index 81fdc444aced..b3ee47aa6fdf 100644 --- a/test-data/unit/check-kwargs.test +++ b/test-data/unit/check-kwargs.test @@ -54,6 +54,7 @@ f(b=[], a=A()) [builtins fixtures/list.pyi] [case testGivingArgumentAsPositionalAndKeywordArg] +# flags: --no-strict-optional import typing class A: pass class B: pass @@ -61,11 +62,13 @@ def f(a: 'A', b: 'B' = None) -> None: pass f(A(), a=A()) # E: "f" gets multiple values for keyword argument "a" [case testGivingArgumentAsPositionalAndKeywordArg2] +# flags: --no-strict-optional import typing class A: pass class B: pass def f(a: 'A' = None, b: 'B' = None) -> None: pass f(A(), a=A()) # E: "f" gets multiple values for keyword argument "a" + [case testPositionalAndKeywordForSameArg] # This used to crash in check_argument_count(). See #1095. def f(a: int): pass @@ -134,7 +137,7 @@ f(otter=A()) # E: Missing positional argument "other" in call to "f" [case testKeywordArgumentsWithDynamicallyTypedCallable] from typing import Any -f = None # type: Any +f: Any f(x=f(), z=None()) # E: "None" not callable f(f, zz=None()) # E: "None" not callable f(x=None) @@ -143,10 +146,12 @@ f(x=None) from typing import Callable class A: pass class B: pass -f = None # type: Callable[[A, B], None] +f: Callable[[A, B], None] f(a=A(), b=B()) # E: Unexpected keyword argument "a" # E: Unexpected keyword argument "b" f(A(), b=B()) # E: Unexpected keyword argument "b" + [case testKeywordOnlyArguments] +# flags: --no-strict-optional import typing class A: pass class B: pass @@ -172,7 +177,9 @@ i(A(), b=B()) i(A(), aa=A()) # E: Missing named argument "b" for "i" i(A(), b=B(), aa=A()) i(A(), aa=A(), b=B()) + [case testKeywordOnlyArgumentsFastparse] +# flags: --no-strict-optional import typing class A: pass @@ -290,10 +297,10 @@ from typing import Dict class A: pass class B: pass def f( **kwargs: 'A') -> None: pass -d = None # type: Dict[str, A] +d: Dict[str, A] f(**d) f(x=A(), **d) -d2 = None # type: Dict[str, B] +d2: Dict[str, B] f(**d2) # E: Argument 1 to "f" has incompatible type "**Dict[str, B]"; expected "A" f(x=A(), **d2) # E: Argument 2 to "f" has incompatible type "**Dict[str, B]"; expected "A" f(**{'x': B()}) # E: Argument 1 to "f" has incompatible type "**Dict[str, B]"; expected "A" @@ -324,9 +331,9 @@ reveal_type(formatter.__call__) # N: Revealed type is "def (message: builtins.s [case testPassingMappingForKeywordVarArg] from typing import Mapping def f(**kwargs: 'A') -> None: pass -b = None # type: Mapping -d = None # type: Mapping[A, A] -m = None # type: Mapping[str, A] +b: Mapping +d: Mapping[A, A] +m: Mapping[str, A] f(**d) # E: Keywords must be strings f(**m) f(**b) @@ -337,7 +344,7 @@ class A: pass from typing import Mapping class MappingSubclass(Mapping[str, str]): pass def f(**kwargs: 'A') -> None: pass -d = None # type: MappingSubclass +d: MappingSubclass f(**d) class A: pass [builtins fixtures/dict.pyi] @@ -357,9 +364,9 @@ f(**kwargs) # E: Argument after ** must be a mapping, not "Optional[Any]" [case testPassingKeywordVarArgsToNonVarArgsFunction] from typing import Any, Dict def f(a: 'A', b: 'B') -> None: pass -d = None # type: Dict[str, Any] +d: Dict[str, Any] f(**d) -d2 = None # type: Dict[str, A] +d2: Dict[str, A] f(**d2) # E: Argument 1 to "f" has incompatible type "**Dict[str, A]"; expected "B" class A: pass class B: pass @@ -368,8 +375,8 @@ class B: pass [case testBothKindsOfVarArgs] from typing import Any, List, Dict def f(a: 'A', b: 'A') -> None: pass -l = None # type: List[Any] -d = None # type: Dict[Any, Any] +l: List[Any] +d: Dict[Any, Any] f(*l, **d) class A: pass [builtins fixtures/dict.pyi] @@ -380,8 +387,8 @@ def f1(a: 'A', b: 'A') -> None: pass def f2(a: 'A') -> None: pass def f3(a: 'A', **kwargs: 'A') -> None: pass def f4(**kwargs: 'A') -> None: pass -d = None # type: Dict[Any, Any] -d2 = None # type: Dict[Any, Any] +d: Dict[Any, Any] +d2: Dict[Any, Any] f1(**d, **d2) f2(**d, **d2) f3(**d, **d2) @@ -392,7 +399,7 @@ class A: pass [case testPassingKeywordVarArgsToVarArgsOnlyFunction] from typing import Any, Dict def f(*args: 'A') -> None: pass -d = None # type: Dict[Any, Any] +d: Dict[Any, Any] f(**d) class A: pass [builtins fixtures/dict.pyi] diff --git a/test-data/unit/check-literal.test b/test-data/unit/check-literal.test index 1a529051259f..104dd4d144e0 100644 --- a/test-data/unit/check-literal.test +++ b/test-data/unit/check-literal.test @@ -753,16 +753,16 @@ Foo = Literal[5] [case testLiteralBiasTowardsAssumingForwardReferencesForTypeComments] from typing_extensions import Literal -a = None # type: Foo +a: Foo reveal_type(a) # N: Revealed type is "__main__.Foo" -b = None # type: "Foo" +b: "Foo" reveal_type(b) # N: Revealed type is "__main__.Foo" -c = None # type: Literal["Foo"] +c: Literal["Foo"] reveal_type(c) # N: Revealed type is "Literal['Foo']" -d = None # type: Literal[Foo] # E: Parameter 1 of Literal[...] is invalid +d: Literal[Foo] # E: Parameter 1 of Literal[...] is invalid class Foo: pass [builtins fixtures/tuple.pyi] @@ -1760,7 +1760,7 @@ reveal_type(func1(identity(b))) # N: Revealed type is "builtins.int" -- [case testLiteralMeets] -from typing import TypeVar, List, Callable, Union +from typing import TypeVar, List, Callable, Union, Optional from typing_extensions import Literal a: Callable[[Literal[1]], int] @@ -1794,12 +1794,14 @@ def f2(x: Literal[1], y: Literal[2]) -> None: pass def f3(x: Literal[1], y: int) -> None: pass def f4(x: Literal[1], y: object) -> None: pass def f5(x: Literal[1], y: Union[Literal[1], Literal[2]]) -> None: pass +def f6(x: Optional[Literal[1]], y: Optional[Literal[2]]) -> None: pass reveal_type(unify(f1)) # N: Revealed type is "Literal[1]" -reveal_type(unify(f2)) # N: Revealed type is "None" +reveal_type(unify(f2)) # N: Revealed type is "" reveal_type(unify(f3)) # N: Revealed type is "Literal[1]" reveal_type(unify(f4)) # N: Revealed type is "Literal[1]" reveal_type(unify(f5)) # N: Revealed type is "Literal[1]" +reveal_type(unify(f6)) # N: Revealed type is "None" [builtins fixtures/list.pyi] [out] @@ -2013,7 +2015,7 @@ optional_keys: Literal["d", "e"] bad_keys: Literal["a", "bad"] reveal_type(test[good_keys]) # N: Revealed type is "Union[__main__.A, __main__.B]" -reveal_type(test.get(good_keys)) # N: Revealed type is "Union[__main__.A, __main__.B]" +reveal_type(test.get(good_keys)) # N: Revealed type is "Union[__main__.A, __main__.B, None]" reveal_type(test.get(good_keys, 3)) # N: Revealed type is "Union[__main__.A, Literal[3]?, __main__.B]" reveal_type(test.pop(optional_keys)) # N: Revealed type is "Union[__main__.D, __main__.E]" reveal_type(test.pop(optional_keys, 3)) # N: Revealed type is "Union[__main__.D, __main__.E, Literal[3]?]" @@ -2066,7 +2068,7 @@ x[bad_keys] # E: TypedDict "D1" has no key "d" \ # E: TypedDict "D2" has no key "a" reveal_type(x[good_keys]) # N: Revealed type is "Union[__main__.B, __main__.C]" -reveal_type(x.get(good_keys)) # N: Revealed type is "Union[__main__.B, __main__.C]" +reveal_type(x.get(good_keys)) # N: Revealed type is "Union[__main__.B, __main__.C, None]" reveal_type(x.get(good_keys, 3)) # N: Revealed type is "Union[__main__.B, Literal[3]?, __main__.C]" reveal_type(x.get(bad_keys)) # N: Revealed type is "builtins.object" reveal_type(x.get(bad_keys, 3)) # N: Revealed type is "builtins.object"