From f15d7b9d2dccb0aeabd61760a6d042025f596851 Mon Sep 17 00:00:00 2001 From: Mark Byrne Date: Thu, 6 Jul 2023 15:36:28 +0100 Subject: [PATCH 1/7] Fix a crash when inferring a `typing.TypeVar` call. Closes pylint-dev/pylint#8802 --- ChangeLog | 4 ++++ astroid/brain/brain_typing.py | 9 ++++++++- tests/test_typing.py | 22 ++++++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 tests/test_typing.py diff --git a/ChangeLog b/ChangeLog index 8750167f89..babb1c4005 100644 --- a/ChangeLog +++ b/ChangeLog @@ -31,6 +31,10 @@ Release date: TBA * Move ``safe_infer()`` from ``helpers`` to ``util``. This avoids some circular imports. +* Fix a crash when inferring a `typing.TypeVar` call. + + Closes pylint-dev/pylint#8802 + * Reduce file system access in ``ast_from_file()``. * Reduce time to ``import astroid`` by delaying ``astroid_bootstrapping()`` until diff --git a/astroid/brain/brain_typing.py b/astroid/brain/brain_typing.py index d087885a4d..9ae118688d 100644 --- a/astroid/brain/brain_typing.py +++ b/astroid/brain/brain_typing.py @@ -134,7 +134,14 @@ def infer_typing_typevar_or_newtype(node, context_itton=None): raise UseInferenceDefault typename = node.args[0].as_string().strip("'") - node = extract_node(TYPING_TYPE_TEMPLATE.format(typename)) + node = ClassDef( + name=typename, + lineno=node.lineno, + col_offset=node.col_offset, + parent=node.parent, + end_lineno=node.end_lineno, + end_col_offset=node.end_col_offset, + ) return node.infer(context=context_itton) diff --git a/tests/test_typing.py b/tests/test_typing.py new file mode 100644 index 0000000000..6774b72d30 --- /dev/null +++ b/tests/test_typing.py @@ -0,0 +1,22 @@ +import pytest + +from astroid import builder, nodes + + +def test_infer_typevar() -> None: + """ + Regression test for: https://github.com/pylint-dev/pylint/issues/8802 + + Test that an inferred `typing.TypeVar()` call produces a `nodes.ClassDef` + node. + """ + assign_node = builder.extract_node( + """ + from typing import TypeVar + MyType = TypeVar('My.Type') + """ + ) + call = assign_node.value + inferred = next(call.infer()) + assert isinstance(inferred, nodes.ClassDef) + assert inferred.name == "My.Type" From 6f9730503a10206d3fb41395c8802654641f8749 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 6 Jul 2023 14:39:31 +0000 Subject: [PATCH 2/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_typing.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/test_typing.py b/tests/test_typing.py index 6774b72d30..8a33b693e7 100644 --- a/tests/test_typing.py +++ b/tests/test_typing.py @@ -1,3 +1,6 @@ +# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html +# For details: https://github.com/pylint-dev/astroid/blob/main/LICENSE +# Copyright (c) https://github.com/pylint-dev/astroid/blob/main/CONTRIBUTORS.txt import pytest from astroid import builder, nodes From cc15e2490521cbc8fdd5f091a6985c616bdf1597 Mon Sep 17 00:00:00 2001 From: Mark Byrne Date: Thu, 6 Jul 2023 15:44:15 +0100 Subject: [PATCH 3/7] Remove unused import and move module. --- tests/{ => brain}/test_typing.py | 1 - 1 file changed, 1 deletion(-) rename tests/{ => brain}/test_typing.py (98%) diff --git a/tests/test_typing.py b/tests/brain/test_typing.py similarity index 98% rename from tests/test_typing.py rename to tests/brain/test_typing.py index 8a33b693e7..8d75708d6d 100644 --- a/tests/test_typing.py +++ b/tests/brain/test_typing.py @@ -1,7 +1,6 @@ # Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html # For details: https://github.com/pylint-dev/astroid/blob/main/LICENSE # Copyright (c) https://github.com/pylint-dev/astroid/blob/main/CONTRIBUTORS.txt -import pytest from astroid import builder, nodes From 5f2e6fc7aee2e4adb2ff3d5e754f26eca8d92559 Mon Sep 17 00:00:00 2001 From: Mark Byrne Date: Thu, 13 Jul 2023 13:34:29 +0200 Subject: [PATCH 4/7] Move Changelog entry to the `2.15.6` milestone. --- ChangeLog | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index babb1c4005..e1fafd360c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -31,10 +31,6 @@ Release date: TBA * Move ``safe_infer()`` from ``helpers`` to ``util``. This avoids some circular imports. -* Fix a crash when inferring a `typing.TypeVar` call. - - Closes pylint-dev/pylint#8802 - * Reduce file system access in ``ast_from_file()``. * Reduce time to ``import astroid`` by delaying ``astroid_bootstrapping()`` until @@ -208,6 +204,10 @@ Release date: TBA Closes pylint-dev/pylint#8749 +* Fix a crash when inferring a `typing.TypeVar` call. + + Closes pylint-dev/pylint#8802 + * Avoid expensive list/tuple multiplication operations that would result in ``MemoryError``. Closes pylint-dev/pylint#8748 From 017c218f3dfe4fa180d739de0bb4ee44bf8a07a1 Mon Sep 17 00:00:00 2001 From: Mark Byrne Date: Thu, 13 Jul 2023 15:00:40 +0200 Subject: [PATCH 5/7] Move Changelog entry to the `2.15.7` milestone. --- ChangeLog | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index e1fafd360c..daacafc1d7 100644 --- a/ChangeLog +++ b/ChangeLog @@ -196,6 +196,15 @@ Release date: TBA Refs #2204 +What's New in astroid 2.15.7? +============================= +Release date: TBA + +* Fix a crash when inferring a ``typing.TypeVar`` call. + + Closes pylint-dev/pylint#8802 + + What's New in astroid 2.15.6? ============================= Release date: TBA @@ -204,10 +213,6 @@ Release date: TBA Closes pylint-dev/pylint#8749 -* Fix a crash when inferring a `typing.TypeVar` call. - - Closes pylint-dev/pylint#8802 - * Avoid expensive list/tuple multiplication operations that would result in ``MemoryError``. Closes pylint-dev/pylint#8748 From 1b3355c93c0705a50984d7a88f04f1ebcdbbc5f5 Mon Sep 17 00:00:00 2001 From: Mark Byrne Date: Thu, 13 Jul 2023 15:03:54 +0200 Subject: [PATCH 6/7] Add release date to the Changelog for the `2.15.6` release. --- ChangeLog | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index daacafc1d7..3a7aaf4b2e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -207,7 +207,7 @@ Release date: TBA What's New in astroid 2.15.6? ============================= -Release date: TBA +Release date: 2023-07-08 * Harden ``get_module_part()`` against ``"."``. From ea5dd007a03f0a9415ac8d5175719cd015727b5b Mon Sep 17 00:00:00 2001 From: Mark Byrne Date: Thu, 13 Jul 2023 23:41:15 +0200 Subject: [PATCH 7/7] Add type annotations to function definition. --- astroid/brain/brain_typing.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/astroid/brain/brain_typing.py b/astroid/brain/brain_typing.py index 9ae118688d..659cba268d 100644 --- a/astroid/brain/brain_typing.py +++ b/astroid/brain/brain_typing.py @@ -118,7 +118,9 @@ def looks_like_typing_typevar_or_newtype(node) -> bool: return False -def infer_typing_typevar_or_newtype(node, context_itton=None): +def infer_typing_typevar_or_newtype( + node: Call, context_itton: context.InferenceContext | None = None +) -> Iterator[ClassDef]: """Infer a typing.TypeVar(...) or typing.NewType(...) call.""" try: func = next(node.func.infer(context=context_itton))