-
-
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
Merged
mbyrnepr2
merged 7 commits into
pylint-dev:main
from
mbyrnepr2:pylint_issue_8802_typing_typevar_crash
Jul 17, 2023
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f15d7b9
Fix a crash when inferring a `typing.TypeVar` call.
mbyrnepr2 6f97305
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] cc15e24
Remove unused import and move module.
mbyrnepr2 5f2e6fc
Move Changelog entry to the `2.15.6` milestone.
mbyrnepr2 017c218
Move Changelog entry to the `2.15.7` milestone.
mbyrnepr2 1b3355c
Add release date to the Changelog for the `2.15.6` release.
mbyrnepr2 ea5dd00
Add type annotations to function definition.
mbyrnepr2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 eithertyping.TypeVar
ortyping.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 ofnodes.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:
...gives:
E1101: Class 'Union' has no '__args__' member (no-member)