Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 28, 2025
Merged
Changes from all 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
37 changes: 37 additions & 0 deletions Doc/library/ast.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading