From d208c58f8507c81263b4f799433ed6fe54bbef2a Mon Sep 17 00:00:00 2001 From: Yuki Kobayashi Date: Sun, 27 Apr 2025 02:03:14 +0000 Subject: [PATCH 01/12] gh-133033: Add document for `TypeIgnore` Fixes gh-133033 --- Doc/library/ast.rst | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/Doc/library/ast.rst b/Doc/library/ast.rst index 1a04d42e5f5d7e..0b5c11fd5f3554 100644 --- a/Doc/library/ast.rst +++ b/Doc/library/ast.rst @@ -1761,10 +1761,34 @@ Pattern matching .. versionadded:: 3.10 +Type annotations +^^^^^^^^^^^^^^^^ + +.. class:: TypeIgnore(lineno, tag) + + A type ignore comment. ``lineno`` is the location of the ``#type ignore`` comment. + ``tag`` is the optional tag specified by the form ``#type ignore ``. + + see :func:`ast.parse` for more details. + + .. 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='')]) + + .. versionadded:: 3.8 + .. _ast-type-params: Type parameters -^^^^^^^^^^^^^^^ +~~~~~~~~~~~~~~~ :ref:`Type parameters ` can exist on classes, functions, and type aliases. From 7a835bc10f84c8c0af69382705e5a467211dcc0f Mon Sep 17 00:00:00 2001 From: Yuki Kobayashi Date: Sun, 27 Apr 2025 02:36:46 +0000 Subject: [PATCH 02/12] fixup --- Doc/library/ast.rst | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Doc/library/ast.rst b/Doc/library/ast.rst index 0b5c11fd5f3554..e4043627686352 100644 --- a/Doc/library/ast.rst +++ b/Doc/library/ast.rst @@ -1775,13 +1775,13 @@ Type annotations >>> 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='')]) + body=[ + Assign( + targets=[ + Name(id='x', ctx=Store())], + value=Constant(value=1))], + type_ignores=[ + TypeIgnore(lineno=1, tag='')]) .. versionadded:: 3.8 From fc06a53b79383f3e9dceed85b729f44435f8e073 Mon Sep 17 00:00:00 2001 From: Yuki Kobayashi Date: Mon, 28 Apr 2025 09:28:41 +0900 Subject: [PATCH 03/12] Update Doc/library/ast.rst Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com> --- Doc/library/ast.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/ast.rst b/Doc/library/ast.rst index e4043627686352..969f60b8bf53dc 100644 --- a/Doc/library/ast.rst +++ b/Doc/library/ast.rst @@ -1766,7 +1766,7 @@ Type annotations .. class:: TypeIgnore(lineno, tag) - A type ignore comment. ``lineno`` is the location of the ``#type ignore`` comment. + A type ignore comment. *lineno* is the location of the ``#type ignore`` comment. ``tag`` is the optional tag specified by the form ``#type ignore ``. see :func:`ast.parse` for more details. From bce8ace10d52a7156ba482515d58ab8a98e271a0 Mon Sep 17 00:00:00 2001 From: Yuki Kobayashi Date: Mon, 28 Apr 2025 09:28:50 +0900 Subject: [PATCH 04/12] Update Doc/library/ast.rst Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com> --- Doc/library/ast.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/ast.rst b/Doc/library/ast.rst index 969f60b8bf53dc..02a4f6a27994e8 100644 --- a/Doc/library/ast.rst +++ b/Doc/library/ast.rst @@ -1769,7 +1769,7 @@ Type annotations A type ignore comment. *lineno* is the location of the ``#type ignore`` comment. ``tag`` is the optional tag specified by the form ``#type ignore ``. - see :func:`ast.parse` for more details. + See :func:`ast.parse` for more details. .. doctest:: From 3731757de5c41035a21b428520c1870dd6aa47f3 Mon Sep 17 00:00:00 2001 From: Yuki Kobayashi Date: Mon, 28 Apr 2025 09:28:59 +0900 Subject: [PATCH 05/12] Update Doc/library/ast.rst Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com> --- Doc/library/ast.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/ast.rst b/Doc/library/ast.rst index 02a4f6a27994e8..dfe438b713d72f 100644 --- a/Doc/library/ast.rst +++ b/Doc/library/ast.rst @@ -1767,7 +1767,7 @@ Type annotations .. class:: TypeIgnore(lineno, tag) A type ignore comment. *lineno* is the location of the ``#type ignore`` comment. - ``tag`` is the optional tag specified by the form ``#type ignore ``. + *tag* is the optional tag specified by the form ``#type ignore ``. See :func:`ast.parse` for more details. From 4902adcce6e75eeb9907ab9c4ffeb3d68b911c2f Mon Sep 17 00:00:00 2001 From: Yuki Kobayashi Date: Mon, 28 Apr 2025 00:33:38 +0000 Subject: [PATCH 06/12] Update `Doc/library/ast.rst` --- Doc/library/ast.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/library/ast.rst b/Doc/library/ast.rst index dfe438b713d72f..c3d9cc911f16c4 100644 --- a/Doc/library/ast.rst +++ b/Doc/library/ast.rst @@ -1766,8 +1766,8 @@ Type annotations .. class:: TypeIgnore(lineno, tag) - A type ignore comment. *lineno* is the location of the ``#type ignore`` comment. - *tag* is the optional tag specified by the form ``#type ignore ``. + A type ignore comment. *lineno* is the location of the ``# type ignore`` comment. + *tag* is the optional tag specified by the form ``# type ignore ``. See :func:`ast.parse` for more details. From 4755dc2ac49c44cd08c949de7502d37b6c2b8077 Mon Sep 17 00:00:00 2001 From: Yuki Kobayashi Date: Mon, 28 Apr 2025 00:35:09 +0000 Subject: [PATCH 07/12] Updated `Doc/library/ast.rst` --- Doc/library/ast.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/ast.rst b/Doc/library/ast.rst index c3d9cc911f16c4..4bb4a6a0804942 100644 --- a/Doc/library/ast.rst +++ b/Doc/library/ast.rst @@ -1788,7 +1788,7 @@ Type annotations .. _ast-type-params: Type parameters -~~~~~~~~~~~~~~~ +^^^^^^^^^^^^^^^ :ref:`Type parameters ` can exist on classes, functions, and type aliases. From c897b510b715eaf37af563e53a67debdf8f31d54 Mon Sep 17 00:00:00 2001 From: Yuki Kobayashi Date: Mon, 28 Apr 2025 09:37:34 +0900 Subject: [PATCH 08/12] Update Doc/library/ast.rst Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com> --- Doc/library/ast.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/Doc/library/ast.rst b/Doc/library/ast.rst index 4bb4a6a0804942..f9322cb6c90a2b 100644 --- a/Doc/library/ast.rst +++ b/Doc/library/ast.rst @@ -1761,6 +1761,7 @@ Pattern matching .. versionadded:: 3.10 + Type annotations ^^^^^^^^^^^^^^^^ From 6cd63c9023cc159bda632d99fcfb232e6920d83b Mon Sep 17 00:00:00 2001 From: Yuki Kobayashi Date: Mon, 28 Apr 2025 00:42:03 +0000 Subject: [PATCH 09/12] Updated `Doc/library/ast.rst` --- Doc/library/ast.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/ast.rst b/Doc/library/ast.rst index f9322cb6c90a2b..6c10000b900f1c 100644 --- a/Doc/library/ast.rst +++ b/Doc/library/ast.rst @@ -1767,7 +1767,7 @@ Type annotations .. class:: TypeIgnore(lineno, tag) - A type ignore comment. *lineno* is the location of the ``# type ignore`` comment. + A ``# type ignore`` comment located at *lineno*. *tag* is the optional tag specified by the form ``# type ignore ``. See :func:`ast.parse` for more details. From 83cf03858567f4d50d9350229496a84607b0379e Mon Sep 17 00:00:00 2001 From: Yuki Kobayashi Date: Mon, 28 Apr 2025 00:44:10 +0000 Subject: [PATCH 10/12] Updated `Doc/library/ast.rst` --- Doc/library/ast.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/library/ast.rst b/Doc/library/ast.rst index 6c10000b900f1c..53d7745bc30218 100644 --- a/Doc/library/ast.rst +++ b/Doc/library/ast.rst @@ -1767,8 +1767,8 @@ Type annotations .. class:: TypeIgnore(lineno, tag) - A ``# type ignore`` comment located at *lineno*. - *tag* is the optional tag specified by the form ``# type ignore ``. + A ``# type: ignore`` comment located at *lineno*. + *tag* is the optional tag specified by the form ``# type: ignore ``. See :func:`ast.parse` for more details. From c67ea98f61cbfdf3c2385b3f1c52a6a67cfd949d Mon Sep 17 00:00:00 2001 From: Yuki Kobayashi Date: Mon, 28 Apr 2025 01:42:22 +0000 Subject: [PATCH 11/12] Add an example with non-empty tag --- Doc/library/ast.rst | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Doc/library/ast.rst b/Doc/library/ast.rst index 53d7745bc30218..57fda5bba5389a 100644 --- a/Doc/library/ast.rst +++ b/Doc/library/ast.rst @@ -1783,6 +1783,16 @@ Type annotations 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]')]) .. versionadded:: 3.8 From 98e601cbb1e964e8c58421dfe131a3904e5f8450 Mon Sep 17 00:00:00 2001 From: Yuki Kobayashi Date: Mon, 28 Apr 2025 02:03:29 +0000 Subject: [PATCH 12/12] Add note about `type_comments` parameter --- Doc/library/ast.rst | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Doc/library/ast.rst b/Doc/library/ast.rst index 57fda5bba5389a..776c63d1f0fda0 100644 --- a/Doc/library/ast.rst +++ b/Doc/library/ast.rst @@ -1770,8 +1770,6 @@ Type annotations A ``# type: ignore`` comment located at *lineno*. *tag* is the optional tag specified by the form ``# type: ignore ``. - See :func:`ast.parse` for more details. - .. doctest:: >>> print(ast.dump(ast.parse('x = 1 # type: ignore', type_comments=True), indent=4)) @@ -1794,6 +1792,10 @@ Type annotations 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: