-
-
Notifications
You must be signed in to change notification settings - Fork 276
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
Fix a crash when inferring a typing.TypeVar
call.
#2239
Conversation
tests/brain/test_typing.py
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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? |
@@ -134,7 +134,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( |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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)
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>
β¦ev#2239)" This reverts commit 89dfb48.
Closes pylint-dev/pylint#8802
Type of Changes
Description
Closes pylint-dev/pylint#8802