Skip to content

Commit 4e04511

Browse files
gh-133033: Add docs for TypeIgnore (#133034)
Co-authored-by: Stan Ulbrych <[email protected]>
1 parent af3f6fc commit 4e04511

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

Doc/library/ast.rst

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1761,6 +1761,43 @@ Pattern matching
17611761

17621762
.. versionadded:: 3.10
17631763

1764+
1765+
Type annotations
1766+
^^^^^^^^^^^^^^^^
1767+
1768+
.. class:: TypeIgnore(lineno, tag)
1769+
1770+
A ``# type: ignore`` comment located at *lineno*.
1771+
*tag* is the optional tag specified by the form ``# type: ignore <tag>``.
1772+
1773+
.. doctest::
1774+
1775+
>>> print(ast.dump(ast.parse('x = 1 # type: ignore', type_comments=True), indent=4))
1776+
Module(
1777+
body=[
1778+
Assign(
1779+
targets=[
1780+
Name(id='x', ctx=Store())],
1781+
value=Constant(value=1))],
1782+
type_ignores=[
1783+
TypeIgnore(lineno=1, tag='')])
1784+
>>> print(ast.dump(ast.parse('x: bool = 1 # type: ignore[assignment]', type_comments=True), indent=4))
1785+
Module(
1786+
body=[
1787+
AnnAssign(
1788+
target=Name(id='x', ctx=Store()),
1789+
annotation=Name(id='bool', ctx=Load()),
1790+
value=Constant(value=1),
1791+
simple=1)],
1792+
type_ignores=[
1793+
TypeIgnore(lineno=1, tag='[assignment]')])
1794+
1795+
.. note::
1796+
:class:`!TypeIgnore` nodes are not generated when the *type_comments* parameter
1797+
is set to ``False`` (default). See :func:`ast.parse` for more details.
1798+
1799+
.. versionadded:: 3.8
1800+
17641801
.. _ast-type-params:
17651802

17661803
Type parameters

0 commit comments

Comments
 (0)