Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mypy/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ def add_error_info(self, info: ErrorInfo) -> None:
if ignored_codes and info.code:
# Something is ignored on the line, but not this error, so maybe the error
# code is incorrect.
msg = f'Error code "{info.code.code}" not covered by "type: ignore" comment'
msg = f'Error code "{info.code.code}" not covered by "type: ignore" comment ignoring [{", ".join(ignored_codes)}]'
if info.code in original_error_codes:
# If there seems to be a "type: ignore" with a stale error
# code, report a more specific note.
Expand Down
60 changes: 39 additions & 21 deletions test-data/unit/check-errorcodes.test
Original file line number Diff line number Diff line change
Expand Up @@ -60,26 +60,31 @@ for v in x: # type: int, int # type: ignore[syntax]
[case testErrorCodeIgnore1]
'x'.foobar # type: ignore[attr-defined]
'x'.foobar # type: ignore[xyz] # E: "str" has no attribute "foobar" [attr-defined] \
# N: Error code "attr-defined" not covered by "type: ignore" comment
# N: Error code "attr-defined" not covered by "type: ignore" comment ignoring [xyz]
'x'.foobar # type: ignore


[case testErrorCodeIgnore2]
a = 'x'.foobar # type: int # type: ignore[attr-defined]
b = 'x'.foobar # type: int # type: ignore[xyz] # E: "str" has no attribute "foobar" [attr-defined] \
# N: Error code "attr-defined" not covered by "type: ignore" comment
# N: Error code "attr-defined" not covered by "type: ignore" comment ignoring [xyz]
c = 'x'.foobar # type: int # type: ignore


[case testErrorCodeIgnoreMultiple1]
a = 'x'.foobar(b) # type: ignore[name-defined, attr-defined]
a = 'x'.foobar(b) # type: ignore[name-defined, xyz] # E: "str" has no attribute "foobar" [attr-defined] \
# N: Error code "attr-defined" not covered by "type: ignore" comment
# N: Error code "attr-defined" not covered by "type: ignore" comment ignoring [name-defined, xyz]
a = 'x'.foobar(b) # type: ignore[xyz, w, attr-defined] # E: Name "b" is not defined [name-defined] \
# N: Error code "name-defined" not covered by "type: ignore" comment
# N: Error code "name-defined" not covered by "type: ignore" comment ignoring [xyz, w, attr-defined]



[case testErrorCodeIgnoreMultiple2]
a = 'x'.foobar(c) # type: int # type: ignore[name-defined, attr-defined]
b = 'x'.foobar(c) # type: int # type: ignore[name-defined, xyz] # E: "str" has no attribute "foobar" [attr-defined] \
# N: Error code "attr-defined" not covered by "type: ignore" comment
# N: Error code "attr-defined" not covered by "type: ignore" comment ignoring [name-defined, xyz]


[case testErrorCodeWarnUnusedIgnores1]
# flags: --warn-unused-ignores
Expand Down Expand Up @@ -131,7 +136,9 @@ z # type: ignore[name-defined]
"y" # type: ignore[ignore-without-code] # E: Unused "type: ignore" comment [unused-ignore]
z # type: ignore[ignore-without-code] # E: Unused "type: ignore" comment [unused-ignore] \
# E: Name "z" is not defined [name-defined] \
# N: Error code "name-defined" not covered by "type: ignore" comment
# N: Error code "name-defined" not covered by "type: ignore" comment ignoring [ignore-without-code]



[case testErrorCodeMissingWholeFileIgnores]
# flags: --enable-error-code ignore-without-code
Expand All @@ -157,21 +164,27 @@ x2 # type: ignore [ name-defined ]
x3 # type: ignore [ xyz , name-defined ]
x4 # type: ignore[xyz,name-defined]
y # type: ignore [xyz] # E: Name "y" is not defined [name-defined] \
# N: Error code "name-defined" not covered by "type: ignore" comment
# N: Error code "name-defined" not covered by "type: ignore" comment ignoring [xyz]
y # type: ignore[ xyz ] # E: Name "y" is not defined [name-defined] \
# N: Error code "name-defined" not covered by "type: ignore" comment
# N: Error code "name-defined" not covered by "type: ignore" comment ignoring [xyz]
y # type: ignore[ xyz , foo ] # E: Name "y" is not defined [name-defined] \
# N: Error code "name-defined" not covered by "type: ignore" comment
# N: Error code "name-defined" not covered by "type: ignore" comment ignoring [xyz, foo]

