-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
[syntax-errors] type
statements before Python 3.12
#16478
Merged
+98
−0
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b27c961
[syntax-errors] `type` statements before Python 3.12
ntBre 64fbac6
Merge branch 'main' into brent/syn-type-stmt
ntBre c991bf2
use current_token_range
ntBre 9993164
move tests to right after parsing `type`
ntBre 423ba8d
TypeStmt -> TypeAliasStatement
ntBre 0482e51
add "alias" to message and fix missing "Cannot use" prefixes
ntBre 9f3f6bd
move error too
ntBre 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
2 changes: 2 additions & 0 deletions
2
crates/ruff_python_parser/resources/inline/err/type_stmt_py311.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,2 @@ | ||
# parse_options: {"target-version": "3.11"} | ||
type x = int |
2 changes: 2 additions & 0 deletions
2
crates/ruff_python_parser/resources/inline/ok/type_stmt_py312.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,2 @@ | ||
# parse_options: {"target-version": "3.12"} | ||
type x = int |
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
42 changes: 42 additions & 0 deletions
42
crates/ruff_python_parser/tests/snapshots/invalid_syntax@type_stmt_py311.py.snap
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,42 @@ | ||
--- | ||
source: crates/ruff_python_parser/tests/fixtures.rs | ||
input_file: crates/ruff_python_parser/resources/inline/err/type_stmt_py311.py | ||
--- | ||
## AST | ||
|
||
``` | ||
Module( | ||
ModModule { | ||
range: 0..57, | ||
body: [ | ||
TypeAlias( | ||
StmtTypeAlias { | ||
range: 44..56, | ||
name: Name( | ||
ExprName { | ||
range: 49..50, | ||
id: Name("x"), | ||
ctx: Store, | ||
}, | ||
), | ||
type_params: None, | ||
value: Name( | ||
ExprName { | ||
range: 53..56, | ||
id: Name("int"), | ||
ctx: Load, | ||
}, | ||
), | ||
}, | ||
), | ||
], | ||
}, | ||
) | ||
``` | ||
## Unsupported Syntax Errors | ||
|
||
| | ||
1 | # parse_options: {"target-version": "3.11"} | ||
2 | type x = int | ||
| ^^^^ Syntax Error: Cannot use `type` statement on Python 3.11 (syntax was added in Python 3.12) | ||
| |
35 changes: 35 additions & 0 deletions
35
crates/ruff_python_parser/tests/snapshots/valid_syntax@type_stmt_py312.py.snap
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,35 @@ | ||
--- | ||
source: crates/ruff_python_parser/tests/fixtures.rs | ||
input_file: crates/ruff_python_parser/resources/inline/ok/type_stmt_py312.py | ||
--- | ||
## AST | ||
|
||
``` | ||
Module( | ||
ModModule { | ||
range: 0..57, | ||
body: [ | ||
TypeAlias( | ||
StmtTypeAlias { | ||
range: 44..56, | ||
name: Name( | ||
ExprName { | ||
range: 49..50, | ||
id: Name("x"), | ||
ctx: Store, | ||
}, | ||
), | ||
type_params: None, | ||
value: Name( | ||
ExprName { | ||
range: 53..56, | ||
id: Name("int"), | ||
ctx: Load, | ||
}, | ||
), | ||
}, | ||
), | ||
], | ||
}, | ||
) | ||
``` |
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.
Should we use type alias instead of type statement? I would find this more approachable as a user, unless the term is ambiguous.
https://docs.python.org/3/library/typing.html#type-aliases
If not
TypeAlias
, then rename toTypeStatement
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.
pyright calls them "type alias statements," which I think sounds good for the user-facing message. For the variant name, I don't have strong feelings either way. They do call it a
type_stmt
in the Python reference, so I might lean towardTypeStatement
if you prefer that toStmt
.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'll just call it
TypeAliasStatement
, I see that's what we call the parser function now.