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

Fix a crash when inferring a typing.TypeVar call. #2239

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,19 @@ Release date: TBA
Refs #2204


What's New in astroid 2.15.6?
What's New in astroid 2.15.7?
=============================
Release date: TBA

* 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

* Harden ``get_module_part()`` against ``"."``.

Closes pylint-dev/pylint#8749
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 @@ -118,7 +118,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 @@ -134,7 +136,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(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall we add the Meta class as base? Then we still have the __getitem__ inference correctly.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for such quick feedbackπŸ‘.
I started a short vacation right after opening this so I’ll think about a test case when I can.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @DanielNoord I've taken a look finally :)

It seems to me there is no need for __getitem__ to be attached to the astroid node representing either typing.TypeVar or typing.NewType since neither of these are subscriptable. What do you think?
Similar point for __args__ since that is used to fetch the arguments used in subscripting.

As a sidenote - __getitem__ is already part of nodes.ClassDef since it inherits that method from LocalsDictNodeNG.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay! Can we then remove that code?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took a look into that just now. The template is used by this function which needs the __args__ member since the function is inferring subscriptable types.
As an illustration, If I were to remove the code and use a similar ClassDef construction for the code above, this unitest fails:
FAILED astroid/tests/brain/test_brain.py::TypingBrain::test_has_dunder_args - astroid.exceptions.InferenceError: StopIteration raised without any error information.
In other words, running Pylint on this code:

from typing import Union
NumericTypes = Union[int, float]
NumericTypes.__args__ #@

...gives: E1101: Class 'Union' has no '__args__' member (no-member)

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
24 changes: 24 additions & 0 deletions tests/brain/test_typing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# 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"
Loading