Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

False positive unsubscriptable-object #2307

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,16 @@ 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
Expand Down
17 changes: 6 additions & 11 deletions 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 @@ -118,9 +119,7 @@ def looks_like_typing_typevar_or_newtype(node) -> bool:
return False


def infer_typing_typevar_or_newtype(
node: Call, context_itton: context.InferenceContext | None = None
) -> Iterator[ClassDef]:
def infer_typing_typevar_or_newtype(node, context_itton=None):
mbyrnepr2 marked this conversation as resolved.
Show resolved Hide resolved
"""Infer a typing.TypeVar(...) or typing.NewType(...) call."""
try:
func = next(node.func.infer(context=context_itton))
Expand All @@ -136,14 +135,10 @@ def infer_typing_typevar_or_newtype(
raise UseInferenceDefault

typename = node.args[0].as_string().strip("'")
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,
)
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
13 changes: 7 additions & 6 deletions tests/brain/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
# 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
import pytest

from astroid import builder
from astroid.exceptions import InferenceError


def test_infer_typevar() -> None:
Expand All @@ -15,10 +18,8 @@ def test_infer_typevar() -> None:
assign_node = builder.extract_node(
"""
from typing import TypeVar
MyType = TypeVar('My.Type')
TypeVar('My.Type')
"""
)
call = assign_node.value
inferred = next(call.infer())
assert isinstance(inferred, nodes.ClassDef)
assert inferred.name == "My.Type"
with pytest.raises(InferenceError):
assign_node.inferred()
mbyrnepr2 marked this conversation as resolved.
Show resolved Hide resolved