mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-17 09:00:48 +00:00
Support type alias statements in simple statement positions (#8916)
<!-- Thank you for contributing to Ruff! To help us out with reviewing, please consider the following: - Does this pull request include a summary of the change? (See below.) - Does this pull request include a descriptive title? - Does this pull request include references to any relevant issues? --> ## Summary Our `SoftKeywordTokenizer` only respected soft keywords in compound statement positions -- for example, at the start of a logical line: ```python type X = int ``` However, type aliases can also appear in simple statement positions, like: ```python class Class: type X = int ``` (Note that `match` and `case` are _not_ valid keywords in such positions.) This PR upgrades the tokenizer to track both kinds of valid positions. Closes https://github.com/astral-sh/ruff/issues/8900. Closes https://github.com/astral-sh/ruff/issues/8899. ## Test Plan `cargo test`
This commit is contained in:
parent
073eddb1d9
commit
20782ab02c
4 changed files with 224 additions and 18 deletions
|
@ -822,6 +822,10 @@ type X \
|
|||
[T] = T
|
||||
type X[T] \
|
||||
= T
|
||||
|
||||
# simple statements
|
||||
type X = int; type X = str; type X = type
|
||||
class X: type X = int
|
||||
"#;
|
||||
insta::assert_debug_snapshot!(parse_suite(source, "<test>").unwrap());
|
||||
}
|
||||
|
@ -859,10 +863,17 @@ type (
|
|||
type = 1
|
||||
type = x = 1
|
||||
x = type = 1
|
||||
lambda x: type
|
||||
";
|
||||
insta::assert_debug_snapshot!(parse_suite(source, "<test>").unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_invalid_type() {
|
||||
assert!(parse_suite("a: type X = int", "<test>").is_err());
|
||||
assert!(parse_suite("lambda: type X = int", "<test>").is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn numeric_literals() {
|
||||
let source = r"x = 123456789
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue