Skip to content

Commit 069acb9

Browse files
comments and documentation
1 parent c0e0171 commit 069acb9

File tree

3 files changed

+38
-17
lines changed

3 files changed

+38
-17
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ may necessitate changing the location of a `# type: ignore` comment.
5656
Contributed by Shantanu Jain (PR [18392](https://github.com/python/mypy/pull/18392),
5757
PR [18397](https://github.com/python/mypy/pull/18397)).
5858

59+
### `mypy: ignore`
60+
61+
`mypy: ignore` comments are now honored by mypy exactly as though they said `type: ignore`.
62+
This allows one to suppress type errors from mypy in particular, without suppressing other type checkers,
63+
which can be desirable if mypy has a bug and other type checkers one runs on the same code do not.
64+
65+
Contributed by Wyatt S Carpenter (PR [17875](https://github.com/python/mypy/pull/17875)).
66+
5967
## Mypy 1.14
6068

6169
We’ve just uploaded mypy 1.14 to the Python Package Index ([PyPI](https://pypi.org/project/mypy/)).

docs/source/common_issues.rst

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -148,21 +148,31 @@ error:
148148
The second line is now fine, since the ignore comment causes the name
149149
``frobnicate`` to get an implicit ``Any`` type.
150150

151-
.. note::
152-
153-
You can use the form ``# type: ignore[<code>]`` to only ignore
154-
specific errors on the line. This way you are less likely to
155-
silence unexpected errors that are not safe to ignore, and this
156-
will also document what the purpose of the comment is. See
157-
:ref:`error-codes` for more information.
158-
159151
.. note::
160152

161153
The ``# type: ignore`` comment will only assign the implicit ``Any``
162154
type if mypy cannot find information about that particular module. So,
163155
if we did have a stub available for ``frobnicate`` then mypy would
164156
ignore the ``# type: ignore`` comment and typecheck the stub as usual.
165157

158+
You can use the form ``# type: ignore[<code>]`` to only ignore
159+
specific errors on the line. This way you are less likely to
160+
silence unexpected errors that are not safe to ignore, and this
161+
will also document what the purpose of the comment is. See
162+
:ref:`error-codes` for more information.
163+
164+
Instead of ``# type: ignore``, you may also use the mypy-specific
165+
``# mypy: ignore``, to tell only mypy — and not other type checkers — to
166+
ignore that line. This is mostly useful if mypy has a bug that produces
167+
known-spurious error messages. You may also use the ``# mypy: ignore[<code>]``
168+
form to only ignore specific errors. Indeed, it is often wisest to use
169+
``# mypy: ignore[<code>]``, as this minimizes your chances of accidentally
170+
suppressing other errors that you don't really want to ignore!
171+
172+
(``# mypy: ignore`` comments are per-line and should not be confused with
173+
:ref:`inline-configuration` comments, which are per-file configuration directives
174+
that just happen to be formatted a similar way.)
175+
166176
Another option is to explicitly annotate values with type ``Any`` --
167177
mypy will let you perform arbitrary operations on ``Any``
168178
values. Sometimes there is no more precise type you can use for a

mypy/fastparse.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -140,16 +140,18 @@ def ast3_parse(
140140
source: str | bytes, filename: str, mode: str, feature_version: int = PY_MINOR_VERSION
141141
) -> AST:
142142
"""This function is just a convenience wrapper around ast.parse, with default flags useful to Mypy.
143-
It also incorporates a hack to accomodate `# mypy: ignore` comments, which are treated by mypy as `# type: ignore` comments.
143+
It also handles `# mypy: ignore` comments, by turning them into `# type: ignore` comments.
144144
"""
145145
# Hack to support "mypy: ignore" comments until the builtin compile function changes to allow us to detect it otherwise:
146-
# (Note: completely distinct from https://mypy.readthedocs.io/en/stable/inline_config.html ; see also, util.get_mypy_comments in this codebase)
146+
# (Note: completely distinct from https://mypy.readthedocs.io/en/stable/inline_config.html ; see also, util.get_mypy_comments in this codebase.)
147147

148148
# We make the substitution in comments, and to find those comments we use Python's `tokenize`.
149149
# https://docs.python.org/3/library/tokenize.html has a big red **Warning:**
150-
# Note that the functions in this module are only designed to parse syntactically valid Python code (code that does not raise when parsed using ast.parse()). The behavior of the functions in this module is **undefined** when providing invalid Python code and it can change at any point.
150+
# Note that the functions in this module are only designed to parse syntactically valid Python
151+
# code (code that does not raise when parsed using ast.parse()). The behavior of the functions
152+
# in this module is **undefined** when providing invalid Python code and it can change at any point.
151153
# So, we cannot rely on roundtrip behavior in tokenize iff ast.parse would throw when given `source`.
152-
# The simplest way to deal with that is just to call ast.parse twice, once before and once after. So, we do that.
154+
# The simplest way to deal with that is just to call ast.parse twice, once before and once after!
153155
def p() -> AST:
154156
return ast3.parse(
155157
source, filename, mode, type_comments=True, feature_version=feature_version
@@ -160,11 +162,12 @@ def p() -> AST:
160162
tokens = tokenize.generate_tokens(io.StringIO(source).readline)
161163
else:
162164
tokens = tokenize.tokenize(io.BytesIO(source).readline)
163-
# We do a workaround for a roundtripping error https://github.com/python/cpython/issues/125008
164-
# An error that was introduced in 3.12.3 (https://github.com/python/cpython/blob/v3.12.3/Lib/tokenize.py#L205) and fixed in 3.12.8 (not out yet at time of writing but it's probably https://github.com/python/cpython/blob/v3.12.8/Lib/tokenize.py#L203 or thereabouts).
165+
# We do a workaround for a roundtripping error (https://github.com/python/cpython/issues/125008)
166+
# that was introduced in 3.12.3 (https://github.com/python/cpython/blob/v3.12.3/Lib/tokenize.py#L205)
167+
# and fixed in 3.12.8 (https://github.com/python/cpython/blob/v3.12.8/Lib/tokenize.py#L205).
165168
# Luckily, it was caught before the first official (non-rc) release of python 3.13.
166169
is_defective_version = (3, 12, 3) <= sys.version_info[:3] <= (3, 12, 7)
167-
# Could the workaround ever come back to bite us? I confess I don't know enough about FSTRING_MIDDLE to say no for certain, even though I have tried to examine all relevant cases.
170+
# This is a gnarly list comprehension, but that's basically just how untokenize is supposed to work.
168171
source = tokenize.untokenize(
169172
(
170173
t,
@@ -182,11 +185,11 @@ def p() -> AST:
182185
else s
183186
)
184187
),
185-
*_, # this improves whitespace roundtripping (possibly always making it perfect, for this usecase?)
188+
*_, # Including this bit improves whitespace roundtripping (possibly always making it perfect).
186189
)
187190
for t, s, *_ in tokens
188191
)
189-
return p()
192+
return p() # `source` has changed, so this result is different than the first call.
190193

191194

192195
NamedExpr = ast3.NamedExpr

0 commit comments

Comments
 (0)