Fix a crash when inferring a typing.TypeVar call.#2239
Conversation
There was a problem hiding this comment.
Created this module since it felt like all brain_typing tests were going into brain/test_brain.py - let me know what you think.
Codecov Report
Additional details and impacted files@@ Coverage Diff @@
## main #2239 +/- ##
=======================================
Coverage 92.80% 92.80%
=======================================
Files 94 94
Lines 10941 10941
=======================================
Hits 10154 10154
Misses 787 787
Flags with carried forward coverage won't be shown. Click here to find out more.
|
|
Let's backport. Shall we then move the changelog to 2.15.6? |
|
|
||
| typename = node.args[0].as_string().strip("'") | ||
| node = extract_node(TYPING_TYPE_TEMPLATE.format(typename)) | ||
| node = ClassDef( |
There was a problem hiding this comment.
Shall we add the Meta class as base? Then we still have the __getitem__ inference correctly.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Okay! Can we then remove that code?
There was a problem hiding this comment.
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)
for more information, see https://pre-commit.ci
e845e19 to
017c218
Compare
f8562cb to
5b671d3
Compare
5b671d3 to
ea5dd00
Compare
Closes pylint-dev/pylint#8802 (cherry picked from commit 89dfb48)
…peVar` 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>
Closes pylint-dev/pylint#8802
Type of Changes
Description
Closes pylint-dev/pylint#8802