Skip to content

Commit 223da4b

Browse files
committed
Switch to ignore-without-code control via error-code enablement
In preference to adding another specific command line flag.
1 parent 19e5ccf commit 223da4b

File tree

5 files changed

+32
-15
lines changed

5 files changed

+32
-15
lines changed

docs/source/config_file.rst

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -507,13 +507,6 @@ section of the command line docs.
507507

508508
Warns about unneeded ``# type: ignore`` comments.
509509

510-
.. confval:: warn_ignores_without_codes
511-
512-
:type: boolean
513-
:default: False
514-
515-
Warn about '# type: ignore' comments which do not have error codes.
516-
517510
.. confval:: warn_no_return
518511

519512
:type: boolean

docs/source/error_code_list2.rst

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,3 +255,31 @@ no error in practice. In such case, it might be prudent to annotate ``items: Seq
255255
This is similar in concept to ensuring that an expression's type implements an expected interface (e.g. ``Sized``),
256256
except that attempting to invoke an undefined method (e.g. ``__len__``) results in an error,
257257
while attempting to evaluate an object in boolean context without a concrete implementation results in a truthy value.
258+
259+
260+
Check that ``# type: ignore`` include an error code
261+
---------------------------------------------------
262+
263+
Warn when an ``# type: ignore`` comment does not specify any error codes.
264+
This clarifies the intent of the ignore as well as ensuring that only the
265+
expected errors are silenced.
266+
267+
Example:
268+
269+
.. code-block:: python
270+
271+
# mypy: enable-error-code ignore-without-code
272+
273+
class Foo:
274+
def __init__(self, name: str) -> None:
275+
self.name = name
276+
277+
f = Foo('foo')
278+
279+
# This line has a typo that mypy can't help with as both the expected 'assignment' and 'attr-defined' are silenced
280+
# Error: "type: ignore" comment without error code (currently ignored: [attr-defined])
281+
f.nme = 42 # type: ignore
282+
283+
# This line warns correctly about the typo in the attribute name
284+
# Error: "Foo" has no attribute "nme"; maybe "name"?
285+
f.nme = 42 # type: ignore[assignment]

mypy/build.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2370,7 +2370,7 @@ def generate_unused_ignore_notes(self) -> None:
23702370
self.manager.errors.generate_unused_ignore_errors(self.xpath)
23712371

23722372
def generate_ignore_without_code_notes(self) -> None:
2373-
if self.options.warn_ignores_without_codes:
2373+
if self.manager.errors.is_error_code_enabled(codes.IGNORE_WITHOUT_CODE):
23742374
self.manager.errors.generate_ignore_without_code_errors(
23752375
self.xpath,
23762376
self.options.warn_unused_ignores,

mypy/options.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ class BuildType:
4949
"strict_equality",
5050
"strict_optional",
5151
"strict_optional_whitelist",
52-
"warn_ignores_without_codes",
5352
"warn_no_return",
5453
"warn_return_any",
5554
"warn_unreachable",
@@ -144,9 +143,6 @@ def __init__(self) -> None:
144143
# Warn about unused '# type: ignore' comments
145144
self.warn_unused_ignores = False
146145

147-
# Warn about '# type: ignore' comments which do not have error codes
148-
self.warn_ignores_without_codes = False
149-
150146
# Warn about unused '[mypy-<pattern>]' or '[[tool.mypy.overrides]]' config sections
151147
self.warn_unused_configs = False
152148

test-data/unit/check-errorcodes.test

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,20 +136,20 @@ x # type: ignore[name-defined, attr-defined] # E: Unused "type: ignore[attr-defi
136136
"x" # type: ignore[name-defined] # E: Unused "type: ignore" comment
137137

138138
[case testErrorCodeMissingWhenRequired]
139-
# flags: --warn-ignores-without-codes
139+
# flags: --enable-error-code ignore-without-code
140140
"x" # type: ignore # E: "type: ignore" comment without error code [ignore-without-code]
141141
y # type: ignore # E: "type: ignore" comment without error code (hint: add [name-defined]) [ignore-without-code]
142142
z # type: ignore[name-defined]
143143
"a" # type: ignore[ignore-without-code]
144144

145145
[case testErrorCodeMissingDoesntTrampleUnusedIgnoresWarning]
146-
# flags: --warn-ignores-without-codes --warn-unused-ignores
146+
# flags: --enable-error-code ignore-without-code --warn-unused-ignores
147147
"x" # type: ignore # E: Unused "type: ignore" comment
148148
"y" # type: ignore[ignore-without-code] # E: Unused "type: ignore" comment
149149
z # type: ignore[ignore-without-code] # E: Unused "type: ignore" comment # E: Name "z" is not defined [name-defined]
150150

151151
[case testErrorCodeMissingWholeFileIgnores]
152-
# flags: --warn-ignores-without-codes
152+
# flags: --enable-error-code ignore-without-code
153153
# type: ignore # whole file ignore
154154
x
155155
y # type: ignore # ignore the lack of error code since we're ignore the whole file

0 commit comments

Comments
 (0)