a = z # type: int # type: ignore [name-defined]
b = z2 # type: int # type: ignore [ name-defined ]
c = z2 # type: int # type: ignore [ name-defined , xyz ]
d = zz # type: int # type: ignore [xyz] # E: Name "zz" is not defined [name-defined] \
# N: Error code "name-defined" not covered by "type: ignore" comment
# N: Error code "name-defined" not covered by "type: ignore" comment ignoring [xyz]
e = zz # type: int # type: ignore [ xyz ] # E: Name "zz" is not defined [name-defined] \
# N: Error code "name-defined" not covered by "type: ignore" comment
# N: Error code "name-defined" not covered by "type: ignore" comment ignoring [xyz]
f = zz # type: int # type: ignore [ xyz,foo ] # E: Name "zz" is not defined [name-defined] \
# N: Error code "name-defined" not covered by "type: ignore" comment
# N: Error code "name-defined" not covered by "type: ignore" comment ignoring [xyz, foo]







[case testErrorCodeIgnoreAfterArgComment]
def f(x # type: xyz # type: ignore[name-defined] # Comment
Expand All @@ -185,11 +198,12 @@ def g(x # type: xyz # type: ignore # Comment
pass

def h(x # type: xyz # type: ignore[foo] # E: Name "xyz" is not defined [name-defined] \
# N: Error code "name-defined" not covered by "type: ignore" comment
# N: Error code "name-defined" not covered by "type: ignore" comment ignoring [foo]
):
# type () -> None
pass


[case testErrorCodeIgnoreWithNote]
import nostub # type: ignore[import]
from defusedxml import xyz # type: ignore[import]
Expand Down Expand Up @@ -1011,27 +1025,31 @@ class D(TypedDict):
x: int

def f(d: D, s: str) -> None:
d[s] # type: ignore[xyz] \
# E: TypedDict key must be a string literal; expected one of ("x") [literal-required] \
# N: Error code "literal-required" not covered by "type: ignore" comment
d[s] # type: ignore[xyz] # E: TypedDict key must be a string literal; expected one of ("x") [literal-required] \
# N: Error code "literal-required" not covered by "type: ignore" comment ignoring [xyz]
d[s] # E: TypedDict key must be a string literal; expected one of ("x") [literal-required]
d[s] # type: ignore[misc] \
# E: TypedDict key must be a string literal; expected one of ("x") [literal-required] \
# N: Error code changed to literal-required; "type: ignore" comment may be out of date
d[s] # type: ignore[misc] # E: TypedDict key must be a string literal; expected one of ("x") [literal-required] \
# N: Error code changed to literal-required; "type: ignore" comment may be out of date
d[s] # type: ignore[literal-required]




[builtins fixtures/dict.pyi]
[typing fixtures/typing-typeddict.pyi]

[case testRecommendErrorCode]
# type: ignore[whatever] # E: type ignore with error code is not supported for modules; use `# mypy: disable-error-code="whatever"` [syntax] \
# N: Error code "syntax" not covered by "type: ignore" comment
# N: Error code "syntax" not covered by "type: ignore" comment ignoring [whatever]
1 + "asdf"


[case testRecommendErrorCode2]
# type: ignore[whatever, other] # E: type ignore with error code is not supported for modules; use `# mypy: disable-error-code="whatever, other"` [syntax] \
# N: Error code "syntax" not covered by "type: ignore" comment
# N: Error code "syntax" not covered by "type: ignore" comment ignoring [whatever, other]
1 + "asdf"


[case testShowErrorCodesInConfig]
# flags: --config-file tmp/mypy.ini
# Test 'show_error_codes = True' in config doesn't raise an exception
Expand Down
Loading