From ed2e2484f0d49309511378d30a7b10bc356ce252 Mon Sep 17 00:00:00 2001 From: Mark Byrne <31762852+mbyrnepr2@users.noreply.github.com> Date: Tue, 26 Sep 2023 14:05:04 +0200 Subject: [PATCH] False positive ``unsubscriptable-object`` (#2307) Fix a regression in 2.15.7 for ``unsubscriptable-object``. Raise an `InferenceError` when there is a `SyntaxError` due to an invalid `TypeVar` name. This reverts commit 89dfb4857670a67920d0f3ab88857d697787901d. Closes #2305 Closes pylint-dev/pylint#9069 (cherry picked from commit 1f0f2f8f780c3b639307a5f326b3963fdd6fa160) --- ChangeLog | 10 ++++++++++ astroid/brain/brain_typing.py | 13 +++++-------- tests/brain/test_typing.py | 15 ++++++++------- 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/ChangeLog b/ChangeLog index 5c003823e..a2fbf85f1 100644 --- a/ChangeLog +++ b/ChangeLog @@ -7,6 +7,16 @@ What's New in astroid 2.15.8? Release date: TBA +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 diff --git a/astroid/brain/brain_typing.py b/astroid/brain/brain_typing.py index 2cd5a3289..564396c4c 100644 --- a/astroid/brain/brain_typing.py +++ b/astroid/brain/brain_typing.py @@ -15,6 +15,7 @@ from astroid.builder import _extract_single_node from astroid.const import PY38_PLUS, PY39_PLUS from astroid.exceptions import ( + AstroidSyntaxError, AttributeInferenceError, InferenceError, UseInferenceDefault, @@ -139,14 +140,10 @@ 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, - ) + 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 index 4fdff77ae..f8b47cc2e 100644 --- a/tests/brain/test_typing.py +++ b/tests/brain/test_typing.py @@ -5,7 +5,10 @@ # 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 +import pytest + +from astroid import builder +from astroid.exceptions import InferenceError def test_infer_typevar() -> None: @@ -15,13 +18,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 - MyType = TypeVar('My.Type') + TypeVar('My.Type') """ ) - call = assign_node.value - inferred = next(call.infer()) - assert isinstance(inferred, nodes.ClassDef) - assert inferred.name == "My.Type" + with pytest.raises(InferenceError): + call_node.inferred()