From 89dfb4857670a67920d0f3ab88857d697787901d Mon Sep 17 00:00:00 2001 From: Mark Byrne <31762852+mbyrnepr2@users.noreply.github.com> Date: Mon, 17 Jul 2023 12:39:18 +0200 Subject: [PATCH] Fix a crash when inferring a `typing.TypeVar` call. (#2239) Closes pylint-dev/pylint#8802 --- ChangeLog | 11 ++++++++++- astroid/brain/brain_typing.py | 13 +++++++++++-- tests/brain/test_typing.py | 24 ++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 tests/brain/test_typing.py diff --git a/ChangeLog b/ChangeLog index 16f98f25eb..277171b4e9 100644 --- a/ChangeLog +++ b/ChangeLog @@ -195,10 +195,19 @@ Release date: TBA Refs #2204 -What's New in astroid 2.15.6? +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: 2023-07-08 + * Harden ``get_module_part()`` against ``"."``. Closes pylint-dev/pylint#8749 diff --git a/astroid/brain/brain_typing.py b/astroid/brain/brain_typing.py index d087885a4d..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)) @@ -134,7 +136,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/brain/test_typing.py b/tests/brain/test_typing.py new file mode 100644 index 0000000000..8d75708d6d --- /dev/null +++ b/tests/brain/test_typing.py @@ -0,0 +1,24 @@ +# 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"