-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Bypass class attribute lookup for PEP 695 typevars #20292
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
Open
A5rocks
wants to merge
2
commits into
python:master
Choose a base branch
from
A5rocks:use-locals-always
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -1842,7 +1842,7 @@ def push_type_args( | |||||||||
| return None | ||||||||||
| tvs.append((p.name, tv)) | ||||||||||
|
|
||||||||||
| if self.is_defined_type_param(p.name): | ||||||||||
| if self.get_defined_type_param(p.name): | ||||||||||
| self.fail(f'"{p.name}" already defined as a type parameter', context) | ||||||||||
| else: | ||||||||||
| assert self.add_symbol( | ||||||||||
|
|
@@ -1851,15 +1851,15 @@ def push_type_args( | |||||||||
|
|
||||||||||
| return tvs | ||||||||||
|
|
||||||||||
| def is_defined_type_param(self, name: str) -> bool: | ||||||||||
| def get_defined_type_param(self, name: str) -> TypeVarLikeExpr | None: | ||||||||||
| for names in self.locals: | ||||||||||
| if names is None: | ||||||||||
| continue | ||||||||||
| if name in names: | ||||||||||
|
Comment on lines
1856
to
1858
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And just to save a bit on branching,
Suggested change
|
||||||||||
| node = names[name].node | ||||||||||
| if isinstance(node, TypeVarLikeExpr): | ||||||||||
| return True | ||||||||||
| return False | ||||||||||
| return node | ||||||||||
| return None | ||||||||||
|
|
||||||||||
| def analyze_type_param( | ||||||||||
| self, type_param: TypeParam, context: Context | ||||||||||
|
|
@@ -2272,15 +2272,15 @@ class Foo(Bar, Generic[T]): ... | |||||||||
| has_type_var_tuple = False | ||||||||||
| if defn.type_args is not None: | ||||||||||
| for p in defn.type_args: | ||||||||||
| node = self.lookup(p.name, context) | ||||||||||
| node = self.get_defined_type_param(p.name) | ||||||||||
| assert node is not None | ||||||||||
| assert isinstance(node.node, TypeVarLikeExpr) | ||||||||||
| if isinstance(node.node, TypeVarTupleExpr): | ||||||||||
| assert isinstance(node, TypeVarLikeExpr) | ||||||||||
| if isinstance(node, TypeVarTupleExpr): | ||||||||||
| if has_type_var_tuple: | ||||||||||
| self.fail("Can only use one type var tuple in a class def", context) | ||||||||||
| continue | ||||||||||
| has_type_var_tuple = True | ||||||||||
| declared_tvars.append((p.name, node.node)) | ||||||||||
| declared_tvars.append((p.name, node)) | ||||||||||
|
|
||||||||||
| for i, base_expr in enumerate(base_type_exprs): | ||||||||||
| if isinstance(base_expr, StarExpr): | ||||||||||
|
|
||||||||||
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Ough, I spelled that out but didn't realize that this goes in the wrong direction (?). We are typically interested in the most recent locals frame, so shouldn't this be
? It didn't matter when we only requested a boolean "is defined" result, but now could potentially resolve to a wrong symbol. I don't have a testcase for that, but anyway the variable will normally be found in the most recent local frame, so checking it first makes more sense? Upd: no, it cannot resolve to the wrong symbol, we only accept type variables, and type param redefinitions are never added to symtable, but maybe it's still better to avoid the unnecessary work?
reversedis fast