-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Add semantic analysis of type aliases and parameters #6109
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
958fb8d
Add `ScopeKind::Type`
zanieb 9c7e579
Visit class decorators before the rest of the class definition
zanieb 90ee170
Add binding of type parameters in classes and functions
zanieb 4f8a3bd
Add deferred detection of undefined names in type variable bounds
zanieb 99607e5
Add "step" comments
zanieb 103ae9c
Visit type alias parameters before the value
zanieb e88c5c5
Implement visitation of type alias statements in the checker
zanieb 112a745
Simplify match
zanieb 7a42b3a
Clippy
zanieb 2589d74
Add F821 test case for type parameters
zanieb 79e9ff5
Fix classification of functions
zanieb 093d40f
Fix scoping of class variables
zanieb fd6e6f4
Implement scoping in `lookup_symbol`
zanieb 8e94ac9
Commit snapshot file
zanieb 65682a1
Use `while let` instead of `loop`
zanieb 4aaed6c
Rename non-type scope parent method for clarity
zanieb eec45ba
Fix whitespace
zanieb 04cf38a
Rename methods (review feedback)
zanieb c35fd54
Deduplicate forward ref declarations
zanieb 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
103 changes: 103 additions & 0 deletions
103
crates/ruff/resources/test/fixtures/pyflakes/F821_17.py
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,103 @@ | ||
"""Test type parameters and aliases""" | ||
|
||
# Type parameters in type alias statements | ||
|
||
from some_module import Bar | ||
|
||
type Foo[T] = T # OK | ||
type Foo[T] = list[T] # OK | ||
type Foo[T: ForwardA] = T # OK | ||
type Foo[*Ts] = Bar[Ts] # OK | ||
type Foo[**P] = Bar[P] # OK | ||
class ForwardA: ... | ||
|
||
# Types used in aliased assignment must exist | ||
|
||
type Foo = DoesNotExist # F821: Undefined name `DoesNotExist` | ||
type Foo = list[DoesNotExist] # F821: Undefined name `DoesNotExist` | ||
|
||
# Type parameters do not escape alias scopes | ||
|
||
type Foo[T] = T | ||
T # F821: Undefined name `T` - not accessible afterward alias scope | ||
|
||
# Type parameters in functions | ||
|
||
def foo[T](t: T) -> T: return t # OK | ||
async def afoo[T](t: T) -> T: return t # OK | ||
def with_forward_ref[T: ForwardB](t: T) -> T: return t # OK | ||
def can_access_inside[T](t: T) -> T: # OK | ||
print(T) # OK | ||
return t # OK | ||
class ForwardB: ... | ||
|
||
|
||
# Type parameters do not escape function scopes | ||
|
||
from some_library import some_decorator | ||
|
||
@some_decorator(T) # F821: Undefined name `T` - not accessible in decorators | ||
|
||
def foo[T](t: T) -> None: ... | ||
T # F821: Undefined name `T` - not accessible afterward function scope | ||
|
||
|
||
# Type parameters in classes | ||
|
||
class Foo[T](list[T]): ... # OK | ||
class UsesForward[T: ForwardC](list[T]): ... # OK | ||
class ForwardC: ... | ||
class WithinBody[T](list[T]): # OK | ||
t = T # OK | ||
x: T # OK | ||
|
||
def foo(self, x: T) -> T: # OK | ||
return x | ||
|
||
def foo(self): | ||
T # OK | ||
|
||
|
||
# Type parameters do not escape class scopes | ||
|
||
from some_library import some_decorator | ||
@some_decorator(T) # F821: Undefined name `T` - not accessible in decorators | ||
|
||
class Foo[T](list[T]): ... | ||
T # F821: Undefined name `T` - not accessible after class scope | ||
|
||
# Types specified in bounds should exist | ||
|
||
type Foo[T: DoesNotExist] = T # F821: Undefined name `DoesNotExist` | ||
def foo[T: DoesNotExist](t: T) -> T: return t # F821: Undefined name `DoesNotExist` | ||
class Foo[T: DoesNotExist](list[T]): ... # F821: Undefined name `DoesNotExist` | ||
|
||
type Foo[T: (DoesNotExist1, DoesNotExist2)] = T # F821: Undefined name `DoesNotExist1`, Undefined name `DoesNotExist2` | ||
def foo[T: (DoesNotExist1, DoesNotExist2)](t: T) -> T: return t # F821: Undefined name `DoesNotExist1`, Undefined name `DoesNotExist2` | ||
class Foo[T: (DoesNotExist1, DoesNotExist2)](list[T]): ... # F821: Undefined name `DoesNotExist1`, Undefined name `DoesNotExist2` | ||
|
||
# Type parameters in nested classes | ||
|
||
class Parent[T]: | ||
t = T # OK | ||
|
||
def can_use_class_variable(self, x: t) -> t: # OK | ||
return x | ||
|
||
class Child: | ||
def can_access_parent_type_parameter(self, x: T) -> T: # OK | ||
T # OK | ||
return x | ||
|
||
def cannot_access_parent_variable(self, x: t) -> t: # F821: Undefined name `T` | ||
t # F821: Undefined name `t` | ||
return x | ||
|
||
# Type parameters in nested functions | ||
|
||
def can_access_inside_nested[T](t: T) -> T: # OK | ||
def bar(x: T) -> T: # OK | ||
T # OK | ||
return x | ||
|
||
bar(t) |
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
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
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.
Were you able to test these against the updated version of Pyflakes?
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 haven't! I'll do that and confirm they match. I tested for correctness with Pyright and Python runtime errors.
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.
The results are identical except that pyflakes emits two incorrect warnings
They appear to handle binding of type var tuples and param specs incorrectly.