]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-133033: Add docs for `TypeIgnore` (#133034)
authorYuki Kobayashi <drsuaimqjgar@gmail.com>
Mon, 28 Apr 2025 10:49:07 +0000 (19:49 +0900)
committerGitHub <noreply@github.com>
Mon, 28 Apr 2025 10:49:07 +0000 (13:49 +0300)
Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>
Doc/library/ast.rst

index 1a04d42e5f5d7e8478e9d9b46144d9865a75424f..776c63d1f0fda0200fbb25120ceac1ab217ab7ce 100644 (file)
@@ -1761,6 +1761,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