From 0518e01c2638a3ecdb68b0c314da3f52531ecb99 Mon Sep 17 00:00:00 2001 From: Mark Byrne Date: Mon, 25 Sep 2023 20:16:27 +0200 Subject: [PATCH] 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 78fb227222..44143405e7 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 5c3a41487f..6c7c0391f2 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 0000000000..0f49e9db3a --- /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()