Skip to content

Commit 1f0f2f8

Browse files
authored
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 89dfb48. Closes #2305 Closes pylint-dev/pylint#9069
1 parent 2380f6f commit 1f0f2f8

File tree

3 files changed

+23
-15
lines changed

3 files changed

+23
-15
lines changed

ChangeLog

+10
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,16 @@ Release date: TBA
223223
Closes pylint-dev/pylint#9015
224224

225225

226+
What's New in astroid 2.15.8?
227+
=============================
228+
Release date: TBA
229+
230+
* Fix a regression in 2.15.7 for ``unsubscriptable-object``.
231+
232+
Closes #2305
233+
Closes pylint-dev/pylint#9069
234+
235+
226236
What's New in astroid 2.15.7?
227237
=============================
228238
Release date: 2023-09-23

astroid/brain/brain_typing.py

+5-8
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from astroid.builder import AstroidBuilder, _extract_single_node
1818
from astroid.const import PY39_PLUS, PY312_PLUS
1919
from astroid.exceptions import (
20+
AstroidSyntaxError,
2021
AttributeInferenceError,
2122
InferenceError,
2223
UseInferenceDefault,
@@ -136,14 +137,10 @@ def infer_typing_typevar_or_newtype(
136137
raise UseInferenceDefault
137138

138139
typename = node.args[0].as_string().strip("'")
139-
node = ClassDef(
140-
name=typename,
141-
lineno=node.lineno,
142-
col_offset=node.col_offset,
143-
parent=node.parent,
144-
end_lineno=node.end_lineno,
145-
end_col_offset=node.end_col_offset,
146-
)
140+
try:
141+
node = extract_node(TYPING_TYPE_TEMPLATE.format(typename))
142+
except AstroidSyntaxError as exc:
143+
raise InferenceError from exc
147144
return node.infer(context=context_itton)
148145

149146

tests/brain/test_typing.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
# For details: https://github.com/pylint-dev/astroid/blob/main/LICENSE
33
# Copyright (c) https://github.com/pylint-dev/astroid/blob/main/CONTRIBUTORS.txt
44

5-
from astroid import builder, nodes
5+
import pytest
6+
7+
from astroid import builder
8+
from astroid.exceptions import InferenceError
69

710

811
def test_infer_typevar() -> None:
@@ -12,13 +15,11 @@ def test_infer_typevar() -> None:
1215
Test that an inferred `typing.TypeVar()` call produces a `nodes.ClassDef`
1316
node.
1417
"""
15-
assign_node = builder.extract_node(
18+
call_node = builder.extract_node(
1619
"""
1720
from typing import TypeVar
18-
MyType = TypeVar('My.Type')
21+
TypeVar('My.Type')
1922
"""
2023
)
21-
call = assign_node.value
22-
inferred = next(call.infer())
23-
assert isinstance(inferred, nodes.ClassDef)
24-
assert inferred.name == "My.Type"
24+
with pytest.raises(InferenceError):
25+
call_node.inferred()

0 commit comments

Comments
 (0)