[case testIgnoreTypeError] x = 1 x() # type: ignore x() # E: "int" not callable [case testIgnoreUndefinedName] x = 1 y # type: ignore z # E: Name "z" is not defined [case testIgnoreImportError] import xyz_m # type: ignore xyz_m.foo 1() # E: "int" not callable [case testIgnoreImportFromError] from xyz_m import a, b # type: ignore a.foo b() 1() # E: "int" not callable [case testIgnoreImportFromErrorMultiline] from xyz_m import ( # type: ignore a, b ) a.foo b() 1() # E: "int" not callable [case testIgnoreImportAllError] from xyz_m import * # type: ignore x # E: Name "x" is not defined 1() # E: "int" not callable [case testIgnoreImportBadModule_no_native_parse] import m # type: ignore from m import a # type: ignore [file m.py] + [out] tmp/m.py:1: error: Invalid syntax [case testIgnoreAppliesOnlyToMissing] import a # type: ignore import b # type: ignore reveal_type(a.foo) # N: Revealed type is "Any" reveal_type(b.foo) # N: Revealed type is "builtins.int" a.bar() b.bar() # E: Module has no attribute "bar" [file b.py] foo = 3 [builtins fixtures/module_all.pyi] [out] [case testIgnoreImportStarFromBadModule_no_native_parse] from m import * # type: ignore [file m.py] + [out] tmp/m.py:1: error: Invalid syntax [case testIgnoreAssignmentTypeError] x = 1 if int(): x = '' # type: ignore if int(): x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int") [case testIgnoreInvalidOverride] class A: def f(self) -> int: pass class B(A): def f(self) -> str: pass # type: ignore [case testIgnoreMissingModuleAttribute] import m m.x = object # type: ignore m.f() # type: ignore m.y # E: Module has no attribute "y" [file m.py] [builtins fixtures/module.pyi] [case testIgnoreTypeInferenceError] x = [] # type: ignore y = x x.append(1) [builtins fixtures/list.pyi] [case testIgnoreTypeInferenceError2] def f() -> None: pass x = f() # type: ignore y = x x = 1 [builtins fixtures/list.pyi] [case testIgnoreTypeInferenceErrorAndMultipleAssignment] x, y = [], [] # type: ignore z = x z = y [builtins fixtures/list.pyi] [case testIgnoreSomeStarImportErrors] from m1 import * from m2 import * # type: ignore # We should still import things that don't conflict. y() # E: "str" not callable z() # E: "int" not callable x() # E: "int" not callable [file m1.py] x = 1 y = '' [file m2.py] x = '' z = 1 [case testIgnoredModuleDefinesBaseClass1] from m import B # type: ignore class C(B): def f(self) -> None: self.f(1) # E: Too many arguments for "f" of "C" self.g(1) [out] [case testIgnoredModuleDefinesBaseClass2] import m # type: ignore class C(m.B): def f(self) -> None: ... c = C() c.f(1) # E: Too many arguments for "f" of "C" c.g(1) c.x = 1 [out] [case testIgnoredModuleDefinesBaseClassAndClassAttribute] import m # type: ignore class C(m.B): @staticmethod def f() -> None: pass C.f(1) # E: Too many arguments for "f" of "C" C.g(1) C.x = 1 [builtins fixtures/staticmethod.pyi] [out] [case testIgnoredModuleDefinesBaseClassWithInheritance1] from m import B # type: ignore class C: pass class D(C, B): def f(self) -> None: self.f(1) # E: Too many arguments for "f" of "D" self.g(1) [out] [case testIgnoredModuleDefinesBaseClassWithInheritance2] from m import B # type: ignore class C(B): pass class D(C): def f(self) -> None: self.f(1) # E: Too many arguments for "f" of "D" self.g(1) [out] [case testIgnoreWithFollowingIndentedComment] if 1: # type: ignore # blah pass [out] [case testIgnoreTooManyTypeArguments] from typing import TypeVar, Generic T = TypeVar('T') U = TypeVar('U') class Base(Generic[T, U]): pass class PartialBase(Base[T, int], Generic[T]): pass class Child(PartialBase[str, int]): # type: ignore pass def foo(x: Base[str, int]) -> None: pass foo(Child()) def bar(x: Base[str, str]) -> None: pass bar(Child()) [out] main:19: error: Argument 1 to "bar" has incompatible type "Child"; expected "Base[str, str]" [case testTypeIgnoreLineNumberWithinFile] import m pass # type: ignore m.f(kw=1) [file m.py] pass def f() -> None: pass [out] main:3: error: Unexpected keyword argument "kw" for "f" main:3: note: "f" defined in "m" [case testIgnoreUnexpectedKeywordArgument] import m m.f(kw=1) # type: ignore [file m.py] def f() -> None: pass [out] [case testCannotIgnoreBlockingError] yield # type: ignore # E: "yield" outside function [case testIgnoreWholeModule1] # type: ignore if True: IGNORE [case testIgnoreWholeModule2] # type: ignore @d class C: ... IGNORE [case testIgnoreWholeModule3] # type: ignore @d def f(): ... IGNORE [case testIgnoreWholeModule4] # type: ignore import MISSING [case testDontIgnoreWholeModule1] if True: # type: ignore ERROR # E: Name "ERROR" is not defined ERROR # E: Name "ERROR" is not defined [case testDontIgnoreWholeModule2] @d # type: ignore class C: ... ERROR # E: Name "ERROR" is not defined [case testDontIgnoreWholeModule3] @d # type: ignore def f(): ... ERROR # E: Name "ERROR" is not defined [case testIgnoreInsideFunctionDoesntAffectWhole] # flags: --disallow-untyped-defs def f(): # E: Function is missing a return type annotation 42 + 'no way' # type: ignore return 0 [case testIgnoreInsideClassDoesntAffectWhole] import six class M(type): pass @six.add_metaclass(M) class CD(six.with_metaclass(M)): # E: Multiple metaclass definitions 42 + 'no way' # type: ignore [builtins fixtures/tuple.pyi] [case testUnusedIgnoreCodeOrder] # flags: --warn-unused-ignores 5 # type: ignore[import, steven] # E: Unused "type: ignore[import, steven]" comment -- User ordering of codes is preserved 5 # type: ignore[steven, import] # E: Unused "type: ignore[steven, import]" comment -- Spacing is not preserved 5 # type: ignore[ steven, import ] # E: Unused "type: ignore[steven, import]" comment -- Make sure it works as intended in more complex situations 1 + "ok" + "ok".foo # type: ignore[ operator,steven,attr-defined, import] # E: Unused "type: ignore[steven, import]" comment [case testUnusedIgnoreTryExcept] # flags: --warn-unused-ignores try: import foo # type: ignore # E: Unused "type: ignore" comment import bar # type: ignore[import] # E: Unused "type: ignore" comment import foobar # type: ignore[unused-ignore] import barfoo # type: ignore[import,unused-ignore] import missing # type: ignore[import,unused-ignore] except Exception: pass [file foo.py] [file bar.py] [file foobar.py] [file barfoo.py] [builtins fixtures/exception.pyi]