diff --git a/mypy/test/testcheck.py b/mypy/test/testcheck.py index c91bd915096f..a77da090f740 100644 --- a/mypy/test/testcheck.py +++ b/mypy/test/testcheck.py @@ -52,11 +52,6 @@ # 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-namedtuple.test", - "check-overloading.test", - "check-plugin-attrs.test", - "check-statements.test", - "check-tuples.test", "check-unions.test", "check-varargs.test", } diff --git a/test-data/unit/check-namedtuple.test b/test-data/unit/check-namedtuple.test index 5eed70246e8c..83cc8c099deb 100644 --- a/test-data/unit/check-namedtuple.test +++ b/test-data/unit/check-namedtuple.test @@ -2,7 +2,7 @@ from collections import namedtuple X = namedtuple('X', 'x y') -x = None # type: X +x: X a, b = x b = x[0] a = x[1] @@ -14,7 +14,7 @@ x[2] # E: Tuple index out of range from collections import namedtuple X = namedtuple('X', ('x', 'y')) -x = None # type: X +x: X a, b = x b = x[0] a = x[1] @@ -32,7 +32,7 @@ X = namedtuple('X', 'x, _y, _z') # E: "namedtuple()" field names cannot start w from collections import namedtuple X = namedtuple('X', 'x y') -x = None # type: X +x: X x.x x.y x.z # E: "X" has no attribute "z" @@ -63,13 +63,13 @@ class A(NamedTuple): from collections import namedtuple X = namedtuple('X', 'x y') -x = None # type: X +x: X x.x = 5 # E: Property "x" defined in "X" is read-only x.y = 5 # E: Property "y" defined in "X" is read-only x.z = 5 # E: "X" has no attribute "z" class A(X): pass -a = None # type: A +a: A a.x = 5 # E: Property "x" defined in "X" is read-only a.y = 5 # E: Property "y" defined in "X" is read-only -- a.z = 5 # not supported yet @@ -292,7 +292,7 @@ A = NamedTuple('A', [('a', int), ('b', str)]) class B(A): pass a = A(1, '') b = B(1, '') -t = None # type: Tuple[int, str] +t: Tuple[int, str] if int(): b = a # E: Incompatible types in assignment (expression has type "A", variable has type "B") if int(): @@ -357,7 +357,7 @@ C(2).b from collections import namedtuple X = namedtuple('X', ['x', 'y']) -x = None # type: X +x: X reveal_type(x._asdict()) # N: Revealed type is "builtins.dict[builtins.str, Any]" [builtins fixtures/dict.pyi] @@ -366,7 +366,7 @@ reveal_type(x._asdict()) # N: Revealed type is "builtins.dict[builtins.str, Any from collections import namedtuple X = namedtuple('X', ['x', 'y']) -x = None # type: X +x: X reveal_type(x._replace()) # N: Revealed type is "Tuple[Any, Any, fallback=__main__.X]" x._replace(y=5) x._replace(x=3) @@ -391,7 +391,7 @@ X._replace(x=1, y=2) # E: Missing positional argument "_self" in call to "_repl from typing import NamedTuple X = NamedTuple('X', [('x', int), ('y', str)]) -x = None # type: X +x: X reveal_type(x._replace()) # N: Revealed type is "Tuple[builtins.int, builtins.str, fallback=__main__.X]" x._replace(x=5) 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 X._make('a b') # E: Argument 1 to "_make" of "X" has incompatible type "str"; expected "Iterable[Any]" -- # FIX: not a proper class method --- x = None # type: X +-- x: X -- reveal_type(x._make([5, 'a'])) # N: Revealed type is "Tuple[builtins.int, builtins.str, fallback=__main__.X]" -- x._make('a b') # E: Argument 1 to "_make" of "X" has incompatible type "str"; expected Iterable[Any] @@ -423,7 +423,7 @@ from typing import NamedTuple X = NamedTuple('X', [('x', int), ('y', str)]) reveal_type(X._source) # N: Revealed type is "builtins.str" -x = None # type: X +x: X reveal_type(x._source) # N: Revealed type is "builtins.str" [builtins fixtures/tuple.pyi] @@ -459,7 +459,7 @@ from typing import NamedTuple X = NamedTuple('X', [('x', int), ('y', str)]) reveal_type(X._field_types) # N: Revealed type is "builtins.dict[builtins.str, Any]" -x = None # type: X +x: X reveal_type(x._field_types) # N: Revealed type is "builtins.dict[builtins.str, Any]" [builtins fixtures/dict.pyi] @@ -472,7 +472,7 @@ def f(x: A) -> None: pass class B(NamedTuple('B', []), A): pass f(B()) -x = None # type: A +x: A if int(): x = B() @@ -482,7 +482,7 @@ def g(x: C) -> None: pass class D(NamedTuple('D', []), A): pass g(D()) # E: Argument 1 to "g" has incompatible type "D"; expected "C" -y = None # type: C +y: C if int(): y = D() # E: Incompatible types in assignment (expression has type "D", variable has type "C") [builtins fixtures/tuple.pyi] @@ -499,9 +499,9 @@ class A(NamedTuple('A', [('x', str)])): class B(A): pass -a = None # type: A +a: A a = A('').member() -b = None # type: B +b: B b = B('').member() a = B('') a = B('').member() @@ -511,14 +511,14 @@ a = B('').member() from typing import NamedTuple, TypeVar A = NamedTuple('A', [('x', str)]) reveal_type(A('hello')._replace(x='')) # N: Revealed type is "Tuple[builtins.str, fallback=__main__.A]" -a = None # type: A +a: A a = A('hello')._replace(x='') class B(A): pass reveal_type(B('hello')._replace(x='')) # N: Revealed type is "Tuple[builtins.str, fallback=__main__.B]" -b = None # type: B +b: B b = B('hello')._replace(x='') [builtins fixtures/tuple.pyi] diff --git a/test-data/unit/check-overloading.test b/test-data/unit/check-overloading.test index 96fbaa77514e..4851cc96e6da 100644 --- a/test-data/unit/check-overloading.test +++ b/test-data/unit/check-overloading.test @@ -450,7 +450,8 @@ class C: pass from foo import * [file foo.pyi] from typing import overload -a, b = None, None # type: (A, B) +a: A +b: B if int(): b = f(a) # E: Incompatible types in assignment (expression has type "A", variable has type "B") if int(): @@ -492,7 +493,8 @@ class C: pass from foo import * [file foo.pyi] from typing import overload -a, b = None, None # type: (A, B) +a: A +b: B if int(): b = a.f(a) # E: Incompatible types in assignment (expression has type "A", variable has type "B") if int(): @@ -514,7 +516,8 @@ class B: pass from foo import * [file foo.pyi] from typing import overload -a, b = None, None # type: (A, B) +a: A +b: B if int(): a = f(a) if int(): @@ -549,7 +552,10 @@ from foo import * [file foo.pyi] from typing import overload, TypeVar, Generic t = TypeVar('t') -ab, ac, b, c = None, None, None, None # type: (A[B], A[C], B, C) +ab: A[B] +ac: A[C] +b: B +c: C if int(): b = f(ab) c = f(ac) @@ -569,7 +575,8 @@ class C: pass from foo import * [file foo.pyi] from typing import overload -a, b = None, None # type: (A, B) +a: A +b: B a = A(a) a = A(b) a = A(object()) # E: No overload variant of "A" matches argument type "object" \ @@ -589,8 +596,8 @@ class B: pass from foo import * [file foo.pyi] from typing import overload, Callable -o = None # type: object -a = None # type: A +o: object +a: A if int(): a = f # E: Incompatible types in assignment (expression has type overloaded function, variable has type "A") @@ -607,7 +614,8 @@ class A: pass from foo import * [file foo.pyi] from typing import overload -t, a = None, None # type: (type, A) +t: type +a: A if int(): a = A # E: Incompatible types in assignment (expression has type "Type[A]", variable has type "A") @@ -625,7 +633,8 @@ class B: pass from foo import * [file foo.pyi] from typing import overload -a, b = None, None # type: int, str +a: int +b: str if int(): a = A()[a] if int(): @@ -647,7 +656,9 @@ from foo import * [file foo.pyi] from typing import TypeVar, Generic, overload t = TypeVar('t') -a, b, c = None, None, None # type: (A, B, C[A]) +a: A +b: B +c: C[A] if int(): a = c[a] b = c[a] # E: Incompatible types in assignment (expression has type "A", variable has type "B") @@ -761,7 +772,8 @@ from typing import overload def f(t: type) -> 'A': pass @overload def f(t: 'A') -> 'B': pass -a, b = None, None # type: (A, B) +a: A +b: B if int(): a = f(A) if int(): @@ -1215,6 +1227,7 @@ f(*(1, '', 1))() # E: No overload variant of "f" matches argument type "Tuple[in [builtins fixtures/tuple.pyi] [case testPreferExactSignatureMatchInOverload] +# flags: --no-strict-optional from foo import * [file foo.pyi] from typing import overload, List diff --git a/test-data/unit/check-plugin-attrs.test b/test-data/unit/check-plugin-attrs.test index 5b8c361906a8..edae6c096015 100644 --- a/test-data/unit/check-plugin-attrs.test +++ b/test-data/unit/check-plugin-attrs.test @@ -672,6 +672,7 @@ class A(Generic[T]): [builtins fixtures/classmethod.pyi] [case testAttrsForwardReference] +# flags: --no-strict-optional import attr @attr.s(auto_attribs=True) class A: @@ -687,6 +688,7 @@ A(B(None)) [builtins fixtures/list.pyi] [case testAttrsForwardReferenceInClass] +# flags: --no-strict-optional import attr @attr.s(auto_attribs=True) class A: @@ -1593,6 +1595,7 @@ def f(t: TA) -> None: [builtins fixtures/plugin_attrs.pyi] [case testNonattrsFields] +# flags: --no-strict-optional from typing import Any, cast, Type from attrs import fields diff --git a/test-data/unit/check-statements.test b/test-data/unit/check-statements.test index 29e2c5ba3266..8a232d52968f 100644 --- a/test-data/unit/check-statements.test +++ b/test-data/unit/check-statements.test @@ -95,10 +95,10 @@ def f() -> Iterator[int]: [case testIfStatement] -a = None # type: A -a2 = None # type: A -a3 = None # type: A -b = None # type: bool +a: A +a2: A +a3: A +b: bool if a: a = b # E: Incompatible types in assignment (expression has type "bool", variable has type "A") elif a2: @@ -124,8 +124,8 @@ class A: pass [case testWhileStatement] -a = None # type: A -b = None # type: bool +a: A +b: bool while a: a = b # Fail else: @@ -142,13 +142,14 @@ main:7: error: Incompatible types in assignment (expression has type "bool", var [case testForStatement] class A: pass -a = None # type: A -b = None # type: object +a: A +b: object for a in [A()]: a = b # E: Incompatible types in assignment (expression has type "object", variable has type "A") else: a = b # E: Incompatible types in assignment (expression has type "object", variable has type "A") [builtins fixtures/list.pyi] + [case testBreakStatement] import typing while None: @@ -205,8 +206,9 @@ for a, b in x: # type: int, int, int # E: Incompatible number of tuple items [case testPlusAssign] - -a, b, c = None, None, None # type: (A, B, C) +a: A +b: B +c: C a += b # Fail b += a # Fail c += a # Fail @@ -221,13 +223,14 @@ class B: class C: pass [builtins fixtures/tuple.pyi] [out] -main:3: error: Unsupported operand types for + ("A" and "B") -main:4: error: Incompatible types in assignment (expression has type "C", variable has type "B") -main:5: error: Unsupported left operand type for + ("C") +main:4: error: Unsupported operand types for + ("A" and "B") +main:5: error: Incompatible types in assignment (expression has type "C", variable has type "B") +main:6: error: Unsupported left operand type for + ("C") [case testMinusAssign] - -a, b, c = None, None, None # type: (A, B, C) +a: A +b: B +c: C a -= b # Fail b -= a # Fail c -= a # Fail @@ -242,13 +245,13 @@ class B: class C: pass [builtins fixtures/tuple.pyi] [out] -main:3: error: Unsupported operand types for - ("A" and "B") -main:4: error: Incompatible types in assignment (expression has type "C", variable has type "B") -main:5: error: Unsupported left operand type for - ("C") +main:4: error: Unsupported operand types for - ("A" and "B") +main:5: error: Incompatible types in assignment (expression has type "C", variable has type "B") +main:6: error: Unsupported left operand type for - ("C") [case testMulAssign] - -a, c = None, None # type: (A, C) +a: A +c: C a *= a # Fail c *= a # Fail a *= c @@ -263,7 +266,8 @@ main:3: error: Unsupported operand types for * ("A" and "A") main:4: error: Unsupported left operand type for * ("C") [case testMatMulAssign] -a, c = None, None # type: (A, C) +a: A +c: C a @= a # E: Unsupported operand types for @ ("A" and "A") c @= a # E: Unsupported left operand type for @ ("C") a @= c @@ -275,8 +279,8 @@ class C: pass [builtins fixtures/tuple.pyi] [case testDivAssign] - -a, c = None, None # type: (A, C) +a: A +c: C a /= a # Fail c /= a # Fail a /= c @@ -291,8 +295,8 @@ main:3: error: Unsupported operand types for / ("A" and "A") main:4: error: Unsupported left operand type for / ("C") [case testPowAssign] - -a, c = None, None # type: (A, C) +a: A +c: C a **= a # Fail c **= a # Fail a **= c @@ -307,8 +311,8 @@ main:3: error: Unsupported operand types for ** ("A" and "A") main:4: error: Unsupported left operand type for ** ("C") [case testSubtypesInOperatorAssignment] - -a, b = None, None # type: (A, B) +a: A +b: B b += b b += a a += b @@ -321,8 +325,8 @@ class B(A): pass [out] [case testAdditionalOperatorsInOpAssign] - -a, c = None, None # type: (A, C) +a: A +c: C a &= a # Fail a >>= a # Fail a //= a # Fail @@ -389,9 +393,9 @@ main:2: error: Unsupported left operand type for + ("None") [case testRaiseStatement] -e = None # type: BaseException -f = None # type: MyError -a = None # type: A +e: BaseException +f: MyError +a: A raise a # Fail raise e raise f @@ -441,7 +445,7 @@ raise MyKwError # E: Missing named argument "kwonly" for "MyKwError" [case testRaiseExceptionType] import typing -x = None # type: typing.Type[BaseException] +x: typing.Type[BaseException] raise x [builtins fixtures/exception.pyi] @@ -453,21 +457,21 @@ raise x # E: Exception must be derived from BaseException [case testRaiseUnion] import typing -x = None # type: typing.Union[BaseException, typing.Type[BaseException]] +x: typing.Union[BaseException, typing.Type[BaseException]] raise x [builtins fixtures/exception.pyi] [case testRaiseNonExceptionUnionFails] import typing -x = None # type: typing.Union[BaseException, int] +x: typing.Union[BaseException, int] raise x # E: Exception must be derived from BaseException [builtins fixtures/exception.pyi] [case testRaiseFromStatement] -e = None # type: BaseException -f = None # type: MyError -a = None # type: A -x = None # type: BaseException +e: BaseException +f: MyError +a: A +x: BaseException del x raise e from a # E: Exception must be derived from BaseException raise e from e @@ -505,27 +509,30 @@ main:5: error: Incompatible types in assignment (expression has type "object", v try: pass except BaseException as e: - a, o = None, None # type: (BaseException, object) + a: BaseException + o: object e = a e = o # Fail class A: pass class B: pass [builtins fixtures/exception.pyi] [out] -main:7: error: Incompatible types in assignment (expression has type "object", variable has type "BaseException") +main:8: error: Incompatible types in assignment (expression has type "object", variable has type "BaseException") [case testTypeErrorInBlock] class A: pass class B: pass -while object: - x = None # type: A +while int(): + x: A if int(): x = object() # E: Incompatible types in assignment (expression has type "object", variable has type "A") x = B() # E: Incompatible types in assignment (expression has type "B", variable has type "A") + [case testTypeErrorInvolvingBaseException] class A: pass -x, a = None, None # type: (BaseException, A) +x: BaseException +a: A if int(): a = BaseException() # E: Incompatible types in assignment (expression has type "BaseException", variable has type "A") if int(): @@ -1042,7 +1049,8 @@ main:12: error: Exception type must be derived from BaseException (or be a tuple [case testDelStmtWithIndex] -a, b = None, None # type: (A, B) +a: A +b: B del b[a] del b[b] # E: Argument 1 to "__delitem__" of "B" has incompatible type "B"; expected "A" del a[a] # E: "A" has no attribute "__delitem__" @@ -1965,6 +1973,7 @@ def f() -> None: [out] [case testChainedAssignmentWithType] +# flags: --no-strict-optional x = y = None # type: int if int(): x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int") @@ -1982,7 +1991,8 @@ if int(): [case testAssignListToStarExpr] from typing import List -bs, cs = None, None # type: List[A], List[B] +bs: List[A] +cs: List[B] if int(): *bs, b = bs if int(): diff --git a/test-data/unit/check-tuples.test b/test-data/unit/check-tuples.test index f8d6573a56fe..5cb89a6854be 100644 --- a/test-data/unit/check-tuples.test +++ b/test-data/unit/check-tuples.test @@ -4,11 +4,11 @@ [case testTupleAssignmentWithTupleTypes] from typing import Tuple -t1 = None # type: Tuple[A] -t2 = None # type: Tuple[B] -t3 = None # type: Tuple[A, A] -t4 = None # type: Tuple[A, B] -t5 = None # type: Tuple[B, A] +t1: Tuple[A] +t2: Tuple[B] +t3: Tuple[A, A] +t4: Tuple[A, B] +t5: Tuple[B, A] if int(): t1 = t2 # E: Incompatible types in assignment (expression has type "Tuple[B]", variable has type "Tuple[A]") @@ -39,9 +39,9 @@ class B: pass [case testTupleSubtyping] from typing import Tuple -t1 = None # type: Tuple[A, A] -t2 = None # type: Tuple[A, B] -t3 = None # type: Tuple[B, A] +t1: Tuple[A, A] +t2: Tuple[A, B] +t3: Tuple[B, A] if int(): t2 = t1 # E: Incompatible types in assignment (expression has type "Tuple[A, A]", variable has type "Tuple[A, B]") @@ -57,6 +57,7 @@ class B(A): pass [builtins fixtures/tuple.pyi] [case testTupleCompatibilityWithOtherTypes] +# flags: --no-strict-optional from typing import Tuple a, o = None, None # type: (A, object) t = None # type: Tuple[A, A] @@ -80,8 +81,8 @@ class A: pass [case testNestedTupleTypes] from typing import Tuple -t1 = None # type: Tuple[A, Tuple[A, A]] -t2 = None # type: Tuple[B, Tuple[B, B]] +t1: Tuple[A, Tuple[A, A]] +t2: Tuple[B, Tuple[B, B]] if int(): t2 = t1 # E: Incompatible types in assignment (expression has type "Tuple[A, Tuple[A, A]]", variable has type "Tuple[B, Tuple[B, B]]") @@ -94,8 +95,8 @@ class B(A): pass [case testNestedTupleTypes2] from typing import Tuple -t1 = None # type: Tuple[A, Tuple[A, A]] -t2 = None # type: Tuple[B, Tuple[B, B]] +t1: Tuple[A, Tuple[A, A]] +t2: Tuple[B, Tuple[B, B]] if int(): t2 = t1 # E: Incompatible types in assignment (expression has type "Tuple[A, Tuple[A, A]]", variable has type "Tuple[B, Tuple[B, B]]") @@ -108,8 +109,8 @@ class B(A): pass [case testSubtypingWithNamedTupleType] from typing import Tuple -t1 = None # type: Tuple[A, A] -t2 = None # type: tuple +t1: Tuple[A, A] +t2: tuple if int(): t1 = t2 # E: Incompatible types in assignment (expression has type "Tuple[Any, ...]", variable has type "Tuple[A, A]") @@ -120,6 +121,7 @@ class A: pass [builtins fixtures/tuple.pyi] [case testTupleInitializationWithNone] +# flags: --no-strict-optional from typing import Tuple t = None # type: Tuple[A, A] t = None @@ -132,6 +134,7 @@ class A: pass [case testTupleExpressions] +# flags: --no-strict-optional from typing import Tuple t1 = None # type: tuple t2 = None # type: Tuple[A] @@ -177,12 +180,13 @@ def f() -> None: pass [case testIndexingTuples] from typing import Tuple -t1 = None # type: Tuple[A, B] -t2 = None # type: Tuple[A] -t3 = None # type: Tuple[A, B, C, D, E] -a, b = None, None # type: (A, B) -x = None # type: Tuple[A, B, C] -y = None # type: Tuple[A, C, E] +t1: Tuple[A, B] +t2: Tuple[A] +t3: Tuple[A, B, C, D, E] +a: A +b: B +x: Tuple[A, B, C] +y: Tuple[A, C, E] n = 0 if int(): @@ -221,9 +225,10 @@ class E: pass [case testIndexingTuplesWithNegativeIntegers] from typing import Tuple -t1 = None # type: Tuple[A, B] -t2 = None # type: Tuple[A] -a, b = None, None # type: A, B +t1: Tuple[A, B] +t2: Tuple[A] +a: A +b: B if int(): a = t1[-1] # E: Incompatible types in assignment (expression has type "B", variable has type "A") @@ -251,7 +256,7 @@ from typing import Tuple class A: pass class B: pass -t = None # type: Tuple[A, B] +t: Tuple[A, B] n = 0 t[0] = A() # E: Unsupported target for indexed assignment ("Tuple[A, B]") @@ -265,6 +270,7 @@ t[n] = A() # E: Unsupported target for indexed assignment ("Tuple[A, B]") [case testMultipleAssignmentWithTuples] +# flags: --no-strict-optional from typing import Tuple t1 = None # type: Tuple[A, B] t2 = None # type: Tuple[A, B, A] @@ -291,6 +297,7 @@ class B: pass [builtins fixtures/tuple.pyi] [case testMultipleAssignmentWithSquareBracketTuples] +# flags: --no-strict-optional from typing import Tuple def avoid_confusing_test_parser() -> None: @@ -322,8 +329,8 @@ class B: pass [case testMultipleAssignmentWithInvalidNumberOfValues] from typing import Tuple -t1 = None # type: Tuple[A, A, A] -a = None # type: A +t1: Tuple[A, A, A] +a: A a, a = t1 # E: Too many values to unpack (2 expected, 3 provided) a, a, a, a = t1 # E: Need more than 3 values to unpack (4 expected) @@ -334,8 +341,8 @@ class A: pass [builtins fixtures/tuple.pyi] [case testMultipleAssignmentWithTupleExpressionRvalue] - -a, b = None, None # type: (A, B) +a: A +b: B if int(): a, b = a, a # E: Incompatible types in assignment (expression has type "A", variable has type "B") @@ -354,7 +361,8 @@ class B: pass [builtins fixtures/tuple.pyi] [case testSubtypingInMultipleAssignment] -a, b = None, None # type: (A, B) +a: A +b: B if int(): b, b = a, b # E: Incompatible types in assignment (expression has type "A", variable has type "B") @@ -371,7 +379,7 @@ class B(A): pass [builtins fixtures/tuple.pyi] [case testInitializationWithMultipleValues] - +# flags: --no-strict-optional a, b = None, None # type: (A, B) a1, b1 = a, a # type: (A, B) # E: Incompatible types in assignment (expression has type "A", variable has type "B") @@ -387,8 +395,8 @@ class B: pass [builtins fixtures/tuple.pyi] [case testMultipleAssignmentWithNonTupleRvalue] - -a, b = None, None # type: (A, B) +a: A +b: B def f(): pass a, b = None # E: "None" object is not iterable @@ -400,9 +408,10 @@ class B: pass [builtins fixtures/tuple.pyi] [case testMultipleAssignmentWithIndexedLvalues] - -a, b = None, None # type: (A, B) -aa, bb = None, None # type: (AA, BB) +a: A +b: B +aa: AA +bb: BB a[a], b[b] = a, bb # E: Incompatible types in assignment (expression has type "A", target has type "AA") a[a], b[b] = aa, b # E: Incompatible types in assignment (expression has type "B", target has type "BB") @@ -420,6 +429,7 @@ class BB: pass [builtins fixtures/tuple.pyi] [case testMultipleDeclarationWithParentheses] +# flags: --no-strict-optional (a, b) = (None, None) # type: int, str if int(): a = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int") @@ -430,8 +440,8 @@ if int(): [builtins fixtures/tuple.pyi] [case testMultipleAssignmentWithExtraParentheses] - -a, b = None, None # type: (A, B) +a: A +b: B if int(): (a, b) = (a, a) # E: Incompatible types in assignment (expression has type "A", variable has type "B") @@ -458,6 +468,7 @@ class B: pass [builtins fixtures/tuple.pyi] [case testMultipleAssignmentUsingSingleTupleType] +# flags: --no-strict-optional from typing import Tuple a, b = None, None # type: Tuple[int, str] if int(): @@ -490,6 +501,7 @@ aa, bb, *cc = t # E: Need type annotation for "cc" (hint: "cc: List[] = . [builtins fixtures/list.pyi] [case testAssignmentToStarAnnotation] +# flags: --no-strict-optional from typing import List li, lo = None, None # type: List[int], List[object] a, b, *c = 1, 2 # type: int, int, List[int] @@ -501,7 +513,7 @@ if int(): [case testAssignmentToStarCount1] from typing import List -ca = None # type: List[int] +ca: List[int] c = [1] if int(): a, b, *c = 1, # E: Need more than 1 value to unpack (2 expected) @@ -515,7 +527,7 @@ if int(): [case testAssignmentToStarCount2] from typing import List -ca = None # type: List[int] +ca: List[int] t1 = 1, t2 = 1, 2 t3 = 1, 2, 3 @@ -541,7 +553,7 @@ c = a c = q [case testAssignmentToComplexStar] from typing import List -li = None # type: List[int] +li: List[int] if int(): a, *(li) = 1, a, *(b, c) = 1, 2 # E: Need more than 1 value to unpack (2 expected) @@ -553,9 +565,9 @@ if int(): [case testAssignmentToStarFromTupleType] from typing import List, Tuple -li = None # type: List[int] -la = None # type: List[A] -ta = None # type: Tuple[A, A, A] +li: List[int] +la: List[A] +ta: Tuple[A, A, A] if int(): a, *la = ta if int(): @@ -573,8 +585,8 @@ class A: pass [case testAssignmentToStarFromTupleInference] from typing import List class A: pass -li = None # type: List[int] -la = None # type: List[A] +li: List[int] +la: List[A] a, *l = A(), A() if int(): l = li # E: Incompatible types in assignment (expression has type "List[int]", variable has type "List[A]") @@ -588,8 +600,8 @@ from typing import List class A: pass -li = None # type: List[int] -la = None # type: List[A] +li: List[int] +la: List[A] a, *l = [A(), A()] if int(): l = li # E: Incompatible types in assignment (expression has type "List[int]", variable has type "List[A]") @@ -600,9 +612,9 @@ if int(): [case testAssignmentToStarFromTupleTypeInference] from typing import List, Tuple -li = None # type: List[int] -la = None # type: List[A] -ta = None # type: Tuple[A, A, A] +li: List[int] +la: List[A] +ta: Tuple[A, A, A] a, *l = ta if int(): l = li # E: Incompatible types in assignment (expression has type "List[int]", variable has type "List[A]") @@ -615,8 +627,8 @@ class A: pass [case testAssignmentToStarFromListTypeInference] from typing import List -li = None # type: List[int] -la = None # type: List[A] +li: List[int] +la: List[A] a, *l = la if int(): l = li # E: Incompatible types in assignment (expression has type "List[int]", variable has type "List[A]") @@ -656,9 +668,12 @@ reveal_type(e2) # N: Revealed type is "builtins.list[builtins.int]" [case testNestedTupleAssignment1] - -a1, b1, c1 = None, None, None # type: (A, B, C) -a2, b2, c2 = None, None, None # type: (A, B, C) +a1: A +a2: A +b1: B +b2: B +c1: C +c2: C if int(): a1, (b1, c1) = a2, (b2, c2) @@ -673,9 +688,12 @@ class C: pass [builtins fixtures/tuple.pyi] [case testNestedTupleAssignment2] - -a1, b1, c1 = None, None, None # type: (A, B, C) -a2, b2, c2 = None, None, None # type: (A, B, C) +a1: A +a2: A +b1: B +b2: B +c1: C +c2: C t = a1, b1 if int(): @@ -714,7 +732,7 @@ class A: def __add__(self, x: 'A') -> 'A': pass def f(x: 'A') -> None: pass -a = None # type: A +a: A (a, a) + a # E: Unsupported operand types for + ("Tuple[A, A]" and "A") a + (a, a) # E: Unsupported operand types for + ("A" and "Tuple[A, A]") @@ -724,7 +742,7 @@ f((a, a)) # E: Argument 1 to "f" has incompatible type "Tuple[A, A]"; expected [case testLargeTuplesInErrorMessages] -a = None # type: LongTypeName +a: LongTypeName a + (a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a) # Fail class LongTypeName: @@ -740,7 +758,7 @@ main:3: error: Unsupported operand types for + ("LongTypeName" and "Tuple[LongTy [case testTupleMethods] from typing import Tuple -t = None # type: Tuple[int, str] +t: Tuple[int, str] i = 0 s = '' b = bool() @@ -869,7 +887,7 @@ class A(tuple): pass import m [file m.pyi] from typing import Tuple -a = None # type: A +a: A class A(Tuple[int, str]): pass x, y = a x() # E: "int" not callable @@ -932,14 +950,14 @@ fb(aa) # E: Argument 1 to "fb" has incompatible type "Tuple[A, A]"; expected "Tu [case testSubtypingTupleIsContainer] from typing import Container -a = None # type: Container[str] +a: Container[str] a = () [typing fixtures/typing-full.pyi] [builtins fixtures/tuple.pyi] [case testSubtypingTupleIsSized] from typing import Sized -a = None # type: Sized +a: Sized a = () [typing fixtures/typing-medium.pyi] [builtins fixtures/tuple.pyi] @@ -1046,8 +1064,8 @@ class B1(A): pass class B2(A): pass class C: pass -x = None # type: Tuple[A, ...] -y = None # type: Tuple[Union[B1, C], Union[B2, C]] +x: Tuple[A, ...] +y: Tuple[Union[B1, C], Union[B2, C]] def g(x: T) -> Tuple[T, T]: return (x, x) @@ -1063,13 +1081,13 @@ from typing import Tuple class A: pass class B(A): pass -fixtup = None # type: Tuple[B, B] +fixtup: Tuple[B, B] -vartup_b = None # type: Tuple[B, ...] +vartup_b: Tuple[B, ...] reveal_type(fixtup if int() else vartup_b) # N: Revealed type is "builtins.tuple[__main__.B, ...]" reveal_type(vartup_b if int() else fixtup) # N: Revealed type is "builtins.tuple[__main__.B, ...]" -vartup_a = None # type: Tuple[A, ...] +vartup_a: Tuple[A, ...] reveal_type(fixtup if int() else vartup_a) # N: Revealed type is "builtins.tuple[__main__.A, ...]" reveal_type(vartup_a if int() else fixtup) # N: Revealed type is "builtins.tuple[__main__.A, ...]" @@ -1083,13 +1101,13 @@ from typing import Tuple, List class A: pass class B(A): pass -fixtup = None # type: Tuple[B, B] +fixtup: Tuple[B, B] -lst_b = None # type: List[B] +lst_b: List[B] reveal_type(fixtup if int() else lst_b) # N: Revealed type is "typing.Sequence[__main__.B]" reveal_type(lst_b if int() else fixtup) # N: Revealed type is "typing.Sequence[__main__.B]" -lst_a = None # type: List[A] +lst_a: List[A] reveal_type(fixtup if int() else lst_a) # N: Revealed type is "typing.Sequence[__main__.A]" reveal_type(lst_a if int() else fixtup) # N: Revealed type is "typing.Sequence[__main__.A]" @@ -1103,15 +1121,15 @@ class A: pass empty = () -fixtup = None # type: Tuple[A] +fixtup: Tuple[A] reveal_type(fixtup if int() else empty) # N: Revealed type is "builtins.tuple[__main__.A, ...]" reveal_type(empty if int() else fixtup) # N: Revealed type is "builtins.tuple[__main__.A, ...]" -vartup = None # type: Tuple[A, ...] +vartup: Tuple[A, ...] reveal_type(empty if int() else vartup) # N: Revealed type is "builtins.tuple[__main__.A, ...]" reveal_type(vartup if int() else empty) # N: Revealed type is "builtins.tuple[__main__.A, ...]" -lst = None # type: List[A] +lst: List[A] reveal_type(empty if int() else lst) # N: Revealed type is "typing.Sequence[__main__.A]" reveal_type(lst if int() else empty) # N: Revealed type is "typing.Sequence[__main__.A]" @@ -1128,9 +1146,9 @@ class NTup(NamedTuple): class SubTuple(Tuple[bool]): ... class SubVarTuple(Tuple[int, ...]): ... -ntup = None # type: NTup -subtup = None # type: SubTuple -vartup = None # type: SubVarTuple +ntup: NTup +subtup: SubTuple +vartup: SubVarTuple reveal_type(ntup if int() else vartup) # N: Revealed type is "builtins.tuple[builtins.int, ...]" reveal_type(subtup if int() else vartup) # N: Revealed type is "builtins.tuple[builtins.int, ...]" @@ -1141,8 +1159,8 @@ reveal_type(subtup if int() else vartup) # N: Revealed type is "builtins.tuple[ [case testTupleJoinIrregular] from typing import Tuple -tup1 = None # type: Tuple[bool, int] -tup2 = None # type: Tuple[bool] +tup1: Tuple[bool, int] +tup2: Tuple[bool] reveal_type(tup1 if int() else tup2) # N: Revealed type is "builtins.tuple[builtins.int, ...]" reveal_type(tup2 if int() else tup1) # N: Revealed type is "builtins.tuple[builtins.int, ...]" @@ -1168,9 +1186,9 @@ class NTup2(NamedTuple): class SubTuple(Tuple[bool, int, int]): ... -tup1 = None # type: NTup1 -tup2 = None # type: NTup2 -subtup = None # type: SubTuple +tup1: NTup1 +tup2: NTup2 +subtup: SubTuple reveal_type(tup1 if int() else tup2) # N: Revealed type is "builtins.tuple[builtins.bool, ...]" reveal_type(tup2 if int() else tup1) # N: Revealed type is "builtins.tuple[builtins.bool, ...]"