Skip to content

Commit

Permalink
Fix a regression in 2.15.7 for unsubscriptable-object.
Browse files Browse the repository at this point in the history
- Raise an `InferenceError` when there is a `SyntaxError` due to an invalid `TypeVar` name.

Closes pylint-dev#2305
Closes pylint-dev/pylint#9069
  • Loading branch information
mbyrnepr2 committed Sep 25, 2023
1 parent bf31af2 commit 0518e01
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
16 changes: 15 additions & 1 deletion ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 ``"."``.

Expand Down
6 changes: 5 additions & 1 deletion astroid/brain/brain_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)


Expand Down
25 changes: 25 additions & 0 deletions tests/brain/test_typing.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 0518e01

Please sign in to comment.