-- Test cases for the redefinition of variable with a different type. -- Redefine local variable -- ----------------------- [case testRedefineLocalWithDifferentType] # flags: --allow-redefinition-old def f() -> None: x = 0 reveal_type(x) # N: Revealed type is "builtins.int" x = '' reveal_type(x) # N: Revealed type is "builtins.str" [case testCannotConditionallyRedefineLocalWithDifferentType] # flags: --allow-redefinition-old def f() -> None: y = 0 reveal_type(y) # N: Revealed type is "builtins.int" if int(): y = '' \ # E: Incompatible types in assignment (expression has type "str", variable has type "int") reveal_type(y) # N: Revealed type is "builtins.int" reveal_type(y) # N: Revealed type is "builtins.int" [case testRedefineFunctionArg] # flags: --allow-redefinition-old def f(x: int) -> None: g(x) x = '' reveal_type(x) # N: Revealed type is "builtins.str" def g(x: int) -> None: if int(): x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int") reveal_type(x) # N: Revealed type is "builtins.int" [case testRedefineAnnotationOnly] # flags: --allow-redefinition-old def f() -> None: x: int x = '' \ # E: Incompatible types in assignment (expression has type "str", variable has type "int") reveal_type(x) # N: Revealed type is "builtins.int" def g() -> None: x: int x = 1 reveal_type(x) # N: Revealed type is "builtins.int" x = '' reveal_type(x) # N: Revealed type is "builtins.str" [case testRedefineLocalUsingOldValue] # flags: --allow-redefinition-old from typing import TypeVar, Union T = TypeVar('T') def f(x: int) -> None: x = g(x) reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" y = 1 y = g(y) reveal_type(y) # N: Revealed type is "builtins.int | builtins.str" def g(x: T) -> Union[T, str]: pass [case testRedefineLocalForLoopIndexVariable] # flags: --allow-redefinition-old from typing import Iterable def f(a: Iterable[int], b: Iterable[str]) -> None: for x in a: x = '' \ # E: Incompatible types in assignment (expression has type "str", variable has type "int") reveal_type(x) # N: Revealed type is "builtins.int" for x in b: x = 1 \ # E: Incompatible types in assignment (expression has type "int", variable has type "str") reveal_type(x) # N: Revealed type is "builtins.str" def g(a: Iterable[int]) -> None: for x in a: pass x = '' def h(a: Iterable[int]) -> None: x = '' reveal_type(x) # N: Revealed type is "builtins.str" for x in a: pass [case testCannotRedefineLocalWithinTry] # flags: --allow-redefinition-old def g(): pass def f() -> None: try: x = 0 x g() # Might raise an exception x = '' \ # E: Incompatible types in assignment (expression has type "str", variable has type "int") except: pass reveal_type(x) # N: Revealed type is "builtins.int" y = 0 y y = '' [case testRedefineLocalWithinTryClauses] # flags: --allow-redefinition-old def fn_str(_: str) -> int: ... def fn_int(_: int) -> None: ... def in_block() -> None: try: a = "" a = fn_str(a) # E: Incompatible types in assignment (expression has type "int", variable has type "str") fn_int(a) # E: Argument 1 to "fn_int" has incompatible type "str"; expected "int" except: b = "" b = fn_str(b) fn_int(b) else: c = "" c = fn_str(c) fn_int(c) finally: d = "" d = fn_str(d) fn_int(d) reveal_type(a) # N: Revealed type is "builtins.str" reveal_type(b) # N: Revealed type is "builtins.int" reveal_type(c) # N: Revealed type is "builtins.int" reveal_type(d) # N: Revealed type is "builtins.int" def across_blocks() -> None: try: a = "" except: pass else: a = fn_str(a) # E: Incompatible types in assignment (expression has type "int", variable has type "str") reveal_type(a) # N: Revealed type is "builtins.str" [case testRedefineLocalExceptVar] # flags: --allow-redefinition-old def fn_exc(_: Exception) -> str: ... def exc_name() -> None: try: pass except RuntimeError as e: e = fn_exc(e) [builtins fixtures/exception.pyi] [case testRedefineNestedInTry] # flags: --allow-redefinition-old def fn_int(_: int) -> None: ... try: try: ... finally: a = "" a = 5 # E: Incompatible types in assignment (expression has type "int", variable has type "str") fn_int(a) # E: Argument 1 to "fn_int" has incompatible type "str"; expected "int" except: pass [case testRedefineLocalWithinWith] # flags: --allow-redefinition-old def f() -> None: with g(): x = 0 x g() # Might raise an exception, but we ignore this x = '' reveal_type(x) # N: Revealed type is "builtins.str" y = 0 y y = '' def g(): pass [case testCannotRedefineAcrossNestedFunction] # flags: --allow-redefinition-old def f() -> None: x = 0 x def g() -> None: x g() x = '' \ # E: Incompatible types in assignment (expression has type "str", variable has type "int") g() y = 0 y y = '' [case testCannotRedefineAcrossNestedDecoratedFunction] # flags: --allow-redefinition-old def dec(f): return f def f() -> None: x = 0 x @dec def g() -> None: x g() x = '' \ # E: Incompatible types in assignment (expression has type "str", variable has type "int") g() y = 0 y y = '' [case testCannotRedefineAcrossNestedOverloadedFunction] # flags: --allow-redefinition-old from typing import overload def f() -> None: x = 0 x @overload def g() -> None: pass @overload def g(x: int) -> None: pass def g(x=0): pass g() x = '' \ # E: Incompatible types in assignment (expression has type "str", variable has type "int") g() y = 0 y y = '' [case testRedefineLocalInMultipleAssignment] # flags: --allow-redefinition-old def f() -> None: x, x = 1, '' reveal_type(x) # N: Revealed type is "builtins.str" x = object() reveal_type(x) # N: Revealed type is "builtins.object" def g() -> None: x = 1 if 1: x, x = '', 1 \ # E: Incompatible types in assignment (expression has type "str", variable has type "int") [case testRedefineUnderscore] # flags: --allow-redefinition-old def f() -> None: _, _ = 1, '' if 1: _, _ = '', 1 # This is unintentional but probably fine. No one is going to read _ value. reveal_type(_) # N: Revealed type is "builtins.int" [case testRedefineWithBreakAndContinue] # flags: --allow-redefinition-old def f() -> None: y = 0 y while int(): z = 0 z z = '' x = 0 if int(): break x = '' \ # E: Incompatible types in assignment (expression has type "str", variable has type "int") reveal_type(x) # N: Revealed type is "builtins.int" y = '' def g() -> None: y = 0 y for a in h(): z = 0 z z = '' x = 0 if int(): continue x = '' \ # E: Incompatible types in assignment (expression has type "str", variable has type "int") reveal_type(x) # N: Revealed type is "builtins.int" y = '' def h(): pass [case testRedefineLocalAndNestedLoops] # flags: --allow-redefinition-old def f() -> None: z = 0 z while int(): x = 0 x while int(): if 1: y = 1 y if int(): break y = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int") x = '' z = '' [case testCannotRedefineVarAsFunction] # flags: --allow-redefinition-old def f() -> None: def x(): pass x = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "Callable[[], Any]") reveal_type(x) # N: Revealed type is "def () -> Any" y = 1 def y(): pass # E: Name "y" already defined on line 6 [case testCannotRedefineVarAsClass] # flags: --allow-redefinition-old def f() -> None: class x: pass x = 1 # E: Cannot assign to a type \ # E: Incompatible types in assignment (expression has type "int", variable has type "type[x]") y = 1 class y: pass # E: Name "y" already defined on line 5 [case testRedefineVarAsTypeVar] # flags: --allow-redefinition-old from typing import TypeVar def f() -> None: x = TypeVar('x') x = 1 # E: Invalid assignment target \ # E: Incompatible types in assignment (expression has type "int", variable has type "TypeVar") reveal_type(x) # N: Revealed type is "typing.TypeVar" y = 1 y = TypeVar('y') # E: Cannot redefine "y" as a type variable \ # E: Incompatible types in assignment (expression has type "TypeVar", variable has type "int") def h(a: y) -> y: return a # E: Variable "y" is not valid as a type \ # N: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases [builtins fixtures/tuple.pyi] [typing fixtures/typing-full.pyi] [case testCannotRedefineVarAsModule] # flags: --allow-redefinition-old def f() -> None: import typing as m m = 1 # E: Incompatible types in assignment (expression has type "int", variable has type Module) n = 1 import typing as n # E: Incompatible import of "n" (imported name has type Module, local name has type "int") [builtins fixtures/module.pyi] [typing fixtures/typing-full.pyi] [case testRedefineLocalWithTypeAnnotation] # flags: --allow-redefinition-old def f() -> None: x = 1 reveal_type(x) # N: Revealed type is "builtins.int" x = '' # type: object reveal_type(x) # N: Revealed type is "builtins.object" def g() -> None: x = 1 reveal_type(x) # N: Revealed type is "builtins.int" x: object = '' reveal_type(x) # N: Revealed type is "builtins.object" def h() -> None: x: int x = 1 reveal_type(x) # N: Revealed type is "builtins.int" x: object x: object = '' # E: Name "x" already defined on line 16 def farg(x: int) -> None: x: str = '' # E: Name "x" already defined on line 18 def farg2(x: int) -> None: x: str = x # E: Incompatible types in assignment (expression has type "int", variable has type "str") [case testRedefineLocalWithTypeAnnotationSpecialCases] # flags: --allow-redefinition-old def f() -> None: x: object x = 1 if int(): x = '' reveal_type(x) # N: Revealed type is "builtins.str | builtins.int" x = '' reveal_type(x) # N: Revealed type is "builtins.str" if int(): x = 2 \ # E: Incompatible types in assignment (expression has type "int", variable has type "str") [case testCannotRedefineSelf] # flags: --allow-redefinition-old class A: x = 0 def f(self) -> None: reveal_type(self.x) # N: Revealed type is "builtins.int" self = f() self.y: str = '' reveal_type(self.y) # N: Revealed type is "builtins.str" def f() -> A: return A() -- Redefine global variable -- ------------------------ [case testRedefineGlobalWithDifferentType] # flags: --allow-redefinition-old import m reveal_type(m.x) # N: Revealed type is "builtins.str" [file m.py] x = 0 reveal_type(x) # N: Revealed type is "builtins.int" x = object() reveal_type(x) # N: Revealed type is "builtins.object" x = '' reveal_type(x) # N: Revealed type is "builtins.str" [case testRedefineGlobalForIndex] # flags: --allow-redefinition-old import m reveal_type(m.x) # N: Revealed type is "builtins.str" [file m.py] from typing import Iterable def f(): pass it1: Iterable[int] = f() it2: Iterable[str] = f() for x in it1: reveal_type(x) # N: Revealed type is "builtins.int" for x in it2: reveal_type(x) # N: Revealed type is "builtins.str" reveal_type(x) # N: Revealed type is "builtins.str" [case testRedefineGlobalBasedOnPreviousValues] # flags: --allow-redefinition-old from typing import TypeVar, Iterable T = TypeVar('T') def f(x: T) -> Iterable[T]: pass a = 0 a = f(a) reveal_type(a) # N: Revealed type is "typing.Iterable[builtins.int]" [case testRedefineGlobalWithSeparateDeclaration] # flags: --allow-redefinition-old x = '' reveal_type(x) # N: Revealed type is "builtins.str" x: int x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int") reveal_type(x) # N: Revealed type is "builtins.int" x: object x = 1 reveal_type(x) # N: Revealed type is "builtins.int" if int(): x = object() [case testRedefineGlobalUsingForLoop] # flags: --allow-redefinition-old from typing import Iterable, TypeVar, Union T = TypeVar('T') def f(x: T) -> Iterable[Union[T, str]]: pass x = 0 reveal_type(x) # N: Revealed type is "builtins.int" for x in f(x): pass reveal_type(x) # N: Revealed type is "builtins.int | builtins.str" [case testNoRedefinitionIfOnlyInitialized] # flags: --allow-redefinition-old --no-strict-optional x = None # type: int x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int") x = object() # E: Incompatible types in assignment (expression has type "object", variable has type "int") x # Reference to variable x = '' y = 0 y = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int") [case testNoRedefinitionIfNoValueAssigned] # flags: --allow-redefinition-old x: int x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int") reveal_type(x) # N: Revealed type is "builtins.int" x: object [case testNoRedefinitionIfExplicitlyDisallowed] # flags: --disallow-redefinition-old x = 0 x = 2 x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int") def f() -> None: y = 0 y = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int") class C: y = 0 y = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int") def g() -> None: # _ is a special case _ = 0 _ = '' x, _ = 0, C() [builtins fixtures/tuple.pyi] [case testRedefineAsException] # flags: --allow-redefinition-old e = 1 reveal_type(e) # N: Revealed type is "builtins.int" try: pass except Exception as e: reveal_type(e) # N: Revealed type is "builtins.Exception" e = '' reveal_type(e) # N: Revealed type is "builtins.str" [builtins fixtures/exception.pyi] [case testRedefineUsingWithStatement] # flags: --allow-redefinition-old class A: def __enter__(self) -> int: ... def __exit__(self, x, y, z) -> None: ... class B: def __enter__(self) -> str: ... def __exit__(self, x, y, z) -> None: ... with A() as x: reveal_type(x) # N: Revealed type is "builtins.int" with B() as x: x = 0 # E: Incompatible types in assignment (expression has type "int", variable has type "str") [case testRedefineModuleAsException] import typing try: pass except Exception as typing: pass [builtins fixtures/exception.pyi] [typing fixtures/typing-full.pyi] [case testRedefiningUnderscoreFunctionIsntAnError] def _(arg): pass def _(arg): pass [case testTypeErrorsInUnderscoreFunctionsReported] def _(arg: str): x = arg + 1 # E: Unsupported left operand type for + ("str") def _(arg: int) -> int: return 'a' # E: Incompatible return value type (got "str", expected "int") [case testFunctionStillTypeCheckedWhenAliasedAsUnderscoreDuringImport] from a import f as _ _(1) # E: Argument 1 to "f" has incompatible type "int"; expected "str" reveal_type(_('a')) # N: Revealed type is "builtins.str" [file a.py] def f(arg: str) -> str: return arg [case testCallToFunctionStillTypeCheckedWhenAssignedToUnderscoreVariable] from a import g _ = g _('a') # E: Argument 1 has incompatible type "str"; expected "int" reveal_type(_(1)) # N: Revealed type is "builtins.int" [file a.py] def g(arg: int) -> int: return arg [case testRedefiningUnderscoreFunctionWithDecoratorWithUnderscoreFunctionsNextToEachOther] def dec(f): return f @dec def _(arg): pass @dec def _(arg): pass [case testRedefiningUnderscoreFunctionWithDecoratorInDifferentPlaces] def dec(f): return f def dec2(f): return f @dec def _(arg): pass def f(arg): pass @dec2 def _(arg): pass [case testOverwritingImportedFunctionThatWasAliasedAsUnderscore] from a import f as _ def _(arg: str) -> str: # E: Name "_" already defined (possibly by an import) return arg [file a.py] def f(s: str) -> str: return s