From bf31af2643cfd3236b9ec9120f8e911681c5dc9f Mon Sep 17 00:00:00 2001 From: Mark Byrne Date: Mon, 25 Sep 2023 19:54:08 +0200 Subject: [PATCH 1/5] Revert "Fix a crash when inferring a `typing.TypeVar` call. (#2239)" This reverts commit 89dfb4857670a67920d0f3ab88857d697787901d. --- ChangeLog | 6 +----- astroid/brain/brain_typing.py | 13 ++----------- tests/brain/test_typing.py | 24 ------------------------ 3 files changed, 3 insertions(+), 40 deletions(-) delete mode 100644 tests/brain/test_typing.py diff --git a/ChangeLog b/ChangeLog index 52e064edf..78fb22722 100644 --- a/ChangeLog +++ b/ChangeLog @@ -227,10 +227,6 @@ What's New in astroid 2.15.7? ============================= Release date: 2023-09-23 -* Fix a crash when inferring a ``typing.TypeVar`` call. - - Closes pylint-dev/pylint#8802 - * Infer user-defined enum classes by checking if the class is a subtype of ``enum.Enum``. Closes pylint-dev/pylint#8897 @@ -248,7 +244,7 @@ Release date: 2023-09-23 What's New in astroid 2.15.6? ============================= -Release date: 2023-07-08 +Release date: TBA * Harden ``get_module_part()`` against ``"."``. diff --git a/astroid/brain/brain_typing.py b/astroid/brain/brain_typing.py index cb3d1ce96..5c3a41487 100644 --- a/astroid/brain/brain_typing.py +++ b/astroid/brain/brain_typing.py @@ -118,9 +118,7 @@ def looks_like_typing_typevar_or_newtype(node) -> bool: return False -def infer_typing_typevar_or_newtype( - node: Call, context_itton: context.InferenceContext | None = None -) -> Iterator[ClassDef]: +def infer_typing_typevar_or_newtype(node, context_itton=None): """Infer a typing.TypeVar(...) or typing.NewType(...) call.""" try: func = next(node.func.infer(context=context_itton)) @@ -136,14 +134,7 @@ def infer_typing_typevar_or_newtype( raise UseInferenceDefault typename = node.args[0].as_string().strip("'") - 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, - ) + node = extract_node(TYPING_TYPE_TEMPLATE.format(typename)) return node.infer(context=context_itton) diff --git a/tests/brain/test_typing.py b/tests/brain/test_typing.py deleted file mode 100644 index 8d75708d6..000000000 --- a/tests/brain/test_typing.py +++ /dev/null @@ -1,24 +0,0 @@ -# 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 - -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 0518e01c2638a3ecdb68b0c314da3f52531ecb99 Mon Sep 17 00:00:00 2001 From: Mark Byrne Date: Mon, 25 Sep 2023 20:16:27 +0200 Subject: [PATCH 2/5] Fix a regression in 2.15.7 for ``unsubscriptable-object``. - Raise an `InferenceError` when there is a `SyntaxError` due to an invalid `TypeVar` name. Closes #2305 Closes pylint-dev/pylint#9069 --- ChangeLog | 16 +++++++++++++++- astroid/brain/brain_typing.py | 6 +++++- tests/brain/test_typing.py | 25 +++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 tests/brain/test_typing.py diff --git a/ChangeLog b/ChangeLog index 78fb22722..44143405e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -223,10 +223,24 @@ Release date: TBA Closes pylint-dev/pylint#9015 +What's New in astroid 2.15.8? +============================= +Release date: TBA + +* Fix a regression in 2.15.7 for ``unsubscriptable-object``. + + Closes #2305 + Closes pylint-dev/pylint#9069 + + What's New in astroid 2.15.7? ============================= Release date: 2023-09-23 +* Fix a crash when inferring a ``typing.TypeVar`` call. + + Closes pylint-dev/pylint#8802 + * Infer user-defined enum classes by checking if the class is a subtype of ``enum.Enum``. Closes pylint-dev/pylint#8897 @@ -244,7 +258,7 @@ Release date: 2023-09-23 What's New in astroid 2.15.6? ============================= -Release date: TBA +Release date: 2023-07-08 * Harden ``get_module_part()`` against ``"."``. diff --git a/astroid/brain/brain_typing.py b/astroid/brain/brain_typing.py index 5c3a41487..6c7c0391f 100644 --- a/astroid/brain/brain_typing.py +++ b/astroid/brain/brain_typing.py @@ -17,6 +17,7 @@ from astroid.builder import AstroidBuilder, _extract_single_node from astroid.const import PY39_PLUS, PY312_PLUS from astroid.exceptions import ( + AstroidSyntaxError, AttributeInferenceError, InferenceError, UseInferenceDefault, @@ -134,7 +135,10 @@ 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)) + try: + node = extract_node(TYPING_TYPE_TEMPLATE.format(typename)) + except AstroidSyntaxError as exc: + raise InferenceError from exc return node.infer(context=context_itton) diff --git a/tests/brain/test_typing.py b/tests/brain/test_typing.py new file mode 100644 index 000000000..0f49e9db3 --- /dev/null +++ b/tests/brain/test_typing.py @@ -0,0 +1,25 @@ +# 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 astroid.exceptions import InferenceError + + +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 + TypeVar('My.Type') + """ + ) + with pytest.raises(InferenceError): + assign_node.inferred() From b93922fcbbf30d7d23ec7b340f539137c3c83507 Mon Sep 17 00:00:00 2001 From: Mark Byrne Date: Mon, 25 Sep 2023 20:44:29 +0200 Subject: [PATCH 3/5] Remove unused import. --- tests/brain/test_typing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/brain/test_typing.py b/tests/brain/test_typing.py index 0f49e9db3..8b43d2181 100644 --- a/tests/brain/test_typing.py +++ b/tests/brain/test_typing.py @@ -4,7 +4,7 @@ import pytest -from astroid import builder, nodes +from astroid import builder from astroid.exceptions import InferenceError From cae2a5df3559c0cd18fb978278abba76337fdc33 Mon Sep 17 00:00:00 2001 From: Mark Byrne <31762852+mbyrnepr2@users.noreply.github.com> Date: Mon, 25 Sep 2023 21:28:25 +0200 Subject: [PATCH 4/5] Update astroid/brain/brain_typing.py Co-authored-by: Pierre Sassoulas --- 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 6c7c0391f..d4b2ca0a5 100644 --- a/astroid/brain/brain_typing.py +++ b/astroid/brain/brain_typing.py @@ -119,7 +119,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)) From 43a459bf653554946f88864dcdc4b4930c9af0e2 Mon Sep 17 00:00:00 2001 From: Mark Byrne Date: Tue, 26 Sep 2023 08:33:49 +0200 Subject: [PATCH 5/5] More accurate naming in test. --- tests/brain/test_typing.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/brain/test_typing.py b/tests/brain/test_typing.py index 8b43d2181..1e8e3eaf8 100644 --- a/tests/brain/test_typing.py +++ b/tests/brain/test_typing.py @@ -15,11 +15,11 @@ def test_infer_typevar() -> None: Test that an inferred `typing.TypeVar()` call produces a `nodes.ClassDef` node. """ - assign_node = builder.extract_node( + call_node = builder.extract_node( """ from typing import TypeVar TypeVar('My.Type') """ ) with pytest.raises(InferenceError): - assign_node.inferred() + call_node.inferred()