Skip to content

Commit 28ef038

Browse files
github-actions[bot]mbyrnepr2pre-commit-ci[bot]
authored
[Backport maintenance/2.15.x] Fix a crash when inferring a typing.TypeVar 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>
1 parent 30c3112 commit 28ef038

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

ChangeLog

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ Release date: TBA
1818

1919

2020

21+
What's New in astroid 2.15.7?
22+
=============================
23+
Release date: 2023-07-08
24+
25+
* Fix a crash when inferring a ``typing.TypeVar`` call.
26+
27+
Closes pylint-dev/pylint#8802
28+
29+
2130
What's New in astroid 2.15.6?
2231
=============================
2332
Release date: 2023-07-08

astroid/brain/brain_typing.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,9 @@ def looks_like_typing_typevar_or_newtype(node) -> bool:
121121
return False
122122

123123

124-
def infer_typing_typevar_or_newtype(node, context_itton=None):
124+
def infer_typing_typevar_or_newtype(
125+
node: Call, context_itton: context.InferenceContext | None = None
126+
) -> Iterator[ClassDef]:
125127
"""Infer a typing.TypeVar(...) or typing.NewType(...) call."""
126128
try:
127129
func = next(node.func.infer(context=context_itton))
@@ -137,7 +139,14 @@ def infer_typing_typevar_or_newtype(node, context_itton=None):
137139
raise UseInferenceDefault
138140

139141
typename = node.args[0].as_string().strip("'")
140-
node = extract_node(TYPING_TYPE_TEMPLATE.format(typename))
142+
node = ClassDef(
143+
name=typename,
144+
lineno=node.lineno,
145+
col_offset=node.col_offset,
146+
parent=node.parent,
147+
end_lineno=node.end_lineno,
148+
end_col_offset=node.end_col_offset,
149+
)
141150
return node.infer(context=context_itton)
142151

143152

tests/brain/test_typing.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
2+
# For details: https://github.com/PyCQA/astroid/blob/main/LICENSE
3+
# Copyright (c) https://github.com/PyCQA/astroid/blob/main/CONTRIBUTORS.txt
4+
# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
5+
# For details: https://github.com/pylint-dev/astroid/blob/main/LICENSE
6+
# Copyright (c) https://github.com/pylint-dev/astroid/blob/main/CONTRIBUTORS.txt
7+
8+
from astroid import builder, nodes
9+
10+
11+
def test_infer_typevar() -> None:
12+
"""
13+
Regression test for: https://github.com/pylint-dev/pylint/issues/8802
14+
15+
Test that an inferred `typing.TypeVar()` call produces a `nodes.ClassDef`
16+
node.
17+
"""
18+
assign_node = builder.extract_node(
19+
"""
20+
from typing import TypeVar
21+
MyType = TypeVar('My.Type')
22+
"""
23+
)
24+
call = assign_node.value
25+
inferred = next(call.infer())
26+
assert isinstance(inferred, nodes.ClassDef)
27+
assert inferred.name == "My.Type"

0 commit comments

Comments
 (0)