blob: 9a30490e883347f74dbfd89884a04b590090ef57 [file]
[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]
import m # type: ignore
from m import a # type: ignore
[file m.py]
+
[out]
tmp/m.py:1: error: Expected an expression
[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]
from m import * # type: ignore
[file m.py]
+
[out]
tmp/m.py:1: error: Expected an expression
[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 testIgnoreWholeModule5]
# mypy: ignore
import MISSING
[case testIgnoreWholeModule6]
# flags: --warn-unused-ignores
# type: ignore
x = 1 # mypy: ignore
[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 testDontIgnoreWholeModule4]
@d # mypy: 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, valid-type] # E: Unused "type: ignore[import, valid-type]" comment
-- User ordering of codes is preserved
5 # type: ignore[valid-type, import] # E: Unused "type: ignore[valid-type, import]" comment
-- Spacing is not preserved
5 # type: ignore[ valid-type, import ] # E: Unused "type: ignore[valid-type, import]" comment
-- Make sure it works as intended in more complex situations
1 + "ok" + "ok".foo # type: ignore[ operator,valid-type,attr-defined, import] # E: Unused "type: ignore[valid-type, 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]
[case testNamespaceErrorCodes]
x: str = 42 # type: ignore[mypy:assignment, other:foobar]
y: str = 42 # type: ignore[mypy:arg-type, other:foobar]
x = 1 # type: ignore[mypy:foobar]
[out]
main:2: error: Incompatible types in assignment (expression has type "int", variable has type "str")
main:2: note: Error code "assignment" not covered by "type: ignore[arg-type, other:foobar]" comment
main:3: error: Unrecognized error code "foobar"
main:3: note: Error code "misc" not covered by "type: ignore[foobar]" comment
main:3: error: Incompatible types in assignment (expression has type "int", variable has type "str")
main:3: note: Error code "assignment" not covered by "type: ignore[foobar]" comment
[case testIgnoreNonBlockingParseError]
# flags: --python-version=3.10
m[*(), :] # type: ignore[syntax]
[builtins fixtures/tuple.pyi]
[out]
main:2: error: Name "m" is not defined
main:2: note: Error code "name-defined" not covered by "type: ignore[syntax]" comment
[case testIgnoreNonBlockingParseErrorSilent]
# cmd: mypy -m a
# flags: --python-version=3.10 --follow-import=silent
[file a.py]
import m
reveal_type(m.x) # N: Revealed type is "builtins.int"
[file m.py]
x: int
x = m[*(), :]
[builtins fixtures/tuple.pyi]
[out]
[case testIgnoresWorkAfterOtherComments]
x: str = 1 # noqa # pragma: no cover # type: ignore
y: str = 1 # noqa # pragma: no cover # mypy: ignore
z: str = 1 # noqa # pragma: no cover # type: ignore[assignment]
t: str = 1 # noqa # pragma: no cover # mypy: ignore[assignment]
x1: str = 1 ## type: ignore
y1: str = 1 ## mypy: ignore
z1: str = 1 ## type: ignore[assignment]
t1: str = 1 ## mypy: ignore[assignment]
x2: str = 1 # noqa ## type: ignore
y2: str = 1 # noqa ## mypy: ignore
z2: str = 1 # noqa ## type: ignore[assignment]
t2: str = 1 # noqa ## mypy: ignore[assignment]
[out]
main:6: error: Incompatible types in assignment (expression has type "int", variable has type "str")
main:7: error: Incompatible types in assignment (expression has type "int", variable has type "str")
main:8: error: Incompatible types in assignment (expression has type "int", variable has type "str")
main:9: error: Incompatible types in assignment (expression has type "int", variable has type "str")
main:11: error: Incompatible types in assignment (expression has type "int", variable has type "str")
main:12: error: Incompatible types in assignment (expression has type "int", variable has type "str")
main:13: error: Incompatible types in assignment (expression has type "int", variable has type "str")
main:14: error: Incompatible types in assignment (expression has type "int", variable has type "str")
[case testPrefixInMypyCommentNotAllowed]
x: str = 1 # mypy: ignore[mypy:assignment]
[out]
main:1: error: Error codes in "mypy: ignore" should not be prefixed with "mypy:"
main:1: note: Error code "misc" not covered by "type: ignore[assignment]" comment
[case testDuplicateErrorCodeNotAllowed]
x: str = 1 # mypy: ignore[assignment] # type: ignore[assignment]
[out]
main:1: error: Duplicate error code "assignment"
main:1: note: Error code "misc" not covered by "type: ignore[assignment]" comment
[case testUnusedMypyIgnoreReported]
# flags: --warn-unused-ignores
x = 1 # mypy: ignore
y = 1 # mypy: ignore[assignment]
z: str = 1 # mypy: ignore[assignment, arg-type]
[out]
main:2: error: Unused "type: ignore" comment
main:3: error: Unused "type: ignore" comment
main:4: error: Unused "type: ignore[arg-type]" comment
[case testNoUnusedIgnoreInCommentedBlock]
# flags: --warn-unused-ignores
# x: str = 1 # mypy: ignore
# y: str = 1 # type: ignore
# z: str = 1 # mypy: ignore[assignment]
# t: str = 1 # type: ignore[assignment]
[case testIgnoreErrorCodesMerged]
def f() -> None: pass
f(x) # mypy: ignore[name-defined] # type: ignore[call-arg]
f(x) # type: ignore[name-defined] # mypy: ignore[call-arg]
[case testBlanketIgnoreWinsWhenMixed]
x # mypy: ignore # type: ignore[assignment]
y # mypy: ignore[assignment] # type: ignore
[case testUnknownErrorCodeNotAllowedInMypyIgnore]
1 # mypy: ignore[whatever]
[out]
main:1: error: Unrecognized error code "whatever"
main:1: note: Error code "misc" not covered by "type: ignore[whatever]" comment