[3.13] gh-133033: Add docs for TypeIgnore (GH-133034) (#133078)

gh-133033: Add docs for `TypeIgnore` (GH-133034)
(cherry picked from commit 4e04511cb9)

Co-authored-by: Yuki Kobayashi <drsuaimqjgar@gmail.com>
Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>
This commit is contained in:
Miss Islington (bot) 2025-04-28 12:55:19 +02:00 committed by GitHub
parent 0d53872443
commit 6ade0c3b2b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1756,6 +1756,43 @@ Pattern matching
.. versionadded:: 3.10
Type annotations
^^^^^^^^^^^^^^^^
.. class:: TypeIgnore(lineno, tag)
A ``# type: ignore`` comment located at *lineno*.
*tag* is the optional tag specified by the form ``# type: ignore <tag>``.
.. doctest::
>>> print(ast.dump(ast.parse('x = 1 # type: ignore', type_comments=True), indent=4))
Module(
body=[
Assign(
targets=[
Name(id='x', ctx=Store())],
value=Constant(value=1))],
type_ignores=[
TypeIgnore(lineno=1, tag='')])
>>> print(ast.dump(ast.parse('x: bool = 1 # type: ignore[assignment]', type_comments=True), indent=4))
Module(
body=[
AnnAssign(
target=Name(id='x', ctx=Store()),
annotation=Name(id='bool', ctx=Load()),
value=Constant(value=1),
simple=1)],
type_ignores=[
TypeIgnore(lineno=1, tag='[assignment]')])
.. note::
:class:`!TypeIgnore` nodes are not generated when the *type_comments* parameter
is set to ``False`` (default). See :func:`ast.parse` for more details.
.. versionadded:: 3.8
.. _ast-type-params:
Type parameters