Skip to content

Commit

Permalink
[Backport maintenance/2.15.x] Fix a crash when inferring a `typing.Ty…
Browse files Browse the repository at this point in the history
…peVar` call. (#2254)

* Fix a crash when inferring a `typing.TypeVar` call. (#2239)

Closes pylint-dev/pylint#8802

(cherry picked from commit 89dfb48)

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: Mark Byrne <31762852+mbyrnepr2@users.noreply.github.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Jul 17, 2023
1 parent 30c3112 commit 28ef038
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
9 changes: 9 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ Release date: TBA



What's New in astroid 2.15.7?
=============================
Release date: 2023-07-08

* 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
Expand Down
13 changes: 11 additions & 2 deletions astroid/brain/brain_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,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))
Expand All @@ -137,7 +139,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)


Expand Down
27 changes: 27 additions & 0 deletions tests/brain/test_typing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
# For details: https://github.com/PyCQA/astroid/blob/main/LICENSE
# Copyright (c) https://github.com/PyCQA/astroid/blob/main/CONTRIBUTORS.txt
# 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"

0 comments on commit 28ef038

Please sign in to comment.