diff --git a/Cargo.lock b/Cargo.lock index e4b3156f04..ab7727be8e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2130,6 +2130,13 @@ dependencies = [ "wild", ] +[[package]] +name = "ruff_allocator" +version = "0.0.0" +dependencies = [ + "bumpalo", +] + [[package]] name = "ruff_benchmark" version = "0.0.0" @@ -2137,11 +2144,8 @@ dependencies = [ "codspeed-criterion-compat", "mimalloc", "once_cell", - "red_knot", - "ruff_db", - "ruff_linter", + "ruff_allocator", "ruff_python_ast", - "ruff_python_formatter", "ruff_python_parser", "ruff_python_trivia", "serde", @@ -2366,6 +2370,7 @@ dependencies = [ "is-macro", "itertools 0.13.0", "once_cell", + "ruff_allocator", "ruff_cache", "ruff_macros", "ruff_python_trivia", @@ -2462,6 +2467,7 @@ dependencies = [ "compact_str", "insta", "memchr", + "ruff_allocator", "ruff_python_ast", "ruff_python_trivia", "ruff_source_file", diff --git a/Cargo.toml b/Cargo.toml index 771d9311ef..11a209f5c7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,7 @@ license = "MIT" [workspace.dependencies] ruff = { path = "crates/ruff" } +ruff_allocator = { path = "crates/ruff_allocator" } ruff_cache = { path = "crates/ruff_cache" } ruff_db = { path = "crates/ruff_db" } ruff_diagnostics = { path = "crates/ruff_diagnostics" } @@ -46,6 +47,7 @@ argfile = { version = "0.2.0" } bincode = { version = "1.3.3" } bitflags = { version = "2.5.0" } bstr = { version = "1.9.1" } +bumpalo = { version = "3.16.0" } cachedir = { version = "0.3.1" } camino = { version = "1.1.7" } chrono = { version = "0.4.35", default-features = false, features = ["clock"] } diff --git a/crates/ruff_allocator/Cargo.toml b/crates/ruff_allocator/Cargo.toml new file mode 100644 index 0000000000..a9df13c7d5 --- /dev/null +++ b/crates/ruff_allocator/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "ruff_allocator" +version = "0.0.0" +publish = false +authors = { workspace = true } +edition = { workspace = true } +rust-version = { workspace = true } +homepage = { workspace = true } +documentation = { workspace = true } +repository = { workspace = true } +license = { workspace = true } + +[dependencies] +bumpalo = { workspace = true, features = ["boxed", "collections"] } + +[lints] +workspace = true diff --git a/crates/ruff_allocator/src/lib.rs b/crates/ruff_allocator/src/lib.rs new file mode 100644 index 0000000000..fbc8bd9a57 --- /dev/null +++ b/crates/ruff_allocator/src/lib.rs @@ -0,0 +1,137 @@ +use std::fmt::{Display, Formatter}; +use std::iter::FusedIterator; +use std::ops::{Deref, DerefMut}; +use std::{borrow, fmt}; + +pub type Allocator = bumpalo::Bump; + +pub type String<'allocator> = bumpalo::collections::String<'allocator>; +pub type Vec<'allocator, T> = bumpalo::collections::Vec<'allocator, T>; + +#[derive(PartialEq, Eq, PartialOrd, Ord, Hash)] +pub struct Box<'allocator, T: ?Sized>(bumpalo::boxed::Box<'allocator, T>); + +impl<'allocator, T> Box<'allocator, T> { + pub fn new_in(value: T, arena: &'allocator Allocator) -> Self { + Self(bumpalo::boxed::Box::new_in(value, arena)) + } + + pub fn into_inner(self) -> T { + bumpalo::boxed::Box::into_inner(self.0) + } +} + +impl<'allocator, T> CloneIn<'allocator> for Box<'allocator, T> +where + T: CloneIn<'allocator>, +{ + fn clone_in(&self, allocator: &'allocator Allocator) -> Self { + Self(bumpalo::boxed::Box::new_in( + self.0.as_ref().clone_in(allocator), + allocator, + )) + } +} + +impl AsRef for Box<'_, T> { + fn as_ref(&self) -> &T { + self.0.as_ref() + } +} + +impl std::fmt::Display for Box<'_, T> +where + T: Display, +{ + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + self.0.fmt(f) + } +} + +impl<'a, T: fmt::Debug + ?Sized> fmt::Debug for Box<'a, T> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt::Debug::fmt(&self.0, f) + } +} + +impl<'a, T: ?Sized> borrow::Borrow for Box<'a, T> { + fn borrow(&self) -> &T { + &self.0 + } +} + +impl<'a, T: ?Sized> borrow::BorrowMut for Box<'a, T> { + fn borrow_mut(&mut self) -> &mut T { + &mut self.0 + } +} + +impl<'a, T: ?Sized> AsMut for Box<'a, T> { + fn as_mut(&mut self) -> &mut T { + &mut self.0 + } +} + +impl<'a, T: ?Sized> Deref for Box<'a, T> { + type Target = T; + + fn deref(&self) -> &T { + &self.0 + } +} + +impl<'a, T: ?Sized> DerefMut for Box<'a, T> { + fn deref_mut(&mut self) -> &mut T { + &mut self.0 + } +} + +impl<'a, I: Iterator + ?Sized> Iterator for Box<'a, I> { + type Item = I::Item; + fn next(&mut self) -> Option { + self.0.next() + } + fn size_hint(&self) -> (usize, Option) { + self.0.size_hint() + } + fn nth(&mut self, n: usize) -> Option { + self.0.nth(n) + } + fn last(self) -> Option { + #[inline] + fn some(_: Option, x: T) -> Option { + Some(x) + } + self.fold(None, some) + } +} + +impl<'a, I: DoubleEndedIterator + ?Sized> DoubleEndedIterator for Box<'a, I> { + fn next_back(&mut self) -> Option { + self.0.next_back() + } + fn nth_back(&mut self, n: usize) -> Option { + self.0.nth_back(n) + } +} +impl<'a, I: ExactSizeIterator + ?Sized> ExactSizeIterator for Box<'a, I> { + fn len(&self) -> usize { + self.0.len() + } +} + +impl<'a, I: FusedIterator + ?Sized> FusedIterator for Box<'a, I> {} + +pub trait CloneIn<'allocator> { + #[must_use] + fn clone_in(&self, allocator: &'allocator Allocator) -> Self; +} + +impl CloneIn<'_> for T +where + T: Clone, +{ + fn clone_in(&self, _: &Allocator) -> Self { + self.clone() + } +} diff --git a/crates/ruff_benchmark/Cargo.toml b/crates/ruff_benchmark/Cargo.toml index 763a622988..1d3042e24b 100644 --- a/crates/ruff_benchmark/Cargo.toml +++ b/crates/ruff_benchmark/Cargo.toml @@ -15,9 +15,9 @@ license = { workspace = true } bench = false doctest = false -[[bench]] -name = "linter" -harness = false +#[[bench]] +#name = "linter" +#harness = false [[bench]] name = "lexer" @@ -27,13 +27,13 @@ harness = false name = "parser" harness = false -[[bench]] -name = "formatter" -harness = false +#[[bench]] +#name = "formatter" +#harness = false -[[bench]] -name = "red_knot" -harness = false +#[[bench]] +#name = "red_knot" +#harness = false [dependencies] once_cell = { workspace = true } @@ -44,13 +44,14 @@ ureq = { workspace = true } codspeed-criterion-compat = { workspace = true, default-features = false } [dev-dependencies] -ruff_db = { workspace = true } -ruff_linter = { workspace = true } +ruff_allocator = { workspace = true } +#ruff_db = { workspace = true } +#ruff_linter = { workspace = true } ruff_python_ast = { workspace = true } -ruff_python_formatter = { workspace = true } +#ruff_python_formatter = { workspace = true } ruff_python_parser = { workspace = true } ruff_python_trivia = { workspace = true } -red_knot = { workspace = true } +#red_knot = { workspace = true } [lints] workspace = true diff --git a/crates/ruff_benchmark/benches/formatter.rs b/crates/ruff_benchmark/benches/formatter.rs index 740fcf1fa3..6c1696697b 100644 --- a/crates/ruff_benchmark/benches/formatter.rs +++ b/crates/ruff_benchmark/benches/formatter.rs @@ -1,78 +1,78 @@ -use std::path::Path; - -use codspeed_criterion_compat::{ - criterion_group, criterion_main, BenchmarkId, Criterion, Throughput, -}; - -use ruff_benchmark::{TestCase, TestFile, TestFileDownloadError}; -use ruff_python_formatter::{format_module_ast, PreviewMode, PyFormatOptions}; -use ruff_python_parser::{parse, Mode}; -use ruff_python_trivia::CommentRanges; - -#[cfg(target_os = "windows")] -#[global_allocator] -static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc; - -#[cfg(all( - not(target_os = "windows"), - not(target_os = "openbsd"), - any( - target_arch = "x86_64", - target_arch = "aarch64", - target_arch = "powerpc64" - ) -))] -#[global_allocator] -static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc; - -fn create_test_cases() -> Result, TestFileDownloadError> { - Ok(vec![ - TestCase::fast(TestFile::try_download("numpy/globals.py", "https://raw.githubusercontent.com/numpy/numpy/89d64415e349ca75a25250f22b874aa16e5c0973/numpy/_globals.py")?), - TestCase::fast(TestFile::try_download("unicode/pypinyin.py", "https://raw.githubusercontent.com/mozillazg/python-pinyin/9521e47d96e3583a5477f5e43a2e82d513f27a3f/pypinyin/standard.py")?), - TestCase::normal(TestFile::try_download( - "pydantic/types.py", - "https://raw.githubusercontent.com/pydantic/pydantic/83b3c49e99ceb4599d9286a3d793cea44ac36d4b/pydantic/types.py", - )?), - TestCase::normal(TestFile::try_download("numpy/ctypeslib.py", "https://raw.githubusercontent.com/numpy/numpy/e42c9503a14d66adfd41356ef5640c6975c45218/numpy/ctypeslib.py")?), - TestCase::slow(TestFile::try_download( - "large/dataset.py", - "https://raw.githubusercontent.com/DHI/mikeio/b7d26418f4db2909b0aa965253dbe83194d7bb5b/tests/test_dataset.py", - )?), - ]) -} - -fn benchmark_formatter(criterion: &mut Criterion) { - let mut group = criterion.benchmark_group("formatter"); - let test_cases = create_test_cases().unwrap(); - - for case in test_cases { - group.throughput(Throughput::Bytes(case.code().len() as u64)); - - group.bench_with_input( - BenchmarkId::from_parameter(case.name()), - &case, - |b, case| { - // Parse the source. - let parsed = - parse(case.code(), Mode::Module).expect("Input should be a valid Python code"); - - let comment_ranges = CommentRanges::from(parsed.tokens()); - - b.iter(|| { - let options = PyFormatOptions::from_extension(Path::new(case.name())) - .with_preview(PreviewMode::Enabled); - let formatted = - format_module_ast(&parsed, &comment_ranges, case.code(), options) - .expect("Formatting to succeed"); - - formatted.print().expect("Printing to succeed") - }); - }, - ); - } - - group.finish(); -} - -criterion_group!(formatter, benchmark_formatter); -criterion_main!(formatter); +// use std::path::Path; +// +// use codspeed_criterion_compat::{ +// criterion_group, criterion_main, BenchmarkId, Criterion, Throughput, +// }; +// +// use ruff_benchmark::{TestCase, TestFile, TestFileDownloadError}; +// use ruff_python_formatter::{format_module_ast, PreviewMode, PyFormatOptions}; +// use ruff_python_parser::{parse, Mode}; +// use ruff_python_trivia::CommentRanges; +// +// #[cfg(target_os = "windows")] +// #[global_allocator] +// static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc; +// +// #[cfg(all( +// not(target_os = "windows"), +// not(target_os = "openbsd"), +// any( +// target_arch = "x86_64", +// target_arch = "aarch64", +// target_arch = "powerpc64" +// ) +// ))] +// #[global_allocator] +// static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc; +// +// fn create_test_cases() -> Result, TestFileDownloadError> { +// Ok(vec![ +// TestCase::fast(TestFile::try_download("numpy/globals.py", "https://raw.githubusercontent.com/numpy/numpy/89d64415e349ca75a25250f22b874aa16e5c0973/numpy/_globals.py")?), +// TestCase::fast(TestFile::try_download("unicode/pypinyin.py", "https://raw.githubusercontent.com/mozillazg/python-pinyin/9521e47d96e3583a5477f5e43a2e82d513f27a3f/pypinyin/standard.py")?), +// TestCase::normal(TestFile::try_download( +// "pydantic/types.py", +// "https://raw.githubusercontent.com/pydantic/pydantic/83b3c49e99ceb4599d9286a3d793cea44ac36d4b/pydantic/types.py", +// )?), +// TestCase::normal(TestFile::try_download("numpy/ctypeslib.py", "https://raw.githubusercontent.com/numpy/numpy/e42c9503a14d66adfd41356ef5640c6975c45218/numpy/ctypeslib.py")?), +// TestCase::slow(TestFile::try_download( +// "large/dataset.py", +// "https://raw.githubusercontent.com/DHI/mikeio/b7d26418f4db2909b0aa965253dbe83194d7bb5b/tests/test_dataset.py", +// )?), +// ]) +// } +// +// fn benchmark_formatter(criterion: &mut Criterion) { +// let mut group = criterion.benchmark_group("formatter"); +// let test_cases = create_test_cases().unwrap(); +// +// for case in test_cases { +// group.throughput(Throughput::Bytes(case.code().len() as u64)); +// +// group.bench_with_input( +// BenchmarkId::from_parameter(case.name()), +// &case, +// |b, case| { +// // Parse the source. +// let parsed = +// parse(case.code(), Mode::Module).expect("Input should be a valid Python code"); +// +// let comment_ranges = CommentRanges::from(parsed.tokens()); +// +// b.iter(|| { +// let options = PyFormatOptions::from_extension(Path::new(case.name())) +// .with_preview(PreviewMode::Enabled); +// let formatted = +// format_module_ast(&parsed, &comment_ranges, case.code(), options) +// .expect("Formatting to succeed"); +// +// formatted.print().expect("Printing to succeed") +// }); +// }, +// ); +// } +// +// group.finish(); +// } +// +// criterion_group!(formatter, benchmark_formatter); +// criterion_main!(formatter); diff --git a/crates/ruff_benchmark/benches/linter.rs b/crates/ruff_benchmark/benches/linter.rs index 9da2437ced..7e1d13a5af 100644 --- a/crates/ruff_benchmark/benches/linter.rs +++ b/crates/ruff_benchmark/benches/linter.rs @@ -1,137 +1,137 @@ -use codspeed_criterion_compat::{ - self as criterion, criterion_group, criterion_main, BenchmarkGroup, BenchmarkId, Criterion, - Throughput, -}; -use criterion::measurement; -use ruff_benchmark::{TestCase, TestFile, TestFileDownloadError}; -use ruff_linter::linter::{lint_only, ParseSource}; -use ruff_linter::rule_selector::PreviewOptions; -use ruff_linter::settings::rule_table::RuleTable; -use ruff_linter::settings::types::PreviewMode; -use ruff_linter::settings::{flags, LinterSettings}; -use ruff_linter::source_kind::SourceKind; -use ruff_linter::{registry::Rule, RuleSelector}; -use ruff_python_ast::PySourceType; -use ruff_python_parser::parse_module; - -#[cfg(target_os = "windows")] -#[global_allocator] -static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc; - -#[cfg(all( - not(target_os = "windows"), - not(target_os = "openbsd"), - any( - target_arch = "x86_64", - target_arch = "aarch64", - target_arch = "powerpc64" - ) -))] -#[global_allocator] -static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc; - -fn create_test_cases() -> Result, TestFileDownloadError> { - Ok(vec![ - TestCase::fast(TestFile::try_download("numpy/globals.py", "https://raw.githubusercontent.com/numpy/numpy/89d64415e349ca75a25250f22b874aa16e5c0973/numpy/_globals.py")?), - TestCase::fast(TestFile::try_download("unicode/pypinyin.py", "https://raw.githubusercontent.com/mozillazg/python-pinyin/9521e47d96e3583a5477f5e43a2e82d513f27a3f/pypinyin/standard.py")?), - TestCase::normal(TestFile::try_download( - "pydantic/types.py", - "https://raw.githubusercontent.com/pydantic/pydantic/83b3c49e99ceb4599d9286a3d793cea44ac36d4b/pydantic/types.py", - )?), - TestCase::normal(TestFile::try_download("numpy/ctypeslib.py", "https://raw.githubusercontent.com/numpy/numpy/e42c9503a14d66adfd41356ef5640c6975c45218/numpy/ctypeslib.py")?), - TestCase::slow(TestFile::try_download( - "large/dataset.py", - "https://raw.githubusercontent.com/DHI/mikeio/b7d26418f4db2909b0aa965253dbe83194d7bb5b/tests/test_dataset.py", - )?), - ]) -} - -fn benchmark_linter(mut group: BenchmarkGroup, settings: &LinterSettings) { - let test_cases = create_test_cases().unwrap(); - - for case in test_cases { - group.throughput(Throughput::Bytes(case.code().len() as u64)); - - group.bench_with_input( - BenchmarkId::from_parameter(case.name()), - &case, - |b, case| { - // Parse the source. - let parsed = - parse_module(case.code()).expect("Input should be a valid Python code"); - - b.iter_batched( - || parsed.clone(), - |parsed| { - let path = case.path(); - let result = lint_only( - &path, - None, - settings, - flags::Noqa::Enabled, - &SourceKind::Python(case.code().to_string()), - PySourceType::from(path.as_path()), - ParseSource::Precomputed(parsed), - ); - - // Assert that file contains no parse errors - assert!(!result.has_syntax_error); - }, - criterion::BatchSize::SmallInput, - ); - }, - ); - } - - group.finish(); -} - -fn benchmark_default_rules(criterion: &mut Criterion) { - let group = criterion.benchmark_group("linter/default-rules"); - benchmark_linter(group, &LinterSettings::default()); -} - -/// Disables IO based rules because they are a source of flakiness -fn disable_io_rules(rules: &mut RuleTable) { - rules.disable(Rule::ShebangMissingExecutableFile); - rules.disable(Rule::ShebangNotExecutable); -} - -fn benchmark_all_rules(criterion: &mut Criterion) { - let mut rules: RuleTable = RuleSelector::All - .rules(&PreviewOptions { - mode: PreviewMode::Disabled, - require_explicit: false, - }) - .collect(); - - disable_io_rules(&mut rules); - - let settings = LinterSettings { - rules, - ..LinterSettings::default() - }; - - let group = criterion.benchmark_group("linter/all-rules"); - benchmark_linter(group, &settings); -} - -fn benchmark_preview_rules(criterion: &mut Criterion) { - let mut rules: RuleTable = RuleSelector::All.all_rules().collect(); - - disable_io_rules(&mut rules); - - let settings = LinterSettings { - rules, - preview: PreviewMode::Enabled, - ..LinterSettings::default() - }; - - let group = criterion.benchmark_group("linter/all-with-preview-rules"); - benchmark_linter(group, &settings); -} - -criterion_group!(default_rules, benchmark_default_rules); -criterion_group!(all_rules, benchmark_all_rules); -criterion_group!(preview_rules, benchmark_preview_rules); -criterion_main!(default_rules, all_rules, preview_rules); +// use codspeed_criterion_compat::{ +// self as criterion, criterion_group, criterion_main, BenchmarkGroup, BenchmarkId, Criterion, +// Throughput, +// }; +// use criterion::measurement; +// use ruff_benchmark::{TestCase, TestFile, TestFileDownloadError}; +// use ruff_linter::linter::{lint_only, ParseSource}; +// use ruff_linter::rule_selector::PreviewOptions; +// use ruff_linter::settings::rule_table::RuleTable; +// use ruff_linter::settings::types::PreviewMode; +// use ruff_linter::settings::{flags, LinterSettings}; +// use ruff_linter::source_kind::SourceKind; +// use ruff_linter::{registry::Rule, RuleSelector}; +// use ruff_python_ast::PySourceType; +// use ruff_python_parser::parse_module; +// +// #[cfg(target_os = "windows")] +// #[global_allocator] +// static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc; +// +// #[cfg(all( +// not(target_os = "windows"), +// not(target_os = "openbsd"), +// any( +// target_arch = "x86_64", +// target_arch = "aarch64", +// target_arch = "powerpc64" +// ) +// ))] +// #[global_allocator] +// static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc; +// +// fn create_test_cases() -> Result, TestFileDownloadError> { +// Ok(vec![ +// TestCase::fast(TestFile::try_download("numpy/globals.py", "https://raw.githubusercontent.com/numpy/numpy/89d64415e349ca75a25250f22b874aa16e5c0973/numpy/_globals.py")?), +// TestCase::fast(TestFile::try_download("unicode/pypinyin.py", "https://raw.githubusercontent.com/mozillazg/python-pinyin/9521e47d96e3583a5477f5e43a2e82d513f27a3f/pypinyin/standard.py")?), +// TestCase::normal(TestFile::try_download( +// "pydantic/types.py", +// "https://raw.githubusercontent.com/pydantic/pydantic/83b3c49e99ceb4599d9286a3d793cea44ac36d4b/pydantic/types.py", +// )?), +// TestCase::normal(TestFile::try_download("numpy/ctypeslib.py", "https://raw.githubusercontent.com/numpy/numpy/e42c9503a14d66adfd41356ef5640c6975c45218/numpy/ctypeslib.py")?), +// TestCase::slow(TestFile::try_download( +// "large/dataset.py", +// "https://raw.githubusercontent.com/DHI/mikeio/b7d26418f4db2909b0aa965253dbe83194d7bb5b/tests/test_dataset.py", +// )?), +// ]) +// } +// +// fn benchmark_linter(mut group: BenchmarkGroup, settings: &LinterSettings) { +// let test_cases = create_test_cases().unwrap(); +// +// for case in test_cases { +// group.throughput(Throughput::Bytes(case.code().len() as u64)); +// +// group.bench_with_input( +// BenchmarkId::from_parameter(case.name()), +// &case, +// |b, case| { +// // Parse the source. +// let parsed = +// parse_module(case.code()).expect("Input should be a valid Python code"); +// +// b.iter_batched( +// || parsed.clone(), +// |parsed| { +// let path = case.path(); +// let result = lint_only( +// &path, +// None, +// settings, +// flags::Noqa::Enabled, +// &SourceKind::Python(case.code().to_string()), +// PySourceType::from(path.as_path()), +// ParseSource::Precomputed(parsed), +// ); +// +// // Assert that file contains no parse errors +// assert!(!result.has_syntax_error); +// }, +// criterion::BatchSize::SmallInput, +// ); +// }, +// ); +// } +// +// group.finish(); +// } +// +// fn benchmark_default_rules(criterion: &mut Criterion) { +// let group = criterion.benchmark_group("linter/default-rules"); +// benchmark_linter(group, &LinterSettings::default()); +// } +// +// /// Disables IO based rules because they are a source of flakiness +// fn disable_io_rules(rules: &mut RuleTable) { +// rules.disable(Rule::ShebangMissingExecutableFile); +// rules.disable(Rule::ShebangNotExecutable); +// } +// +// fn benchmark_all_rules(criterion: &mut Criterion) { +// let mut rules: RuleTable = RuleSelector::All +// .rules(&PreviewOptions { +// mode: PreviewMode::Disabled, +// require_explicit: false, +// }) +// .collect(); +// +// disable_io_rules(&mut rules); +// +// let settings = LinterSettings { +// rules, +// ..LinterSettings::default() +// }; +// +// let group = criterion.benchmark_group("linter/all-rules"); +// benchmark_linter(group, &settings); +// } +// +// fn benchmark_preview_rules(criterion: &mut Criterion) { +// let mut rules: RuleTable = RuleSelector::All.all_rules().collect(); +// +// disable_io_rules(&mut rules); +// +// let settings = LinterSettings { +// rules, +// preview: PreviewMode::Enabled, +// ..LinterSettings::default() +// }; +// +// let group = criterion.benchmark_group("linter/all-with-preview-rules"); +// benchmark_linter(group, &settings); +// } +// +// criterion_group!(default_rules, benchmark_default_rules); +// criterion_group!(all_rules, benchmark_all_rules); +// criterion_group!(preview_rules, benchmark_preview_rules); +// criterion_main!(default_rules, all_rules, preview_rules); diff --git a/crates/ruff_benchmark/benches/parser.rs b/crates/ruff_benchmark/benches/parser.rs index f12526d072..c27a5fa74e 100644 --- a/crates/ruff_benchmark/benches/parser.rs +++ b/crates/ruff_benchmark/benches/parser.rs @@ -2,6 +2,7 @@ use codspeed_criterion_compat::{ criterion_group, criterion_main, measurement::WallTime, BenchmarkId, Criterion, Throughput, }; +use ruff_allocator::Allocator; use ruff_benchmark::{TestCase, TestFile, TestFileDownloadError}; use ruff_python_ast::statement_visitor::{walk_stmt, StatementVisitor}; use ruff_python_ast::Stmt; @@ -43,8 +44,8 @@ struct CountVisitor { count: usize, } -impl<'a> StatementVisitor<'a> for CountVisitor { - fn visit_stmt(&mut self, stmt: &'a Stmt) { +impl<'a, 'ast> StatementVisitor<'a, 'ast> for CountVisitor { + fn visit_stmt(&mut self, stmt: &'a Stmt<'ast>) { walk_stmt(self, stmt); self.count += 1; } @@ -61,7 +62,8 @@ fn benchmark_parser(criterion: &mut Criterion) { &case, |b, case| { b.iter(|| { - let parsed = parse_module(case.code()) + let allocator = Allocator::new(); + let parsed = parse_module(case.code(), &allocator) .expect("Input should be a valid Python code") .into_suite(); diff --git a/crates/ruff_benchmark/benches/red_knot.rs b/crates/ruff_benchmark/benches/red_knot.rs index 07abdafa6b..5448f017b9 100644 --- a/crates/ruff_benchmark/benches/red_knot.rs +++ b/crates/ruff_benchmark/benches/red_knot.rs @@ -1,173 +1,173 @@ -#![allow(clippy::disallowed_names)] - -use codspeed_criterion_compat::{criterion_group, criterion_main, BatchSize, Criterion}; - -use red_knot::db::RootDatabase; -use red_knot::workspace::WorkspaceMetadata; -use ruff_db::files::{system_path_to_file, vendored_path_to_file, File}; -use ruff_db::parsed::parsed_module; -use ruff_db::program::{ProgramSettings, SearchPathSettings, TargetVersion}; -use ruff_db::system::{MemoryFileSystem, SystemPath, TestSystem}; -use ruff_db::vendored::VendoredPath; -use ruff_db::Upcast; - -static FOO_CODE: &str = r#" -import typing - -from bar import Bar - -class Foo(Bar): - def foo() -> object: - return "foo" - - @typing.override - def bar() -> object: - return "foo_bar" -"#; - -static BAR_CODE: &str = r#" -class Bar: - def bar() -> object: - return "bar" - - def random(arg: int) -> int: - if arg == 1: - return 48472783 - if arg < 10: - return 20 - while arg < 50: - arg += 1 - return 36673 -"#; - -static TYPING_CODE: &str = r#" -def override(): ... -"#; - -struct Case { - db: RootDatabase, - fs: MemoryFileSystem, - foo: File, - bar: File, - typing: File, - builtins: File, -} - -fn setup_case() -> Case { - let system = TestSystem::default(); - let fs = system.memory_file_system().clone(); - let foo_path = SystemPath::new("/src/foo.py"); - let bar_path = SystemPath::new("/src/bar.py"); - let typing_path = SystemPath::new("/src/typing.pyi"); - let builtins_path = VendoredPath::new("stdlib/builtins.pyi"); - fs.write_files([ - (foo_path, FOO_CODE), - (bar_path, BAR_CODE), - (typing_path, TYPING_CODE), - ]) - .unwrap(); - - let workspace_root = SystemPath::new("/src"); - let metadata = WorkspaceMetadata::from_path(workspace_root, &system).unwrap(); - let settings = ProgramSettings { - target_version: TargetVersion::default(), - search_paths: SearchPathSettings { - extra_paths: vec![], - workspace_root: workspace_root.to_path_buf(), - site_packages: None, - custom_typeshed: None, - }, - }; - - let mut db = RootDatabase::new(metadata, settings, system); - let foo = system_path_to_file(&db, foo_path).unwrap(); - - db.workspace().open_file(&mut db, foo); - - let bar = system_path_to_file(&db, bar_path).unwrap(); - let typing = system_path_to_file(&db, typing_path).unwrap(); - let builtins = vendored_path_to_file(&db, builtins_path).unwrap(); - - Case { - db, - fs, - foo, - bar, - typing, - builtins, - } -} - -fn benchmark_without_parse(criterion: &mut Criterion) { - criterion.bench_function("red_knot_check_file[without_parse]", |b| { - b.iter_batched_ref( - || { - let case = setup_case(); - // Pre-parse the module to only measure the semantic time. - parsed_module(case.db.upcast(), case.foo); - parsed_module(case.db.upcast(), case.bar); - parsed_module(case.db.upcast(), case.typing); - parsed_module(case.db.upcast(), case.builtins); - case - }, - |case| { - let Case { db, foo, .. } = case; - let result = db.check_file(*foo).unwrap(); - - assert_eq!(result.as_slice(), [] as [String; 0]); - }, - BatchSize::SmallInput, - ); - }); -} - -fn benchmark_incremental(criterion: &mut Criterion) { - criterion.bench_function("red_knot_check_file[incremental]", |b| { - b.iter_batched_ref( - || { - let mut case = setup_case(); - case.db.check_file(case.foo).unwrap(); - - case.fs - .write_file( - SystemPath::new("/src/bar.py"), - format!("{BAR_CODE}\n# A comment\n"), - ) - .unwrap(); - - case.bar.sync(&mut case.db); - case - }, - |case| { - let Case { db, foo, .. } = case; - let result = db.check_file(*foo).unwrap(); - - assert_eq!(result.as_slice(), [] as [String; 0]); - }, - BatchSize::SmallInput, - ); - }); -} - -fn benchmark_cold(criterion: &mut Criterion) { - criterion.bench_function("red_knot_check_file[cold]", |b| { - b.iter_batched_ref( - setup_case, - |case| { - let Case { db, foo, .. } = case; - let result = db.check_file(*foo).unwrap(); - - assert_eq!(result.as_slice(), [] as [String; 0]); - }, - BatchSize::SmallInput, - ); - }); -} - -criterion_group!( - check_file, - benchmark_cold, - benchmark_without_parse, - benchmark_incremental -); -criterion_main!(check_file); +// #![allow(clippy::disallowed_names)] +// +// use codspeed_criterion_compat::{criterion_group, criterion_main, BatchSize, Criterion}; +// +// use red_knot::db::RootDatabase; +// use red_knot::workspace::WorkspaceMetadata; +// use ruff_db::files::{system_path_to_file, vendored_path_to_file, File}; +// use ruff_db::parsed::parsed_module; +// use ruff_db::program::{ProgramSettings, SearchPathSettings, TargetVersion}; +// use ruff_db::system::{MemoryFileSystem, SystemPath, TestSystem}; +// use ruff_db::vendored::VendoredPath; +// use ruff_db::Upcast; +// +// static FOO_CODE: &str = r#" +// import typing +// +// from bar import Bar +// +// class Foo(Bar): +// def foo() -> object: +// return "foo" +// +// @typing.override +// def bar() -> object: +// return "foo_bar" +// "#; +// +// static BAR_CODE: &str = r#" +// class Bar: +// def bar() -> object: +// return "bar" +// +// def random(arg: int) -> int: +// if arg == 1: +// return 48472783 +// if arg < 10: +// return 20 +// while arg < 50: +// arg += 1 +// return 36673 +// "#; +// +// static TYPING_CODE: &str = r#" +// def override(): ... +// "#; +// +// struct Case { +// db: RootDatabase, +// fs: MemoryFileSystem, +// foo: File, +// bar: File, +// typing: File, +// builtins: File, +// } +// +// fn setup_case() -> Case { +// let system = TestSystem::default(); +// let fs = system.memory_file_system().clone(); +// let foo_path = SystemPath::new("/src/foo.py"); +// let bar_path = SystemPath::new("/src/bar.py"); +// let typing_path = SystemPath::new("/src/typing.pyi"); +// let builtins_path = VendoredPath::new("stdlib/builtins.pyi"); +// fs.write_files([ +// (foo_path, FOO_CODE), +// (bar_path, BAR_CODE), +// (typing_path, TYPING_CODE), +// ]) +// .unwrap(); +// +// let workspace_root = SystemPath::new("/src"); +// let metadata = WorkspaceMetadata::from_path(workspace_root, &system).unwrap(); +// let settings = ProgramSettings { +// target_version: TargetVersion::default(), +// search_paths: SearchPathSettings { +// extra_paths: vec![], +// workspace_root: workspace_root.to_path_buf(), +// site_packages: None, +// custom_typeshed: None, +// }, +// }; +// +// let mut db = RootDatabase::new(metadata, settings, system); +// let foo = system_path_to_file(&db, foo_path).unwrap(); +// +// db.workspace().open_file(&mut db, foo); +// +// let bar = system_path_to_file(&db, bar_path).unwrap(); +// let typing = system_path_to_file(&db, typing_path).unwrap(); +// let builtins = vendored_path_to_file(&db, builtins_path).unwrap(); +// +// Case { +// db, +// fs, +// foo, +// bar, +// typing, +// builtins, +// } +// } +// +// fn benchmark_without_parse(criterion: &mut Criterion) { +// criterion.bench_function("red_knot_check_file[without_parse]", |b| { +// b.iter_batched_ref( +// || { +// let case = setup_case(); +// // Pre-parse the module to only measure the semantic time. +// parsed_module(case.db.upcast(), case.foo); +// parsed_module(case.db.upcast(), case.bar); +// parsed_module(case.db.upcast(), case.typing); +// parsed_module(case.db.upcast(), case.builtins); +// case +// }, +// |case| { +// let Case { db, foo, .. } = case; +// let result = db.check_file(*foo).unwrap(); +// +// assert_eq!(result.as_slice(), [] as [String; 0]); +// }, +// BatchSize::SmallInput, +// ); +// }); +// } +// +// fn benchmark_incremental(criterion: &mut Criterion) { +// criterion.bench_function("red_knot_check_file[incremental]", |b| { +// b.iter_batched_ref( +// || { +// let mut case = setup_case(); +// case.db.check_file(case.foo).unwrap(); +// +// case.fs +// .write_file( +// SystemPath::new("/src/bar.py"), +// format!("{BAR_CODE}\n# A comment\n"), +// ) +// .unwrap(); +// +// case.bar.sync(&mut case.db); +// case +// }, +// |case| { +// let Case { db, foo, .. } = case; +// let result = db.check_file(*foo).unwrap(); +// +// assert_eq!(result.as_slice(), [] as [String; 0]); +// }, +// BatchSize::SmallInput, +// ); +// }); +// } +// +// fn benchmark_cold(criterion: &mut Criterion) { +// criterion.bench_function("red_knot_check_file[cold]", |b| { +// b.iter_batched_ref( +// setup_case, +// |case| { +// let Case { db, foo, .. } = case; +// let result = db.check_file(*foo).unwrap(); +// +// assert_eq!(result.as_slice(), [] as [String; 0]); +// }, +// BatchSize::SmallInput, +// ); +// }); +// } +// +// criterion_group!( +// check_file, +// benchmark_cold, +// benchmark_without_parse, +// benchmark_incremental +// ); +// criterion_main!(check_file); diff --git a/crates/ruff_python_ast/Cargo.toml b/crates/ruff_python_ast/Cargo.toml index 401f56975c..7acd7a2a91 100644 --- a/crates/ruff_python_ast/Cargo.toml +++ b/crates/ruff_python_ast/Cargo.toml @@ -21,6 +21,7 @@ ruff_text_size = { workspace = true } aho-corasick = { workspace = true } bitflags = { workspace = true } +ruff_allocator = { workspace = true } is-macro = { workspace = true } itertools = { workspace = true } once_cell = { workspace = true } diff --git a/crates/ruff_python_ast/src/comparable.rs b/crates/ruff_python_ast/src/comparable.rs index 369bc84aaf..ef9feb9f51 100644 --- a/crates/ruff_python_ast/src/comparable.rs +++ b/crates/ruff_python_ast/src/comparable.rs @@ -125,23 +125,23 @@ pub struct ComparableAlias<'a> { asname: Option<&'a str>, } -impl<'a> From<&'a ast::Alias> for ComparableAlias<'a> { - fn from(alias: &'a ast::Alias) -> Self { +impl<'a, 'ast> From<&'a ast::Alias<'ast>> for ComparableAlias<'ast> { + fn from(alias: &'a ast::Alias<'ast>) -> Self { Self { name: alias.name.as_str(), - asname: alias.asname.as_deref(), + asname: alias.asname.as_ref().map(|name| name.as_str()), } } } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct ComparableWithItem<'a> { - context_expr: ComparableExpr<'a>, - optional_vars: Option>, +pub struct ComparableWithItem<'a, 'ast> { + context_expr: ComparableExpr<'a, 'ast>, + optional_vars: Option>, } -impl<'a> From<&'a ast::WithItem> for ComparableWithItem<'a> { - fn from(with_item: &'a ast::WithItem) -> Self { +impl<'a, 'ast> From<&'a ast::WithItem<'ast>> for ComparableWithItem<'a, 'ast> { + fn from(with_item: &'a ast::WithItem<'ast>) -> Self { Self { context_expr: (&with_item.context_expr).into(), optional_vars: with_item.optional_vars.as_ref().map(Into::into), @@ -150,13 +150,13 @@ impl<'a> From<&'a ast::WithItem> for ComparableWithItem<'a> { } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct ComparablePatternArguments<'a> { - patterns: Vec>, - keywords: Vec>, +pub struct ComparablePatternArguments<'a, 'ast> { + patterns: Vec>, + keywords: Vec>, } -impl<'a> From<&'a ast::PatternArguments> for ComparablePatternArguments<'a> { - fn from(parameters: &'a ast::PatternArguments) -> Self { +impl<'a, 'ast> From<&'a ast::PatternArguments<'ast>> for ComparablePatternArguments<'a, 'ast> { + fn from(parameters: &'a ast::PatternArguments<'ast>) -> Self { Self { patterns: parameters.patterns.iter().map(Into::into).collect(), keywords: parameters.keywords.iter().map(Into::into).collect(), @@ -165,13 +165,13 @@ impl<'a> From<&'a ast::PatternArguments> for ComparablePatternArguments<'a> { } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct ComparablePatternKeyword<'a> { - attr: &'a str, - pattern: ComparablePattern<'a>, +pub struct ComparablePatternKeyword<'a, 'ast> { + attr: &'ast str, + pattern: ComparablePattern<'a, 'ast>, } -impl<'a> From<&'a ast::PatternKeyword> for ComparablePatternKeyword<'a> { - fn from(keyword: &'a ast::PatternKeyword) -> Self { +impl<'a, 'ast> From<&'a ast::PatternKeyword<'ast>> for ComparablePatternKeyword<'a, 'ast> { + fn from(keyword: &'a ast::PatternKeyword<'ast>) -> Self { Self { attr: keyword.attr.as_str(), pattern: (&keyword.pattern).into(), @@ -180,8 +180,8 @@ impl<'a> From<&'a ast::PatternKeyword> for ComparablePatternKeyword<'a> { } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct PatternMatchValue<'a> { - value: ComparableExpr<'a>, +pub struct PatternMatchValue<'a, 'ast> { + value: ComparableExpr<'a, 'ast>, } #[derive(Debug, PartialEq, Eq, Hash)] @@ -190,54 +190,54 @@ pub struct PatternMatchSingleton { } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct PatternMatchSequence<'a> { - patterns: Vec>, +pub struct PatternMatchSequence<'a, 'ast> { + patterns: Vec>, } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct PatternMatchMapping<'a> { - keys: Vec>, - patterns: Vec>, - rest: Option<&'a str>, +pub struct PatternMatchMapping<'a, 'ast> { + keys: Vec>, + patterns: Vec>, + rest: Option<&'ast str>, } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct PatternMatchClass<'a> { - cls: ComparableExpr<'a>, - arguments: ComparablePatternArguments<'a>, +pub struct PatternMatchClass<'a, 'ast> { + cls: ComparableExpr<'a, 'ast>, + arguments: ComparablePatternArguments<'a, 'ast>, } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct PatternMatchStar<'a> { - name: Option<&'a str>, +pub struct PatternMatchStar<'ast> { + name: Option<&'ast str>, } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct PatternMatchAs<'a> { - pattern: Option>>, - name: Option<&'a str>, +pub struct PatternMatchAs<'a, 'ast> { + pattern: Option>>, + name: Option<&'ast str>, } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct PatternMatchOr<'a> { - patterns: Vec>, +pub struct PatternMatchOr<'a, 'ast> { + patterns: Vec>, } #[allow(clippy::enum_variant_names)] #[derive(Debug, PartialEq, Eq, Hash)] -pub enum ComparablePattern<'a> { - MatchValue(PatternMatchValue<'a>), +pub enum ComparablePattern<'a, 'ast> { + MatchValue(PatternMatchValue<'a, 'ast>), MatchSingleton(PatternMatchSingleton), - MatchSequence(PatternMatchSequence<'a>), - MatchMapping(PatternMatchMapping<'a>), - MatchClass(PatternMatchClass<'a>), - MatchStar(PatternMatchStar<'a>), - MatchAs(PatternMatchAs<'a>), - MatchOr(PatternMatchOr<'a>), + MatchSequence(PatternMatchSequence<'a, 'ast>), + MatchMapping(PatternMatchMapping<'a, 'ast>), + MatchClass(PatternMatchClass<'a, 'ast>), + MatchStar(PatternMatchStar<'ast>), + MatchAs(PatternMatchAs<'a, 'ast>), + MatchOr(PatternMatchOr<'a, 'ast>), } -impl<'a> From<&'a ast::Pattern> for ComparablePattern<'a> { - fn from(pattern: &'a ast::Pattern) -> Self { +impl<'a, 'ast> From<&'a ast::Pattern<'ast>> for ComparablePattern<'a, 'ast> { + fn from(pattern: &'a ast::Pattern<'ast>) -> Self { match pattern { ast::Pattern::MatchValue(ast::PatternMatchValue { value, .. }) => { Self::MatchValue(PatternMatchValue { @@ -262,7 +262,7 @@ impl<'a> From<&'a ast::Pattern> for ComparablePattern<'a> { }) => Self::MatchMapping(PatternMatchMapping { keys: keys.iter().map(Into::into).collect(), patterns: patterns.iter().map(Into::into).collect(), - rest: rest.as_deref(), + rest: rest.as_ref().map(|rest| rest.as_str()), }), ast::Pattern::MatchClass(ast::PatternMatchClass { cls, arguments, .. }) => { Self::MatchClass(PatternMatchClass { @@ -272,13 +272,13 @@ impl<'a> From<&'a ast::Pattern> for ComparablePattern<'a> { } ast::Pattern::MatchStar(ast::PatternMatchStar { name, .. }) => { Self::MatchStar(PatternMatchStar { - name: name.as_deref(), + name: name.as_ref().map(|name| name.as_str()), }) } ast::Pattern::MatchAs(ast::PatternMatchAs { pattern, name, .. }) => { Self::MatchAs(PatternMatchAs { - pattern: pattern.as_ref().map(Into::into), - name: name.as_deref(), + pattern: pattern.as_ref().map(|pattern| pattern.into()), + name: name.as_ref().map(|name| name.as_str()), }) } ast::Pattern::MatchOr(ast::PatternMatchOr { patterns, .. }) => { @@ -290,21 +290,23 @@ impl<'a> From<&'a ast::Pattern> for ComparablePattern<'a> { } } -impl<'a> From<&'a Box> for Box> { - fn from(pattern: &'a Box) -> Self { - Box::new((pattern.as_ref()).into()) +impl<'a, 'ast> From<&'a ruff_allocator::Box<'ast, ast::Pattern<'ast>>> + for Box> +{ + fn from(pattern: &'a ruff_allocator::Box<'a, ast::Pattern<'ast>>) -> Self { + Box::new((&**pattern).into()) } } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct ComparableMatchCase<'a> { - pattern: ComparablePattern<'a>, - guard: Option>, - body: Vec>, +pub struct ComparableMatchCase<'a, 'ast> { + pattern: ComparablePattern<'a, 'ast>, + guard: Option>, + body: Vec>, } -impl<'a> From<&'a ast::MatchCase> for ComparableMatchCase<'a> { - fn from(match_case: &'a ast::MatchCase) -> Self { +impl<'a, 'ast> From<&'a ast::MatchCase<'ast>> for ComparableMatchCase<'a, 'ast> { + fn from(match_case: &'a ast::MatchCase<'ast>) -> Self { Self { pattern: (&match_case.pattern).into(), guard: match_case.guard.as_ref().map(Into::into), @@ -314,12 +316,12 @@ impl<'a> From<&'a ast::MatchCase> for ComparableMatchCase<'a> { } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct ComparableDecorator<'a> { - expression: ComparableExpr<'a>, +pub struct ComparableDecorator<'a, 'ast> { + expression: ComparableExpr<'a, 'ast>, } -impl<'a> From<&'a ast::Decorator> for ComparableDecorator<'a> { - fn from(decorator: &'a ast::Decorator) -> Self { +impl<'a, 'ast> From<&'a ast::Decorator<'ast>> for ComparableDecorator<'a, 'ast> { + fn from(decorator: &'a ast::Decorator<'ast>) -> Self { Self { expression: (&decorator.expression).into(), } @@ -350,10 +352,10 @@ pub enum ComparableNumber<'a> { Complex { real: u64, imag: u64 }, } -impl<'a> From<&'a ast::Number> for ComparableNumber<'a> { +impl<'a, 'ast> From<&'a ast::Number> for ComparableNumber<'a> { fn from(number: &'a ast::Number) -> Self { match number { - ast::Number::Int(value) => Self::Int(value), + ast::Number::Int(value) => Self::Int(&value), ast::Number::Float(value) => Self::Float(value.to_bits()), ast::Number::Complex { real, imag } => Self::Complex { real: real.to_bits(), @@ -364,13 +366,13 @@ impl<'a> From<&'a ast::Number> for ComparableNumber<'a> { } #[derive(Debug, Default, PartialEq, Eq, Hash)] -pub struct ComparableArguments<'a> { - args: Vec>, - keywords: Vec>, +pub struct ComparableArguments<'a, 'ast> { + args: Vec>, + keywords: Vec>, } -impl<'a> From<&'a ast::Arguments> for ComparableArguments<'a> { - fn from(arguments: &'a ast::Arguments) -> Self { +impl<'a, 'ast> From<&'a ast::Arguments<'ast>> for ComparableArguments<'a, 'ast> { + fn from(arguments: &'a ast::Arguments<'ast>) -> Self { Self { args: arguments.args.iter().map(Into::into).collect(), keywords: arguments.keywords.iter().map(Into::into).collect(), @@ -378,23 +380,25 @@ impl<'a> From<&'a ast::Arguments> for ComparableArguments<'a> { } } -impl<'a> From<&'a Box> for ComparableArguments<'a> { - fn from(arguments: &'a Box) -> Self { +impl<'a, 'ast> From<&'a ruff_allocator::Box<'ast, ast::Arguments<'ast>>> + for ComparableArguments<'a, 'ast> +{ + fn from(arguments: &'a ruff_allocator::Box<'a, ast::Arguments<'ast>>) -> Self { (arguments.as_ref()).into() } } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct ComparableParameters<'a> { - posonlyargs: Vec>, - args: Vec>, - vararg: Option>, - kwonlyargs: Vec>, - kwarg: Option>, +pub struct ComparableParameters<'a, 'ast> { + posonlyargs: Vec>, + args: Vec>, + vararg: Option>, + kwonlyargs: Vec>, + kwarg: Option>, } -impl<'a> From<&'a ast::Parameters> for ComparableParameters<'a> { - fn from(parameters: &'a ast::Parameters) -> Self { +impl<'a, 'ast> From<&'a ast::Parameters<'ast>> for ComparableParameters<'a, 'ast> { + fn from(parameters: &'a ast::Parameters<'ast>) -> Self { Self { posonlyargs: parameters.posonlyargs.iter().map(Into::into).collect(), args: parameters.args.iter().map(Into::into).collect(), @@ -405,26 +409,30 @@ impl<'a> From<&'a ast::Parameters> for ComparableParameters<'a> { } } -impl<'a> From<&'a Box> for ComparableParameters<'a> { - fn from(parameters: &'a Box) -> Self { - (parameters.as_ref()).into() +impl<'a, 'ast> From<&'a ruff_allocator::Box<'ast, ast::Parameters<'ast>>> + for ComparableParameters<'a, 'ast> +{ + fn from(parameters: &'a ruff_allocator::Box<'ast, ast::Parameters<'ast>>) -> Self { + (&*parameters).into() } } -impl<'a> From<&'a Box> for ComparableParameter<'a> { - fn from(arg: &'a Box) -> Self { - (arg.as_ref()).into() +impl<'a, 'ast> From<&'a ruff_allocator::Box<'ast, ast::Parameter<'ast>>> + for ComparableParameter<'a, 'ast> +{ + fn from(arg: &'a ruff_allocator::Box<'ast, ast::Parameter<'ast>>) -> Self { + (&*arg).into() } } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct ComparableParameter<'a> { - arg: &'a str, - annotation: Option>>, +pub struct ComparableParameter<'a, 'ast> { + arg: &'ast str, + annotation: Option>>, } -impl<'a> From<&'a ast::Parameter> for ComparableParameter<'a> { - fn from(arg: &'a ast::Parameter) -> Self { +impl<'a, 'ast> From<&'a ast::Parameter<'ast>> for ComparableParameter<'a, 'ast> { + fn from(arg: &'a ast::Parameter<'ast>) -> Self { Self { arg: arg.name.as_str(), annotation: arg.annotation.as_ref().map(Into::into), @@ -433,13 +441,15 @@ impl<'a> From<&'a ast::Parameter> for ComparableParameter<'a> { } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct ComparableParameterWithDefault<'a> { - def: ComparableParameter<'a>, - default: Option>, +pub struct ComparableParameterWithDefault<'a, 'ast> { + def: ComparableParameter<'a, 'ast>, + default: Option>, } -impl<'a> From<&'a ast::ParameterWithDefault> for ComparableParameterWithDefault<'a> { - fn from(arg: &'a ast::ParameterWithDefault) -> Self { +impl<'a, 'ast> From<&'a ast::ParameterWithDefault<'ast>> + for ComparableParameterWithDefault<'a, 'ast> +{ + fn from(arg: &'a ast::ParameterWithDefault<'ast>) -> Self { Self { def: (&arg.parameter).into(), default: arg.default.as_ref().map(Into::into), @@ -448,13 +458,13 @@ impl<'a> From<&'a ast::ParameterWithDefault> for ComparableParameterWithDefault< } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct ComparableKeyword<'a> { - arg: Option<&'a str>, - value: ComparableExpr<'a>, +pub struct ComparableKeyword<'a, 'ast> { + arg: Option<&'ast str>, + value: ComparableExpr<'a, 'ast>, } -impl<'a> From<&'a ast::Keyword> for ComparableKeyword<'a> { - fn from(keyword: &'a ast::Keyword) -> Self { +impl<'a, 'ast> From<&'a ast::Keyword<'ast>> for ComparableKeyword<'a, 'ast> { + fn from(keyword: &'a ast::Keyword<'ast>) -> Self { Self { arg: keyword.arg.as_ref().map(ast::Identifier::as_str), value: (&keyword.value).into(), @@ -463,15 +473,15 @@ impl<'a> From<&'a ast::Keyword> for ComparableKeyword<'a> { } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct ComparableComprehension<'a> { - target: ComparableExpr<'a>, - iter: ComparableExpr<'a>, - ifs: Vec>, +pub struct ComparableComprehension<'a, 'ast> { + target: ComparableExpr<'a, 'ast>, + iter: ComparableExpr<'a, 'ast>, + ifs: Vec>, is_async: bool, } -impl<'a> From<&'a ast::Comprehension> for ComparableComprehension<'a> { - fn from(comprehension: &'a ast::Comprehension) -> Self { +impl<'a, 'ast> From<&'a ast::Comprehension<'ast>> for ComparableComprehension<'a, 'ast> { + fn from(comprehension: &'a ast::Comprehension<'ast>) -> Self { Self { target: (&comprehension.target).into(), iter: (&comprehension.iter).into(), @@ -482,19 +492,19 @@ impl<'a> From<&'a ast::Comprehension> for ComparableComprehension<'a> { } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct ExceptHandlerExceptHandler<'a> { - type_: Option>>, - name: Option<&'a str>, - body: Vec>, +pub struct ExceptHandlerExceptHandler<'a, 'ast> { + type_: Option>>, + name: Option<&'ast str>, + body: Vec>, } #[derive(Debug, PartialEq, Eq, Hash)] -pub enum ComparableExceptHandler<'a> { - ExceptHandler(ExceptHandlerExceptHandler<'a>), +pub enum ComparableExceptHandler<'a, 'ast> { + ExceptHandler(ExceptHandlerExceptHandler<'a, 'ast>), } -impl<'a> From<&'a ast::ExceptHandler> for ComparableExceptHandler<'a> { - fn from(except_handler: &'a ast::ExceptHandler) -> Self { +impl<'a, 'ast> From<&'a ast::ExceptHandler<'ast>> for ComparableExceptHandler<'a, 'ast> { + fn from(except_handler: &'a ast::ExceptHandler<'ast>) -> Self { let ast::ExceptHandler::ExceptHandler(ast::ExceptHandlerExceptHandler { type_, name, @@ -503,28 +513,28 @@ impl<'a> From<&'a ast::ExceptHandler> for ComparableExceptHandler<'a> { }) = except_handler; Self::ExceptHandler(ExceptHandlerExceptHandler { type_: type_.as_ref().map(Into::into), - name: name.as_deref(), + name: name.as_ref().map(|name| name.as_str()), body: body.iter().map(Into::into).collect(), }) } } #[derive(Debug, PartialEq, Eq, Hash)] -pub enum ComparableFStringElement<'a> { - Literal(&'a str), - FStringExpressionElement(FStringExpressionElement<'a>), +pub enum ComparableFStringElement<'a, 'ast> { + Literal(&'ast str), + FStringExpressionElement(FStringExpressionElement<'a, 'ast>), } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct FStringExpressionElement<'a> { - expression: ComparableExpr<'a>, +pub struct FStringExpressionElement<'a, 'ast> { + expression: ComparableExpr<'a, 'ast>, debug_text: Option<&'a ast::DebugText>, conversion: ast::ConversionFlag, - format_spec: Option>>, + format_spec: Option>>, } -impl<'a> From<&'a ast::FStringElement> for ComparableFStringElement<'a> { - fn from(fstring_element: &'a ast::FStringElement) -> Self { +impl<'a, 'ast> From<&'a ast::FStringElement<'ast>> for ComparableFStringElement<'a, 'ast> { + fn from(fstring_element: &'a ast::FStringElement<'ast>) -> Self { match fstring_element { ast::FStringElement::Literal(ast::FStringLiteralElement { value, .. }) => { Self::Literal(value) @@ -545,13 +555,13 @@ impl<'a> From<&'a ast::FStringElement> for ComparableFStringElement<'a> { } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct ComparableElifElseClause<'a> { - test: Option>, - body: Vec>, +pub struct ComparableElifElseClause<'a, 'ast> { + test: Option>, + body: Vec>, } -impl<'a> From<&'a ast::ElifElseClause> for ComparableElifElseClause<'a> { - fn from(elif_else_clause: &'a ast::ElifElseClause) -> Self { +impl<'a, 'ast> From<&'a ast::ElifElseClause<'ast>> for ComparableElifElseClause<'a, 'ast> { + fn from(elif_else_clause: &'a ast::ElifElseClause<'ast>) -> Self { let ast::ElifElseClause { range: _, test, @@ -565,23 +575,23 @@ impl<'a> From<&'a ast::ElifElseClause> for ComparableElifElseClause<'a> { } #[derive(Debug, PartialEq, Eq, Hash)] -pub enum ComparableLiteral<'a> { +pub enum ComparableLiteral<'a, 'ast> { None, Ellipsis, - Bool(&'a bool), - Str(Vec>), - Bytes(Vec>), + Bool(bool), + Str(Vec>), + Bytes(Vec>), Number(ComparableNumber<'a>), } -impl<'a> From> for ComparableLiteral<'a> { - fn from(literal: ast::LiteralExpressionRef<'a>) -> Self { +impl<'a, 'ast> From> for ComparableLiteral<'a, 'ast> { + fn from(literal: ast::LiteralExpressionRef<'a, 'ast>) -> Self { match literal { ast::LiteralExpressionRef::NoneLiteral(_) => Self::None, ast::LiteralExpressionRef::EllipsisLiteral(_) => Self::Ellipsis, ast::LiteralExpressionRef::BooleanLiteral(ast::ExprBooleanLiteral { value, .. - }) => Self::Bool(value), + }) => Self::Bool(*value), ast::LiteralExpressionRef::StringLiteral(ast::ExprStringLiteral { value, .. }) => { Self::Str(value.iter().map(Into::into).collect()) } @@ -596,12 +606,12 @@ impl<'a> From> for ComparableLiteral<'a> { } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct ComparableFString<'a> { - elements: Vec>, +pub struct ComparableFString<'a, 'ast> { + elements: Vec>, } -impl<'a> From<&'a ast::FString> for ComparableFString<'a> { - fn from(fstring: &'a ast::FString) -> Self { +impl<'a, 'ast> From<&'a ast::FString<'ast>> for ComparableFString<'a, 'ast> { + fn from(fstring: &'a ast::FString<'ast>) -> Self { Self { elements: fstring.elements.iter().map(Into::into).collect(), } @@ -609,13 +619,13 @@ impl<'a> From<&'a ast::FString> for ComparableFString<'a> { } #[derive(Debug, PartialEq, Eq, Hash)] -pub enum ComparableFStringPart<'a> { - Literal(ComparableStringLiteral<'a>), - FString(ComparableFString<'a>), +pub enum ComparableFStringPart<'a, 'ast> { + Literal(ComparableStringLiteral<'ast>), + FString(ComparableFString<'a, 'ast>), } -impl<'a> From<&'a ast::FStringPart> for ComparableFStringPart<'a> { - fn from(f_string_part: &'a ast::FStringPart) -> Self { +impl<'a, 'ast> From<&'a ast::FStringPart<'ast>> for ComparableFStringPart<'a, 'ast> { + fn from(f_string_part: &'a ast::FStringPart<'ast>) -> Self { match f_string_part { ast::FStringPart::Literal(string_literal) => Self::Literal(string_literal.into()), ast::FStringPart::FString(f_string) => Self::FString(f_string.into()), @@ -628,8 +638,8 @@ pub struct ComparableStringLiteral<'a> { value: &'a str, } -impl<'a> From<&'a ast::StringLiteral> for ComparableStringLiteral<'a> { - fn from(string_literal: &'a ast::StringLiteral) -> Self { +impl<'a, 'ast> From<&'a ast::StringLiteral<'ast>> for ComparableStringLiteral<'ast> { + fn from(string_literal: &'a ast::StringLiteral<'ast>) -> Self { Self { value: &string_literal.value, } @@ -637,12 +647,12 @@ impl<'a> From<&'a ast::StringLiteral> for ComparableStringLiteral<'a> { } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct ComparableBytesLiteral<'a> { - value: &'a [u8], +pub struct ComparableBytesLiteral<'ast> { + value: &'ast [u8], } -impl<'a> From<&'a ast::BytesLiteral> for ComparableBytesLiteral<'a> { - fn from(bytes_literal: &'a ast::BytesLiteral) -> Self { +impl<'a, 'ast> From<&'a ast::BytesLiteral<'ast>> for ComparableBytesLiteral<'ast> { + fn from(bytes_literal: &'a ast::BytesLiteral<'ast>) -> Self { Self { value: &bytes_literal.value, } @@ -650,51 +660,51 @@ impl<'a> From<&'a ast::BytesLiteral> for ComparableBytesLiteral<'a> { } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct ExprBoolOp<'a> { +pub struct ExprBoolOp<'a, 'ast> { op: ComparableBoolOp, - values: Vec>, + values: Vec>, } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct ExprNamed<'a> { - target: Box>, - value: Box>, +pub struct ExprNamed<'a, 'ast> { + target: Box>, + value: Box>, } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct ExprBinOp<'a> { - left: Box>, +pub struct ExprBinOp<'a, 'ast> { + left: Box>, op: ComparableOperator, - right: Box>, + right: Box>, } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct ExprUnaryOp<'a> { +pub struct ExprUnaryOp<'a, 'ast> { op: ComparableUnaryOp, - operand: Box>, + operand: Box>, } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct ExprLambda<'a> { - parameters: Option>, - body: Box>, +pub struct ExprLambda<'a, 'ast> { + parameters: Option>, + body: Box>, } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct ExprIf<'a> { - test: Box>, - body: Box>, - orelse: Box>, +pub struct ExprIf<'a, 'ast> { + test: Box>, + body: Box>, + orelse: Box>, } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct ComparableDictItem<'a> { - key: Option>, - value: ComparableExpr<'a>, +pub struct ComparableDictItem<'a, 'ast> { + key: Option>, + value: ComparableExpr<'a, 'ast>, } -impl<'a> From<&'a ast::DictItem> for ComparableDictItem<'a> { - fn from(ast::DictItem { key, value }: &'a ast::DictItem) -> Self { +impl<'a, 'ast> From<&'a ast::DictItem<'ast>> for ComparableDictItem<'a, 'ast> { + fn from(ast::DictItem { key, value }: &'a ast::DictItem<'ast>) -> Self { Self { key: key.as_ref().map(ComparableExpr::from), value: value.into(), @@ -703,89 +713,89 @@ impl<'a> From<&'a ast::DictItem> for ComparableDictItem<'a> { } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct ExprDict<'a> { - items: Vec>, +pub struct ExprDict<'a, 'ast> { + items: Vec>, } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct ExprSet<'a> { - elts: Vec>, +pub struct ExprSet<'a, 'ast> { + elts: Vec>, } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct ExprListComp<'a> { - elt: Box>, - generators: Vec>, +pub struct ExprListComp<'a, 'ast> { + elt: Box>, + generators: Vec>, } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct ExprSetComp<'a> { - elt: Box>, - generators: Vec>, +pub struct ExprSetComp<'a, 'ast> { + elt: Box>, + generators: Vec>, } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct ExprDictComp<'a> { - key: Box>, - value: Box>, - generators: Vec>, +pub struct ExprDictComp<'a, 'ast> { + key: Box>, + value: Box>, + generators: Vec>, } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct ExprGenerator<'a> { - elt: Box>, - generators: Vec>, +pub struct ExprGenerator<'a, 'ast> { + elt: Box>, + generators: Vec>, } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct ExprAwait<'a> { - value: Box>, +pub struct ExprAwait<'a, 'ast> { + value: Box>, } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct ExprYield<'a> { - value: Option>>, +pub struct ExprYield<'a, 'ast> { + value: Option>>, } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct ExprYieldFrom<'a> { - value: Box>, +pub struct ExprYieldFrom<'a, 'ast> { + value: Box>, } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct ExprCompare<'a> { - left: Box>, +pub struct ExprCompare<'a, 'ast> { + left: Box>, ops: Vec, - comparators: Vec>, + comparators: Vec>, } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct ExprCall<'a> { - func: Box>, - arguments: ComparableArguments<'a>, +pub struct ExprCall<'a, 'ast> { + func: Box>, + arguments: ComparableArguments<'a, 'ast>, } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct ExprFStringExpressionElement<'a> { - value: Box>, +pub struct ExprFStringExpressionElement<'a, 'ast> { + value: Box>, debug_text: Option<&'a ast::DebugText>, conversion: ast::ConversionFlag, - format_spec: Vec>, + format_spec: Vec>, } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct ExprFString<'a> { - parts: Vec>, +pub struct ExprFString<'a, 'ast> { + parts: Vec>, } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct ExprStringLiteral<'a> { - parts: Vec>, +pub struct ExprStringLiteral<'ast> { + parts: Vec>, } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct ExprBytesLiteral<'a> { - parts: Vec>, +pub struct ExprBytesLiteral<'ast> { + parts: Vec>, } #[derive(Debug, PartialEq, Eq, Hash)] @@ -794,106 +804,108 @@ pub struct ExprNumberLiteral<'a> { } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct ExprBoolLiteral<'a> { - value: &'a bool, +pub struct ExprBoolLiteral { + value: bool, } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct ExprAttribute<'a> { - value: Box>, - attr: &'a str, +pub struct ExprAttribute<'a, 'ast> { + value: Box>, + attr: &'ast str, } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct ExprSubscript<'a> { - value: Box>, - slice: Box>, +pub struct ExprSubscript<'a, 'ast> { + value: Box>, + slice: Box>, } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct ExprStarred<'a> { - value: Box>, +pub struct ExprStarred<'a, 'ast> { + value: Box>, } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct ExprName<'a> { - id: &'a str, +pub struct ExprName<'ast> { + id: &'ast str, } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct ExprList<'a> { - elts: Vec>, +pub struct ExprList<'a, 'ast> { + elts: Vec>, } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct ExprTuple<'a> { - elts: Vec>, +pub struct ExprTuple<'a, 'ast> { + elts: Vec>, } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct ExprSlice<'a> { - lower: Option>>, - upper: Option>>, - step: Option>>, +pub struct ExprSlice<'a, 'ast> { + lower: Option>>, + upper: Option>>, + step: Option>>, } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct ExprIpyEscapeCommand<'a> { +pub struct ExprIpyEscapeCommand<'ast> { kind: ast::IpyEscapeKind, - value: &'a str, + value: &'ast str, } #[derive(Debug, PartialEq, Eq, Hash)] -pub enum ComparableExpr<'a> { - BoolOp(ExprBoolOp<'a>), - NamedExpr(ExprNamed<'a>), - BinOp(ExprBinOp<'a>), - UnaryOp(ExprUnaryOp<'a>), - Lambda(ExprLambda<'a>), - IfExp(ExprIf<'a>), - Dict(ExprDict<'a>), - Set(ExprSet<'a>), - ListComp(ExprListComp<'a>), - SetComp(ExprSetComp<'a>), - DictComp(ExprDictComp<'a>), - GeneratorExp(ExprGenerator<'a>), - Await(ExprAwait<'a>), - Yield(ExprYield<'a>), - YieldFrom(ExprYieldFrom<'a>), - Compare(ExprCompare<'a>), - Call(ExprCall<'a>), - FStringExpressionElement(ExprFStringExpressionElement<'a>), - FString(ExprFString<'a>), - StringLiteral(ExprStringLiteral<'a>), - BytesLiteral(ExprBytesLiteral<'a>), +pub enum ComparableExpr<'a, 'ast> { + BoolOp(ExprBoolOp<'a, 'ast>), + NamedExpr(ExprNamed<'a, 'ast>), + BinOp(ExprBinOp<'a, 'ast>), + UnaryOp(ExprUnaryOp<'a, 'ast>), + Lambda(ExprLambda<'a, 'ast>), + IfExp(ExprIf<'a, 'ast>), + Dict(ExprDict<'a, 'ast>), + Set(ExprSet<'a, 'ast>), + ListComp(ExprListComp<'a, 'ast>), + SetComp(ExprSetComp<'a, 'ast>), + DictComp(ExprDictComp<'a, 'ast>), + GeneratorExp(ExprGenerator<'a, 'ast>), + Await(ExprAwait<'a, 'ast>), + Yield(ExprYield<'a, 'ast>), + YieldFrom(ExprYieldFrom<'a, 'ast>), + Compare(ExprCompare<'a, 'ast>), + Call(ExprCall<'a, 'ast>), + FStringExpressionElement(ExprFStringExpressionElement<'a, 'ast>), + FString(ExprFString<'a, 'ast>), + StringLiteral(ExprStringLiteral<'ast>), + BytesLiteral(ExprBytesLiteral<'ast>), NumberLiteral(ExprNumberLiteral<'a>), - BoolLiteral(ExprBoolLiteral<'a>), + BoolLiteral(ExprBoolLiteral), NoneLiteral, EllipsisLiteral, - Attribute(ExprAttribute<'a>), - Subscript(ExprSubscript<'a>), - Starred(ExprStarred<'a>), - Name(ExprName<'a>), - List(ExprList<'a>), - Tuple(ExprTuple<'a>), - Slice(ExprSlice<'a>), - IpyEscapeCommand(ExprIpyEscapeCommand<'a>), + Attribute(ExprAttribute<'a, 'ast>), + Subscript(ExprSubscript<'a, 'ast>), + Starred(ExprStarred<'a, 'ast>), + Name(ExprName<'ast>), + List(ExprList<'a, 'ast>), + Tuple(ExprTuple<'a, 'ast>), + Slice(ExprSlice<'a, 'ast>), + IpyEscapeCommand(ExprIpyEscapeCommand<'ast>), } -impl<'a> From<&'a Box> for Box> { - fn from(expr: &'a Box) -> Self { - Box::new((expr.as_ref()).into()) +impl<'a, 'ast> From<&'a ruff_allocator::Box<'ast, ast::Expr<'ast>>> + for Box> +{ + fn from(expr: &'a ruff_allocator::Box<'ast, ast::Expr<'ast>>) -> Self { + Box::new((&*expr).into()) } } -impl<'a> From<&'a Box> for ComparableExpr<'a> { - fn from(expr: &'a Box) -> Self { - (expr.as_ref()).into() +impl<'a, 'ast> From<&'a ruff_allocator::Box<'ast, ast::Expr<'ast>>> for ComparableExpr<'a, 'ast> { + fn from(expr: &'a ruff_allocator::Box<'ast, ast::Expr<'ast>>) -> Self { + (&*expr).into() } } -impl<'a> From<&'a ast::Expr> for ComparableExpr<'a> { - fn from(expr: &'a ast::Expr) -> Self { +impl<'a, 'ast> From<&'a ast::Expr<'ast>> for ComparableExpr<'a, 'ast> { + fn from(expr: &'a ast::Expr<'ast>) -> Self { match expr { ast::Expr::BoolOp(ast::ExprBoolOp { op, @@ -1038,7 +1050,7 @@ impl<'a> From<&'a ast::Expr> for ComparableExpr<'a> { }) } ast::Expr::BooleanLiteral(ast::ExprBooleanLiteral { value, range: _ }) => { - Self::BoolLiteral(ExprBoolLiteral { value }) + Self::BoolLiteral(ExprBoolLiteral { value: *value }) } ast::Expr::NoneLiteral(_) => Self::NoneLiteral, ast::Expr::EllipsisLiteral(_) => Self::EllipsisLiteral, @@ -1102,79 +1114,79 @@ impl<'a> From<&'a ast::Expr> for ComparableExpr<'a> { } } -impl<'a> From<&'a ast::ExprName> for ComparableExpr<'a> { - fn from(expr: &'a ast::ExprName) -> Self { - Self::Name(ExprName { - id: expr.id.as_str(), - }) +impl<'a, 'ast> From<&'a ast::ExprName<'ast>> for ComparableExpr<'a, 'ast> { + fn from(expr: &'a ast::ExprName<'ast>) -> Self { + Self::Name(ExprName { id: expr.id }) } } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct StmtFunctionDef<'a> { +pub struct StmtFunctionDef<'a, 'ast> { is_async: bool, - decorator_list: Vec>, - name: &'a str, - type_params: Option>, - parameters: ComparableParameters<'a>, - returns: Option>, - body: Vec>, + decorator_list: Vec>, + name: &'ast str, + type_params: Option>, + parameters: ComparableParameters<'a, 'ast>, + returns: Option>, + body: Vec>, } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct StmtClassDef<'a> { - decorator_list: Vec>, - name: &'a str, - type_params: Option>, - arguments: ComparableArguments<'a>, - body: Vec>, +pub struct StmtClassDef<'a, 'ast> { + decorator_list: Vec>, + name: &'ast str, + type_params: Option>, + arguments: ComparableArguments<'a, 'ast>, + body: Vec>, } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct StmtReturn<'a> { - value: Option>, +pub struct StmtReturn<'a, 'ast> { + value: Option>, } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct StmtDelete<'a> { - targets: Vec>, +pub struct StmtDelete<'a, 'ast> { + targets: Vec>, } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct StmtTypeAlias<'a> { - pub name: Box>, - pub type_params: Option>, - pub value: Box>, +pub struct StmtTypeAlias<'a, 'ast> { + pub name: Box>, + pub type_params: Option>, + pub value: Box>, } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct ComparableTypeParams<'a> { - pub type_params: Vec>, +pub struct ComparableTypeParams<'a, 'ast> { + pub type_params: Vec>, } -impl<'a> From<&'a ast::TypeParams> for ComparableTypeParams<'a> { - fn from(type_params: &'a ast::TypeParams) -> Self { +impl<'a, 'ast> From<&'a ast::TypeParams<'ast>> for ComparableTypeParams<'a, 'ast> { + fn from(type_params: &'a ast::TypeParams<'ast>) -> Self { Self { type_params: type_params.iter().map(Into::into).collect(), } } } -impl<'a> From<&'a Box> for ComparableTypeParams<'a> { - fn from(type_params: &'a Box) -> Self { +impl<'a, 'ast> From<&'a ruff_allocator::Box<'ast, ast::TypeParams<'ast>>> + for ComparableTypeParams<'a, 'ast> +{ + fn from(type_params: &'a ruff_allocator::Box<'a, ast::TypeParams<'ast>>) -> Self { type_params.as_ref().into() } } #[derive(Debug, PartialEq, Eq, Hash)] -pub enum ComparableTypeParam<'a> { - TypeVar(TypeParamTypeVar<'a>), - ParamSpec(TypeParamParamSpec<'a>), - TypeVarTuple(TypeParamTypeVarTuple<'a>), +pub enum ComparableTypeParam<'a, 'ast> { + TypeVar(TypeParamTypeVar<'a, 'ast>), + ParamSpec(TypeParamParamSpec<'a, 'ast>), + TypeVarTuple(TypeParamTypeVarTuple<'a, 'ast>), } -impl<'a> From<&'a ast::TypeParam> for ComparableTypeParam<'a> { - fn from(type_param: &'a ast::TypeParam) -> Self { +impl<'a, 'ast> From<&'a ast::TypeParam<'ast>> for ComparableTypeParam<'a, 'ast> { + fn from(type_param: &'a ast::TypeParam<'ast>) -> Self { match type_param { ast::TypeParam::TypeVar(ast::TypeParamTypeVar { name, @@ -1207,166 +1219,166 @@ impl<'a> From<&'a ast::TypeParam> for ComparableTypeParam<'a> { } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct TypeParamTypeVar<'a> { - pub name: &'a str, - pub bound: Option>>, - pub default: Option>>, +pub struct TypeParamTypeVar<'a, 'ast> { + pub name: &'ast str, + pub bound: Option>>, + pub default: Option>>, } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct TypeParamParamSpec<'a> { - pub name: &'a str, - pub default: Option>>, +pub struct TypeParamParamSpec<'a, 'ast> { + pub name: &'ast str, + pub default: Option>>, } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct TypeParamTypeVarTuple<'a> { - pub name: &'a str, - pub default: Option>>, +pub struct TypeParamTypeVarTuple<'a, 'ast> { + pub name: &'ast str, + pub default: Option>>, } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct StmtAssign<'a> { - targets: Vec>, - value: ComparableExpr<'a>, +pub struct StmtAssign<'a, 'ast> { + targets: Vec>, + value: ComparableExpr<'a, 'ast>, } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct StmtAugAssign<'a> { - target: ComparableExpr<'a>, +pub struct StmtAugAssign<'a, 'ast> { + target: ComparableExpr<'a, 'ast>, op: ComparableOperator, - value: ComparableExpr<'a>, + value: ComparableExpr<'a, 'ast>, } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct StmtAnnAssign<'a> { - target: ComparableExpr<'a>, - annotation: ComparableExpr<'a>, - value: Option>, +pub struct StmtAnnAssign<'a, 'ast> { + target: ComparableExpr<'a, 'ast>, + annotation: ComparableExpr<'a, 'ast>, + value: Option>, simple: bool, } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct StmtFor<'a> { +pub struct StmtFor<'a, 'ast> { is_async: bool, - target: ComparableExpr<'a>, - iter: ComparableExpr<'a>, - body: Vec>, - orelse: Vec>, + target: ComparableExpr<'a, 'ast>, + iter: ComparableExpr<'a, 'ast>, + body: Vec>, + orelse: Vec>, } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct StmtWhile<'a> { - test: ComparableExpr<'a>, - body: Vec>, - orelse: Vec>, +pub struct StmtWhile<'a, 'ast> { + test: ComparableExpr<'a, 'ast>, + body: Vec>, + orelse: Vec>, } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct StmtIf<'a> { - test: ComparableExpr<'a>, - body: Vec>, - elif_else_clauses: Vec>, +pub struct StmtIf<'a, 'ast> { + test: ComparableExpr<'a, 'ast>, + body: Vec>, + elif_else_clauses: Vec>, } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct StmtWith<'a> { +pub struct StmtWith<'a, 'ast> { is_async: bool, - items: Vec>, - body: Vec>, + items: Vec>, + body: Vec>, } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct StmtMatch<'a> { - subject: ComparableExpr<'a>, - cases: Vec>, +pub struct StmtMatch<'a, 'ast> { + subject: ComparableExpr<'a, 'ast>, + cases: Vec>, } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct StmtRaise<'a> { - exc: Option>, - cause: Option>, +pub struct StmtRaise<'a, 'ast> { + exc: Option>, + cause: Option>, } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct StmtTry<'a> { - body: Vec>, - handlers: Vec>, - orelse: Vec>, - finalbody: Vec>, +pub struct StmtTry<'a, 'ast> { + body: Vec>, + handlers: Vec>, + orelse: Vec>, + finalbody: Vec>, is_star: bool, } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct StmtAssert<'a> { - test: ComparableExpr<'a>, - msg: Option>, +pub struct StmtAssert<'a, 'ast> { + test: ComparableExpr<'a, 'ast>, + msg: Option>, } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct StmtImport<'a> { - names: Vec>, +pub struct StmtImport<'ast> { + names: Vec>, } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct StmtImportFrom<'a> { - module: Option<&'a str>, - names: Vec>, +pub struct StmtImportFrom<'ast> { + module: Option<&'ast str>, + names: Vec>, level: u32, } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct StmtGlobal<'a> { - names: Vec<&'a str>, +pub struct StmtGlobal<'ast> { + names: Vec<&'ast str>, } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct StmtNonlocal<'a> { - names: Vec<&'a str>, +pub struct StmtNonlocal<'ast> { + names: Vec<&'ast str>, } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct StmtExpr<'a> { - value: ComparableExpr<'a>, +pub struct StmtExpr<'a, 'ast> { + value: ComparableExpr<'a, 'ast>, } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct StmtIpyEscapeCommand<'a> { +pub struct StmtIpyEscapeCommand<'ast> { kind: ast::IpyEscapeKind, - value: &'a str, + value: &'ast str, } #[derive(Debug, PartialEq, Eq, Hash)] -pub enum ComparableStmt<'a> { - FunctionDef(StmtFunctionDef<'a>), - ClassDef(StmtClassDef<'a>), - Return(StmtReturn<'a>), - Delete(StmtDelete<'a>), - Assign(StmtAssign<'a>), - AugAssign(StmtAugAssign<'a>), - AnnAssign(StmtAnnAssign<'a>), - For(StmtFor<'a>), - While(StmtWhile<'a>), - If(StmtIf<'a>), - With(StmtWith<'a>), - Match(StmtMatch<'a>), - Raise(StmtRaise<'a>), - Try(StmtTry<'a>), - TypeAlias(StmtTypeAlias<'a>), - Assert(StmtAssert<'a>), - Import(StmtImport<'a>), - ImportFrom(StmtImportFrom<'a>), - Global(StmtGlobal<'a>), - Nonlocal(StmtNonlocal<'a>), - IpyEscapeCommand(StmtIpyEscapeCommand<'a>), - Expr(StmtExpr<'a>), +pub enum ComparableStmt<'a, 'ast> { + FunctionDef(StmtFunctionDef<'a, 'ast>), + ClassDef(StmtClassDef<'a, 'ast>), + Return(StmtReturn<'a, 'ast>), + Delete(StmtDelete<'a, 'ast>), + Assign(StmtAssign<'a, 'ast>), + AugAssign(StmtAugAssign<'a, 'ast>), + AnnAssign(StmtAnnAssign<'a, 'ast>), + For(StmtFor<'a, 'ast>), + While(StmtWhile<'a, 'ast>), + If(StmtIf<'a, 'ast>), + With(StmtWith<'a, 'ast>), + Match(StmtMatch<'a, 'ast>), + Raise(StmtRaise<'a, 'ast>), + Try(StmtTry<'a, 'ast>), + TypeAlias(StmtTypeAlias<'a, 'ast>), + Assert(StmtAssert<'a, 'ast>), + Import(StmtImport<'ast>), + ImportFrom(StmtImportFrom<'ast>), + Global(StmtGlobal<'ast>), + Nonlocal(StmtNonlocal<'ast>), + IpyEscapeCommand(StmtIpyEscapeCommand<'ast>), + Expr(StmtExpr<'a, 'ast>), Pass, Break, Continue, } -impl<'a> From<&'a ast::Stmt> for ComparableStmt<'a> { - fn from(stmt: &'a ast::Stmt) -> Self { +impl<'a, 'ast> From<&'a ast::Stmt<'ast>> for ComparableStmt<'a, 'ast> { + fn from(stmt: &'a ast::Stmt<'ast>) -> Self { match stmt { ast::Stmt::FunctionDef(ast::StmtFunctionDef { is_async, @@ -1537,7 +1549,7 @@ impl<'a> From<&'a ast::Stmt> for ComparableStmt<'a> { level, range: _, }) => Self::ImportFrom(StmtImportFrom { - module: module.as_deref(), + module: module.as_ref().map(|name| name.as_str()), names: names.iter().map(Into::into).collect(), level: *level, }), @@ -1565,23 +1577,23 @@ impl<'a> From<&'a ast::Stmt> for ComparableStmt<'a> { } #[derive(Debug, PartialEq, Eq, Hash)] -pub enum ComparableMod<'a> { - Module(ComparableModModule<'a>), - Expression(ComparableModExpression<'a>), +pub enum ComparableMod<'a, 'ast> { + Module(ComparableModModule<'a, 'ast>), + Expression(ComparableModExpression<'a, 'ast>), } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct ComparableModModule<'a> { - body: Vec>, +pub struct ComparableModModule<'a, 'ast> { + body: Vec>, } #[derive(Debug, PartialEq, Eq, Hash)] -pub struct ComparableModExpression<'a> { - body: Box>, +pub struct ComparableModExpression<'a, 'ast> { + body: ComparableExpr<'a, 'ast>, } -impl<'a> From<&'a ast::Mod> for ComparableMod<'a> { - fn from(mod_: &'a ast::Mod) -> Self { +impl<'a, 'ast> From<&'a ast::Mod<'ast>> for ComparableMod<'a, 'ast> { + fn from(mod_: &'a ast::Mod<'ast>) -> Self { match mod_ { ast::Mod::Module(module) => Self::Module(module.into()), ast::Mod::Expression(expr) => Self::Expression(expr.into()), @@ -1589,16 +1601,16 @@ impl<'a> From<&'a ast::Mod> for ComparableMod<'a> { } } -impl<'a> From<&'a ast::ModModule> for ComparableModModule<'a> { - fn from(module: &'a ast::ModModule) -> Self { +impl<'a, 'ast> From<&'a ast::ModModule<'ast>> for ComparableModModule<'a, 'ast> { + fn from(module: &'a ast::ModModule<'ast>) -> Self { Self { body: module.body.iter().map(Into::into).collect(), } } } -impl<'a> From<&'a ast::ModExpression> for ComparableModExpression<'a> { - fn from(expr: &'a ast::ModExpression) -> Self { +impl<'a, 'ast> From<&'a ast::ModExpression<'ast>> for ComparableModExpression<'a, 'ast> { + fn from(expr: &'a ast::ModExpression<'ast>) -> Self { Self { body: (&expr.body).into(), } diff --git a/crates/ruff_python_ast/src/expression.rs b/crates/ruff_python_ast/src/expression.rs index b3bd0a6701..0d5aacba03 100644 --- a/crates/ruff_python_ast/src/expression.rs +++ b/crates/ruff_python_ast/src/expression.rs @@ -6,49 +6,49 @@ use crate::{self as ast, AnyNodeRef, AnyStringFlags, Expr}; /// Unowned pendant to [`ast::Expr`] that stores a reference instead of a owned value. #[derive(Copy, Clone, Debug, PartialEq)] -pub enum ExpressionRef<'a> { - BoolOp(&'a ast::ExprBoolOp), - Named(&'a ast::ExprNamed), - BinOp(&'a ast::ExprBinOp), - UnaryOp(&'a ast::ExprUnaryOp), - Lambda(&'a ast::ExprLambda), - If(&'a ast::ExprIf), - Dict(&'a ast::ExprDict), - Set(&'a ast::ExprSet), - ListComp(&'a ast::ExprListComp), - SetComp(&'a ast::ExprSetComp), - DictComp(&'a ast::ExprDictComp), - Generator(&'a ast::ExprGenerator), - Await(&'a ast::ExprAwait), - Yield(&'a ast::ExprYield), - YieldFrom(&'a ast::ExprYieldFrom), - Compare(&'a ast::ExprCompare), - Call(&'a ast::ExprCall), - FString(&'a ast::ExprFString), - StringLiteral(&'a ast::ExprStringLiteral), - BytesLiteral(&'a ast::ExprBytesLiteral), +pub enum ExpressionRef<'a, 'ast> { + BoolOp(&'a ast::ExprBoolOp<'ast>), + Named(&'a ast::ExprNamed<'ast>), + BinOp(&'a ast::ExprBinOp<'ast>), + UnaryOp(&'a ast::ExprUnaryOp<'ast>), + Lambda(&'a ast::ExprLambda<'ast>), + If(&'a ast::ExprIf<'ast>), + Dict(&'a ast::ExprDict<'ast>), + Set(&'a ast::ExprSet<'ast>), + ListComp(&'a ast::ExprListComp<'ast>), + SetComp(&'a ast::ExprSetComp<'ast>), + DictComp(&'a ast::ExprDictComp<'ast>), + Generator(&'a ast::ExprGenerator<'ast>), + Await(&'a ast::ExprAwait<'ast>), + Yield(&'a ast::ExprYield<'ast>), + YieldFrom(&'a ast::ExprYieldFrom<'ast>), + Compare(&'a ast::ExprCompare<'ast>), + Call(&'a ast::ExprCall<'ast>), + FString(&'a ast::ExprFString<'ast>), + StringLiteral(&'a ast::ExprStringLiteral<'ast>), + BytesLiteral(&'a ast::ExprBytesLiteral<'ast>), NumberLiteral(&'a ast::ExprNumberLiteral), BooleanLiteral(&'a ast::ExprBooleanLiteral), NoneLiteral(&'a ast::ExprNoneLiteral), EllipsisLiteral(&'a ast::ExprEllipsisLiteral), - Attribute(&'a ast::ExprAttribute), - Subscript(&'a ast::ExprSubscript), - Starred(&'a ast::ExprStarred), - Name(&'a ast::ExprName), - List(&'a ast::ExprList), - Tuple(&'a ast::ExprTuple), - Slice(&'a ast::ExprSlice), - IpyEscapeCommand(&'a ast::ExprIpyEscapeCommand), + Attribute(&'a ast::ExprAttribute<'ast>), + Subscript(&'a ast::ExprSubscript<'ast>), + Starred(&'a ast::ExprStarred<'ast>), + Name(&'a ast::ExprName<'ast>), + List(&'a ast::ExprList<'ast>), + Tuple(&'a ast::ExprTuple<'ast>), + Slice(&'a ast::ExprSlice<'ast>), + IpyEscapeCommand(&'a ast::ExprIpyEscapeCommand<'ast>), } -impl<'a> From<&'a Box> for ExpressionRef<'a> { - fn from(value: &'a Box) -> Self { +impl<'a, 'ast> From<&'a Box>> for ExpressionRef<'a, 'ast> { + fn from(value: &'a Box>) -> Self { ExpressionRef::from(value.as_ref()) } } -impl<'a> From<&'a Expr> for ExpressionRef<'a> { - fn from(value: &'a Expr) -> Self { +impl<'a, 'ast> From<&'a Expr<'ast>> for ExpressionRef<'a, 'ast> { + fn from(value: &'a Expr<'ast>) -> Self { match value { Expr::BoolOp(value) => ExpressionRef::BoolOp(value), Expr::Named(value) => ExpressionRef::Named(value), @@ -86,169 +86,169 @@ impl<'a> From<&'a Expr> for ExpressionRef<'a> { } } -impl<'a> From<&'a ast::ExprBoolOp> for ExpressionRef<'a> { - fn from(value: &'a ast::ExprBoolOp) -> Self { +impl<'a, 'ast> From<&'a ast::ExprBoolOp<'ast>> for ExpressionRef<'a, 'ast> { + fn from(value: &'a ast::ExprBoolOp<'ast>) -> Self { Self::BoolOp(value) } } -impl<'a> From<&'a ast::ExprNamed> for ExpressionRef<'a> { - fn from(value: &'a ast::ExprNamed) -> Self { +impl<'a, 'ast> From<&'a ast::ExprNamed<'ast>> for ExpressionRef<'a, 'ast> { + fn from(value: &'a ast::ExprNamed<'ast>) -> Self { Self::Named(value) } } -impl<'a> From<&'a ast::ExprBinOp> for ExpressionRef<'a> { - fn from(value: &'a ast::ExprBinOp) -> Self { +impl<'a, 'ast> From<&'a ast::ExprBinOp<'ast>> for ExpressionRef<'a, 'ast> { + fn from(value: &'a ast::ExprBinOp<'ast>) -> Self { Self::BinOp(value) } } -impl<'a> From<&'a ast::ExprUnaryOp> for ExpressionRef<'a> { - fn from(value: &'a ast::ExprUnaryOp) -> Self { +impl<'a, 'ast> From<&'a ast::ExprUnaryOp<'ast>> for ExpressionRef<'a, 'ast> { + fn from(value: &'a ast::ExprUnaryOp<'ast>) -> Self { Self::UnaryOp(value) } } -impl<'a> From<&'a ast::ExprLambda> for ExpressionRef<'a> { - fn from(value: &'a ast::ExprLambda) -> Self { +impl<'a, 'ast> From<&'a ast::ExprLambda<'ast>> for ExpressionRef<'a, 'ast> { + fn from(value: &'a ast::ExprLambda<'ast>) -> Self { Self::Lambda(value) } } -impl<'a> From<&'a ast::ExprIf> for ExpressionRef<'a> { - fn from(value: &'a ast::ExprIf) -> Self { +impl<'a, 'ast> From<&'a ast::ExprIf<'ast>> for ExpressionRef<'a, 'ast> { + fn from(value: &'a ast::ExprIf<'ast>) -> Self { Self::If(value) } } -impl<'a> From<&'a ast::ExprDict> for ExpressionRef<'a> { - fn from(value: &'a ast::ExprDict) -> Self { +impl<'a, 'ast> From<&'a ast::ExprDict<'ast>> for ExpressionRef<'a, 'ast> { + fn from(value: &'a ast::ExprDict<'ast>) -> Self { Self::Dict(value) } } -impl<'a> From<&'a ast::ExprSet> for ExpressionRef<'a> { - fn from(value: &'a ast::ExprSet) -> Self { +impl<'a, 'ast> From<&'a ast::ExprSet<'ast>> for ExpressionRef<'a, 'ast> { + fn from(value: &'a ast::ExprSet<'ast>) -> Self { Self::Set(value) } } -impl<'a> From<&'a ast::ExprListComp> for ExpressionRef<'a> { - fn from(value: &'a ast::ExprListComp) -> Self { +impl<'a, 'ast> From<&'a ast::ExprListComp<'ast>> for ExpressionRef<'a, 'ast> { + fn from(value: &'a ast::ExprListComp<'ast>) -> Self { Self::ListComp(value) } } -impl<'a> From<&'a ast::ExprSetComp> for ExpressionRef<'a> { - fn from(value: &'a ast::ExprSetComp) -> Self { +impl<'a, 'ast> From<&'a ast::ExprSetComp<'ast>> for ExpressionRef<'a, 'ast> { + fn from(value: &'a ast::ExprSetComp<'ast>) -> Self { Self::SetComp(value) } } -impl<'a> From<&'a ast::ExprDictComp> for ExpressionRef<'a> { - fn from(value: &'a ast::ExprDictComp) -> Self { +impl<'a, 'ast> From<&'a ast::ExprDictComp<'ast>> for ExpressionRef<'a, 'ast> { + fn from(value: &'a ast::ExprDictComp<'ast>) -> Self { Self::DictComp(value) } } -impl<'a> From<&'a ast::ExprGenerator> for ExpressionRef<'a> { - fn from(value: &'a ast::ExprGenerator) -> Self { +impl<'a, 'ast> From<&'a ast::ExprGenerator<'ast>> for ExpressionRef<'a, 'ast> { + fn from(value: &'a ast::ExprGenerator<'ast>) -> Self { Self::Generator(value) } } -impl<'a> From<&'a ast::ExprAwait> for ExpressionRef<'a> { - fn from(value: &'a ast::ExprAwait) -> Self { +impl<'a, 'ast> From<&'a ast::ExprAwait<'ast>> for ExpressionRef<'a, 'ast> { + fn from(value: &'a ast::ExprAwait<'ast>) -> Self { Self::Await(value) } } -impl<'a> From<&'a ast::ExprYield> for ExpressionRef<'a> { - fn from(value: &'a ast::ExprYield) -> Self { +impl<'a, 'ast> From<&'a ast::ExprYield<'ast>> for ExpressionRef<'a, 'ast> { + fn from(value: &'a ast::ExprYield<'ast>) -> Self { Self::Yield(value) } } -impl<'a> From<&'a ast::ExprYieldFrom> for ExpressionRef<'a> { - fn from(value: &'a ast::ExprYieldFrom) -> Self { +impl<'a, 'ast> From<&'a ast::ExprYieldFrom<'ast>> for ExpressionRef<'a, 'ast> { + fn from(value: &'a ast::ExprYieldFrom<'ast>) -> Self { Self::YieldFrom(value) } } -impl<'a> From<&'a ast::ExprCompare> for ExpressionRef<'a> { - fn from(value: &'a ast::ExprCompare) -> Self { +impl<'a, 'ast> From<&'a ast::ExprCompare<'ast>> for ExpressionRef<'a, 'ast> { + fn from(value: &'a ast::ExprCompare<'ast>) -> Self { Self::Compare(value) } } -impl<'a> From<&'a ast::ExprCall> for ExpressionRef<'a> { - fn from(value: &'a ast::ExprCall) -> Self { +impl<'a, 'ast> From<&'a ast::ExprCall<'ast>> for ExpressionRef<'a, 'ast> { + fn from(value: &'a ast::ExprCall<'ast>) -> Self { Self::Call(value) } } -impl<'a> From<&'a ast::ExprFString> for ExpressionRef<'a> { - fn from(value: &'a ast::ExprFString) -> Self { +impl<'a, 'ast> From<&'a ast::ExprFString<'ast>> for ExpressionRef<'a, 'ast> { + fn from(value: &'a ast::ExprFString<'ast>) -> Self { Self::FString(value) } } -impl<'a> From<&'a ast::ExprStringLiteral> for ExpressionRef<'a> { - fn from(value: &'a ast::ExprStringLiteral) -> Self { +impl<'a, 'ast> From<&'a ast::ExprStringLiteral<'ast>> for ExpressionRef<'a, 'ast> { + fn from(value: &'a ast::ExprStringLiteral<'ast>) -> Self { Self::StringLiteral(value) } } -impl<'a> From<&'a ast::ExprBytesLiteral> for ExpressionRef<'a> { - fn from(value: &'a ast::ExprBytesLiteral) -> Self { +impl<'a, 'ast> From<&'a ast::ExprBytesLiteral<'ast>> for ExpressionRef<'a, 'ast> { + fn from(value: &'a ast::ExprBytesLiteral<'ast>) -> Self { Self::BytesLiteral(value) } } -impl<'a> From<&'a ast::ExprNumberLiteral> for ExpressionRef<'a> { +impl<'a> From<&'a ast::ExprNumberLiteral> for ExpressionRef<'a, '_> { fn from(value: &'a ast::ExprNumberLiteral) -> Self { Self::NumberLiteral(value) } } -impl<'a> From<&'a ast::ExprBooleanLiteral> for ExpressionRef<'a> { +impl<'a> From<&'a ast::ExprBooleanLiteral> for ExpressionRef<'a, '_> { fn from(value: &'a ast::ExprBooleanLiteral) -> Self { Self::BooleanLiteral(value) } } -impl<'a> From<&'a ast::ExprNoneLiteral> for ExpressionRef<'a> { +impl<'a> From<&'a ast::ExprNoneLiteral> for ExpressionRef<'a, '_> { fn from(value: &'a ast::ExprNoneLiteral) -> Self { Self::NoneLiteral(value) } } -impl<'a> From<&'a ast::ExprEllipsisLiteral> for ExpressionRef<'a> { +impl<'a> From<&'a ast::ExprEllipsisLiteral> for ExpressionRef<'a, '_> { fn from(value: &'a ast::ExprEllipsisLiteral) -> Self { Self::EllipsisLiteral(value) } } -impl<'a> From<&'a ast::ExprAttribute> for ExpressionRef<'a> { - fn from(value: &'a ast::ExprAttribute) -> Self { +impl<'a, 'ast> From<&'a ast::ExprAttribute<'ast>> for ExpressionRef<'a, 'ast> { + fn from(value: &'a ast::ExprAttribute<'ast>) -> Self { Self::Attribute(value) } } -impl<'a> From<&'a ast::ExprSubscript> for ExpressionRef<'a> { - fn from(value: &'a ast::ExprSubscript) -> Self { +impl<'a, 'ast> From<&'a ast::ExprSubscript<'ast>> for ExpressionRef<'a, 'ast> { + fn from(value: &'a ast::ExprSubscript<'ast>) -> Self { Self::Subscript(value) } } -impl<'a> From<&'a ast::ExprStarred> for ExpressionRef<'a> { - fn from(value: &'a ast::ExprStarred) -> Self { +impl<'a, 'ast> From<&'a ast::ExprStarred<'ast>> for ExpressionRef<'a, 'ast> { + fn from(value: &'a ast::ExprStarred<'ast>) -> Self { Self::Starred(value) } } -impl<'a> From<&'a ast::ExprName> for ExpressionRef<'a> { - fn from(value: &'a ast::ExprName) -> Self { +impl<'a, 'ast> From<&'a ast::ExprName<'ast>> for ExpressionRef<'a, 'ast> { + fn from(value: &'a ast::ExprName<'ast>) -> Self { Self::Name(value) } } -impl<'a> From<&'a ast::ExprList> for ExpressionRef<'a> { - fn from(value: &'a ast::ExprList) -> Self { +impl<'a, 'ast> From<&'a ast::ExprList<'ast>> for ExpressionRef<'a, 'ast> { + fn from(value: &'a ast::ExprList<'ast>) -> Self { Self::List(value) } } -impl<'a> From<&'a ast::ExprTuple> for ExpressionRef<'a> { - fn from(value: &'a ast::ExprTuple) -> Self { +impl<'a, 'ast> From<&'a ast::ExprTuple<'ast>> for ExpressionRef<'a, 'ast> { + fn from(value: &'a ast::ExprTuple<'ast>) -> Self { Self::Tuple(value) } } -impl<'a> From<&'a ast::ExprSlice> for ExpressionRef<'a> { - fn from(value: &'a ast::ExprSlice) -> Self { +impl<'a, 'ast> From<&'a ast::ExprSlice<'ast>> for ExpressionRef<'a, 'ast> { + fn from(value: &'a ast::ExprSlice<'ast>) -> Self { Self::Slice(value) } } -impl<'a> From<&'a ast::ExprIpyEscapeCommand> for ExpressionRef<'a> { - fn from(value: &'a ast::ExprIpyEscapeCommand) -> Self { +impl<'a, 'ast> From<&'a ast::ExprIpyEscapeCommand<'ast>> for ExpressionRef<'a, 'ast> { + fn from(value: &'a ast::ExprIpyEscapeCommand<'ast>) -> Self { Self::IpyEscapeCommand(value) } } -impl<'a> From> for AnyNodeRef<'a> { - fn from(value: ExpressionRef<'a>) -> Self { +impl<'a, 'ast> From> for AnyNodeRef<'a, 'ast> { + fn from(value: ExpressionRef<'a, 'ast>) -> Self { match value { ExpressionRef::BoolOp(expression) => AnyNodeRef::ExprBoolOp(expression), ExpressionRef::Named(expression) => AnyNodeRef::ExprNamed(expression), @@ -290,7 +290,7 @@ impl<'a> From> for AnyNodeRef<'a> { } } -impl Ranged for ExpressionRef<'_> { +impl Ranged for ExpressionRef<'_, '_> { fn range(&self) -> TextRange { match self { ExpressionRef::BoolOp(expression) => expression.range(), @@ -332,16 +332,16 @@ impl Ranged for ExpressionRef<'_> { /// Unowned pendant to all the literal variants of [`ast::Expr`] that stores a /// reference instead of an owned value. #[derive(Copy, Clone, Debug, PartialEq, is_macro::Is)] -pub enum LiteralExpressionRef<'a> { - StringLiteral(&'a ast::ExprStringLiteral), - BytesLiteral(&'a ast::ExprBytesLiteral), +pub enum LiteralExpressionRef<'a, 'ast> { + StringLiteral(&'a ast::ExprStringLiteral<'ast>), + BytesLiteral(&'a ast::ExprBytesLiteral<'ast>), NumberLiteral(&'a ast::ExprNumberLiteral), BooleanLiteral(&'a ast::ExprBooleanLiteral), NoneLiteral(&'a ast::ExprNoneLiteral), EllipsisLiteral(&'a ast::ExprEllipsisLiteral), } -impl Ranged for LiteralExpressionRef<'_> { +impl Ranged for LiteralExpressionRef<'_, '_> { fn range(&self) -> TextRange { match self { LiteralExpressionRef::StringLiteral(expression) => expression.range(), @@ -354,8 +354,8 @@ impl Ranged for LiteralExpressionRef<'_> { } } -impl<'a> From> for AnyNodeRef<'a> { - fn from(value: LiteralExpressionRef<'a>) -> Self { +impl<'a, 'ast> From> for AnyNodeRef<'a, 'ast> { + fn from(value: LiteralExpressionRef<'a, 'ast>) -> Self { match value { LiteralExpressionRef::StringLiteral(expression) => { AnyNodeRef::ExprStringLiteral(expression) @@ -379,7 +379,7 @@ impl<'a> From> for AnyNodeRef<'a> { } } -impl LiteralExpressionRef<'_> { +impl LiteralExpressionRef<'_, '_> { /// Returns `true` if the literal is either a string or bytes literal that /// is implicitly concatenated. pub fn is_implicit_concatenated(&self) -> bool { @@ -398,15 +398,15 @@ impl LiteralExpressionRef<'_> { /// An enum that holds a reference to a string-like expression from the AST. This includes string /// literals, bytes literals, and f-strings. #[derive(Copy, Clone, Debug, PartialEq)] -pub enum StringLike<'a> { - String(&'a ast::ExprStringLiteral), - Bytes(&'a ast::ExprBytesLiteral), - FString(&'a ast::ExprFString), +pub enum StringLike<'a, 'ast> { + String(&'a ast::ExprStringLiteral<'ast>), + Bytes(&'a ast::ExprBytesLiteral<'ast>), + FString(&'a ast::ExprFString<'ast>), } -impl<'a> StringLike<'a> { +impl<'a, 'ast> StringLike<'a, 'ast> { /// Returns an iterator over the [`StringLikePart`] contained in this string-like expression. - pub fn parts(&self) -> StringLikePartIter<'_> { + pub fn parts(&self) -> StringLikePartIter<'a, 'ast> { match self { StringLike::String(expr) => StringLikePartIter::String(expr.value.iter()), StringLike::Bytes(expr) => StringLikePartIter::Bytes(expr.value.iter()), @@ -415,25 +415,25 @@ impl<'a> StringLike<'a> { } } -impl<'a> From<&'a ast::ExprStringLiteral> for StringLike<'a> { - fn from(value: &'a ast::ExprStringLiteral) -> Self { +impl<'a, 'ast> From<&'a ast::ExprStringLiteral<'ast>> for StringLike<'a, 'ast> { + fn from(value: &'a ast::ExprStringLiteral<'ast>) -> Self { StringLike::String(value) } } -impl<'a> From<&'a ast::ExprBytesLiteral> for StringLike<'a> { - fn from(value: &'a ast::ExprBytesLiteral) -> Self { +impl<'a, 'ast> From<&'a ast::ExprBytesLiteral<'ast>> for StringLike<'a, 'ast> { + fn from(value: &'a ast::ExprBytesLiteral<'ast>) -> Self { StringLike::Bytes(value) } } -impl<'a> From<&'a ast::ExprFString> for StringLike<'a> { - fn from(value: &'a ast::ExprFString) -> Self { +impl<'a, 'ast> From<&'a ast::ExprFString<'ast>> for StringLike<'a, 'ast> { + fn from(value: &'a ast::ExprFString<'ast>) -> Self { StringLike::FString(value) } } -impl Ranged for StringLike<'_> { +impl Ranged for StringLike<'_, '_> { fn range(&self) -> TextRange { match self { StringLike::String(literal) => literal.range(), @@ -445,13 +445,13 @@ impl Ranged for StringLike<'_> { /// An enum that holds a reference to an individual part of a string-like expression. #[derive(Copy, Clone, Debug, PartialEq)] -pub enum StringLikePart<'a> { - String(&'a ast::StringLiteral), - Bytes(&'a ast::BytesLiteral), - FString(&'a ast::FString), +pub enum StringLikePart<'a, 'ast> { + String(&'a ast::StringLiteral<'ast>), + Bytes(&'a ast::BytesLiteral<'ast>), + FString(&'a ast::FString<'ast>), } -impl StringLikePart<'_> { +impl StringLikePart<'_, '_> { /// Returns the [`AnyStringFlags`] for the current string-like part. pub fn flags(&self) -> AnyStringFlags { match self { @@ -462,25 +462,25 @@ impl StringLikePart<'_> { } } -impl<'a> From<&'a ast::StringLiteral> for StringLikePart<'a> { - fn from(value: &'a ast::StringLiteral) -> Self { +impl<'a, 'ast> From<&'a ast::StringLiteral<'ast>> for StringLikePart<'a, 'ast> { + fn from(value: &'a ast::StringLiteral<'ast>) -> Self { StringLikePart::String(value) } } -impl<'a> From<&'a ast::BytesLiteral> for StringLikePart<'a> { - fn from(value: &'a ast::BytesLiteral) -> Self { +impl<'a, 'ast> From<&'a ast::BytesLiteral<'ast>> for StringLikePart<'a, 'ast> { + fn from(value: &'a ast::BytesLiteral<'ast>) -> Self { StringLikePart::Bytes(value) } } -impl<'a> From<&'a ast::FString> for StringLikePart<'a> { - fn from(value: &'a ast::FString) -> Self { +impl<'a, 'ast> From<&'a ast::FString<'ast>> for StringLikePart<'a, 'ast> { + fn from(value: &'a ast::FString<'ast>) -> Self { StringLikePart::FString(value) } } -impl Ranged for StringLikePart<'_> { +impl Ranged for StringLikePart<'_, '_> { fn range(&self) -> TextRange { match self { StringLikePart::String(part) => part.range(), @@ -493,14 +493,14 @@ impl Ranged for StringLikePart<'_> { /// An iterator over all the [`StringLikePart`] of a string-like expression. /// /// This is created by the [`StringLike::parts`] method. -pub enum StringLikePartIter<'a> { - String(std::slice::Iter<'a, ast::StringLiteral>), - Bytes(std::slice::Iter<'a, ast::BytesLiteral>), - FString(std::slice::Iter<'a, ast::FStringPart>), +pub enum StringLikePartIter<'a, 'ast> { + String(std::slice::Iter<'a, ast::StringLiteral<'ast>>), + Bytes(std::slice::Iter<'a, ast::BytesLiteral<'ast>>), + FString(std::slice::Iter<'a, ast::FStringPart<'ast>>), } -impl<'a> Iterator for StringLikePartIter<'a> { - type Item = StringLikePart<'a>; +impl<'a, 'ast> Iterator for StringLikePartIter<'a, 'ast> { + type Item = StringLikePart<'a, 'ast>; fn next(&mut self) -> Option { let part = match self { @@ -529,5 +529,5 @@ impl<'a> Iterator for StringLikePartIter<'a> { } } -impl FusedIterator for StringLikePartIter<'_> {} -impl ExactSizeIterator for StringLikePartIter<'_> {} +impl FusedIterator for StringLikePartIter<'_, '_> {} +impl ExactSizeIterator for StringLikePartIter<'_, '_> {} diff --git a/crates/ruff_python_ast/src/hashable.rs b/crates/ruff_python_ast/src/hashable.rs index 7332716db4..b6a69fa5f7 100644 --- a/crates/ruff_python_ast/src/hashable.rs +++ b/crates/ruff_python_ast/src/hashable.rs @@ -5,36 +5,36 @@ use crate::Expr; use crate::comparable::ComparableExpr; /// Wrapper around `Expr` that implements `Hash` and `PartialEq`. -pub struct HashableExpr<'a>(&'a Expr); +pub struct HashableExpr<'a, 'ast>(&'a Expr<'ast>); -impl Hash for HashableExpr<'_> { +impl Hash for HashableExpr<'_, '_> { fn hash(&self, state: &mut H) { let comparable = ComparableExpr::from(self.0); comparable.hash(state); } } -impl PartialEq for HashableExpr<'_> { +impl PartialEq for HashableExpr<'_, '_> { fn eq(&self, other: &Self) -> bool { let comparable = ComparableExpr::from(self.0); comparable == ComparableExpr::from(other.0) } } -impl Eq for HashableExpr<'_> {} +impl Eq for HashableExpr<'_, '_> {} -impl<'a> From<&'a Expr> for HashableExpr<'a> { - fn from(expr: &'a Expr) -> Self { +impl<'a, 'ast> From<&'a Expr<'ast>> for HashableExpr<'a, 'ast> { + fn from(expr: &'a Expr<'ast>) -> Self { Self(expr) } } -impl<'a> HashableExpr<'a> { - pub const fn from_expr(expr: &'a Expr) -> Self { +impl<'a, 'ast> HashableExpr<'a, 'ast> { + pub const fn from_expr(expr: &'a Expr<'ast>) -> Self { Self(expr) } - pub const fn as_expr(&self) -> &'a Expr { + pub const fn as_expr(&self) -> &'a Expr<'ast> { self.0 } } diff --git a/crates/ruff_python_ast/src/helpers.rs b/crates/ruff_python_ast/src/helpers.rs index 6613bd9dd0..9a39f7493d 100644 --- a/crates/ruff_python_ast/src/helpers.rs +++ b/crates/ruff_python_ast/src/helpers.rs @@ -1,13 +1,4 @@ -use std::borrow::Cow; -use std::path::Path; - -use rustc_hash::FxHashMap; - -use ruff_python_trivia::{indentation_at_offset, CommentRanges, SimpleTokenKind, SimpleTokenizer}; -use ruff_source_file::Locator; -use ruff_text_size::{Ranged, TextLen, TextRange, TextSize}; - -use crate::name::{Name, QualifiedName, QualifiedNameBuilder}; +use crate::name::{QualifiedName, QualifiedNameBuilder}; use crate::parenthesize::parenthesized_range; use crate::statement_visitor::StatementVisitor; use crate::visitor::Visitor; @@ -16,6 +7,13 @@ use crate::{ Pattern, Stmt, TypeParam, }; use crate::{AnyNodeRef, ExprContext}; +use ruff_allocator::{Allocator, CloneIn}; +use ruff_python_trivia::{indentation_at_offset, CommentRanges, SimpleTokenKind, SimpleTokenizer}; +use ruff_source_file::Locator; +use ruff_text_size::{Ranged, TextLen, TextRange, TextSize}; +use rustc_hash::FxHashMap; +use std::borrow::Cow; +use std::path::Path; /// Return `true` if the `Stmt` is a compound statement (as opposed to a simple statement). pub const fn is_compound_statement(stmt: &Stmt) -> bool { @@ -58,7 +56,7 @@ where // Ex) `list()` if arguments.is_empty() { if let Expr::Name(ast::ExprName { id, .. }) = func.as_ref() { - if !is_iterable_initializer(id.as_str(), |id| is_builtin(id)) { + if !is_iterable_initializer(id, |id| is_builtin(id)) { return true; } return false; @@ -128,7 +126,7 @@ where /// Call `func` over every `Expr` in `expr`, returning `true` if any expression /// returns `true`.. -pub fn any_over_expr(expr: &Expr, func: &dyn Fn(&Expr) -> bool) -> bool { +pub fn any_over_expr<'ast>(expr: &Expr<'ast>, func: &dyn Fn(&Expr<'ast>) -> bool) -> bool { if func(expr) { return true; } @@ -315,9 +313,9 @@ pub fn any_over_pattern(pattern: &Pattern, func: &dyn Fn(&Expr) -> bool) -> bool } } -pub fn any_over_f_string_element( - element: &ast::FStringElement, - func: &dyn Fn(&Expr) -> bool, +pub fn any_over_f_string_element<'ast>( + element: &ast::FStringElement<'ast>, + func: &dyn Fn(&Expr<'ast>) -> bool, ) -> bool { match element { ast::FStringElement::Literal(_) => false, @@ -624,7 +622,9 @@ pub const fn is_mutable_iterable_initializer(expr: &Expr) -> bool { } /// Extract the names of all handled exceptions. -pub fn extract_handled_exceptions(handlers: &[ExceptHandler]) -> Vec<&Expr> { +pub fn extract_handled_exceptions<'a, 'ast>( + handlers: &'a [ExceptHandler<'ast>], +) -> Vec<&'a Expr<'ast>> { let mut handled_exceptions = Vec::new(); for handler in handlers { match handler { @@ -647,7 +647,7 @@ pub fn extract_handled_exceptions(handlers: &[ExceptHandler]) -> Vec<&Expr> { /// Given an [`Expr`] that can be callable or not (like a decorator, which could /// be used with or without explicit call syntax), return the underlying /// callable. -pub fn map_callable(decorator: &Expr) -> &Expr { +pub fn map_callable<'a, 'ast>(decorator: &'a Expr<'ast>) -> &'a Expr<'ast> { if let Expr::Call(ast::ExprCall { func, .. }) = decorator { // Ex) `@decorator()` func @@ -659,7 +659,7 @@ pub fn map_callable(decorator: &Expr) -> &Expr { /// Given an [`Expr`] that can be a [`ExprSubscript`][ast::ExprSubscript] or not /// (like an annotation that may be generic or not), return the underlying expr. -pub fn map_subscript(expr: &Expr) -> &Expr { +pub fn map_subscript<'a, 'ast>(expr: &'a Expr<'ast>) -> &'a Expr<'ast> { if let Expr::Subscript(ast::ExprSubscript { value, .. }) = expr { // Ex) `Iterable[T]` => return `Iterable` value @@ -670,7 +670,7 @@ pub fn map_subscript(expr: &Expr) -> &Expr { } /// Given an [`Expr`] that can be starred, return the underlying starred expression. -pub fn map_starred(expr: &Expr) -> &Expr { +pub fn map_starred<'a, 'ast>(expr: &'a Expr<'ast>) -> &'a Expr<'ast> { if let Expr::Starred(ast::ExprStarred { value, .. }) = expr { // Ex) `*args` value @@ -690,8 +690,8 @@ where any_over_body(body, &|expr| { if let Expr::Call(ast::ExprCall { func, .. }) = expr { if let Expr::Name(ast::ExprName { id, .. }) = func.as_ref() { - if matches!(id.as_str(), "locals" | "globals" | "vars" | "exec" | "eval") { - if is_builtin(id.as_str()) { + if matches!(*id, "locals" | "globals" | "vars" | "exec" | "eval") { + if is_builtin(id) { return true; } } @@ -877,10 +877,10 @@ pub fn resolve_imported_module_path<'a>( #[derive(Debug, Default)] pub struct NameFinder<'a> { /// A map from identifier to defining expression. - pub names: FxHashMap<&'a str, &'a ast::ExprName>, + pub names: FxHashMap<&'a str, &'a ast::ExprName<'a>>, } -impl<'a> Visitor<'a> for NameFinder<'a> { +impl<'a> Visitor<'a, '_> for NameFinder<'a> { fn visit_expr(&mut self, expr: &'a Expr) { if let Expr::Name(name) = expr { self.names.insert(&name.id, name); @@ -893,10 +893,10 @@ impl<'a> Visitor<'a> for NameFinder<'a> { #[derive(Debug, Default)] pub struct StoredNameFinder<'a> { /// A map from identifier to defining expression. - pub names: FxHashMap<&'a str, &'a ast::ExprName>, + pub names: FxHashMap<&'a str, &'a ast::ExprName<'a>>, } -impl<'a> Visitor<'a> for StoredNameFinder<'a> { +impl<'a> Visitor<'a, '_> for StoredNameFinder<'a> { fn visit_expr(&mut self, expr: &'a Expr) { if let Expr::Name(name) = expr { if name.ctx.is_store() { @@ -909,13 +909,13 @@ impl<'a> Visitor<'a> for StoredNameFinder<'a> { /// A [`Visitor`] that collects all `return` statements in a function or method. #[derive(Default)] -pub struct ReturnStatementVisitor<'a> { - pub returns: Vec<&'a ast::StmtReturn>, +pub struct ReturnStatementVisitor<'a, 'ast> { + pub returns: Vec<&'a ast::StmtReturn<'ast>>, pub is_generator: bool, } -impl<'a> Visitor<'a> for ReturnStatementVisitor<'a> { - fn visit_stmt(&mut self, stmt: &'a Stmt) { +impl<'a, 'ast> Visitor<'a, 'ast> for ReturnStatementVisitor<'a, 'ast> { + fn visit_stmt(&mut self, stmt: &'a Stmt<'ast>) { match stmt { Stmt::FunctionDef(_) | Stmt::ClassDef(_) => { // Don't recurse. @@ -925,7 +925,7 @@ impl<'a> Visitor<'a> for ReturnStatementVisitor<'a> { } } - fn visit_expr(&mut self, expr: &'a Expr) { + fn visit_expr(&mut self, expr: &'a Expr<'ast>) { if let Expr::Yield(_) | Expr::YieldFrom(_) = expr { self.is_generator = true; } else { @@ -936,12 +936,12 @@ impl<'a> Visitor<'a> for ReturnStatementVisitor<'a> { /// A [`StatementVisitor`] that collects all `raise` statements in a function or method. #[derive(Default)] -pub struct RaiseStatementVisitor<'a> { - pub raises: Vec<(TextRange, Option<&'a Expr>, Option<&'a Expr>)>, +pub struct RaiseStatementVisitor<'a, 'ast> { + pub raises: Vec<(TextRange, Option<&'a Expr<'ast>>, Option<&'a Expr<'ast>>)>, } -impl<'a> StatementVisitor<'a> for RaiseStatementVisitor<'a> { - fn visit_stmt(&mut self, stmt: &'a Stmt) { +impl<'a, 'ast> StatementVisitor<'a, 'ast> for RaiseStatementVisitor<'a, 'ast> { + fn visit_stmt(&mut self, stmt: &'a Stmt<'ast>) { match stmt { Stmt::Raise(ast::StmtRaise { exc, @@ -983,7 +983,7 @@ pub struct AwaitVisitor { pub seen_await: bool, } -impl Visitor<'_> for AwaitVisitor { +impl Visitor<'_, '_> for AwaitVisitor { fn visit_stmt(&mut self, stmt: &Stmt) { match stmt { Stmt::FunctionDef(_) | Stmt::ClassDef(_) => (), @@ -1016,7 +1016,7 @@ pub fn is_docstring_stmt(stmt: &Stmt) -> bool { } /// Check if a node is part of a conditional branch. -pub fn on_conditional_branch<'a>(parents: &mut impl Iterator) -> bool { +pub fn on_conditional_branch<'a>(parents: &mut impl Iterator>) -> bool { parents.any(|parent| { if matches!(parent, Stmt::If(_) | Stmt::While(_) | Stmt::Match(_)) { return true; @@ -1031,7 +1031,7 @@ pub fn on_conditional_branch<'a>(parents: &mut impl Iterator) - } /// Check if a node is in a nested block. -pub fn in_nested_block<'a>(mut parents: impl Iterator) -> bool { +pub fn in_nested_block<'a>(mut parents: impl Iterator>) -> bool { parents.any(|parent| { matches!( parent, @@ -1041,7 +1041,7 @@ pub fn in_nested_block<'a>(mut parents: impl Iterator) -> bool } /// Check if a node represents an unpacking assignment. -pub fn is_unpacking_assignment(parent: &Stmt, child: &Expr) -> bool { +pub fn is_unpacking_assignment<'ast>(parent: &Stmt<'ast>, child: &Expr<'ast>) -> bool { match parent { Stmt::With(ast::StmtWith { items, .. }) => items.iter().any(|item| { if let Some(optional_vars) = &item.optional_vars { @@ -1196,7 +1196,7 @@ impl Truthiness { func, arguments, .. }) => { if let Expr::Name(ast::ExprName { id, .. }) = func.as_ref() { - if is_iterable_initializer(id.as_str(), |id| is_builtin(id)) { + if is_iterable_initializer(id, |id| is_builtin(id)) { if arguments.is_empty() { // Ex) `list()` Self::Falsey @@ -1372,18 +1372,22 @@ pub fn generate_comparison( } /// Format the expression as a PEP 604-style optional. -pub fn pep_604_optional(expr: &Expr) -> Expr { +pub fn pep_604_optional<'a, 'ast>(expr: &'a Expr<'ast>, allocator: &'ast Allocator) -> Expr<'ast> { ast::ExprBinOp { - left: Box::new(expr.clone()), + // FIXME I think this duple allocates + left: ruff_allocator::Box::new_in(expr.clone_in(allocator), allocator), op: Operator::BitOr, - right: Box::new(Expr::NoneLiteral(ast::ExprNoneLiteral::default())), + right: ruff_allocator::Box::new_in( + Expr::NoneLiteral(ast::ExprNoneLiteral::default()), + allocator, + ), range: TextRange::default(), } .into() } /// Format the expressions as a PEP 604-style union. -pub fn pep_604_union(elts: &[Expr]) -> Expr { +pub fn pep_604_union<'a, 'ast>(elts: &'a [Expr<'ast>], allocator: &'ast Allocator) -> Expr<'ast> { match elts { [] => Expr::Tuple(ast::ExprTuple { elts: vec![], @@ -1391,34 +1395,52 @@ pub fn pep_604_union(elts: &[Expr]) -> Expr { range: TextRange::default(), parenthesized: true, }), - [Expr::Tuple(ast::ExprTuple { elts, .. })] => pep_604_union(elts), - [elt] => elt.clone(), + [Expr::Tuple(ast::ExprTuple { elts, .. })] => pep_604_union(elts, allocator), + [elt] => elt.clone_in(allocator), [rest @ .., elt] => Expr::BinOp(ast::ExprBinOp { - left: Box::new(pep_604_union(rest)), + left: ruff_allocator::Box::new_in(pep_604_union(rest, allocator), allocator), op: Operator::BitOr, - right: Box::new(pep_604_union(&[elt.clone()])), + right: ruff_allocator::Box::new_in( + pep_604_union(&[elt.clone_in(allocator)], allocator), + allocator, + ), range: TextRange::default(), }), } } /// Format the expression as a `typing.Optional`-style optional. -pub fn typing_optional(elt: Expr, binding: Name) -> Expr { +pub fn typing_optional<'ast>( + elt: Expr<'ast>, + binding: &'ast str, + allocator: &'ast Allocator, +) -> Expr<'ast> { Expr::Subscript(ast::ExprSubscript { - value: Box::new(Expr::Name(ast::ExprName { - id: binding, - range: TextRange::default(), - ctx: ExprContext::Load, - })), - slice: Box::new(elt), + value: ruff_allocator::Box::new_in( + Expr::Name(ast::ExprName { + id: binding, + range: TextRange::default(), + ctx: ExprContext::Load, + }), + allocator, + ), + slice: ruff_allocator::Box::new_in(elt, allocator), ctx: ExprContext::Load, range: TextRange::default(), }) } /// Format the expressions as a `typing.Union`-style union. -pub fn typing_union(elts: &[Expr], binding: Name) -> Expr { - fn tuple(elts: &[Expr], binding: Name) -> Expr { +pub fn typing_union<'a, 'ast>( + elts: &'a [Expr<'ast>], + binding: &'ast str, + allocator: &'ast Allocator, +) -> Expr<'ast> { + fn tuple<'a, 'ast>( + elts: &'a [Expr<'ast>], + binding: &'ast str, + allocator: &'ast Allocator, + ) -> Expr<'ast> { match elts { [] => Expr::Tuple(ast::ExprTuple { elts: vec![], @@ -1426,24 +1448,27 @@ pub fn typing_union(elts: &[Expr], binding: Name) -> Expr { range: TextRange::default(), parenthesized: true, }), - [Expr::Tuple(ast::ExprTuple { elts, .. })] => typing_union(elts, binding), - [elt] => elt.clone(), + [Expr::Tuple(ast::ExprTuple { elts, .. })] => typing_union(elts, binding, allocator), + [elt] => elt.clone_in(allocator), [rest @ .., elt] => Expr::BinOp(ast::ExprBinOp { - left: Box::new(tuple(rest, binding)), + left: ruff_allocator::Box::new_in(tuple(rest, binding, allocator), allocator), op: Operator::BitOr, - right: Box::new(elt.clone()), + right: ruff_allocator::Box::new_in(elt.clone_in(allocator), allocator), range: TextRange::default(), }), } } Expr::Subscript(ast::ExprSubscript { - value: Box::new(Expr::Name(ast::ExprName { - id: binding.clone(), - range: TextRange::default(), - ctx: ExprContext::Load, - })), - slice: Box::new(tuple(elts, binding)), + value: ruff_allocator::Box::new_in( + Expr::Name(ast::ExprName { + id: binding, + range: TextRange::default(), + ctx: ExprContext::Load, + }), + allocator, + ), + slice: ruff_allocator::Box::new_in(tuple(elts, binding, allocator), allocator), ctx: ExprContext::Load, range: TextRange::default(), }) diff --git a/crates/ruff_python_ast/src/identifier.rs b/crates/ruff_python_ast/src/identifier.rs index 566b9c7925..3c4bcd45b9 100644 --- a/crates/ruff_python_ast/src/identifier.rs +++ b/crates/ruff_python_ast/src/identifier.rs @@ -20,7 +20,7 @@ pub trait Identifier { fn identifier(&self) -> TextRange; } -impl Identifier for ast::StmtFunctionDef { +impl Identifier for ast::StmtFunctionDef<'_> { /// Return the [`TextRange`] of the identifier in the given function definition. /// /// For example, return the range of `f` in: @@ -33,7 +33,7 @@ impl Identifier for ast::StmtFunctionDef { } } -impl Identifier for ast::StmtClassDef { +impl Identifier for ast::StmtClassDef<'_> { /// Return the [`TextRange`] of the identifier in the given class definition. /// /// For example, return the range of `C` in: @@ -46,7 +46,7 @@ impl Identifier for ast::StmtClassDef { } } -impl Identifier for Stmt { +impl Identifier for Stmt<'_> { /// Return the [`TextRange`] of the identifier in the given statement. /// /// For example, return the range of `f` in: @@ -63,7 +63,7 @@ impl Identifier for Stmt { } } -impl Identifier for Parameter { +impl Identifier for Parameter<'_> { /// Return the [`TextRange`] for the identifier defining an [`Parameter`]. /// /// For example, return the range of `x` in: @@ -76,7 +76,7 @@ impl Identifier for Parameter { } } -impl Identifier for ParameterWithDefault { +impl Identifier for ParameterWithDefault<'_> { /// Return the [`TextRange`] for the identifier defining an [`ParameterWithDefault`]. /// /// For example, return the range of `x` in: @@ -89,7 +89,7 @@ impl Identifier for ParameterWithDefault { } } -impl Identifier for Alias { +impl Identifier for Alias<'_> { /// Return the [`TextRange`] for the identifier defining an [`Alias`]. /// /// For example, return the range of `x` in: diff --git a/crates/ruff_python_ast/src/name.rs b/crates/ruff_python_ast/src/name.rs index 58da7e1459..0de244a858 100644 --- a/crates/ruff_python_ast/src/name.rs +++ b/crates/ruff_python_ast/src/name.rs @@ -378,9 +378,7 @@ impl<'a> UnqualifiedName<'a> { let attr1 = match expr { Expr::Attribute(attr1) => attr1, // Ex) `foo` - Expr::Name(nodes::ExprName { id, .. }) => { - return Some(Self::from_slice(&[id.as_str()])) - } + Expr::Name(nodes::ExprName { id, .. }) => return Some(Self::from_slice(&[id])), _ => return None, }; @@ -388,7 +386,7 @@ impl<'a> UnqualifiedName<'a> { Expr::Attribute(attr2) => attr2, // Ex) `foo.bar` Expr::Name(nodes::ExprName { id, .. }) => { - return Some(Self::from_slice(&[id.as_str(), attr1.attr.as_str()])) + return Some(Self::from_slice(&[id, attr1.attr.as_str()])) } _ => return None, }; @@ -398,7 +396,7 @@ impl<'a> UnqualifiedName<'a> { // Ex) `foo.bar.baz` Expr::Name(nodes::ExprName { id, .. }) => { return Some(Self::from_slice(&[ - id.as_str(), + id, attr2.attr.as_str(), attr1.attr.as_str(), ])); @@ -411,7 +409,7 @@ impl<'a> UnqualifiedName<'a> { // Ex) `foo.bar.baz.bop` Expr::Name(nodes::ExprName { id, .. }) => { return Some(Self::from_slice(&[ - id.as_str(), + id, attr3.attr.as_str(), attr2.attr.as_str(), attr1.attr.as_str(), @@ -425,7 +423,7 @@ impl<'a> UnqualifiedName<'a> { // Ex) `foo.bar.baz.bop.bap` Expr::Name(nodes::ExprName { id, .. }) => { return Some(Self::from_slice(&[ - id.as_str(), + id, attr4.attr.as_str(), attr3.attr.as_str(), attr2.attr.as_str(), @@ -440,7 +438,7 @@ impl<'a> UnqualifiedName<'a> { // Ex) `foo.bar.baz.bop.bap.bab` Expr::Name(nodes::ExprName { id, .. }) => { return Some(Self::from_slice(&[ - id.as_str(), + id, attr5.attr.as_str(), attr4.attr.as_str(), attr3.attr.as_str(), @@ -456,7 +454,7 @@ impl<'a> UnqualifiedName<'a> { // Ex) `foo.bar.baz.bop.bap.bab.bob` Expr::Name(nodes::ExprName { id, .. }) => { return Some(Self::from_slice(&[ - id.as_str(), + id, attr6.attr.as_str(), attr5.attr.as_str(), attr4.attr.as_str(), @@ -473,7 +471,7 @@ impl<'a> UnqualifiedName<'a> { // Ex) `foo.bar.baz.bop.bap.bab.bob.bib` Expr::Name(nodes::ExprName { id, .. }) => { return Some(Self(SegmentsVec::from([ - id.as_str(), + id, attr7.attr.as_str(), attr6.attr.as_str(), attr5.attr.as_str(), @@ -497,7 +495,7 @@ impl<'a> UnqualifiedName<'a> { &*attr.value } Expr::Name(nodes::ExprName { id, .. }) => { - segments.push(id.as_str()); + segments.push(id); break; } _ => { diff --git a/crates/ruff_python_ast/src/node.rs b/crates/ruff_python_ast/src/node.rs index d1ba8de8e0..c72a5a89c5 100644 --- a/crates/ruff_python_ast/src/node.rs +++ b/crates/ruff_python_ast/src/node.rs @@ -12,128 +12,131 @@ use crate::{ use ruff_text_size::{Ranged, TextRange}; use std::ptr::NonNull; -pub trait AstNode: Ranged { - type Ref<'a>; +pub trait AstNode<'ast>: Ranged { + type Ref<'a> + where + 'ast: 'a; - fn cast(kind: AnyNode) -> Option + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized; - fn cast_ref(kind: AnyNodeRef<'_>) -> Option>; + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option>; fn can_cast(kind: NodeKind) -> bool; /// Returns the [`AnyNodeRef`] referencing this node. - fn as_any_node_ref(&self) -> AnyNodeRef; + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast>; /// Consumes `self` and returns its [`AnyNode`] representation. - fn into_any_node(self) -> AnyNode; + fn into_any_node(self) -> AnyNode<'ast>; fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized; + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized; } -#[derive(Clone, Debug, is_macro::Is, PartialEq)] -pub enum AnyNode { - ModModule(ast::ModModule), - ModExpression(ast::ModExpression), - StmtFunctionDef(ast::StmtFunctionDef), - StmtClassDef(ast::StmtClassDef), - StmtReturn(ast::StmtReturn), - StmtDelete(ast::StmtDelete), - StmtTypeAlias(ast::StmtTypeAlias), - StmtAssign(ast::StmtAssign), - StmtAugAssign(ast::StmtAugAssign), - StmtAnnAssign(ast::StmtAnnAssign), - StmtFor(ast::StmtFor), - StmtWhile(ast::StmtWhile), - StmtIf(ast::StmtIf), - StmtWith(ast::StmtWith), - StmtMatch(ast::StmtMatch), - StmtRaise(ast::StmtRaise), - StmtTry(ast::StmtTry), - StmtAssert(ast::StmtAssert), - StmtImport(ast::StmtImport), - StmtImportFrom(ast::StmtImportFrom), - StmtGlobal(ast::StmtGlobal), - StmtNonlocal(ast::StmtNonlocal), - StmtExpr(ast::StmtExpr), +#[derive(Debug, is_macro::Is, PartialEq)] +pub enum AnyNode<'ast> { + ModModule(ast::ModModule<'ast>), + ModExpression(ast::ModExpression<'ast>), + StmtFunctionDef(ast::StmtFunctionDef<'ast>), + StmtClassDef(ast::StmtClassDef<'ast>), + StmtReturn(ast::StmtReturn<'ast>), + StmtDelete(ast::StmtDelete<'ast>), + StmtTypeAlias(ast::StmtTypeAlias<'ast>), + StmtAssign(ast::StmtAssign<'ast>), + StmtAugAssign(ast::StmtAugAssign<'ast>), + StmtAnnAssign(ast::StmtAnnAssign<'ast>), + StmtFor(ast::StmtFor<'ast>), + StmtWhile(ast::StmtWhile<'ast>), + StmtIf(ast::StmtIf<'ast>), + StmtWith(ast::StmtWith<'ast>), + StmtMatch(ast::StmtMatch<'ast>), + StmtRaise(ast::StmtRaise<'ast>), + StmtTry(ast::StmtTry<'ast>), + StmtAssert(ast::StmtAssert<'ast>), + StmtImport(ast::StmtImport<'ast>), + StmtImportFrom(ast::StmtImportFrom<'ast>), + StmtGlobal(ast::StmtGlobal<'ast>), + StmtNonlocal(ast::StmtNonlocal<'ast>), + StmtExpr(ast::StmtExpr<'ast>), StmtPass(ast::StmtPass), StmtBreak(ast::StmtBreak), StmtContinue(ast::StmtContinue), - StmtIpyEscapeCommand(ast::StmtIpyEscapeCommand), - ExprBoolOp(ast::ExprBoolOp), - ExprNamed(ast::ExprNamed), - ExprBinOp(ast::ExprBinOp), - ExprUnaryOp(ast::ExprUnaryOp), - ExprLambda(ast::ExprLambda), - ExprIf(ast::ExprIf), - ExprDict(ast::ExprDict), - ExprSet(ast::ExprSet), - ExprListComp(ast::ExprListComp), - ExprSetComp(ast::ExprSetComp), - ExprDictComp(ast::ExprDictComp), - ExprGenerator(ast::ExprGenerator), - ExprAwait(ast::ExprAwait), - ExprYield(ast::ExprYield), - ExprYieldFrom(ast::ExprYieldFrom), - ExprCompare(ast::ExprCompare), - ExprCall(ast::ExprCall), - ExprFString(ast::ExprFString), - ExprStringLiteral(ast::ExprStringLiteral), - ExprBytesLiteral(ast::ExprBytesLiteral), + StmtIpyEscapeCommand(ast::StmtIpyEscapeCommand<'ast>), + ExprBoolOp(ast::ExprBoolOp<'ast>), + ExprNamed(ast::ExprNamed<'ast>), + ExprBinOp(ast::ExprBinOp<'ast>), + ExprUnaryOp(ast::ExprUnaryOp<'ast>), + ExprLambda(ast::ExprLambda<'ast>), + ExprIf(ast::ExprIf<'ast>), + ExprDict(ast::ExprDict<'ast>), + ExprSet(ast::ExprSet<'ast>), + ExprListComp(ast::ExprListComp<'ast>), + ExprSetComp(ast::ExprSetComp<'ast>), + ExprDictComp(ast::ExprDictComp<'ast>), + ExprGenerator(ast::ExprGenerator<'ast>), + ExprAwait(ast::ExprAwait<'ast>), + ExprYield(ast::ExprYield<'ast>), + ExprYieldFrom(ast::ExprYieldFrom<'ast>), + ExprCompare(ast::ExprCompare<'ast>), + ExprCall(ast::ExprCall<'ast>), + ExprFString(ast::ExprFString<'ast>), + ExprStringLiteral(ast::ExprStringLiteral<'ast>), + ExprBytesLiteral(ast::ExprBytesLiteral<'ast>), ExprNumberLiteral(ast::ExprNumberLiteral), ExprBooleanLiteral(ast::ExprBooleanLiteral), ExprNoneLiteral(ast::ExprNoneLiteral), ExprEllipsisLiteral(ast::ExprEllipsisLiteral), - ExprAttribute(ast::ExprAttribute), - ExprSubscript(ast::ExprSubscript), - ExprStarred(ast::ExprStarred), - ExprName(ast::ExprName), - ExprList(ast::ExprList), - ExprTuple(ast::ExprTuple), - ExprSlice(ast::ExprSlice), - ExprIpyEscapeCommand(ast::ExprIpyEscapeCommand), - ExceptHandlerExceptHandler(ast::ExceptHandlerExceptHandler), - FStringExpressionElement(ast::FStringExpressionElement), - FStringLiteralElement(ast::FStringLiteralElement), - FStringFormatSpec(ast::FStringFormatSpec), - PatternMatchValue(ast::PatternMatchValue), + ExprAttribute(ast::ExprAttribute<'ast>), + ExprSubscript(ast::ExprSubscript<'ast>), + ExprStarred(ast::ExprStarred<'ast>), + ExprName(ast::ExprName<'ast>), + ExprList(ast::ExprList<'ast>), + ExprTuple(ast::ExprTuple<'ast>), + ExprSlice(ast::ExprSlice<'ast>), + ExprIpyEscapeCommand(ast::ExprIpyEscapeCommand<'ast>), + ExceptHandlerExceptHandler(ast::ExceptHandlerExceptHandler<'ast>), + FStringExpressionElement(ast::FStringExpressionElement<'ast>), + FStringLiteralElement(ast::FStringLiteralElement<'ast>), + FStringFormatSpec(ast::FStringFormatSpec<'ast>), + PatternMatchValue(ast::PatternMatchValue<'ast>), PatternMatchSingleton(ast::PatternMatchSingleton), - PatternMatchSequence(ast::PatternMatchSequence), - PatternMatchMapping(ast::PatternMatchMapping), - PatternMatchClass(ast::PatternMatchClass), - PatternMatchStar(ast::PatternMatchStar), - PatternMatchAs(ast::PatternMatchAs), - PatternMatchOr(ast::PatternMatchOr), - PatternArguments(PatternArguments), - PatternKeyword(PatternKeyword), - Comprehension(Comprehension), - Arguments(Arguments), - Parameters(Parameters), - Parameter(Parameter), - ParameterWithDefault(ParameterWithDefault), - Keyword(Keyword), - Alias(Alias), - WithItem(WithItem), - MatchCase(MatchCase), - Decorator(Decorator), - ElifElseClause(ast::ElifElseClause), - TypeParams(TypeParams), - TypeParamTypeVar(TypeParamTypeVar), - TypeParamTypeVarTuple(TypeParamTypeVarTuple), - TypeParamParamSpec(TypeParamParamSpec), - FString(ast::FString), - StringLiteral(ast::StringLiteral), - BytesLiteral(ast::BytesLiteral), + PatternMatchSequence(ast::PatternMatchSequence<'ast>), + PatternMatchMapping(ast::PatternMatchMapping<'ast>), + PatternMatchClass(ast::PatternMatchClass<'ast>), + PatternMatchStar(ast::PatternMatchStar<'ast>), + PatternMatchAs(ast::PatternMatchAs<'ast>), + PatternMatchOr(ast::PatternMatchOr<'ast>), + PatternArguments(PatternArguments<'ast>), + PatternKeyword(PatternKeyword<'ast>), + Comprehension(Comprehension<'ast>), + Arguments(Arguments<'ast>), + Parameters(Parameters<'ast>), + Parameter(Parameter<'ast>), + ParameterWithDefault(ParameterWithDefault<'ast>), + Keyword(Keyword<'ast>), + Alias(Alias<'ast>), + WithItem(WithItem<'ast>), + MatchCase(MatchCase<'ast>), + Decorator(Decorator<'ast>), + ElifElseClause(ast::ElifElseClause<'ast>), + TypeParams(TypeParams<'ast>), + TypeParamTypeVar(TypeParamTypeVar<'ast>), + TypeParamTypeVarTuple(TypeParamTypeVarTuple<'ast>), + TypeParamParamSpec(TypeParamParamSpec<'ast>), + FString(ast::FString<'ast>), + StringLiteral(ast::StringLiteral<'ast>), + BytesLiteral(ast::BytesLiteral<'ast>), } -impl AnyNode { - pub fn statement(self) -> Option { +impl<'ast> AnyNode<'ast> { + pub fn statement(self) -> Option> { Stmt::cast(self) } - pub fn expression(self) -> Option { + pub fn expression(self) -> Option> { match self { AnyNode::ExprBoolOp(node) => Some(Expr::BoolOp(node)), AnyNode::ExprNamed(node) => Some(Expr::Named(node)), @@ -230,7 +233,7 @@ impl AnyNode { } } - pub fn module(self) -> Option { + pub fn module(self) -> Option> { match self { AnyNode::ModModule(node) => Some(Mod::Module(node)), AnyNode::ModExpression(node) => Some(Mod::Expression(node)), @@ -327,7 +330,7 @@ impl AnyNode { } } - pub fn pattern(self) -> Option { + pub fn pattern(self) -> Option> { match self { AnyNode::PatternMatchValue(node) => Some(Pattern::MatchValue(node)), AnyNode::PatternMatchSingleton(node) => Some(Pattern::MatchSingleton(node)), @@ -424,7 +427,7 @@ impl AnyNode { } } - pub fn except_handler(self) -> Option { + pub fn except_handler(self) -> Option> { match self { AnyNode::ExceptHandlerExceptHandler(node) => Some(ExceptHandler::ExceptHandler(node)), @@ -541,7 +544,7 @@ impl AnyNode { self.as_ref().is_except_handler() } - pub const fn as_ref(&self) -> AnyNodeRef { + pub const fn as_ref(&self) -> AnyNodeRef<'_, 'ast> { match self { Self::ModModule(node) => AnyNodeRef::ModModule(node), Self::ModExpression(node) => AnyNodeRef::ModExpression(node), @@ -643,10 +646,10 @@ impl AnyNode { } } -impl AstNode for ast::ModModule { - type Ref<'a> = &'a Self; +impl<'ast> AstNode<'ast> for ast::ModModule<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; - fn cast(kind: AnyNode) -> Option + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -657,7 +660,7 @@ impl AstNode for ast::ModModule { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::ModModule(node) = kind { Some(node) } else { @@ -669,27 +672,28 @@ impl AstNode for ast::ModModule { matches!(kind, NodeKind::ModModule) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::ModModule { body, range: _ } = self; visitor.visit_body(body); } } -impl AstNode for ast::ModExpression { - type Ref<'a> = &'a Self; +impl<'ast> AstNode<'ast> for ast::ModExpression<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; - fn cast(kind: AnyNode) -> Option + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -700,7 +704,7 @@ impl AstNode for ast::ModExpression { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::ModExpression(node) = kind { Some(node) } else { @@ -712,26 +716,27 @@ impl AstNode for ast::ModExpression { matches!(kind, NodeKind::ModExpression) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::ModExpression { body, range: _ } = self; visitor.visit_expr(body); } } -impl AstNode for ast::StmtFunctionDef { - type Ref<'a> = &'a Self; +impl<'ast> AstNode<'ast> for ast::StmtFunctionDef<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; - fn cast(kind: AnyNode) -> Option + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -742,7 +747,7 @@ impl AstNode for ast::StmtFunctionDef { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::StmtFunctionDef(node) = kind { Some(node) } else { @@ -754,17 +759,18 @@ impl AstNode for ast::StmtFunctionDef { matches!(kind, NodeKind::StmtFunctionDef) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::StmtFunctionDef { parameters, @@ -792,10 +798,10 @@ impl AstNode for ast::StmtFunctionDef { visitor.visit_body(body); } } -impl AstNode for ast::StmtClassDef { - type Ref<'a> = &'a Self; +impl<'ast> AstNode<'ast> for ast::StmtClassDef<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; - fn cast(kind: AnyNode) -> Option + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -806,7 +812,7 @@ impl AstNode for ast::StmtClassDef { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::StmtClassDef(node) = kind { Some(node) } else { @@ -818,17 +824,18 @@ impl AstNode for ast::StmtClassDef { matches!(kind, NodeKind::StmtClassDef) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::StmtClassDef { arguments, @@ -853,9 +860,9 @@ impl AstNode for ast::StmtClassDef { visitor.visit_body(body); } } -impl AstNode for ast::StmtReturn { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::StmtReturn<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -866,7 +873,7 @@ impl AstNode for ast::StmtReturn { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::StmtReturn(node) = kind { Some(node) } else { @@ -878,17 +885,18 @@ impl AstNode for ast::StmtReturn { matches!(kind, NodeKind::StmtReturn) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::StmtReturn { value, range: _ } = self; if let Some(expr) = value { @@ -896,9 +904,9 @@ impl AstNode for ast::StmtReturn { } } } -impl AstNode for ast::StmtDelete { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::StmtDelete<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -909,7 +917,7 @@ impl AstNode for ast::StmtDelete { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::StmtDelete(node) = kind { Some(node) } else { @@ -921,17 +929,18 @@ impl AstNode for ast::StmtDelete { matches!(kind, NodeKind::StmtDelete) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::StmtDelete { targets, range: _ } = self; for expr in targets { @@ -939,9 +948,9 @@ impl AstNode for ast::StmtDelete { } } } -impl AstNode for ast::StmtTypeAlias { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::StmtTypeAlias<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -952,7 +961,7 @@ impl AstNode for ast::StmtTypeAlias { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::StmtTypeAlias(node) = kind { Some(node) } else { @@ -964,17 +973,18 @@ impl AstNode for ast::StmtTypeAlias { matches!(kind, NodeKind::StmtTypeAlias) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::StmtTypeAlias { range: _, @@ -990,9 +1000,9 @@ impl AstNode for ast::StmtTypeAlias { visitor.visit_expr(value); } } -impl AstNode for ast::StmtAssign { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::StmtAssign<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -1003,7 +1013,7 @@ impl AstNode for ast::StmtAssign { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::StmtAssign(node) = kind { Some(node) } else { @@ -1015,17 +1025,18 @@ impl AstNode for ast::StmtAssign { matches!(kind, NodeKind::StmtAssign) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::StmtAssign { targets, @@ -1040,9 +1051,9 @@ impl AstNode for ast::StmtAssign { visitor.visit_expr(value); } } -impl AstNode for ast::StmtAugAssign { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::StmtAugAssign<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -1053,7 +1064,7 @@ impl AstNode for ast::StmtAugAssign { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::StmtAugAssign(node) = kind { Some(node) } else { @@ -1065,17 +1076,18 @@ impl AstNode for ast::StmtAugAssign { matches!(kind, NodeKind::StmtAugAssign) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::StmtAugAssign { target, @@ -1089,9 +1101,9 @@ impl AstNode for ast::StmtAugAssign { visitor.visit_expr(value); } } -impl AstNode for ast::StmtAnnAssign { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::StmtAnnAssign<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -1102,7 +1114,7 @@ impl AstNode for ast::StmtAnnAssign { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::StmtAnnAssign(node) = kind { Some(node) } else { @@ -1114,17 +1126,18 @@ impl AstNode for ast::StmtAnnAssign { matches!(kind, NodeKind::StmtAnnAssign) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::StmtAnnAssign { target, @@ -1141,9 +1154,9 @@ impl AstNode for ast::StmtAnnAssign { } } } -impl AstNode for ast::StmtFor { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::StmtFor<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -1154,7 +1167,7 @@ impl AstNode for ast::StmtFor { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::StmtFor(node) = kind { Some(node) } else { @@ -1166,17 +1179,18 @@ impl AstNode for ast::StmtFor { matches!(kind, NodeKind::StmtFor) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::StmtFor { target, @@ -1192,9 +1206,9 @@ impl AstNode for ast::StmtFor { visitor.visit_body(orelse); } } -impl AstNode for ast::StmtWhile { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::StmtWhile<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -1205,7 +1219,7 @@ impl AstNode for ast::StmtWhile { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::StmtWhile(node) = kind { Some(node) } else { @@ -1217,17 +1231,18 @@ impl AstNode for ast::StmtWhile { matches!(kind, NodeKind::StmtWhile) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::StmtWhile { test, @@ -1241,9 +1256,9 @@ impl AstNode for ast::StmtWhile { visitor.visit_body(orelse); } } -impl AstNode for ast::StmtIf { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::StmtIf<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -1254,7 +1269,7 @@ impl AstNode for ast::StmtIf { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::StmtIf(node) = kind { Some(node) } else { @@ -1266,17 +1281,18 @@ impl AstNode for ast::StmtIf { matches!(kind, NodeKind::StmtIf) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::StmtIf { test, @@ -1292,9 +1308,9 @@ impl AstNode for ast::StmtIf { } } } -impl AstNode for ast::ElifElseClause { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::ElifElseClause<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -1305,7 +1321,7 @@ impl AstNode for ast::ElifElseClause { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::ElifElseClause(node) = kind { Some(node) } else { @@ -1317,17 +1333,18 @@ impl AstNode for ast::ElifElseClause { matches!(kind, NodeKind::ElifElseClause) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::ElifElseClause { range: _, @@ -1340,9 +1357,9 @@ impl AstNode for ast::ElifElseClause { visitor.visit_body(body); } } -impl AstNode for ast::StmtWith { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::StmtWith<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -1353,7 +1370,7 @@ impl AstNode for ast::StmtWith { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::StmtWith(node) = kind { Some(node) } else { @@ -1365,17 +1382,18 @@ impl AstNode for ast::StmtWith { matches!(kind, NodeKind::StmtWith) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::StmtWith { items, @@ -1390,9 +1408,9 @@ impl AstNode for ast::StmtWith { visitor.visit_body(body); } } -impl AstNode for ast::StmtMatch { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::StmtMatch<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -1403,7 +1421,7 @@ impl AstNode for ast::StmtMatch { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::StmtMatch(node) = kind { Some(node) } else { @@ -1415,17 +1433,18 @@ impl AstNode for ast::StmtMatch { matches!(kind, NodeKind::StmtMatch) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::StmtMatch { subject, @@ -1439,9 +1458,9 @@ impl AstNode for ast::StmtMatch { } } } -impl AstNode for ast::StmtRaise { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::StmtRaise<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -1452,7 +1471,7 @@ impl AstNode for ast::StmtRaise { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::StmtRaise(node) = kind { Some(node) } else { @@ -1464,17 +1483,18 @@ impl AstNode for ast::StmtRaise { matches!(kind, NodeKind::StmtRaise) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::StmtRaise { exc, @@ -1490,9 +1510,9 @@ impl AstNode for ast::StmtRaise { }; } } -impl AstNode for ast::StmtTry { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::StmtTry<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -1503,7 +1523,7 @@ impl AstNode for ast::StmtTry { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::StmtTry(node) = kind { Some(node) } else { @@ -1515,17 +1535,18 @@ impl AstNode for ast::StmtTry { matches!(kind, NodeKind::StmtTry) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::StmtTry { body, @@ -1544,9 +1565,9 @@ impl AstNode for ast::StmtTry { visitor.visit_body(finalbody); } } -impl AstNode for ast::StmtAssert { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::StmtAssert<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -1557,7 +1578,7 @@ impl AstNode for ast::StmtAssert { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::StmtAssert(node) = kind { Some(node) } else { @@ -1569,17 +1590,18 @@ impl AstNode for ast::StmtAssert { matches!(kind, NodeKind::StmtAssert) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::StmtAssert { test, @@ -1592,9 +1614,9 @@ impl AstNode for ast::StmtAssert { } } } -impl AstNode for ast::StmtImport { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::StmtImport<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -1605,7 +1627,7 @@ impl AstNode for ast::StmtImport { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::StmtImport(node) = kind { Some(node) } else { @@ -1617,17 +1639,19 @@ impl AstNode for ast::StmtImport { matches!(kind, NodeKind::StmtImport) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::StmtImport { names, range: _ } = self; @@ -1636,9 +1660,9 @@ impl AstNode for ast::StmtImport { } } } -impl AstNode for ast::StmtImportFrom { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::StmtImportFrom<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -1649,7 +1673,7 @@ impl AstNode for ast::StmtImportFrom { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::StmtImportFrom(node) = kind { Some(node) } else { @@ -1661,17 +1685,18 @@ impl AstNode for ast::StmtImportFrom { matches!(kind, NodeKind::StmtImportFrom) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::StmtImportFrom { range: _, @@ -1685,9 +1710,9 @@ impl AstNode for ast::StmtImportFrom { } } } -impl AstNode for ast::StmtGlobal { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::StmtGlobal<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -1698,7 +1723,7 @@ impl AstNode for ast::StmtGlobal { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::StmtGlobal(node) = kind { Some(node) } else { @@ -1710,24 +1735,25 @@ impl AstNode for ast::StmtGlobal { matches!(kind, NodeKind::StmtGlobal) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } #[inline] fn visit_source_order<'a, V>(&'a self, _visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { } } -impl AstNode for ast::StmtNonlocal { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::StmtNonlocal<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -1738,7 +1764,7 @@ impl AstNode for ast::StmtNonlocal { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::StmtNonlocal(node) = kind { Some(node) } else { @@ -1750,24 +1776,25 @@ impl AstNode for ast::StmtNonlocal { matches!(kind, NodeKind::StmtNonlocal) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } #[inline] fn visit_source_order<'a, V>(&'a self, _visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { } } -impl AstNode for ast::StmtExpr { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::StmtExpr<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -1778,7 +1805,7 @@ impl AstNode for ast::StmtExpr { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::StmtExpr(node) = kind { Some(node) } else { @@ -1790,26 +1817,27 @@ impl AstNode for ast::StmtExpr { matches!(kind, NodeKind::StmtExpr) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::StmtExpr { value, range: _ } = self; visitor.visit_expr(value); } } -impl AstNode for ast::StmtPass { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::StmtPass { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -1820,7 +1848,7 @@ impl AstNode for ast::StmtPass { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::StmtPass(node) = kind { Some(node) } else { @@ -1832,24 +1860,25 @@ impl AstNode for ast::StmtPass { matches!(kind, NodeKind::StmtPass) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } #[inline] fn visit_source_order<'a, V>(&'a self, _visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { } } -impl AstNode for ast::StmtBreak { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::StmtBreak { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -1860,7 +1889,7 @@ impl AstNode for ast::StmtBreak { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::StmtBreak(node) = kind { Some(node) } else { @@ -1872,24 +1901,25 @@ impl AstNode for ast::StmtBreak { matches!(kind, NodeKind::StmtBreak) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } #[inline] fn visit_source_order<'a, V>(&'a self, _visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { } } -impl AstNode for ast::StmtContinue { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::StmtContinue { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -1900,7 +1930,7 @@ impl AstNode for ast::StmtContinue { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::StmtContinue(node) = kind { Some(node) } else { @@ -1912,24 +1942,25 @@ impl AstNode for ast::StmtContinue { matches!(kind, NodeKind::StmtContinue) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } #[inline] fn visit_source_order<'a, V>(&'a self, _visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { } } -impl AstNode for ast::StmtIpyEscapeCommand { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::StmtIpyEscapeCommand<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -1940,7 +1971,7 @@ impl AstNode for ast::StmtIpyEscapeCommand { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::StmtIpyEscapeCommand(node) = kind { Some(node) } else { @@ -1952,24 +1983,25 @@ impl AstNode for ast::StmtIpyEscapeCommand { matches!(kind, NodeKind::StmtIpyEscapeCommand) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } #[inline] fn visit_source_order<'a, V>(&'a self, _visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { } } -impl AstNode for ast::ExprBoolOp { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::ExprBoolOp<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -1980,7 +2012,7 @@ impl AstNode for ast::ExprBoolOp { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::ExprBoolOp(node) = kind { Some(node) } else { @@ -1992,17 +2024,18 @@ impl AstNode for ast::ExprBoolOp { matches!(kind, NodeKind::ExprBoolOp) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::ExprBoolOp { op, @@ -2023,9 +2056,9 @@ impl AstNode for ast::ExprBoolOp { } } } -impl AstNode for ast::ExprNamed { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::ExprNamed<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -2036,7 +2069,7 @@ impl AstNode for ast::ExprNamed { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::ExprNamed(node) = kind { Some(node) } else { @@ -2048,17 +2081,18 @@ impl AstNode for ast::ExprNamed { matches!(kind, NodeKind::ExprNamed) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::ExprNamed { target, @@ -2069,9 +2103,9 @@ impl AstNode for ast::ExprNamed { visitor.visit_expr(value); } } -impl AstNode for ast::ExprBinOp { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::ExprBinOp<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -2082,7 +2116,7 @@ impl AstNode for ast::ExprBinOp { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::ExprBinOp(node) = kind { Some(node) } else { @@ -2094,17 +2128,18 @@ impl AstNode for ast::ExprBinOp { matches!(kind, NodeKind::ExprBinOp) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::ExprBinOp { left, @@ -2117,9 +2152,9 @@ impl AstNode for ast::ExprBinOp { visitor.visit_expr(right); } } -impl AstNode for ast::ExprUnaryOp { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::ExprUnaryOp<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -2130,7 +2165,7 @@ impl AstNode for ast::ExprUnaryOp { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::ExprUnaryOp(node) = kind { Some(node) } else { @@ -2142,17 +2177,18 @@ impl AstNode for ast::ExprUnaryOp { matches!(kind, NodeKind::ExprUnaryOp) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::ExprUnaryOp { op, @@ -2164,10 +2200,10 @@ impl AstNode for ast::ExprUnaryOp { visitor.visit_expr(operand); } } -impl AstNode for ast::ExprLambda { - type Ref<'a> = &'a Self; +impl<'ast> AstNode<'ast> for ast::ExprLambda<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; - fn cast(kind: AnyNode) -> Option + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -2178,7 +2214,7 @@ impl AstNode for ast::ExprLambda { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::ExprLambda(node) = kind { Some(node) } else { @@ -2190,17 +2226,18 @@ impl AstNode for ast::ExprLambda { matches!(kind, NodeKind::ExprLambda) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::ExprLambda { parameters, @@ -2214,9 +2251,9 @@ impl AstNode for ast::ExprLambda { visitor.visit_expr(body); } } -impl AstNode for ast::ExprIf { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::ExprIf<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -2227,7 +2264,7 @@ impl AstNode for ast::ExprIf { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::ExprIf(node) = kind { Some(node) } else { @@ -2239,17 +2276,18 @@ impl AstNode for ast::ExprIf { matches!(kind, NodeKind::ExprIf) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::ExprIf { test, @@ -2264,9 +2302,9 @@ impl AstNode for ast::ExprIf { visitor.visit_expr(orelse); } } -impl AstNode for ast::ExprDict { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::ExprDict<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -2277,7 +2315,7 @@ impl AstNode for ast::ExprDict { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::ExprDict(node) = kind { Some(node) } else { @@ -2289,17 +2327,18 @@ impl AstNode for ast::ExprDict { matches!(kind, NodeKind::ExprDict) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::ExprDict { items, range: _ } = self; @@ -2311,9 +2350,9 @@ impl AstNode for ast::ExprDict { } } } -impl AstNode for ast::ExprSet { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::ExprSet<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -2324,7 +2363,7 @@ impl AstNode for ast::ExprSet { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::ExprSet(node) = kind { Some(node) } else { @@ -2336,17 +2375,18 @@ impl AstNode for ast::ExprSet { matches!(kind, NodeKind::ExprSet) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::ExprSet { elts, range: _ } = self; @@ -2355,9 +2395,9 @@ impl AstNode for ast::ExprSet { } } } -impl AstNode for ast::ExprListComp { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::ExprListComp<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -2368,7 +2408,7 @@ impl AstNode for ast::ExprListComp { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::ExprListComp(node) = kind { Some(node) } else { @@ -2380,17 +2420,18 @@ impl AstNode for ast::ExprListComp { matches!(kind, NodeKind::ExprListComp) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::ExprListComp { elt, @@ -2404,9 +2445,9 @@ impl AstNode for ast::ExprListComp { } } } -impl AstNode for ast::ExprSetComp { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::ExprSetComp<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -2417,7 +2458,7 @@ impl AstNode for ast::ExprSetComp { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::ExprSetComp(node) = kind { Some(node) } else { @@ -2429,17 +2470,18 @@ impl AstNode for ast::ExprSetComp { matches!(kind, NodeKind::ExprSetComp) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::ExprSetComp { elt, @@ -2453,9 +2495,9 @@ impl AstNode for ast::ExprSetComp { } } } -impl AstNode for ast::ExprDictComp { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::ExprDictComp<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -2466,7 +2508,7 @@ impl AstNode for ast::ExprDictComp { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::ExprDictComp(node) = kind { Some(node) } else { @@ -2478,17 +2520,18 @@ impl AstNode for ast::ExprDictComp { matches!(kind, NodeKind::ExprDictComp) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::ExprDictComp { key, @@ -2505,9 +2548,9 @@ impl AstNode for ast::ExprDictComp { } } } -impl AstNode for ast::ExprGenerator { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::ExprGenerator<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -2518,7 +2561,7 @@ impl AstNode for ast::ExprGenerator { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::ExprGenerator(node) = kind { Some(node) } else { @@ -2530,17 +2573,18 @@ impl AstNode for ast::ExprGenerator { matches!(kind, NodeKind::ExprGenerator) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::ExprGenerator { elt, @@ -2554,9 +2598,9 @@ impl AstNode for ast::ExprGenerator { } } } -impl AstNode for ast::ExprAwait { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::ExprAwait<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -2567,7 +2611,7 @@ impl AstNode for ast::ExprAwait { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::ExprAwait(node) = kind { Some(node) } else { @@ -2579,25 +2623,26 @@ impl AstNode for ast::ExprAwait { matches!(kind, NodeKind::ExprAwait) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::ExprAwait { value, range: _ } = self; visitor.visit_expr(value); } } -impl AstNode for ast::ExprYield { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::ExprYield<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -2608,7 +2653,7 @@ impl AstNode for ast::ExprYield { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::ExprYield(node) = kind { Some(node) } else { @@ -2620,17 +2665,18 @@ impl AstNode for ast::ExprYield { matches!(kind, NodeKind::ExprYield) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::ExprYield { value, range: _ } = self; if let Some(expr) = value { @@ -2638,9 +2684,9 @@ impl AstNode for ast::ExprYield { } } } -impl AstNode for ast::ExprYieldFrom { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::ExprYieldFrom<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -2651,7 +2697,7 @@ impl AstNode for ast::ExprYieldFrom { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::ExprYieldFrom(node) = kind { Some(node) } else { @@ -2663,25 +2709,26 @@ impl AstNode for ast::ExprYieldFrom { matches!(kind, NodeKind::ExprYieldFrom) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::ExprYieldFrom { value, range: _ } = self; visitor.visit_expr(value); } } -impl AstNode for ast::ExprCompare { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::ExprCompare<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -2692,7 +2739,7 @@ impl AstNode for ast::ExprCompare { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::ExprCompare(node) = kind { Some(node) } else { @@ -2704,17 +2751,18 @@ impl AstNode for ast::ExprCompare { matches!(kind, NodeKind::ExprCompare) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::ExprCompare { left, @@ -2731,9 +2779,9 @@ impl AstNode for ast::ExprCompare { } } } -impl AstNode for ast::ExprCall { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::ExprCall<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -2744,7 +2792,7 @@ impl AstNode for ast::ExprCall { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::ExprCall(node) = kind { Some(node) } else { @@ -2756,17 +2804,18 @@ impl AstNode for ast::ExprCall { matches!(kind, NodeKind::ExprCall) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::ExprCall { func, @@ -2777,9 +2826,9 @@ impl AstNode for ast::ExprCall { visitor.visit_arguments(arguments); } } -impl AstNode for ast::FStringFormatSpec { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::FStringFormatSpec<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -2790,7 +2839,7 @@ impl AstNode for ast::FStringFormatSpec { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::FStringFormatSpec(node) = kind { Some(node) } else { @@ -2802,26 +2851,27 @@ impl AstNode for ast::FStringFormatSpec { matches!(kind, NodeKind::FStringFormatSpec) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { for element in &self.elements { visitor.visit_f_string_element(element); } } } -impl AstNode for ast::FStringExpressionElement { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::FStringExpressionElement<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -2832,7 +2882,7 @@ impl AstNode for ast::FStringExpressionElement { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::FStringExpressionElement(node) = kind { Some(node) } else { @@ -2844,17 +2894,18 @@ impl AstNode for ast::FStringExpressionElement { matches!(kind, NodeKind::FStringExpressionElement) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::FStringExpressionElement { expression, @@ -2870,9 +2921,9 @@ impl AstNode for ast::FStringExpressionElement { } } } -impl AstNode for ast::FStringLiteralElement { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::FStringLiteralElement<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -2883,7 +2934,7 @@ impl AstNode for ast::FStringLiteralElement { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::FStringLiteralElement(node) = kind { Some(node) } else { @@ -2895,23 +2946,24 @@ impl AstNode for ast::FStringLiteralElement { matches!(kind, NodeKind::FStringLiteralElement) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, _visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { } } -impl AstNode for ast::ExprFString { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::ExprFString<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -2922,7 +2974,7 @@ impl AstNode for ast::ExprFString { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::ExprFString(node) = kind { Some(node) } else { @@ -2934,17 +2986,18 @@ impl AstNode for ast::ExprFString { matches!(kind, NodeKind::ExprFString) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::ExprFString { value, range: _ } = self; @@ -2960,9 +3013,9 @@ impl AstNode for ast::ExprFString { } } } -impl AstNode for ast::ExprStringLiteral { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::ExprStringLiteral<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -2973,7 +3026,7 @@ impl AstNode for ast::ExprStringLiteral { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::ExprStringLiteral(node) = kind { Some(node) } else { @@ -2985,17 +3038,18 @@ impl AstNode for ast::ExprStringLiteral { matches!(kind, NodeKind::ExprStringLiteral) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::ExprStringLiteral { value, range: _ } = self; @@ -3004,9 +3058,9 @@ impl AstNode for ast::ExprStringLiteral { } } } -impl AstNode for ast::ExprBytesLiteral { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::ExprBytesLiteral<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -3017,7 +3071,7 @@ impl AstNode for ast::ExprBytesLiteral { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::ExprBytesLiteral(node) = kind { Some(node) } else { @@ -3029,17 +3083,18 @@ impl AstNode for ast::ExprBytesLiteral { matches!(kind, NodeKind::ExprBytesLiteral) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::ExprBytesLiteral { value, range: _ } = self; @@ -3048,9 +3103,9 @@ impl AstNode for ast::ExprBytesLiteral { } } } -impl AstNode for ast::ExprNumberLiteral { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::ExprNumberLiteral { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -3061,7 +3116,7 @@ impl AstNode for ast::ExprNumberLiteral { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::ExprNumberLiteral(node) = kind { Some(node) } else { @@ -3073,23 +3128,24 @@ impl AstNode for ast::ExprNumberLiteral { matches!(kind, NodeKind::ExprNumberLiteral) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, _visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { } } -impl AstNode for ast::ExprBooleanLiteral { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::ExprBooleanLiteral { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -3100,7 +3156,7 @@ impl AstNode for ast::ExprBooleanLiteral { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::ExprBooleanLiteral(node) = kind { Some(node) } else { @@ -3112,23 +3168,24 @@ impl AstNode for ast::ExprBooleanLiteral { matches!(kind, NodeKind::ExprBooleanLiteral) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, _visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { } } -impl AstNode for ast::ExprNoneLiteral { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::ExprNoneLiteral { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -3139,7 +3196,7 @@ impl AstNode for ast::ExprNoneLiteral { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::ExprNoneLiteral(node) = kind { Some(node) } else { @@ -3151,23 +3208,24 @@ impl AstNode for ast::ExprNoneLiteral { matches!(kind, NodeKind::ExprNoneLiteral) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, _visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { } } -impl AstNode for ast::ExprEllipsisLiteral { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::ExprEllipsisLiteral { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -3178,7 +3236,7 @@ impl AstNode for ast::ExprEllipsisLiteral { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::ExprEllipsisLiteral(node) = kind { Some(node) } else { @@ -3190,23 +3248,24 @@ impl AstNode for ast::ExprEllipsisLiteral { matches!(kind, NodeKind::ExprEllipsisLiteral) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, _visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { } } -impl AstNode for ast::ExprAttribute { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::ExprAttribute<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -3217,7 +3276,7 @@ impl AstNode for ast::ExprAttribute { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::ExprAttribute(node) = kind { Some(node) } else { @@ -3229,17 +3288,18 @@ impl AstNode for ast::ExprAttribute { matches!(kind, NodeKind::ExprAttribute) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::ExprAttribute { value, @@ -3251,9 +3311,9 @@ impl AstNode for ast::ExprAttribute { visitor.visit_expr(value); } } -impl AstNode for ast::ExprSubscript { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::ExprSubscript<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -3264,7 +3324,7 @@ impl AstNode for ast::ExprSubscript { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::ExprSubscript(node) = kind { Some(node) } else { @@ -3276,17 +3336,18 @@ impl AstNode for ast::ExprSubscript { matches!(kind, NodeKind::ExprSubscript) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::ExprSubscript { value, @@ -3298,9 +3359,9 @@ impl AstNode for ast::ExprSubscript { visitor.visit_expr(slice); } } -impl AstNode for ast::ExprStarred { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::ExprStarred<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -3311,7 +3372,7 @@ impl AstNode for ast::ExprStarred { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::ExprStarred(node) = kind { Some(node) } else { @@ -3323,17 +3384,18 @@ impl AstNode for ast::ExprStarred { matches!(kind, NodeKind::ExprStarred) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::ExprStarred { value, @@ -3344,9 +3406,9 @@ impl AstNode for ast::ExprStarred { visitor.visit_expr(value); } } -impl AstNode for ast::ExprName { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::ExprName<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -3357,7 +3419,7 @@ impl AstNode for ast::ExprName { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::ExprName(node) = kind { Some(node) } else { @@ -3369,18 +3431,19 @@ impl AstNode for ast::ExprName { matches!(kind, NodeKind::ExprName) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } #[inline] fn visit_source_order<'a, V>(&'a self, _visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::ExprName { id: _, @@ -3389,9 +3452,9 @@ impl AstNode for ast::ExprName { } = self; } } -impl AstNode for ast::ExprList { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::ExprList<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -3402,7 +3465,7 @@ impl AstNode for ast::ExprList { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::ExprList(node) = kind { Some(node) } else { @@ -3414,17 +3477,18 @@ impl AstNode for ast::ExprList { matches!(kind, NodeKind::ExprList) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::ExprList { elts, @@ -3437,9 +3501,9 @@ impl AstNode for ast::ExprList { } } } -impl AstNode for ast::ExprTuple { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::ExprTuple<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -3450,7 +3514,7 @@ impl AstNode for ast::ExprTuple { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::ExprTuple(node) = kind { Some(node) } else { @@ -3462,17 +3526,18 @@ impl AstNode for ast::ExprTuple { matches!(kind, NodeKind::ExprTuple) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::ExprTuple { elts, @@ -3486,9 +3551,9 @@ impl AstNode for ast::ExprTuple { } } } -impl AstNode for ast::ExprSlice { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::ExprSlice<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -3499,7 +3564,7 @@ impl AstNode for ast::ExprSlice { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::ExprSlice(node) = kind { Some(node) } else { @@ -3511,16 +3576,17 @@ impl AstNode for ast::ExprSlice { matches!(kind, NodeKind::ExprSlice) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::ExprSlice { lower, @@ -3540,9 +3606,9 @@ impl AstNode for ast::ExprSlice { } } } -impl AstNode for ast::ExprIpyEscapeCommand { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::ExprIpyEscapeCommand<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -3553,7 +3619,7 @@ impl AstNode for ast::ExprIpyEscapeCommand { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::ExprIpyEscapeCommand(node) = kind { Some(node) } else { @@ -3565,18 +3631,19 @@ impl AstNode for ast::ExprIpyEscapeCommand { matches!(kind, NodeKind::ExprIpyEscapeCommand) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } #[inline] fn visit_source_order<'a, V>(&'a self, _visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::ExprIpyEscapeCommand { range: _, @@ -3585,9 +3652,9 @@ impl AstNode for ast::ExprIpyEscapeCommand { } = self; } } -impl AstNode for ast::ExceptHandlerExceptHandler { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::ExceptHandlerExceptHandler<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -3598,7 +3665,7 @@ impl AstNode for ast::ExceptHandlerExceptHandler { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::ExceptHandlerExceptHandler(node) = kind { Some(node) } else { @@ -3610,17 +3677,18 @@ impl AstNode for ast::ExceptHandlerExceptHandler { matches!(kind, NodeKind::ExceptHandlerExceptHandler) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::ExceptHandlerExceptHandler { range: _, @@ -3634,9 +3702,9 @@ impl AstNode for ast::ExceptHandlerExceptHandler { visitor.visit_body(body); } } -impl AstNode for ast::PatternMatchValue { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::PatternMatchValue<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -3647,7 +3715,7 @@ impl AstNode for ast::PatternMatchValue { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::PatternMatchValue(node) = kind { Some(node) } else { @@ -3659,25 +3727,26 @@ impl AstNode for ast::PatternMatchValue { matches!(kind, NodeKind::PatternMatchValue) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::PatternMatchValue { value, range: _ } = self; visitor.visit_expr(value); } } -impl AstNode for ast::PatternMatchSingleton { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::PatternMatchSingleton { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -3688,7 +3757,7 @@ impl AstNode for ast::PatternMatchSingleton { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::PatternMatchSingleton(node) = kind { Some(node) } else { @@ -3700,25 +3769,26 @@ impl AstNode for ast::PatternMatchSingleton { matches!(kind, NodeKind::PatternMatchSingleton) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::PatternMatchSingleton { value, range: _ } = self; visitor.visit_singleton(value); } } -impl AstNode for ast::PatternMatchSequence { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::PatternMatchSequence<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -3729,7 +3799,7 @@ impl AstNode for ast::PatternMatchSequence { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::PatternMatchSequence(node) = kind { Some(node) } else { @@ -3741,17 +3811,18 @@ impl AstNode for ast::PatternMatchSequence { matches!(kind, NodeKind::PatternMatchSequence) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::PatternMatchSequence { patterns, range: _ } = self; for pattern in patterns { @@ -3759,9 +3830,9 @@ impl AstNode for ast::PatternMatchSequence { } } } -impl AstNode for ast::PatternMatchMapping { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::PatternMatchMapping<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -3772,7 +3843,7 @@ impl AstNode for ast::PatternMatchMapping { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::PatternMatchMapping(node) = kind { Some(node) } else { @@ -3784,17 +3855,18 @@ impl AstNode for ast::PatternMatchMapping { matches!(kind, NodeKind::PatternMatchMapping) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::PatternMatchMapping { keys, @@ -3808,9 +3880,9 @@ impl AstNode for ast::PatternMatchMapping { } } } -impl AstNode for ast::PatternMatchClass { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::PatternMatchClass<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -3821,7 +3893,7 @@ impl AstNode for ast::PatternMatchClass { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::PatternMatchClass(node) = kind { Some(node) } else { @@ -3833,17 +3905,18 @@ impl AstNode for ast::PatternMatchClass { matches!(kind, NodeKind::PatternMatchClass) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::PatternMatchClass { cls, @@ -3854,9 +3927,9 @@ impl AstNode for ast::PatternMatchClass { visitor.visit_pattern_arguments(parameters); } } -impl AstNode for ast::PatternMatchStar { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::PatternMatchStar<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -3867,7 +3940,7 @@ impl AstNode for ast::PatternMatchStar { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::PatternMatchStar(node) = kind { Some(node) } else { @@ -3879,25 +3952,26 @@ impl AstNode for ast::PatternMatchStar { matches!(kind, NodeKind::PatternMatchStar) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } #[inline] fn visit_source_order<'a, V>(&'a self, _visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::PatternMatchStar { range: _, name: _ } = self; } } -impl AstNode for ast::PatternMatchAs { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::PatternMatchAs<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -3908,7 +3982,7 @@ impl AstNode for ast::PatternMatchAs { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::PatternMatchAs(node) = kind { Some(node) } else { @@ -3920,17 +3994,18 @@ impl AstNode for ast::PatternMatchAs { matches!(kind, NodeKind::PatternMatchAs) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::PatternMatchAs { pattern, @@ -3942,9 +4017,9 @@ impl AstNode for ast::PatternMatchAs { } } } -impl AstNode for ast::PatternMatchOr { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::PatternMatchOr<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -3955,7 +4030,7 @@ impl AstNode for ast::PatternMatchOr { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::PatternMatchOr(node) = kind { Some(node) } else { @@ -3967,17 +4042,18 @@ impl AstNode for ast::PatternMatchOr { matches!(kind, NodeKind::PatternMatchOr) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::PatternMatchOr { patterns, range: _ } = self; for pattern in patterns { @@ -3985,9 +4061,9 @@ impl AstNode for ast::PatternMatchOr { } } } -impl AstNode for PatternArguments { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for PatternArguments<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -3998,7 +4074,7 @@ impl AstNode for PatternArguments { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::PatternArguments(node) = kind { Some(node) } else { @@ -4010,17 +4086,18 @@ impl AstNode for PatternArguments { matches!(kind, NodeKind::PatternArguments) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let PatternArguments { range: _, @@ -4037,9 +4114,9 @@ impl AstNode for PatternArguments { } } } -impl AstNode for PatternKeyword { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for PatternKeyword<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -4050,7 +4127,7 @@ impl AstNode for PatternKeyword { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::PatternKeyword(node) = kind { Some(node) } else { @@ -4062,17 +4139,18 @@ impl AstNode for PatternKeyword { matches!(kind, NodeKind::PatternKeyword) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let PatternKeyword { range: _, @@ -4084,9 +4162,9 @@ impl AstNode for PatternKeyword { } } -impl AstNode for Comprehension { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for Comprehension<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -4097,7 +4175,7 @@ impl AstNode for Comprehension { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::Comprehension(node) = kind { Some(node) } else { @@ -4109,17 +4187,18 @@ impl AstNode for Comprehension { matches!(kind, NodeKind::Comprehension) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::Comprehension { range: _, @@ -4136,9 +4215,9 @@ impl AstNode for Comprehension { } } } -impl AstNode for Arguments { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for Arguments<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -4149,7 +4228,7 @@ impl AstNode for Arguments { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::Arguments(node) = kind { Some(node) } else { @@ -4161,17 +4240,18 @@ impl AstNode for Arguments { matches!(kind, NodeKind::Arguments) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { for arg_or_keyword in self.arguments_source_order() { match arg_or_keyword { @@ -4181,9 +4261,9 @@ impl AstNode for Arguments { } } } -impl AstNode for Parameters { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for Parameters<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -4194,7 +4274,7 @@ impl AstNode for Parameters { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::Parameters(node) = kind { Some(node) } else { @@ -4206,17 +4286,18 @@ impl AstNode for Parameters { matches!(kind, NodeKind::Parameters) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { for parameter in self { match parameter { @@ -4228,9 +4309,9 @@ impl AstNode for Parameters { } } } -impl AstNode for Parameter { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for Parameter<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -4241,7 +4322,7 @@ impl AstNode for Parameter { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::Parameter(node) = kind { Some(node) } else { @@ -4253,17 +4334,18 @@ impl AstNode for Parameter { matches!(kind, NodeKind::Parameter) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::Parameter { range: _, @@ -4276,9 +4358,9 @@ impl AstNode for Parameter { } } } -impl AstNode for ParameterWithDefault { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ParameterWithDefault<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -4289,7 +4371,7 @@ impl AstNode for ParameterWithDefault { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::ParameterWithDefault(node) = kind { Some(node) } else { @@ -4301,17 +4383,18 @@ impl AstNode for ParameterWithDefault { matches!(kind, NodeKind::ParameterWithDefault) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::ParameterWithDefault { range: _, @@ -4324,9 +4407,9 @@ impl AstNode for ParameterWithDefault { } } } -impl AstNode for Keyword { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for Keyword<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -4337,7 +4420,7 @@ impl AstNode for Keyword { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::Keyword(node) = kind { Some(node) } else { @@ -4349,17 +4432,18 @@ impl AstNode for Keyword { matches!(kind, NodeKind::Keyword) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::Keyword { range: _, @@ -4370,9 +4454,9 @@ impl AstNode for Keyword { visitor.visit_expr(value); } } -impl AstNode for Alias { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for Alias<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -4383,7 +4467,7 @@ impl AstNode for Alias { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::Alias(node) = kind { Some(node) } else { @@ -4395,18 +4479,19 @@ impl AstNode for Alias { matches!(kind, NodeKind::Alias) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } #[inline] fn visit_source_order<'a, V>(&'a self, _visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::Alias { range: _, @@ -4415,9 +4500,9 @@ impl AstNode for Alias { } = self; } } -impl AstNode for WithItem { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for WithItem<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -4428,7 +4513,7 @@ impl AstNode for WithItem { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::WithItem(node) = kind { Some(node) } else { @@ -4440,17 +4525,18 @@ impl AstNode for WithItem { matches!(kind, NodeKind::WithItem) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::WithItem { range: _, @@ -4465,9 +4551,9 @@ impl AstNode for WithItem { } } } -impl AstNode for MatchCase { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for MatchCase<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -4478,7 +4564,7 @@ impl AstNode for MatchCase { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::MatchCase(node) = kind { Some(node) } else { @@ -4490,17 +4576,18 @@ impl AstNode for MatchCase { matches!(kind, NodeKind::MatchCase) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::MatchCase { range: _, @@ -4517,9 +4604,9 @@ impl AstNode for MatchCase { } } -impl AstNode for Decorator { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for Decorator<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -4530,7 +4617,7 @@ impl AstNode for Decorator { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::Decorator(node) = kind { Some(node) } else { @@ -4542,17 +4629,18 @@ impl AstNode for Decorator { matches!(kind, NodeKind::Decorator) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::Decorator { range: _, @@ -4562,9 +4650,9 @@ impl AstNode for Decorator { visitor.visit_expr(expression); } } -impl AstNode for ast::TypeParams { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::TypeParams<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -4575,7 +4663,7 @@ impl AstNode for ast::TypeParams { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::TypeParams(node) = kind { Some(node) } else { @@ -4587,17 +4675,18 @@ impl AstNode for ast::TypeParams { matches!(kind, NodeKind::TypeParams) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::TypeParams { range: _, @@ -4609,9 +4698,9 @@ impl AstNode for ast::TypeParams { } } } -impl AstNode for ast::TypeParamTypeVar { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::TypeParamTypeVar<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -4622,7 +4711,7 @@ impl AstNode for ast::TypeParamTypeVar { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::TypeParamTypeVar(node) = kind { Some(node) } else { @@ -4634,17 +4723,18 @@ impl AstNode for ast::TypeParamTypeVar { matches!(kind, NodeKind::TypeParamTypeVar) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::TypeParamTypeVar { bound, @@ -4661,9 +4751,9 @@ impl AstNode for ast::TypeParamTypeVar { } } } -impl AstNode for ast::TypeParamTypeVarTuple { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::TypeParamTypeVarTuple<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -4674,7 +4764,7 @@ impl AstNode for ast::TypeParamTypeVarTuple { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::TypeParamTypeVarTuple(node) = kind { Some(node) } else { @@ -4686,18 +4776,19 @@ impl AstNode for ast::TypeParamTypeVarTuple { matches!(kind, NodeKind::TypeParamTypeVarTuple) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } #[inline] fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::TypeParamTypeVarTuple { range: _, @@ -4709,9 +4800,9 @@ impl AstNode for ast::TypeParamTypeVarTuple { } } } -impl AstNode for ast::TypeParamParamSpec { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::TypeParamParamSpec<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -4722,7 +4813,7 @@ impl AstNode for ast::TypeParamParamSpec { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::TypeParamParamSpec(node) = kind { Some(node) } else { @@ -4734,18 +4825,19 @@ impl AstNode for ast::TypeParamParamSpec { matches!(kind, NodeKind::TypeParamParamSpec) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } #[inline] fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::TypeParamParamSpec { range: _, @@ -4757,9 +4849,9 @@ impl AstNode for ast::TypeParamParamSpec { } } } -impl AstNode for ast::FString { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::FString<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -4770,7 +4862,7 @@ impl AstNode for ast::FString { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::FString(node) = kind { Some(node) } else { @@ -4782,17 +4874,18 @@ impl AstNode for ast::FString { matches!(kind, NodeKind::FString) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let ast::FString { elements, @@ -4805,9 +4898,9 @@ impl AstNode for ast::FString { } } } -impl AstNode for ast::StringLiteral { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::StringLiteral<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -4818,7 +4911,7 @@ impl AstNode for ast::StringLiteral { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::StringLiteral(node) = kind { Some(node) } else { @@ -4830,23 +4923,24 @@ impl AstNode for ast::StringLiteral { matches!(kind, NodeKind::StringLiteral) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, _visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { } } -impl AstNode for ast::BytesLiteral { - type Ref<'a> = &'a Self; - fn cast(kind: AnyNode) -> Option +impl<'ast> AstNode<'ast> for ast::BytesLiteral<'ast> { + type Ref<'a> = &'a Self where 'ast: 'a; + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -4857,7 +4951,7 @@ impl AstNode for ast::BytesLiteral { } } - fn cast_ref(kind: AnyNodeRef) -> Option<&Self> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { if let AnyNodeRef::BytesLiteral(node) = kind { Some(node) } else { @@ -4869,25 +4963,26 @@ impl AstNode for ast::BytesLiteral { matches!(kind, NodeKind::BytesLiteral) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { AnyNodeRef::from(self) } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { AnyNode::from(self) } fn visit_source_order<'a, V>(&'a self, _visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { } } -impl AstNode for Stmt { - type Ref<'a> = StatementRef<'a>; +impl<'ast> AstNode<'ast> for Stmt<'ast> { + type Ref<'a> = StatementRef<'a, 'ast> where 'ast: 'a; - fn cast(kind: AnyNode) -> Option { + fn cast(kind: AnyNode<'ast>) -> Option { match kind { AnyNode::StmtFunctionDef(node) => Some(Stmt::FunctionDef(node)), AnyNode::StmtClassDef(node) => Some(Stmt::ClassDef(node)), @@ -4984,7 +5079,7 @@ impl AstNode for Stmt { } } - fn cast_ref(kind: AnyNodeRef) -> Option> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { match kind { AnyNodeRef::StmtFunctionDef(statement) => Some(StatementRef::FunctionDef(statement)), AnyNodeRef::StmtClassDef(statement) => Some(StatementRef::ClassDef(statement)), @@ -5181,7 +5276,7 @@ impl AstNode for Stmt { } } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref(&self) -> AnyNodeRef<'_, 'ast> { match self { Stmt::FunctionDef(stmt) => stmt.as_any_node_ref(), Stmt::ClassDef(stmt) => stmt.as_any_node_ref(), @@ -5211,7 +5306,7 @@ impl AstNode for Stmt { } } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { match self { Stmt::FunctionDef(stmt) => stmt.into_any_node(), Stmt::ClassDef(stmt) => stmt.into_any_node(), @@ -5243,7 +5338,8 @@ impl AstNode for Stmt { fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { match self { Stmt::FunctionDef(stmt) => stmt.visit_source_order(visitor), @@ -5275,10 +5371,10 @@ impl AstNode for Stmt { } } -impl AstNode for TypeParam { - type Ref<'a> = TypeParamRef<'a>; +impl<'ast> AstNode<'ast> for TypeParam<'ast> { + type Ref<'a> = TypeParamRef<'a, 'ast> where 'ast: 'a; - fn cast(kind: AnyNode) -> Option + fn cast(kind: AnyNode<'ast>) -> Option where Self: Sized, { @@ -5290,7 +5386,7 @@ impl AstNode for TypeParam { } } - fn cast_ref(kind: AnyNodeRef) -> Option> { + fn cast_ref<'a>(kind: AnyNodeRef<'a, 'ast>) -> Option> { match kind { AnyNodeRef::TypeParamTypeVar(node) => Some(TypeParamRef::TypeVar(node)), AnyNodeRef::TypeParamTypeVarTuple(node) => Some(TypeParamRef::TypeVarTuple(node)), @@ -5308,7 +5404,7 @@ impl AstNode for TypeParam { ) } - fn as_any_node_ref(&self) -> AnyNodeRef { + fn as_any_node_ref<'a>(&'a self) -> AnyNodeRef<'a, 'ast> { match self { TypeParam::TypeVar(node) => node.as_any_node_ref(), TypeParam::TypeVarTuple(node) => node.as_any_node_ref(), @@ -5316,7 +5412,7 @@ impl AstNode for TypeParam { } } - fn into_any_node(self) -> AnyNode { + fn into_any_node(self) -> AnyNode<'ast> { match self { TypeParam::TypeVar(node) => node.into_any_node(), TypeParam::TypeVarTuple(node) => node.into_any_node(), @@ -5326,7 +5422,8 @@ impl AstNode for TypeParam { fn visit_source_order<'a, V>(&'a self, visitor: &mut V) where - V: SourceOrderVisitor<'a> + ?Sized, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { match self { TypeParam::TypeVar(node) => node.visit_source_order(visitor), @@ -5336,8 +5433,8 @@ impl AstNode for TypeParam { } } -impl From for AnyNode { - fn from(stmt: Stmt) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(stmt: Stmt<'ast>) -> Self { match stmt { Stmt::FunctionDef(node) => AnyNode::StmtFunctionDef(node), Stmt::ClassDef(node) => AnyNode::StmtClassDef(node), @@ -5368,8 +5465,8 @@ impl From for AnyNode { } } -impl From for AnyNode { - fn from(expr: Expr) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(expr: Expr<'ast>) -> Self { match expr { Expr::BoolOp(node) => AnyNode::ExprBoolOp(node), Expr::Named(node) => AnyNode::ExprNamed(node), @@ -5407,8 +5504,8 @@ impl From for AnyNode { } } -impl From for AnyNode { - fn from(module: Mod) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(module: Mod<'ast>) -> Self { match module { Mod::Module(node) => AnyNode::ModModule(node), Mod::Expression(node) => AnyNode::ModExpression(node), @@ -5416,8 +5513,8 @@ impl From for AnyNode { } } -impl From for AnyNode { - fn from(element: FStringElement) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(element: FStringElement<'ast>) -> Self { match element { FStringElement::Literal(node) => AnyNode::FStringLiteralElement(node), FStringElement::Expression(node) => AnyNode::FStringExpressionElement(node), @@ -5425,8 +5522,8 @@ impl From for AnyNode { } } -impl From for AnyNode { - fn from(pattern: Pattern) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(pattern: Pattern<'ast>) -> Self { match pattern { Pattern::MatchValue(node) => AnyNode::PatternMatchValue(node), Pattern::MatchSingleton(node) => AnyNode::PatternMatchSingleton(node), @@ -5440,550 +5537,550 @@ impl From for AnyNode { } } -impl From for AnyNode { - fn from(handler: ExceptHandler) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(handler: ExceptHandler<'ast>) -> Self { match handler { ExceptHandler::ExceptHandler(handler) => AnyNode::ExceptHandlerExceptHandler(handler), } } } -impl From for AnyNode { - fn from(node: ast::ModModule) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: ast::ModModule<'ast>) -> Self { AnyNode::ModModule(node) } } -impl From for AnyNode { - fn from(node: ast::ModExpression) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: ast::ModExpression<'ast>) -> Self { AnyNode::ModExpression(node) } } -impl From for AnyNode { - fn from(node: ast::StmtFunctionDef) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: ast::StmtFunctionDef<'ast>) -> Self { AnyNode::StmtFunctionDef(node) } } -impl From for AnyNode { - fn from(node: ast::StmtClassDef) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: ast::StmtClassDef<'ast>) -> Self { AnyNode::StmtClassDef(node) } } -impl From for AnyNode { - fn from(node: ast::StmtReturn) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: ast::StmtReturn<'ast>) -> Self { AnyNode::StmtReturn(node) } } -impl From for AnyNode { - fn from(node: ast::StmtDelete) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: ast::StmtDelete<'ast>) -> Self { AnyNode::StmtDelete(node) } } -impl From for AnyNode { - fn from(node: ast::StmtTypeAlias) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: ast::StmtTypeAlias<'ast>) -> Self { AnyNode::StmtTypeAlias(node) } } -impl From for AnyNode { - fn from(node: ast::StmtAssign) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: ast::StmtAssign<'ast>) -> Self { AnyNode::StmtAssign(node) } } -impl From for AnyNode { - fn from(node: ast::StmtAugAssign) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: ast::StmtAugAssign<'ast>) -> Self { AnyNode::StmtAugAssign(node) } } -impl From for AnyNode { - fn from(node: ast::StmtAnnAssign) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: ast::StmtAnnAssign<'ast>) -> Self { AnyNode::StmtAnnAssign(node) } } -impl From for AnyNode { - fn from(node: ast::StmtFor) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: ast::StmtFor<'ast>) -> Self { AnyNode::StmtFor(node) } } -impl From for AnyNode { - fn from(node: ast::StmtWhile) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: ast::StmtWhile<'ast>) -> Self { AnyNode::StmtWhile(node) } } -impl From for AnyNode { - fn from(node: ast::StmtIf) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: ast::StmtIf<'ast>) -> Self { AnyNode::StmtIf(node) } } -impl From for AnyNode { - fn from(node: ast::ElifElseClause) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: ast::ElifElseClause<'ast>) -> Self { AnyNode::ElifElseClause(node) } } -impl From for AnyNode { - fn from(node: ast::StmtWith) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: ast::StmtWith<'ast>) -> Self { AnyNode::StmtWith(node) } } -impl From for AnyNode { - fn from(node: ast::StmtMatch) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: ast::StmtMatch<'ast>) -> Self { AnyNode::StmtMatch(node) } } -impl From for AnyNode { - fn from(node: ast::StmtRaise) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: ast::StmtRaise<'ast>) -> Self { AnyNode::StmtRaise(node) } } -impl From for AnyNode { - fn from(node: ast::StmtTry) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: ast::StmtTry<'ast>) -> Self { AnyNode::StmtTry(node) } } -impl From for AnyNode { - fn from(node: ast::StmtAssert) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: ast::StmtAssert<'ast>) -> Self { AnyNode::StmtAssert(node) } } -impl From for AnyNode { - fn from(node: ast::StmtImport) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: ast::StmtImport<'ast>) -> Self { AnyNode::StmtImport(node) } } -impl From for AnyNode { - fn from(node: ast::StmtImportFrom) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: ast::StmtImportFrom<'ast>) -> Self { AnyNode::StmtImportFrom(node) } } -impl From for AnyNode { - fn from(node: ast::StmtGlobal) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: ast::StmtGlobal<'ast>) -> Self { AnyNode::StmtGlobal(node) } } -impl From for AnyNode { - fn from(node: ast::StmtNonlocal) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: ast::StmtNonlocal<'ast>) -> Self { AnyNode::StmtNonlocal(node) } } -impl From for AnyNode { - fn from(node: ast::StmtExpr) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: ast::StmtExpr<'ast>) -> Self { AnyNode::StmtExpr(node) } } -impl From for AnyNode { +impl<'ast> From for AnyNode<'ast> { fn from(node: ast::StmtPass) -> Self { AnyNode::StmtPass(node) } } -impl From for AnyNode { +impl<'ast> From for AnyNode<'ast> { fn from(node: ast::StmtBreak) -> Self { AnyNode::StmtBreak(node) } } -impl From for AnyNode { +impl<'ast> From for AnyNode<'ast> { fn from(node: ast::StmtContinue) -> Self { AnyNode::StmtContinue(node) } } -impl From for AnyNode { - fn from(node: ast::StmtIpyEscapeCommand) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: ast::StmtIpyEscapeCommand<'ast>) -> Self { AnyNode::StmtIpyEscapeCommand(node) } } -impl From for AnyNode { - fn from(node: ast::ExprBoolOp) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: ast::ExprBoolOp<'ast>) -> Self { AnyNode::ExprBoolOp(node) } } -impl From for AnyNode { - fn from(node: ast::ExprNamed) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: ast::ExprNamed<'ast>) -> Self { AnyNode::ExprNamed(node) } } -impl From for AnyNode { - fn from(node: ast::ExprBinOp) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: ast::ExprBinOp<'ast>) -> Self { AnyNode::ExprBinOp(node) } } -impl From for AnyNode { - fn from(node: ast::ExprUnaryOp) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: ast::ExprUnaryOp<'ast>) -> Self { AnyNode::ExprUnaryOp(node) } } -impl From for AnyNode { - fn from(node: ast::ExprLambda) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: ast::ExprLambda<'ast>) -> Self { AnyNode::ExprLambda(node) } } -impl From for AnyNode { - fn from(node: ast::ExprIf) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: ast::ExprIf<'ast>) -> Self { AnyNode::ExprIf(node) } } -impl From for AnyNode { - fn from(node: ast::ExprDict) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: ast::ExprDict<'ast>) -> Self { AnyNode::ExprDict(node) } } -impl From for AnyNode { - fn from(node: ast::ExprSet) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: ast::ExprSet<'ast>) -> Self { AnyNode::ExprSet(node) } } -impl From for AnyNode { - fn from(node: ast::ExprListComp) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: ast::ExprListComp<'ast>) -> Self { AnyNode::ExprListComp(node) } } -impl From for AnyNode { - fn from(node: ast::ExprSetComp) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: ast::ExprSetComp<'ast>) -> Self { AnyNode::ExprSetComp(node) } } -impl From for AnyNode { - fn from(node: ast::ExprDictComp) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: ast::ExprDictComp<'ast>) -> Self { AnyNode::ExprDictComp(node) } } -impl From for AnyNode { - fn from(node: ast::ExprGenerator) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: ast::ExprGenerator<'ast>) -> Self { AnyNode::ExprGenerator(node) } } -impl From for AnyNode { - fn from(node: ast::ExprAwait) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: ast::ExprAwait<'ast>) -> Self { AnyNode::ExprAwait(node) } } -impl From for AnyNode { - fn from(node: ast::ExprYield) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: ast::ExprYield<'ast>) -> Self { AnyNode::ExprYield(node) } } -impl From for AnyNode { - fn from(node: ast::ExprYieldFrom) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: ast::ExprYieldFrom<'ast>) -> Self { AnyNode::ExprYieldFrom(node) } } -impl From for AnyNode { - fn from(node: ast::ExprCompare) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: ast::ExprCompare<'ast>) -> Self { AnyNode::ExprCompare(node) } } -impl From for AnyNode { - fn from(node: ast::ExprCall) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: ast::ExprCall<'ast>) -> Self { AnyNode::ExprCall(node) } } -impl From for AnyNode { - fn from(node: ast::FStringExpressionElement) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: ast::FStringExpressionElement<'ast>) -> Self { AnyNode::FStringExpressionElement(node) } } -impl From for AnyNode { - fn from(node: ast::FStringLiteralElement) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: ast::FStringLiteralElement<'ast>) -> Self { AnyNode::FStringLiteralElement(node) } } -impl From for AnyNode { - fn from(node: ast::FStringFormatSpec) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: ast::FStringFormatSpec<'ast>) -> Self { AnyNode::FStringFormatSpec(node) } } -impl From for AnyNode { - fn from(node: ast::ExprFString) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: ast::ExprFString<'ast>) -> Self { AnyNode::ExprFString(node) } } -impl From for AnyNode { - fn from(node: ast::ExprStringLiteral) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: ast::ExprStringLiteral<'ast>) -> Self { AnyNode::ExprStringLiteral(node) } } -impl From for AnyNode { - fn from(node: ast::ExprBytesLiteral) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: ast::ExprBytesLiteral<'ast>) -> Self { AnyNode::ExprBytesLiteral(node) } } -impl From for AnyNode { +impl<'ast> From for AnyNode<'ast> { fn from(node: ast::ExprNumberLiteral) -> Self { AnyNode::ExprNumberLiteral(node) } } -impl From for AnyNode { +impl<'ast> From for AnyNode<'ast> { fn from(node: ast::ExprBooleanLiteral) -> Self { AnyNode::ExprBooleanLiteral(node) } } -impl From for AnyNode { +impl<'ast> From for AnyNode<'ast> { fn from(node: ast::ExprNoneLiteral) -> Self { AnyNode::ExprNoneLiteral(node) } } -impl From for AnyNode { +impl<'ast> From for AnyNode<'ast> { fn from(node: ast::ExprEllipsisLiteral) -> Self { AnyNode::ExprEllipsisLiteral(node) } } -impl From for AnyNode { - fn from(node: ast::ExprAttribute) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: ast::ExprAttribute<'ast>) -> Self { AnyNode::ExprAttribute(node) } } -impl From for AnyNode { - fn from(node: ast::ExprSubscript) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: ast::ExprSubscript<'ast>) -> Self { AnyNode::ExprSubscript(node) } } -impl From for AnyNode { - fn from(node: ast::ExprStarred) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: ast::ExprStarred<'ast>) -> Self { AnyNode::ExprStarred(node) } } -impl From for AnyNode { - fn from(node: ast::ExprName) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: ast::ExprName<'ast>) -> Self { AnyNode::ExprName(node) } } -impl From for AnyNode { - fn from(node: ast::ExprList) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: ast::ExprList<'ast>) -> Self { AnyNode::ExprList(node) } } -impl From for AnyNode { - fn from(node: ast::ExprTuple) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: ast::ExprTuple<'ast>) -> Self { AnyNode::ExprTuple(node) } } -impl From for AnyNode { - fn from(node: ast::ExprSlice) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: ast::ExprSlice<'ast>) -> Self { AnyNode::ExprSlice(node) } } -impl From for AnyNode { - fn from(node: ast::ExprIpyEscapeCommand) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: ast::ExprIpyEscapeCommand<'ast>) -> Self { AnyNode::ExprIpyEscapeCommand(node) } } -impl From for AnyNode { - fn from(node: ast::ExceptHandlerExceptHandler) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: ast::ExceptHandlerExceptHandler<'ast>) -> Self { AnyNode::ExceptHandlerExceptHandler(node) } } -impl From for AnyNode { - fn from(node: ast::PatternMatchValue) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: ast::PatternMatchValue<'ast>) -> Self { AnyNode::PatternMatchValue(node) } } -impl From for AnyNode { +impl<'ast> From for AnyNode<'ast> { fn from(node: ast::PatternMatchSingleton) -> Self { AnyNode::PatternMatchSingleton(node) } } -impl From for AnyNode { - fn from(node: ast::PatternMatchSequence) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: ast::PatternMatchSequence<'ast>) -> Self { AnyNode::PatternMatchSequence(node) } } -impl From for AnyNode { - fn from(node: ast::PatternMatchMapping) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: ast::PatternMatchMapping<'ast>) -> Self { AnyNode::PatternMatchMapping(node) } } -impl From for AnyNode { - fn from(node: ast::PatternMatchClass) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: ast::PatternMatchClass<'ast>) -> Self { AnyNode::PatternMatchClass(node) } } -impl From for AnyNode { - fn from(node: ast::PatternMatchStar) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: ast::PatternMatchStar<'ast>) -> Self { AnyNode::PatternMatchStar(node) } } -impl From for AnyNode { - fn from(node: ast::PatternMatchAs) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: ast::PatternMatchAs<'ast>) -> Self { AnyNode::PatternMatchAs(node) } } -impl From for AnyNode { - fn from(node: ast::PatternMatchOr) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: ast::PatternMatchOr<'ast>) -> Self { AnyNode::PatternMatchOr(node) } } -impl From for AnyNode { - fn from(node: PatternArguments) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: PatternArguments<'ast>) -> Self { AnyNode::PatternArguments(node) } } -impl From for AnyNode { - fn from(node: PatternKeyword) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: PatternKeyword<'ast>) -> Self { AnyNode::PatternKeyword(node) } } -impl From for AnyNode { - fn from(node: Comprehension) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: Comprehension<'ast>) -> Self { AnyNode::Comprehension(node) } } -impl From for AnyNode { - fn from(node: Arguments) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: Arguments<'ast>) -> Self { AnyNode::Arguments(node) } } -impl From for AnyNode { - fn from(node: Parameters) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: Parameters<'ast>) -> Self { AnyNode::Parameters(node) } } -impl From for AnyNode { - fn from(node: Parameter) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: Parameter<'ast>) -> Self { AnyNode::Parameter(node) } } -impl From for AnyNode { - fn from(node: ParameterWithDefault) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: ParameterWithDefault<'ast>) -> Self { AnyNode::ParameterWithDefault(node) } } -impl From for AnyNode { - fn from(node: Keyword) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: Keyword<'ast>) -> Self { AnyNode::Keyword(node) } } -impl From for AnyNode { - fn from(node: Alias) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: Alias<'ast>) -> Self { AnyNode::Alias(node) } } -impl From for AnyNode { - fn from(node: WithItem) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: WithItem<'ast>) -> Self { AnyNode::WithItem(node) } } -impl From for AnyNode { - fn from(node: MatchCase) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: MatchCase<'ast>) -> Self { AnyNode::MatchCase(node) } } -impl From for AnyNode { - fn from(node: Decorator) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: Decorator<'ast>) -> Self { AnyNode::Decorator(node) } } -impl From for AnyNode { - fn from(node: TypeParams) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: TypeParams<'ast>) -> Self { AnyNode::TypeParams(node) } } -impl From for AnyNode { - fn from(node: TypeParamTypeVar) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: TypeParamTypeVar<'ast>) -> Self { AnyNode::TypeParamTypeVar(node) } } -impl From for AnyNode { - fn from(node: TypeParamTypeVarTuple) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: TypeParamTypeVarTuple<'ast>) -> Self { AnyNode::TypeParamTypeVarTuple(node) } } -impl From for AnyNode { - fn from(node: TypeParamParamSpec) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: TypeParamParamSpec<'ast>) -> Self { AnyNode::TypeParamParamSpec(node) } } -impl From for AnyNode { - fn from(node: ast::FString) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: ast::FString<'ast>) -> Self { AnyNode::FString(node) } } -impl From for AnyNode { - fn from(node: ast::StringLiteral) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: ast::StringLiteral<'ast>) -> Self { AnyNode::StringLiteral(node) } } -impl From for AnyNode { - fn from(node: ast::BytesLiteral) -> Self { +impl<'ast> From> for AnyNode<'ast> { + fn from(node: ast::BytesLiteral<'ast>) -> Self { AnyNode::BytesLiteral(node) } } -impl Ranged for AnyNode { +impl Ranged for AnyNode<'_> { fn range(&self) -> TextRange { match self { AnyNode::ModModule(node) => node.range(), @@ -6082,101 +6179,101 @@ impl Ranged for AnyNode { } #[derive(Copy, Clone, Debug, is_macro::Is, PartialEq)] -pub enum AnyNodeRef<'a> { - ModModule(&'a ast::ModModule), - ModExpression(&'a ast::ModExpression), - StmtFunctionDef(&'a ast::StmtFunctionDef), - StmtClassDef(&'a ast::StmtClassDef), - StmtReturn(&'a ast::StmtReturn), - StmtDelete(&'a ast::StmtDelete), - StmtTypeAlias(&'a ast::StmtTypeAlias), - StmtAssign(&'a ast::StmtAssign), - StmtAugAssign(&'a ast::StmtAugAssign), - StmtAnnAssign(&'a ast::StmtAnnAssign), - StmtFor(&'a ast::StmtFor), - StmtWhile(&'a ast::StmtWhile), - StmtIf(&'a ast::StmtIf), - StmtWith(&'a ast::StmtWith), - StmtMatch(&'a ast::StmtMatch), - StmtRaise(&'a ast::StmtRaise), - StmtTry(&'a ast::StmtTry), - StmtAssert(&'a ast::StmtAssert), - StmtImport(&'a ast::StmtImport), - StmtImportFrom(&'a ast::StmtImportFrom), - StmtGlobal(&'a ast::StmtGlobal), - StmtNonlocal(&'a ast::StmtNonlocal), - StmtExpr(&'a ast::StmtExpr), +pub enum AnyNodeRef<'a, 'ast> { + ModModule(&'a ast::ModModule<'ast>), + ModExpression(&'a ast::ModExpression<'ast>), + StmtFunctionDef(&'a ast::StmtFunctionDef<'ast>), + StmtClassDef(&'a ast::StmtClassDef<'ast>), + StmtReturn(&'a ast::StmtReturn<'ast>), + StmtDelete(&'a ast::StmtDelete<'ast>), + StmtTypeAlias(&'a ast::StmtTypeAlias<'ast>), + StmtAssign(&'a ast::StmtAssign<'ast>), + StmtAugAssign(&'a ast::StmtAugAssign<'ast>), + StmtAnnAssign(&'a ast::StmtAnnAssign<'ast>), + StmtFor(&'a ast::StmtFor<'ast>), + StmtWhile(&'a ast::StmtWhile<'ast>), + StmtIf(&'a ast::StmtIf<'ast>), + StmtWith(&'a ast::StmtWith<'ast>), + StmtMatch(&'a ast::StmtMatch<'ast>), + StmtRaise(&'a ast::StmtRaise<'ast>), + StmtTry(&'a ast::StmtTry<'ast>), + StmtAssert(&'a ast::StmtAssert<'ast>), + StmtImport(&'a ast::StmtImport<'ast>), + StmtImportFrom(&'a ast::StmtImportFrom<'ast>), + StmtGlobal(&'a ast::StmtGlobal<'ast>), + StmtNonlocal(&'a ast::StmtNonlocal<'ast>), + StmtExpr(&'a ast::StmtExpr<'ast>), StmtPass(&'a ast::StmtPass), StmtBreak(&'a ast::StmtBreak), StmtContinue(&'a ast::StmtContinue), - StmtIpyEscapeCommand(&'a ast::StmtIpyEscapeCommand), - ExprBoolOp(&'a ast::ExprBoolOp), - ExprNamed(&'a ast::ExprNamed), - ExprBinOp(&'a ast::ExprBinOp), - ExprUnaryOp(&'a ast::ExprUnaryOp), - ExprLambda(&'a ast::ExprLambda), - ExprIf(&'a ast::ExprIf), - ExprDict(&'a ast::ExprDict), - ExprSet(&'a ast::ExprSet), - ExprListComp(&'a ast::ExprListComp), - ExprSetComp(&'a ast::ExprSetComp), - ExprDictComp(&'a ast::ExprDictComp), - ExprGenerator(&'a ast::ExprGenerator), - ExprAwait(&'a ast::ExprAwait), - ExprYield(&'a ast::ExprYield), - ExprYieldFrom(&'a ast::ExprYieldFrom), - ExprCompare(&'a ast::ExprCompare), - ExprCall(&'a ast::ExprCall), - FStringExpressionElement(&'a ast::FStringExpressionElement), - FStringLiteralElement(&'a ast::FStringLiteralElement), - FStringFormatSpec(&'a ast::FStringFormatSpec), - ExprFString(&'a ast::ExprFString), - ExprStringLiteral(&'a ast::ExprStringLiteral), - ExprBytesLiteral(&'a ast::ExprBytesLiteral), + StmtIpyEscapeCommand(&'a ast::StmtIpyEscapeCommand<'ast>), + ExprBoolOp(&'a ast::ExprBoolOp<'ast>), + ExprNamed(&'a ast::ExprNamed<'ast>), + ExprBinOp(&'a ast::ExprBinOp<'ast>), + ExprUnaryOp(&'a ast::ExprUnaryOp<'ast>), + ExprLambda(&'a ast::ExprLambda<'ast>), + ExprIf(&'a ast::ExprIf<'ast>), + ExprDict(&'a ast::ExprDict<'ast>), + ExprSet(&'a ast::ExprSet<'ast>), + ExprListComp(&'a ast::ExprListComp<'ast>), + ExprSetComp(&'a ast::ExprSetComp<'ast>), + ExprDictComp(&'a ast::ExprDictComp<'ast>), + ExprGenerator(&'a ast::ExprGenerator<'ast>), + ExprAwait(&'a ast::ExprAwait<'ast>), + ExprYield(&'a ast::ExprYield<'ast>), + ExprYieldFrom(&'a ast::ExprYieldFrom<'ast>), + ExprCompare(&'a ast::ExprCompare<'ast>), + ExprCall(&'a ast::ExprCall<'ast>), + FStringExpressionElement(&'a ast::FStringExpressionElement<'ast>), + FStringLiteralElement(&'a ast::FStringLiteralElement<'ast>), + FStringFormatSpec(&'a ast::FStringFormatSpec<'ast>), + ExprFString(&'a ast::ExprFString<'ast>), + ExprStringLiteral(&'a ast::ExprStringLiteral<'ast>), + ExprBytesLiteral(&'a ast::ExprBytesLiteral<'ast>), ExprNumberLiteral(&'a ast::ExprNumberLiteral), ExprBooleanLiteral(&'a ast::ExprBooleanLiteral), ExprNoneLiteral(&'a ast::ExprNoneLiteral), ExprEllipsisLiteral(&'a ast::ExprEllipsisLiteral), - ExprAttribute(&'a ast::ExprAttribute), - ExprSubscript(&'a ast::ExprSubscript), - ExprStarred(&'a ast::ExprStarred), - ExprName(&'a ast::ExprName), - ExprList(&'a ast::ExprList), - ExprTuple(&'a ast::ExprTuple), - ExprSlice(&'a ast::ExprSlice), - ExprIpyEscapeCommand(&'a ast::ExprIpyEscapeCommand), - ExceptHandlerExceptHandler(&'a ast::ExceptHandlerExceptHandler), - PatternMatchValue(&'a ast::PatternMatchValue), + ExprAttribute(&'a ast::ExprAttribute<'ast>), + ExprSubscript(&'a ast::ExprSubscript<'ast>), + ExprStarred(&'a ast::ExprStarred<'ast>), + ExprName(&'a ast::ExprName<'ast>), + ExprList(&'a ast::ExprList<'ast>), + ExprTuple(&'a ast::ExprTuple<'ast>), + ExprSlice(&'a ast::ExprSlice<'ast>), + ExprIpyEscapeCommand(&'a ast::ExprIpyEscapeCommand<'ast>), + ExceptHandlerExceptHandler(&'a ast::ExceptHandlerExceptHandler<'ast>), + PatternMatchValue(&'a ast::PatternMatchValue<'ast>), PatternMatchSingleton(&'a ast::PatternMatchSingleton), - PatternMatchSequence(&'a ast::PatternMatchSequence), - PatternMatchMapping(&'a ast::PatternMatchMapping), - PatternMatchClass(&'a ast::PatternMatchClass), - PatternMatchStar(&'a ast::PatternMatchStar), - PatternMatchAs(&'a ast::PatternMatchAs), - PatternMatchOr(&'a ast::PatternMatchOr), - PatternArguments(&'a ast::PatternArguments), - PatternKeyword(&'a ast::PatternKeyword), - Comprehension(&'a Comprehension), - Arguments(&'a Arguments), - Parameters(&'a Parameters), - Parameter(&'a Parameter), - ParameterWithDefault(&'a ParameterWithDefault), - Keyword(&'a Keyword), - Alias(&'a Alias), - WithItem(&'a WithItem), - MatchCase(&'a MatchCase), - Decorator(&'a Decorator), - TypeParams(&'a TypeParams), - TypeParamTypeVar(&'a TypeParamTypeVar), - TypeParamTypeVarTuple(&'a TypeParamTypeVarTuple), - TypeParamParamSpec(&'a TypeParamParamSpec), - FString(&'a ast::FString), - StringLiteral(&'a ast::StringLiteral), - BytesLiteral(&'a ast::BytesLiteral), - ElifElseClause(&'a ast::ElifElseClause), + PatternMatchSequence(&'a ast::PatternMatchSequence<'ast>), + PatternMatchMapping(&'a ast::PatternMatchMapping<'ast>), + PatternMatchClass(&'a ast::PatternMatchClass<'ast>), + PatternMatchStar(&'a ast::PatternMatchStar<'ast>), + PatternMatchAs(&'a ast::PatternMatchAs<'ast>), + PatternMatchOr(&'a ast::PatternMatchOr<'ast>), + PatternArguments(&'a ast::PatternArguments<'ast>), + PatternKeyword(&'a ast::PatternKeyword<'ast>), + Comprehension(&'a Comprehension<'ast>), + Arguments(&'a Arguments<'ast>), + Parameters(&'a Parameters<'ast>), + Parameter(&'a Parameter<'ast>), + ParameterWithDefault(&'a ParameterWithDefault<'ast>), + Keyword(&'a Keyword<'ast>), + Alias(&'a Alias<'ast>), + WithItem(&'a WithItem<'ast>), + MatchCase(&'a MatchCase<'ast>), + Decorator(&'a Decorator<'ast>), + TypeParams(&'a TypeParams<'ast>), + TypeParamTypeVar(&'a TypeParamTypeVar<'ast>), + TypeParamTypeVarTuple(&'a TypeParamTypeVarTuple<'ast>), + TypeParamParamSpec(&'a TypeParamParamSpec<'ast>), + FString(&'a ast::FString<'ast>), + StringLiteral(&'a ast::StringLiteral<'ast>), + BytesLiteral(&'a ast::BytesLiteral<'ast>), + ElifElseClause(&'a ast::ElifElseClause<'ast>), } -impl<'a> AnyNodeRef<'a> { +impl<'a, 'ast> AnyNodeRef<'a, 'ast> { pub fn as_ptr(&self) -> NonNull<()> { match self { AnyNodeRef::ModModule(node) => NonNull::from(*node).cast(), @@ -6869,10 +6966,10 @@ impl<'a> AnyNodeRef<'a> { ) } - pub fn visit_preorder<'b, V>(self, visitor: &mut V) + pub fn visit_preorder(self, visitor: &mut V) where - V: SourceOrderVisitor<'b> + ?Sized, - 'a: 'b, + 'ast: 'a, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { match self { AnyNodeRef::ModModule(node) => node.visit_source_order(visitor), @@ -6970,7 +7067,7 @@ impl<'a> AnyNodeRef<'a> { } /// The last child of the last branch, if the node has multiple branches. - pub fn last_child_in_body(&self) -> Option> { + pub fn last_child_in_body(&self) -> Option> { let body = match self { AnyNodeRef::StmtFunctionDef(ast::StmtFunctionDef { body, .. }) | AnyNodeRef::StmtClassDef(ast::StmtClassDef { body, .. }) @@ -7137,506 +7234,507 @@ impl<'a> AnyNodeRef<'a> { } /// Returns `true` if `right` is `Some` and `left` and `right` are referentially equal. -fn are_same_optional<'a, T>(left: AnyNodeRef, right: Option) -> bool +fn are_same_optional<'a, 'ast, T>(left: AnyNodeRef, right: Option) -> bool where - T: Into>, + 'ast: 'a, + T: Into>, { right.is_some_and(|right| left.ptr_eq(right.into())) } -impl<'a> From<&'a ast::ModModule> for AnyNodeRef<'a> { - fn from(node: &'a ast::ModModule) -> Self { +impl<'a, 'ast> From<&'a ast::ModModule<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::ModModule<'ast>) -> Self { AnyNodeRef::ModModule(node) } } -impl<'a> From<&'a ast::ModExpression> for AnyNodeRef<'a> { - fn from(node: &'a ast::ModExpression) -> Self { +impl<'a, 'ast> From<&'a ast::ModExpression<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::ModExpression<'ast>) -> Self { AnyNodeRef::ModExpression(node) } } -impl<'a> From<&'a ast::StmtFunctionDef> for AnyNodeRef<'a> { - fn from(node: &'a ast::StmtFunctionDef) -> Self { +impl<'a, 'ast> From<&'a ast::StmtFunctionDef<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::StmtFunctionDef<'ast>) -> Self { AnyNodeRef::StmtFunctionDef(node) } } -impl<'a> From<&'a ast::StmtClassDef> for AnyNodeRef<'a> { - fn from(node: &'a ast::StmtClassDef) -> Self { +impl<'a, 'ast> From<&'a ast::StmtClassDef<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::StmtClassDef<'ast>) -> Self { AnyNodeRef::StmtClassDef(node) } } -impl<'a> From<&'a ast::StmtReturn> for AnyNodeRef<'a> { - fn from(node: &'a ast::StmtReturn) -> Self { +impl<'a, 'ast> From<&'a ast::StmtReturn<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::StmtReturn<'ast>) -> Self { AnyNodeRef::StmtReturn(node) } } -impl<'a> From<&'a ast::StmtDelete> for AnyNodeRef<'a> { - fn from(node: &'a ast::StmtDelete) -> Self { +impl<'a, 'ast> From<&'a ast::StmtDelete<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::StmtDelete<'ast>) -> Self { AnyNodeRef::StmtDelete(node) } } -impl<'a> From<&'a ast::StmtTypeAlias> for AnyNodeRef<'a> { - fn from(node: &'a ast::StmtTypeAlias) -> Self { +impl<'a, 'ast> From<&'a ast::StmtTypeAlias<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::StmtTypeAlias<'ast>) -> Self { AnyNodeRef::StmtTypeAlias(node) } } -impl<'a> From<&'a ast::StmtAssign> for AnyNodeRef<'a> { - fn from(node: &'a ast::StmtAssign) -> Self { +impl<'a, 'ast> From<&'a ast::StmtAssign<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::StmtAssign<'ast>) -> Self { AnyNodeRef::StmtAssign(node) } } -impl<'a> From<&'a ast::StmtAugAssign> for AnyNodeRef<'a> { - fn from(node: &'a ast::StmtAugAssign) -> Self { +impl<'a, 'ast> From<&'a ast::StmtAugAssign<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::StmtAugAssign<'ast>) -> Self { AnyNodeRef::StmtAugAssign(node) } } -impl<'a> From<&'a ast::StmtAnnAssign> for AnyNodeRef<'a> { - fn from(node: &'a ast::StmtAnnAssign) -> Self { +impl<'a, 'ast> From<&'a ast::StmtAnnAssign<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::StmtAnnAssign<'ast>) -> Self { AnyNodeRef::StmtAnnAssign(node) } } -impl<'a> From<&'a ast::StmtFor> for AnyNodeRef<'a> { - fn from(node: &'a ast::StmtFor) -> Self { +impl<'a, 'ast> From<&'a ast::StmtFor<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::StmtFor<'ast>) -> Self { AnyNodeRef::StmtFor(node) } } -impl<'a> From<&'a ast::StmtWhile> for AnyNodeRef<'a> { - fn from(node: &'a ast::StmtWhile) -> Self { +impl<'a, 'ast> From<&'a ast::StmtWhile<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::StmtWhile<'ast>) -> Self { AnyNodeRef::StmtWhile(node) } } -impl<'a> From<&'a ast::StmtIf> for AnyNodeRef<'a> { - fn from(node: &'a ast::StmtIf) -> Self { +impl<'a, 'ast> From<&'a ast::StmtIf<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::StmtIf<'ast>) -> Self { AnyNodeRef::StmtIf(node) } } -impl<'a> From<&'a ast::ElifElseClause> for AnyNodeRef<'a> { - fn from(node: &'a ast::ElifElseClause) -> Self { +impl<'a, 'ast> From<&'a ast::ElifElseClause<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::ElifElseClause<'ast>) -> Self { AnyNodeRef::ElifElseClause(node) } } -impl<'a> From<&'a ast::StmtWith> for AnyNodeRef<'a> { - fn from(node: &'a ast::StmtWith) -> Self { +impl<'a, 'ast> From<&'a ast::StmtWith<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::StmtWith<'ast>) -> Self { AnyNodeRef::StmtWith(node) } } -impl<'a> From<&'a ast::StmtMatch> for AnyNodeRef<'a> { - fn from(node: &'a ast::StmtMatch) -> Self { +impl<'a, 'ast> From<&'a ast::StmtMatch<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::StmtMatch<'ast>) -> Self { AnyNodeRef::StmtMatch(node) } } -impl<'a> From<&'a ast::StmtRaise> for AnyNodeRef<'a> { - fn from(node: &'a ast::StmtRaise) -> Self { +impl<'a, 'ast> From<&'a ast::StmtRaise<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::StmtRaise<'ast>) -> Self { AnyNodeRef::StmtRaise(node) } } -impl<'a> From<&'a ast::StmtTry> for AnyNodeRef<'a> { - fn from(node: &'a ast::StmtTry) -> Self { +impl<'a, 'ast> From<&'a ast::StmtTry<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::StmtTry<'ast>) -> Self { AnyNodeRef::StmtTry(node) } } -impl<'a> From<&'a ast::StmtAssert> for AnyNodeRef<'a> { - fn from(node: &'a ast::StmtAssert) -> Self { +impl<'a, 'ast> From<&'a ast::StmtAssert<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::StmtAssert<'ast>) -> Self { AnyNodeRef::StmtAssert(node) } } -impl<'a> From<&'a ast::StmtImport> for AnyNodeRef<'a> { - fn from(node: &'a ast::StmtImport) -> Self { +impl<'a, 'ast> From<&'a ast::StmtImport<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::StmtImport<'ast>) -> Self { AnyNodeRef::StmtImport(node) } } -impl<'a> From<&'a ast::StmtImportFrom> for AnyNodeRef<'a> { - fn from(node: &'a ast::StmtImportFrom) -> Self { +impl<'a, 'ast> From<&'a ast::StmtImportFrom<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::StmtImportFrom<'ast>) -> Self { AnyNodeRef::StmtImportFrom(node) } } -impl<'a> From<&'a ast::StmtGlobal> for AnyNodeRef<'a> { - fn from(node: &'a ast::StmtGlobal) -> Self { +impl<'a, 'ast> From<&'a ast::StmtGlobal<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::StmtGlobal<'ast>) -> Self { AnyNodeRef::StmtGlobal(node) } } -impl<'a> From<&'a ast::StmtNonlocal> for AnyNodeRef<'a> { - fn from(node: &'a ast::StmtNonlocal) -> Self { +impl<'a, 'ast> From<&'a ast::StmtNonlocal<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::StmtNonlocal<'ast>) -> Self { AnyNodeRef::StmtNonlocal(node) } } -impl<'a> From<&'a ast::StmtExpr> for AnyNodeRef<'a> { - fn from(node: &'a ast::StmtExpr) -> Self { +impl<'a, 'ast> From<&'a ast::StmtExpr<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::StmtExpr<'ast>) -> Self { AnyNodeRef::StmtExpr(node) } } -impl<'a> From<&'a ast::StmtPass> for AnyNodeRef<'a> { +impl<'a, 'ast> From<&'a ast::StmtPass> for AnyNodeRef<'a, 'ast> { fn from(node: &'a ast::StmtPass) -> Self { AnyNodeRef::StmtPass(node) } } -impl<'a> From<&'a ast::StmtBreak> for AnyNodeRef<'a> { +impl<'a, 'ast> From<&'a ast::StmtBreak> for AnyNodeRef<'a, 'ast> { fn from(node: &'a ast::StmtBreak) -> Self { AnyNodeRef::StmtBreak(node) } } -impl<'a> From<&'a ast::StmtContinue> for AnyNodeRef<'a> { +impl<'a, 'ast> From<&'a ast::StmtContinue> for AnyNodeRef<'a, 'ast> { fn from(node: &'a ast::StmtContinue) -> Self { AnyNodeRef::StmtContinue(node) } } -impl<'a> From<&'a ast::StmtIpyEscapeCommand> for AnyNodeRef<'a> { - fn from(node: &'a ast::StmtIpyEscapeCommand) -> Self { +impl<'a, 'ast> From<&'a ast::StmtIpyEscapeCommand<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::StmtIpyEscapeCommand<'ast>) -> Self { AnyNodeRef::StmtIpyEscapeCommand(node) } } -impl<'a> From<&'a ast::ExprBoolOp> for AnyNodeRef<'a> { - fn from(node: &'a ast::ExprBoolOp) -> Self { +impl<'a, 'ast> From<&'a ast::ExprBoolOp<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::ExprBoolOp<'ast>) -> Self { AnyNodeRef::ExprBoolOp(node) } } -impl<'a> From<&'a ast::ExprNamed> for AnyNodeRef<'a> { - fn from(node: &'a ast::ExprNamed) -> Self { +impl<'a, 'ast> From<&'a ast::ExprNamed<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::ExprNamed<'ast>) -> Self { AnyNodeRef::ExprNamed(node) } } -impl<'a> From<&'a ast::ExprBinOp> for AnyNodeRef<'a> { - fn from(node: &'a ast::ExprBinOp) -> Self { +impl<'a, 'ast> From<&'a ast::ExprBinOp<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::ExprBinOp<'ast>) -> Self { AnyNodeRef::ExprBinOp(node) } } -impl<'a> From<&'a ast::ExprUnaryOp> for AnyNodeRef<'a> { - fn from(node: &'a ast::ExprUnaryOp) -> Self { +impl<'a, 'ast> From<&'a ast::ExprUnaryOp<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::ExprUnaryOp<'ast>) -> Self { AnyNodeRef::ExprUnaryOp(node) } } -impl<'a> From<&'a ast::ExprLambda> for AnyNodeRef<'a> { - fn from(node: &'a ast::ExprLambda) -> Self { +impl<'a, 'ast> From<&'a ast::ExprLambda<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::ExprLambda<'ast>) -> Self { AnyNodeRef::ExprLambda(node) } } -impl<'a> From<&'a ast::ExprIf> for AnyNodeRef<'a> { - fn from(node: &'a ast::ExprIf) -> Self { +impl<'a, 'ast> From<&'a ast::ExprIf<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::ExprIf<'ast>) -> Self { AnyNodeRef::ExprIf(node) } } -impl<'a> From<&'a ast::ExprDict> for AnyNodeRef<'a> { - fn from(node: &'a ast::ExprDict) -> Self { +impl<'a, 'ast> From<&'a ast::ExprDict<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::ExprDict<'ast>) -> Self { AnyNodeRef::ExprDict(node) } } -impl<'a> From<&'a ast::ExprSet> for AnyNodeRef<'a> { - fn from(node: &'a ast::ExprSet) -> Self { +impl<'a, 'ast> From<&'a ast::ExprSet<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::ExprSet<'ast>) -> Self { AnyNodeRef::ExprSet(node) } } -impl<'a> From<&'a ast::ExprListComp> for AnyNodeRef<'a> { - fn from(node: &'a ast::ExprListComp) -> Self { +impl<'a, 'ast> From<&'a ast::ExprListComp<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::ExprListComp<'ast>) -> Self { AnyNodeRef::ExprListComp(node) } } -impl<'a> From<&'a ast::ExprSetComp> for AnyNodeRef<'a> { - fn from(node: &'a ast::ExprSetComp) -> Self { +impl<'a, 'ast> From<&'a ast::ExprSetComp<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::ExprSetComp<'ast>) -> Self { AnyNodeRef::ExprSetComp(node) } } -impl<'a> From<&'a ast::ExprDictComp> for AnyNodeRef<'a> { - fn from(node: &'a ast::ExprDictComp) -> Self { +impl<'a, 'ast> From<&'a ast::ExprDictComp<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::ExprDictComp<'ast>) -> Self { AnyNodeRef::ExprDictComp(node) } } -impl<'a> From<&'a ast::ExprGenerator> for AnyNodeRef<'a> { - fn from(node: &'a ast::ExprGenerator) -> Self { +impl<'a, 'ast> From<&'a ast::ExprGenerator<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::ExprGenerator<'ast>) -> Self { AnyNodeRef::ExprGenerator(node) } } -impl<'a> From<&'a ast::ExprAwait> for AnyNodeRef<'a> { - fn from(node: &'a ast::ExprAwait) -> Self { +impl<'a, 'ast> From<&'a ast::ExprAwait<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::ExprAwait<'ast>) -> Self { AnyNodeRef::ExprAwait(node) } } -impl<'a> From<&'a ast::ExprYield> for AnyNodeRef<'a> { - fn from(node: &'a ast::ExprYield) -> Self { +impl<'a, 'ast> From<&'a ast::ExprYield<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::ExprYield<'ast>) -> Self { AnyNodeRef::ExprYield(node) } } -impl<'a> From<&'a ast::ExprYieldFrom> for AnyNodeRef<'a> { - fn from(node: &'a ast::ExprYieldFrom) -> Self { +impl<'a, 'ast> From<&'a ast::ExprYieldFrom<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::ExprYieldFrom<'ast>) -> Self { AnyNodeRef::ExprYieldFrom(node) } } -impl<'a> From<&'a ast::ExprCompare> for AnyNodeRef<'a> { - fn from(node: &'a ast::ExprCompare) -> Self { +impl<'a, 'ast> From<&'a ast::ExprCompare<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::ExprCompare<'ast>) -> Self { AnyNodeRef::ExprCompare(node) } } -impl<'a> From<&'a ast::ExprCall> for AnyNodeRef<'a> { - fn from(node: &'a ast::ExprCall) -> Self { +impl<'a, 'ast> From<&'a ast::ExprCall<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::ExprCall<'ast>) -> Self { AnyNodeRef::ExprCall(node) } } -impl<'a> From<&'a ast::FStringExpressionElement> for AnyNodeRef<'a> { - fn from(node: &'a ast::FStringExpressionElement) -> Self { +impl<'a, 'ast> From<&'a ast::FStringExpressionElement<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::FStringExpressionElement<'ast>) -> Self { AnyNodeRef::FStringExpressionElement(node) } } -impl<'a> From<&'a ast::FStringLiteralElement> for AnyNodeRef<'a> { - fn from(node: &'a ast::FStringLiteralElement) -> Self { +impl<'a, 'ast> From<&'a ast::FStringLiteralElement<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::FStringLiteralElement<'ast>) -> Self { AnyNodeRef::FStringLiteralElement(node) } } -impl<'a> From<&'a ast::FStringFormatSpec> for AnyNodeRef<'a> { - fn from(node: &'a ast::FStringFormatSpec) -> Self { +impl<'a, 'ast> From<&'a ast::FStringFormatSpec<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::FStringFormatSpec<'ast>) -> Self { AnyNodeRef::FStringFormatSpec(node) } } -impl<'a> From<&'a ast::ExprFString> for AnyNodeRef<'a> { - fn from(node: &'a ast::ExprFString) -> Self { +impl<'a, 'ast> From<&'a ast::ExprFString<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::ExprFString<'ast>) -> Self { AnyNodeRef::ExprFString(node) } } -impl<'a> From<&'a ast::ExprStringLiteral> for AnyNodeRef<'a> { - fn from(node: &'a ast::ExprStringLiteral) -> Self { +impl<'a, 'ast> From<&'a ast::ExprStringLiteral<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::ExprStringLiteral<'ast>) -> Self { AnyNodeRef::ExprStringLiteral(node) } } -impl<'a> From<&'a ast::ExprBytesLiteral> for AnyNodeRef<'a> { - fn from(node: &'a ast::ExprBytesLiteral) -> Self { +impl<'a, 'ast> From<&'a ast::ExprBytesLiteral<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::ExprBytesLiteral<'ast>) -> Self { AnyNodeRef::ExprBytesLiteral(node) } } -impl<'a> From<&'a ast::ExprNumberLiteral> for AnyNodeRef<'a> { +impl<'a, 'ast> From<&'a ast::ExprNumberLiteral> for AnyNodeRef<'a, 'ast> { fn from(node: &'a ast::ExprNumberLiteral) -> Self { AnyNodeRef::ExprNumberLiteral(node) } } -impl<'a> From<&'a ast::ExprBooleanLiteral> for AnyNodeRef<'a> { +impl<'a, 'ast> From<&'a ast::ExprBooleanLiteral> for AnyNodeRef<'a, 'ast> { fn from(node: &'a ast::ExprBooleanLiteral) -> Self { AnyNodeRef::ExprBooleanLiteral(node) } } -impl<'a> From<&'a ast::ExprNoneLiteral> for AnyNodeRef<'a> { +impl<'a, 'ast> From<&'a ast::ExprNoneLiteral> for AnyNodeRef<'a, 'ast> { fn from(node: &'a ast::ExprNoneLiteral) -> Self { AnyNodeRef::ExprNoneLiteral(node) } } -impl<'a> From<&'a ast::ExprEllipsisLiteral> for AnyNodeRef<'a> { +impl<'a, 'ast> From<&'a ast::ExprEllipsisLiteral> for AnyNodeRef<'a, 'ast> { fn from(node: &'a ast::ExprEllipsisLiteral) -> Self { AnyNodeRef::ExprEllipsisLiteral(node) } } -impl<'a> From<&'a ast::ExprAttribute> for AnyNodeRef<'a> { - fn from(node: &'a ast::ExprAttribute) -> Self { +impl<'a, 'ast> From<&'a ast::ExprAttribute<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::ExprAttribute<'ast>) -> Self { AnyNodeRef::ExprAttribute(node) } } -impl<'a> From<&'a ast::ExprSubscript> for AnyNodeRef<'a> { - fn from(node: &'a ast::ExprSubscript) -> Self { +impl<'a, 'ast> From<&'a ast::ExprSubscript<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::ExprSubscript<'ast>) -> Self { AnyNodeRef::ExprSubscript(node) } } -impl<'a> From<&'a ast::ExprStarred> for AnyNodeRef<'a> { - fn from(node: &'a ast::ExprStarred) -> Self { +impl<'a, 'ast> From<&'a ast::ExprStarred<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::ExprStarred<'ast>) -> Self { AnyNodeRef::ExprStarred(node) } } -impl<'a> From<&'a ast::ExprName> for AnyNodeRef<'a> { - fn from(node: &'a ast::ExprName) -> Self { +impl<'a, 'ast> From<&'a ast::ExprName<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::ExprName<'ast>) -> Self { AnyNodeRef::ExprName(node) } } -impl<'a> From<&'a ast::ExprList> for AnyNodeRef<'a> { - fn from(node: &'a ast::ExprList) -> Self { +impl<'a, 'ast> From<&'a ast::ExprList<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::ExprList<'ast>) -> Self { AnyNodeRef::ExprList(node) } } -impl<'a> From<&'a ast::ExprTuple> for AnyNodeRef<'a> { - fn from(node: &'a ast::ExprTuple) -> Self { +impl<'a, 'ast> From<&'a ast::ExprTuple<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::ExprTuple<'ast>) -> Self { AnyNodeRef::ExprTuple(node) } } -impl<'a> From<&'a ast::ExprSlice> for AnyNodeRef<'a> { - fn from(node: &'a ast::ExprSlice) -> Self { +impl<'a, 'ast> From<&'a ast::ExprSlice<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::ExprSlice<'ast>) -> Self { AnyNodeRef::ExprSlice(node) } } -impl<'a> From<&'a ast::ExprIpyEscapeCommand> for AnyNodeRef<'a> { - fn from(node: &'a ast::ExprIpyEscapeCommand) -> Self { +impl<'a, 'ast> From<&'a ast::ExprIpyEscapeCommand<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::ExprIpyEscapeCommand<'ast>) -> Self { AnyNodeRef::ExprIpyEscapeCommand(node) } } -impl<'a> From<&'a ast::ExceptHandlerExceptHandler> for AnyNodeRef<'a> { - fn from(node: &'a ast::ExceptHandlerExceptHandler) -> Self { +impl<'a, 'ast> From<&'a ast::ExceptHandlerExceptHandler<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::ExceptHandlerExceptHandler<'ast>) -> Self { AnyNodeRef::ExceptHandlerExceptHandler(node) } } -impl<'a> From<&'a ast::PatternMatchValue> for AnyNodeRef<'a> { - fn from(node: &'a ast::PatternMatchValue) -> Self { +impl<'a, 'ast> From<&'a ast::PatternMatchValue<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::PatternMatchValue<'ast>) -> Self { AnyNodeRef::PatternMatchValue(node) } } -impl<'a> From<&'a ast::PatternMatchSingleton> for AnyNodeRef<'a> { +impl<'a, 'ast> From<&'a ast::PatternMatchSingleton> for AnyNodeRef<'a, 'ast> { fn from(node: &'a ast::PatternMatchSingleton) -> Self { AnyNodeRef::PatternMatchSingleton(node) } } -impl<'a> From<&'a ast::PatternMatchSequence> for AnyNodeRef<'a> { - fn from(node: &'a ast::PatternMatchSequence) -> Self { +impl<'a, 'ast> From<&'a ast::PatternMatchSequence<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::PatternMatchSequence<'ast>) -> Self { AnyNodeRef::PatternMatchSequence(node) } } -impl<'a> From<&'a ast::PatternMatchMapping> for AnyNodeRef<'a> { - fn from(node: &'a ast::PatternMatchMapping) -> Self { +impl<'a, 'ast> From<&'a ast::PatternMatchMapping<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::PatternMatchMapping<'ast>) -> Self { AnyNodeRef::PatternMatchMapping(node) } } -impl<'a> From<&'a ast::PatternMatchClass> for AnyNodeRef<'a> { - fn from(node: &'a ast::PatternMatchClass) -> Self { +impl<'a, 'ast> From<&'a ast::PatternMatchClass<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::PatternMatchClass<'ast>) -> Self { AnyNodeRef::PatternMatchClass(node) } } -impl<'a> From<&'a ast::PatternMatchStar> for AnyNodeRef<'a> { - fn from(node: &'a ast::PatternMatchStar) -> Self { +impl<'a, 'ast> From<&'a ast::PatternMatchStar<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::PatternMatchStar<'ast>) -> Self { AnyNodeRef::PatternMatchStar(node) } } -impl<'a> From<&'a ast::PatternMatchAs> for AnyNodeRef<'a> { - fn from(node: &'a ast::PatternMatchAs) -> Self { +impl<'a, 'ast> From<&'a ast::PatternMatchAs<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::PatternMatchAs<'ast>) -> Self { AnyNodeRef::PatternMatchAs(node) } } -impl<'a> From<&'a ast::PatternMatchOr> for AnyNodeRef<'a> { - fn from(node: &'a ast::PatternMatchOr) -> Self { +impl<'a, 'ast> From<&'a ast::PatternMatchOr<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::PatternMatchOr<'ast>) -> Self { AnyNodeRef::PatternMatchOr(node) } } -impl<'a> From<&'a ast::PatternArguments> for AnyNodeRef<'a> { - fn from(node: &'a ast::PatternArguments) -> Self { +impl<'a, 'ast> From<&'a ast::PatternArguments<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::PatternArguments<'ast>) -> Self { AnyNodeRef::PatternArguments(node) } } -impl<'a> From<&'a ast::PatternKeyword> for AnyNodeRef<'a> { - fn from(node: &'a ast::PatternKeyword) -> Self { +impl<'a, 'ast> From<&'a ast::PatternKeyword<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::PatternKeyword<'ast>) -> Self { AnyNodeRef::PatternKeyword(node) } } -impl<'a> From<&'a Decorator> for AnyNodeRef<'a> { - fn from(node: &'a Decorator) -> Self { +impl<'a, 'ast> From<&'a Decorator<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a Decorator<'ast>) -> Self { AnyNodeRef::Decorator(node) } } -impl<'a> From<&'a ast::TypeParams> for AnyNodeRef<'a> { - fn from(node: &'a ast::TypeParams) -> Self { +impl<'a, 'ast> From<&'a ast::TypeParams<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::TypeParams<'ast>) -> Self { AnyNodeRef::TypeParams(node) } } -impl<'a> From<&'a TypeParamTypeVar> for AnyNodeRef<'a> { - fn from(node: &'a TypeParamTypeVar) -> Self { +impl<'a, 'ast> From<&'a TypeParamTypeVar<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a TypeParamTypeVar<'ast>) -> Self { AnyNodeRef::TypeParamTypeVar(node) } } -impl<'a> From<&'a TypeParamTypeVarTuple> for AnyNodeRef<'a> { - fn from(node: &'a TypeParamTypeVarTuple) -> Self { +impl<'a, 'ast> From<&'a TypeParamTypeVarTuple<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a TypeParamTypeVarTuple<'ast>) -> Self { AnyNodeRef::TypeParamTypeVarTuple(node) } } -impl<'a> From<&'a TypeParamParamSpec> for AnyNodeRef<'a> { - fn from(node: &'a TypeParamParamSpec) -> Self { +impl<'a, 'ast> From<&'a TypeParamParamSpec<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a TypeParamParamSpec<'ast>) -> Self { AnyNodeRef::TypeParamParamSpec(node) } } -impl<'a> From<&'a ast::FString> for AnyNodeRef<'a> { - fn from(node: &'a ast::FString) -> Self { +impl<'a, 'ast> From<&'a ast::FString<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::FString<'ast>) -> Self { AnyNodeRef::FString(node) } } -impl<'a> From<&'a ast::StringLiteral> for AnyNodeRef<'a> { - fn from(node: &'a ast::StringLiteral) -> Self { +impl<'a, 'ast> From<&'a ast::StringLiteral<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::StringLiteral<'ast>) -> Self { AnyNodeRef::StringLiteral(node) } } -impl<'a> From<&'a ast::BytesLiteral> for AnyNodeRef<'a> { - fn from(node: &'a ast::BytesLiteral) -> Self { +impl<'a, 'ast> From<&'a ast::BytesLiteral<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ast::BytesLiteral<'ast>) -> Self { AnyNodeRef::BytesLiteral(node) } } -impl<'a> From<&'a Stmt> for AnyNodeRef<'a> { - fn from(stmt: &'a Stmt) -> Self { +impl<'a, 'ast> From<&'a Stmt<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(stmt: &'a Stmt<'ast>) -> Self { match stmt { Stmt::FunctionDef(node) => AnyNodeRef::StmtFunctionDef(node), Stmt::ClassDef(node) => AnyNodeRef::StmtClassDef(node), @@ -7667,8 +7765,8 @@ impl<'a> From<&'a Stmt> for AnyNodeRef<'a> { } } -impl<'a> From<&'a Expr> for AnyNodeRef<'a> { - fn from(expr: &'a Expr) -> Self { +impl<'a, 'ast> From<&'a Expr<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(expr: &'a Expr<'ast>) -> Self { match expr { Expr::BoolOp(node) => AnyNodeRef::ExprBoolOp(node), Expr::Named(node) => AnyNodeRef::ExprNamed(node), @@ -7706,8 +7804,8 @@ impl<'a> From<&'a Expr> for AnyNodeRef<'a> { } } -impl<'a> From<&'a Mod> for AnyNodeRef<'a> { - fn from(module: &'a Mod) -> Self { +impl<'a, 'ast> From<&'a Mod<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(module: &'a Mod<'ast>) -> Self { match module { Mod::Module(node) => AnyNodeRef::ModModule(node), Mod::Expression(node) => AnyNodeRef::ModExpression(node), @@ -7715,8 +7813,8 @@ impl<'a> From<&'a Mod> for AnyNodeRef<'a> { } } -impl<'a> From<&'a FStringElement> for AnyNodeRef<'a> { - fn from(element: &'a FStringElement) -> Self { +impl<'a, 'ast> From<&'a FStringElement<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(element: &'a FStringElement<'ast>) -> Self { match element { FStringElement::Expression(node) => AnyNodeRef::FStringExpressionElement(node), FStringElement::Literal(node) => AnyNodeRef::FStringLiteralElement(node), @@ -7724,8 +7822,8 @@ impl<'a> From<&'a FStringElement> for AnyNodeRef<'a> { } } -impl<'a> From<&'a Pattern> for AnyNodeRef<'a> { - fn from(pattern: &'a Pattern) -> Self { +impl<'a, 'ast> From<&'a Pattern<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(pattern: &'a Pattern<'ast>) -> Self { match pattern { Pattern::MatchValue(node) => AnyNodeRef::PatternMatchValue(node), Pattern::MatchSingleton(node) => AnyNodeRef::PatternMatchSingleton(node), @@ -7739,8 +7837,8 @@ impl<'a> From<&'a Pattern> for AnyNodeRef<'a> { } } -impl<'a> From<&'a TypeParam> for AnyNodeRef<'a> { - fn from(type_param: &'a TypeParam) -> Self { +impl<'a, 'ast> From<&'a TypeParam<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(type_param: &'a TypeParam<'ast>) -> Self { match type_param { TypeParam::TypeVar(node) => AnyNodeRef::TypeParamTypeVar(node), TypeParam::TypeVarTuple(node) => AnyNodeRef::TypeParamTypeVarTuple(node), @@ -7749,8 +7847,8 @@ impl<'a> From<&'a TypeParam> for AnyNodeRef<'a> { } } -impl<'a> From<&'a ExceptHandler> for AnyNodeRef<'a> { - fn from(handler: &'a ExceptHandler) -> Self { +impl<'a, 'ast> From<&'a ExceptHandler<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(handler: &'a ExceptHandler<'ast>) -> Self { match handler { ExceptHandler::ExceptHandler(handler) => { AnyNodeRef::ExceptHandlerExceptHandler(handler) @@ -7759,53 +7857,53 @@ impl<'a> From<&'a ExceptHandler> for AnyNodeRef<'a> { } } -impl<'a> From<&'a Comprehension> for AnyNodeRef<'a> { - fn from(node: &'a Comprehension) -> Self { +impl<'a, 'ast> From<&'a Comprehension<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a Comprehension<'ast>) -> Self { AnyNodeRef::Comprehension(node) } } -impl<'a> From<&'a Arguments> for AnyNodeRef<'a> { - fn from(node: &'a Arguments) -> Self { +impl<'a, 'ast> From<&'a Arguments<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a Arguments<'ast>) -> Self { AnyNodeRef::Arguments(node) } } -impl<'a> From<&'a Parameters> for AnyNodeRef<'a> { - fn from(node: &'a Parameters) -> Self { +impl<'a, 'ast> From<&'a Parameters<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a Parameters<'ast>) -> Self { AnyNodeRef::Parameters(node) } } -impl<'a> From<&'a Parameter> for AnyNodeRef<'a> { - fn from(node: &'a Parameter) -> Self { +impl<'a, 'ast> From<&'a Parameter<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a Parameter<'ast>) -> Self { AnyNodeRef::Parameter(node) } } -impl<'a> From<&'a ParameterWithDefault> for AnyNodeRef<'a> { - fn from(node: &'a ParameterWithDefault) -> Self { +impl<'a, 'ast> From<&'a ParameterWithDefault<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a ParameterWithDefault<'ast>) -> Self { AnyNodeRef::ParameterWithDefault(node) } } -impl<'a> From<&'a Keyword> for AnyNodeRef<'a> { - fn from(node: &'a Keyword) -> Self { +impl<'a, 'ast> From<&'a Keyword<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a Keyword<'ast>) -> Self { AnyNodeRef::Keyword(node) } } -impl<'a> From<&'a Alias> for AnyNodeRef<'a> { - fn from(node: &'a Alias) -> Self { +impl<'a, 'ast> From<&'a Alias<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a Alias<'ast>) -> Self { AnyNodeRef::Alias(node) } } -impl<'a> From<&'a WithItem> for AnyNodeRef<'a> { - fn from(node: &'a WithItem) -> Self { +impl<'a, 'ast> From<&'a WithItem<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a WithItem<'ast>) -> Self { AnyNodeRef::WithItem(node) } } -impl<'a> From<&'a MatchCase> for AnyNodeRef<'a> { - fn from(node: &'a MatchCase) -> Self { +impl<'a, 'ast> From<&'a MatchCase<'ast>> for AnyNodeRef<'a, 'ast> { + fn from(node: &'a MatchCase<'ast>) -> Self { AnyNodeRef::MatchCase(node) } } -impl Ranged for AnyNodeRef<'_> { +impl Ranged for AnyNodeRef<'_, '_> { fn range(&self) -> TextRange { match self { AnyNodeRef::ModModule(node) => node.range(), @@ -8009,162 +8107,162 @@ pub enum NodeKind { // doesn't implement `AstNode` itself and thus, can't be used as `AstNodeKey` or passed to query the `ast_id` (because that requires that the node implements `HasAstId` which extends `AstNode`). // I don't know how a solution to this would look like but this isn't the first time where this problem has come up. #[derive(Clone, Copy, Debug, PartialEq)] -pub enum StatementRef<'a> { - FunctionDef(&'a StmtFunctionDef), - ClassDef(&'a StmtClassDef), - Return(&'a StmtReturn), - Delete(&'a StmtDelete), - Assign(&'a StmtAssign), - AugAssign(&'a StmtAugAssign), - AnnAssign(&'a StmtAnnAssign), - TypeAlias(&'a StmtTypeAlias), - For(&'a StmtFor), - While(&'a StmtWhile), - If(&'a StmtIf), - With(&'a StmtWith), - Match(&'a StmtMatch), - Raise(&'a StmtRaise), - Try(&'a StmtTry), - Assert(&'a StmtAssert), - Import(&'a StmtImport), - ImportFrom(&'a StmtImportFrom), - Global(&'a StmtGlobal), - Nonlocal(&'a StmtNonlocal), - Expr(&'a StmtExpr), +pub enum StatementRef<'a, 'ast> { + FunctionDef(&'a StmtFunctionDef<'ast>), + ClassDef(&'a StmtClassDef<'ast>), + Return(&'a StmtReturn<'ast>), + Delete(&'a StmtDelete<'ast>), + Assign(&'a StmtAssign<'ast>), + AugAssign(&'a StmtAugAssign<'ast>), + AnnAssign(&'a StmtAnnAssign<'ast>), + TypeAlias(&'a StmtTypeAlias<'ast>), + For(&'a StmtFor<'ast>), + While(&'a StmtWhile<'ast>), + If(&'a StmtIf<'ast>), + With(&'a StmtWith<'ast>), + Match(&'a StmtMatch<'ast>), + Raise(&'a StmtRaise<'ast>), + Try(&'a StmtTry<'ast>), + Assert(&'a StmtAssert<'ast>), + Import(&'a StmtImport<'ast>), + ImportFrom(&'a StmtImportFrom<'ast>), + Global(&'a StmtGlobal<'ast>), + Nonlocal(&'a StmtNonlocal<'ast>), + Expr(&'a StmtExpr<'ast>), Pass(&'a StmtPass), Break(&'a StmtBreak), Continue(&'a StmtContinue), - IpyEscapeCommand(&'a StmtIpyEscapeCommand), + IpyEscapeCommand(&'a StmtIpyEscapeCommand<'ast>), } -impl<'a> From<&'a StmtFunctionDef> for StatementRef<'a> { - fn from(value: &'a StmtFunctionDef) -> Self { +impl<'a, 'ast> From<&'a StmtFunctionDef<'ast>> for StatementRef<'a, 'ast> { + fn from(value: &'a StmtFunctionDef<'ast>) -> Self { Self::FunctionDef(value) } } -impl<'a> From<&'a StmtClassDef> for StatementRef<'a> { - fn from(value: &'a StmtClassDef) -> Self { +impl<'a, 'ast> From<&'a StmtClassDef<'ast>> for StatementRef<'a, 'ast> { + fn from(value: &'a StmtClassDef<'ast>) -> Self { Self::ClassDef(value) } } -impl<'a> From<&'a StmtReturn> for StatementRef<'a> { - fn from(value: &'a StmtReturn) -> Self { +impl<'a, 'ast> From<&'a StmtReturn<'ast>> for StatementRef<'a, 'ast> { + fn from(value: &'a StmtReturn<'ast>) -> Self { Self::Return(value) } } -impl<'a> From<&'a StmtDelete> for StatementRef<'a> { - fn from(value: &'a StmtDelete) -> Self { +impl<'a, 'ast> From<&'a StmtDelete<'ast>> for StatementRef<'a, 'ast> { + fn from(value: &'a StmtDelete<'ast>) -> Self { Self::Delete(value) } } -impl<'a> From<&'a StmtAssign> for StatementRef<'a> { - fn from(value: &'a StmtAssign) -> Self { +impl<'a, 'ast> From<&'a StmtAssign<'ast>> for StatementRef<'a, 'ast> { + fn from(value: &'a StmtAssign<'ast>) -> Self { Self::Assign(value) } } -impl<'a> From<&'a StmtAugAssign> for StatementRef<'a> { - fn from(value: &'a StmtAugAssign) -> Self { +impl<'a, 'ast> From<&'a StmtAugAssign<'ast>> for StatementRef<'a, 'ast> { + fn from(value: &'a StmtAugAssign<'ast>) -> Self { Self::AugAssign(value) } } -impl<'a> From<&'a StmtAnnAssign> for StatementRef<'a> { - fn from(value: &'a StmtAnnAssign) -> Self { +impl<'a, 'ast> From<&'a StmtAnnAssign<'ast>> for StatementRef<'a, 'ast> { + fn from(value: &'a StmtAnnAssign<'ast>) -> Self { Self::AnnAssign(value) } } -impl<'a> From<&'a StmtTypeAlias> for StatementRef<'a> { - fn from(value: &'a StmtTypeAlias) -> Self { +impl<'a, 'ast> From<&'a StmtTypeAlias<'ast>> for StatementRef<'a, 'ast> { + fn from(value: &'a StmtTypeAlias<'ast>) -> Self { Self::TypeAlias(value) } } -impl<'a> From<&'a StmtFor> for StatementRef<'a> { - fn from(value: &'a StmtFor) -> Self { +impl<'a, 'ast> From<&'a StmtFor<'ast>> for StatementRef<'a, 'ast> { + fn from(value: &'a StmtFor<'ast>) -> Self { Self::For(value) } } -impl<'a> From<&'a StmtWhile> for StatementRef<'a> { - fn from(value: &'a StmtWhile) -> Self { +impl<'a, 'ast> From<&'a StmtWhile<'ast>> for StatementRef<'a, 'ast> { + fn from(value: &'a StmtWhile<'ast>) -> Self { Self::While(value) } } -impl<'a> From<&'a StmtIf> for StatementRef<'a> { - fn from(value: &'a StmtIf) -> Self { +impl<'a, 'ast> From<&'a StmtIf<'ast>> for StatementRef<'a, 'ast> { + fn from(value: &'a StmtIf<'ast>) -> Self { Self::If(value) } } -impl<'a> From<&'a StmtWith> for StatementRef<'a> { - fn from(value: &'a StmtWith) -> Self { +impl<'a, 'ast> From<&'a StmtWith<'ast>> for StatementRef<'a, 'ast> { + fn from(value: &'a StmtWith<'ast>) -> Self { Self::With(value) } } -impl<'a> From<&'a StmtMatch> for StatementRef<'a> { - fn from(value: &'a StmtMatch) -> Self { +impl<'a, 'ast> From<&'a StmtMatch<'ast>> for StatementRef<'a, 'ast> { + fn from(value: &'a StmtMatch<'ast>) -> Self { Self::Match(value) } } -impl<'a> From<&'a StmtRaise> for StatementRef<'a> { - fn from(value: &'a StmtRaise) -> Self { +impl<'a, 'ast> From<&'a StmtRaise<'ast>> for StatementRef<'a, 'ast> { + fn from(value: &'a StmtRaise<'ast>) -> Self { Self::Raise(value) } } -impl<'a> From<&'a StmtTry> for StatementRef<'a> { - fn from(value: &'a StmtTry) -> Self { +impl<'a, 'ast> From<&'a StmtTry<'ast>> for StatementRef<'a, 'ast> { + fn from(value: &'a StmtTry<'ast>) -> Self { Self::Try(value) } } -impl<'a> From<&'a StmtAssert> for StatementRef<'a> { - fn from(value: &'a StmtAssert) -> Self { +impl<'a, 'ast> From<&'a StmtAssert<'ast>> for StatementRef<'a, 'ast> { + fn from(value: &'a StmtAssert<'ast>) -> Self { Self::Assert(value) } } -impl<'a> From<&'a StmtImport> for StatementRef<'a> { - fn from(value: &'a StmtImport) -> Self { +impl<'a, 'ast> From<&'a StmtImport<'ast>> for StatementRef<'a, 'ast> { + fn from(value: &'a StmtImport<'ast>) -> Self { Self::Import(value) } } -impl<'a> From<&'a StmtImportFrom> for StatementRef<'a> { - fn from(value: &'a StmtImportFrom) -> Self { +impl<'a, 'ast> From<&'a StmtImportFrom<'ast>> for StatementRef<'a, 'ast> { + fn from(value: &'a StmtImportFrom<'ast>) -> Self { Self::ImportFrom(value) } } -impl<'a> From<&'a StmtGlobal> for StatementRef<'a> { - fn from(value: &'a StmtGlobal) -> Self { +impl<'a, 'ast> From<&'a StmtGlobal<'ast>> for StatementRef<'a, 'ast> { + fn from(value: &'a StmtGlobal<'ast>) -> Self { Self::Global(value) } } -impl<'a> From<&'a StmtNonlocal> for StatementRef<'a> { - fn from(value: &'a StmtNonlocal) -> Self { +impl<'a, 'ast> From<&'a StmtNonlocal<'ast>> for StatementRef<'a, 'ast> { + fn from(value: &'a StmtNonlocal<'ast>) -> Self { Self::Nonlocal(value) } } -impl<'a> From<&'a StmtExpr> for StatementRef<'a> { - fn from(value: &'a StmtExpr) -> Self { +impl<'a, 'ast> From<&'a StmtExpr<'ast>> for StatementRef<'a, 'ast> { + fn from(value: &'a StmtExpr<'ast>) -> Self { Self::Expr(value) } } -impl<'a> From<&'a StmtPass> for StatementRef<'a> { +impl<'a, 'ast> From<&'a StmtPass> for StatementRef<'a, 'ast> { fn from(value: &'a StmtPass) -> Self { Self::Pass(value) } } -impl<'a> From<&'a StmtBreak> for StatementRef<'a> { +impl<'a, 'ast> From<&'a StmtBreak> for StatementRef<'a, 'ast> { fn from(value: &'a StmtBreak) -> Self { Self::Break(value) } } -impl<'a> From<&'a StmtContinue> for StatementRef<'a> { +impl<'a, 'ast> From<&'a StmtContinue> for StatementRef<'a, 'ast> { fn from(value: &'a StmtContinue) -> Self { Self::Continue(value) } } -impl<'a> From<&'a StmtIpyEscapeCommand> for StatementRef<'a> { - fn from(value: &'a StmtIpyEscapeCommand) -> Self { +impl<'a, 'ast> From<&'a StmtIpyEscapeCommand<'ast>> for StatementRef<'a, 'ast> { + fn from(value: &'a StmtIpyEscapeCommand<'ast>) -> Self { Self::IpyEscapeCommand(value) } } -impl<'a> From<&'a Stmt> for StatementRef<'a> { - fn from(value: &'a Stmt) -> Self { +impl<'a, 'ast> From<&'a Stmt<'ast>> for StatementRef<'a, 'ast> { + fn from(value: &'a Stmt<'ast>) -> Self { match value { Stmt::FunctionDef(statement) => Self::FunctionDef(statement), Stmt::ClassDef(statement) => Self::ClassDef(statement), @@ -8196,32 +8294,32 @@ impl<'a> From<&'a Stmt> for StatementRef<'a> { } #[derive(Copy, Clone, Debug, PartialEq)] -pub enum TypeParamRef<'a> { - TypeVar(&'a TypeParamTypeVar), - ParamSpec(&'a TypeParamParamSpec), - TypeVarTuple(&'a TypeParamTypeVarTuple), +pub enum TypeParamRef<'a, 'ast> { + TypeVar(&'a TypeParamTypeVar<'ast>), + ParamSpec(&'a TypeParamParamSpec<'ast>), + TypeVarTuple(&'a TypeParamTypeVarTuple<'ast>), } -impl<'a> From<&'a TypeParamTypeVar> for TypeParamRef<'a> { - fn from(value: &'a TypeParamTypeVar) -> Self { +impl<'a, 'ast> From<&'a TypeParamTypeVar<'ast>> for TypeParamRef<'a, 'ast> { + fn from(value: &'a TypeParamTypeVar<'ast>) -> Self { Self::TypeVar(value) } } -impl<'a> From<&'a TypeParamParamSpec> for TypeParamRef<'a> { - fn from(value: &'a TypeParamParamSpec) -> Self { +impl<'a, 'ast> From<&'a TypeParamParamSpec<'ast>> for TypeParamRef<'a, 'ast> { + fn from(value: &'a TypeParamParamSpec<'ast>) -> Self { Self::ParamSpec(value) } } -impl<'a> From<&'a TypeParamTypeVarTuple> for TypeParamRef<'a> { - fn from(value: &'a TypeParamTypeVarTuple) -> Self { +impl<'a, 'ast> From<&'a TypeParamTypeVarTuple<'ast>> for TypeParamRef<'a, 'ast> { + fn from(value: &'a TypeParamTypeVarTuple<'ast>) -> Self { Self::TypeVarTuple(value) } } -impl<'a> From<&'a TypeParam> for TypeParamRef<'a> { - fn from(value: &'a TypeParam) -> Self { +impl<'a, 'ast> From<&'a TypeParam<'ast>> for TypeParamRef<'a, 'ast> { + fn from(value: &'a TypeParam<'ast>) -> Self { match value { TypeParam::TypeVar(value) => Self::TypeVar(value), TypeParam::ParamSpec(value) => Self::ParamSpec(value), diff --git a/crates/ruff_python_ast/src/nodes.rs b/crates/ruff_python_ast/src/nodes.rs index 8861218153..a0985ddf20 100644 --- a/crates/ruff_python_ast/src/nodes.rs +++ b/crates/ruff_python_ast/src/nodes.rs @@ -9,10 +9,10 @@ use std::sync::OnceLock; use bitflags::bitflags; use itertools::Itertools; +use ruff_allocator::{Allocator, Box, CloneIn}; use ruff_text_size::{Ranged, TextLen, TextRange, TextSize}; -use crate::name::Name; use crate::{ int, str::Quote, @@ -21,83 +21,83 @@ use crate::{ }; /// See also [mod](https://docs.python.org/3/library/ast.html#ast.mod) -#[derive(Clone, Debug, PartialEq, is_macro::Is)] -pub enum Mod { - Module(ModModule), - Expression(ModExpression), +#[derive(Debug, PartialEq, is_macro::Is)] +pub enum Mod<'ast> { + Module(ModModule<'ast>), + Expression(ModExpression<'ast>), } /// See also [Module](https://docs.python.org/3/library/ast.html#ast.Module) -#[derive(Clone, Debug, PartialEq)] -pub struct ModModule { +#[derive(Debug, PartialEq)] +pub struct ModModule<'ast> { pub range: TextRange, - pub body: Vec, + pub body: Vec>, } -impl From for Mod { - fn from(payload: ModModule) -> Self { +impl<'ast> From> for Mod<'ast> { + fn from(payload: ModModule<'ast>) -> Self { Mod::Module(payload) } } /// See also [Expression](https://docs.python.org/3/library/ast.html#ast.Expression) -#[derive(Clone, Debug, PartialEq)] -pub struct ModExpression { +#[derive(Debug, PartialEq)] +pub struct ModExpression<'ast> { pub range: TextRange, - pub body: Box, + pub body: Expr<'ast>, } -impl From for Mod { - fn from(payload: ModExpression) -> Self { +impl<'ast> From> for Mod<'ast> { + fn from(payload: ModExpression<'ast>) -> Self { Mod::Expression(payload) } } /// See also [stmt](https://docs.python.org/3/library/ast.html#ast.stmt) -#[derive(Clone, Debug, PartialEq, is_macro::Is)] -pub enum Stmt { +#[derive(Debug, PartialEq, is_macro::Is)] +pub enum Stmt<'ast> { #[is(name = "function_def_stmt")] - FunctionDef(StmtFunctionDef), + FunctionDef(StmtFunctionDef<'ast>), #[is(name = "class_def_stmt")] - ClassDef(StmtClassDef), + ClassDef(StmtClassDef<'ast>), #[is(name = "return_stmt")] - Return(StmtReturn), + Return(StmtReturn<'ast>), #[is(name = "delete_stmt")] - Delete(StmtDelete), + Delete(StmtDelete<'ast>), #[is(name = "assign_stmt")] - Assign(StmtAssign), + Assign(StmtAssign<'ast>), #[is(name = "aug_assign_stmt")] - AugAssign(StmtAugAssign), + AugAssign(StmtAugAssign<'ast>), #[is(name = "ann_assign_stmt")] - AnnAssign(StmtAnnAssign), + AnnAssign(StmtAnnAssign<'ast>), #[is(name = "type_alias_stmt")] - TypeAlias(StmtTypeAlias), + TypeAlias(StmtTypeAlias<'ast>), #[is(name = "for_stmt")] - For(StmtFor), + For(StmtFor<'ast>), #[is(name = "while_stmt")] - While(StmtWhile), + While(StmtWhile<'ast>), #[is(name = "if_stmt")] - If(StmtIf), + If(StmtIf<'ast>), #[is(name = "with_stmt")] - With(StmtWith), + With(StmtWith<'ast>), #[is(name = "match_stmt")] - Match(StmtMatch), + Match(StmtMatch<'ast>), #[is(name = "raise_stmt")] - Raise(StmtRaise), + Raise(StmtRaise<'ast>), #[is(name = "try_stmt")] - Try(StmtTry), + Try(StmtTry<'ast>), #[is(name = "assert_stmt")] - Assert(StmtAssert), + Assert(StmtAssert<'ast>), #[is(name = "import_stmt")] - Import(StmtImport), + Import(StmtImport<'ast>), #[is(name = "import_from_stmt")] - ImportFrom(StmtImportFrom), + ImportFrom(StmtImportFrom<'ast>), #[is(name = "global_stmt")] - Global(StmtGlobal), + Global(StmtGlobal<'ast>), #[is(name = "nonlocal_stmt")] - Nonlocal(StmtNonlocal), + Nonlocal(StmtNonlocal<'ast>), #[is(name = "expr_stmt")] - Expr(StmtExpr), + Expr(StmtExpr<'ast>), #[is(name = "pass_stmt")] Pass(StmtPass), #[is(name = "break_stmt")] @@ -107,7 +107,7 @@ pub enum Stmt { // Jupyter notebook specific #[is(name = "ipy_escape_command_stmt")] - IpyEscapeCommand(StmtIpyEscapeCommand), + IpyEscapeCommand(StmtIpyEscapeCommand<'ast>), } /// An AST node used to represent a IPython escape command at the statement level. @@ -163,15 +163,15 @@ pub enum Stmt { /// `%%timeit??`, etc. /// /// [Escape kind]: IpyEscapeKind -#[derive(Clone, Debug, PartialEq)] -pub struct StmtIpyEscapeCommand { +#[derive(Debug, PartialEq)] +pub struct StmtIpyEscapeCommand<'ast> { pub range: TextRange, pub kind: IpyEscapeKind, - pub value: Box, + pub value: &'ast str, } -impl From for Stmt { - fn from(payload: StmtIpyEscapeCommand) -> Self { +impl<'ast> From> for Stmt<'ast> { + fn from(payload: StmtIpyEscapeCommand<'ast>) -> Self { Stmt::IpyEscapeCommand(payload) } } @@ -181,38 +181,38 @@ impl From for Stmt { /// /// This type differs from the original Python AST, as it collapses the /// synchronous and asynchronous variants into a single type. -#[derive(Clone, Debug, PartialEq)] -pub struct StmtFunctionDef { +#[derive(Debug, PartialEq)] +pub struct StmtFunctionDef<'ast> { pub range: TextRange, pub is_async: bool, - pub decorator_list: Vec, - pub name: Identifier, - pub type_params: Option>, - pub parameters: Box, - pub returns: Option>, - pub body: Vec, + pub decorator_list: Vec>, + pub name: Identifier<'ast>, + pub type_params: Option>>, + pub parameters: Box<'ast, Parameters<'ast>>, + pub returns: Option>>, + pub body: Vec>, } -impl From for Stmt { - fn from(payload: StmtFunctionDef) -> Self { +impl<'ast> From> for Stmt<'ast> { + fn from(payload: StmtFunctionDef<'ast>) -> Self { Stmt::FunctionDef(payload) } } /// See also [ClassDef](https://docs.python.org/3/library/ast.html#ast.ClassDef) -#[derive(Clone, Debug, PartialEq)] -pub struct StmtClassDef { +#[derive(Debug, PartialEq)] +pub struct StmtClassDef<'ast> { pub range: TextRange, - pub decorator_list: Vec, - pub name: Identifier, - pub type_params: Option>, - pub arguments: Option>, - pub body: Vec, + pub decorator_list: Vec>, + pub name: Identifier<'ast>, + pub type_params: Option>>, + pub arguments: Option>>, + pub body: Vec>, } -impl StmtClassDef { +impl<'ast> StmtClassDef<'ast> { /// Return an iterator over the bases of the class. - pub fn bases(&self) -> &[Expr] { + pub fn bases(&self) -> &[Expr<'ast>] { match &self.arguments { Some(arguments) => &arguments.args, None => &[], @@ -220,7 +220,7 @@ impl StmtClassDef { } /// Return an iterator over the metaclass keywords of the class. - pub fn keywords(&self) -> &[Keyword] { + pub fn keywords(&self) -> &[Keyword<'ast>] { match &self.arguments { Some(arguments) => &arguments.keywords, None => &[], @@ -228,94 +228,94 @@ impl StmtClassDef { } } -impl From for Stmt { - fn from(payload: StmtClassDef) -> Self { +impl<'ast> From> for Stmt<'ast> { + fn from(payload: StmtClassDef<'ast>) -> Self { Stmt::ClassDef(payload) } } /// See also [Return](https://docs.python.org/3/library/ast.html#ast.Return) -#[derive(Clone, Debug, PartialEq)] -pub struct StmtReturn { +#[derive(Debug, PartialEq)] +pub struct StmtReturn<'ast> { pub range: TextRange, - pub value: Option>, + pub value: Option>>, } -impl From for Stmt { - fn from(payload: StmtReturn) -> Self { +impl<'ast> From> for Stmt<'ast> { + fn from(payload: StmtReturn<'ast>) -> Self { Stmt::Return(payload) } } /// See also [Delete](https://docs.python.org/3/library/ast.html#ast.Delete) -#[derive(Clone, Debug, PartialEq)] -pub struct StmtDelete { +#[derive(Debug, PartialEq)] +pub struct StmtDelete<'ast> { pub range: TextRange, - pub targets: Vec, + pub targets: Vec>, } -impl From for Stmt { - fn from(payload: StmtDelete) -> Self { +impl<'ast> From> for Stmt<'ast> { + fn from(payload: StmtDelete<'ast>) -> Self { Stmt::Delete(payload) } } /// See also [TypeAlias](https://docs.python.org/3/library/ast.html#ast.TypeAlias) -#[derive(Clone, Debug, PartialEq)] -pub struct StmtTypeAlias { +#[derive(Debug, PartialEq)] +pub struct StmtTypeAlias<'ast> { pub range: TextRange, - pub name: Box, - pub type_params: Option, - pub value: Box, + pub name: Box<'ast, Expr<'ast>>, + pub type_params: Option>, + pub value: Box<'ast, Expr<'ast>>, } -impl From for Stmt { - fn from(payload: StmtTypeAlias) -> Self { +impl<'ast> From> for Stmt<'ast> { + fn from(payload: StmtTypeAlias<'ast>) -> Self { Stmt::TypeAlias(payload) } } /// See also [Assign](https://docs.python.org/3/library/ast.html#ast.Assign) -#[derive(Clone, Debug, PartialEq)] -pub struct StmtAssign { +#[derive(Debug, PartialEq)] +pub struct StmtAssign<'ast> { pub range: TextRange, - pub targets: Vec, - pub value: Box, + pub targets: Vec>, + pub value: Box<'ast, Expr<'ast>>, } -impl From for Stmt { - fn from(payload: StmtAssign) -> Self { +impl<'ast> From> for Stmt<'ast> { + fn from(payload: StmtAssign<'ast>) -> Self { Stmt::Assign(payload) } } /// See also [AugAssign](https://docs.python.org/3/library/ast.html#ast.AugAssign) -#[derive(Clone, Debug, PartialEq)] -pub struct StmtAugAssign { +#[derive(Debug, PartialEq)] +pub struct StmtAugAssign<'ast> { pub range: TextRange, - pub target: Box, + pub target: Box<'ast, Expr<'ast>>, pub op: Operator, - pub value: Box, + pub value: Box<'ast, Expr<'ast>>, } -impl From for Stmt { - fn from(payload: StmtAugAssign) -> Self { +impl<'ast> From> for Stmt<'ast> { + fn from(payload: StmtAugAssign<'ast>) -> Self { Stmt::AugAssign(payload) } } /// See also [AnnAssign](https://docs.python.org/3/library/ast.html#ast.AnnAssign) -#[derive(Clone, Debug, PartialEq)] -pub struct StmtAnnAssign { +#[derive(Debug, PartialEq)] +pub struct StmtAnnAssign<'ast> { pub range: TextRange, - pub target: Box, - pub annotation: Box, - pub value: Option>, + pub target: Box<'ast, Expr<'ast>>, + pub annotation: Box<'ast, Expr<'ast>>, + pub value: Option>>, pub simple: bool, } -impl From for Stmt { - fn from(payload: StmtAnnAssign) -> Self { +impl<'ast> From> for Stmt<'ast> { + fn from(payload: StmtAnnAssign<'ast>) -> Self { Stmt::AnnAssign(payload) } } @@ -325,58 +325,58 @@ impl From for Stmt { /// /// This type differs from the original Python AST, as it collapses the /// synchronous and asynchronous variants into a single type. -#[derive(Clone, Debug, PartialEq)] -pub struct StmtFor { +#[derive(Debug, PartialEq)] +pub struct StmtFor<'ast> { pub range: TextRange, pub is_async: bool, - pub target: Box, - pub iter: Box, - pub body: Vec, - pub orelse: Vec, + pub target: Box<'ast, Expr<'ast>>, + pub iter: Box<'ast, Expr<'ast>>, + pub body: Vec>, + pub orelse: Vec>, } -impl From for Stmt { - fn from(payload: StmtFor) -> Self { +impl<'ast> From> for Stmt<'ast> { + fn from(payload: StmtFor<'ast>) -> Self { Stmt::For(payload) } } /// See also [While](https://docs.python.org/3/library/ast.html#ast.While) and /// [AsyncWhile](https://docs.python.org/3/library/ast.html#ast.AsyncWhile). -#[derive(Clone, Debug, PartialEq)] -pub struct StmtWhile { +#[derive(Debug, PartialEq)] +pub struct StmtWhile<'ast> { pub range: TextRange, - pub test: Box, - pub body: Vec, - pub orelse: Vec, + pub test: Box<'ast, Expr<'ast>>, + pub body: Vec>, + pub orelse: Vec>, } -impl From for Stmt { - fn from(payload: StmtWhile) -> Self { +impl<'ast> From> for Stmt<'ast> { + fn from(payload: StmtWhile<'ast>) -> Self { Stmt::While(payload) } } /// See also [If](https://docs.python.org/3/library/ast.html#ast.If) -#[derive(Clone, Debug, PartialEq)] -pub struct StmtIf { +#[derive(Debug, PartialEq)] +pub struct StmtIf<'ast> { pub range: TextRange, - pub test: Box, - pub body: Vec, - pub elif_else_clauses: Vec, + pub test: Box<'ast, Expr<'ast>>, + pub body: Vec>, + pub elif_else_clauses: Vec>, } -impl From for Stmt { - fn from(payload: StmtIf) -> Self { +impl<'ast> From> for Stmt<'ast> { + fn from(payload: StmtIf<'ast>) -> Self { Stmt::If(payload) } } -#[derive(Clone, Debug, PartialEq)] -pub struct ElifElseClause { +#[derive(Debug, PartialEq)] +pub struct ElifElseClause<'ast> { pub range: TextRange, - pub test: Option, - pub body: Vec, + pub test: Option>, + pub body: Vec>, } /// See also [With](https://docs.python.org/3/library/ast.html#ast.With) and @@ -384,143 +384,143 @@ pub struct ElifElseClause { /// /// This type differs from the original Python AST, as it collapses the /// synchronous and asynchronous variants into a single type. -#[derive(Clone, Debug, PartialEq)] -pub struct StmtWith { +#[derive(Debug, PartialEq)] +pub struct StmtWith<'ast> { pub range: TextRange, pub is_async: bool, - pub items: Vec, - pub body: Vec, + pub items: Vec>, + pub body: Vec>, } -impl From for Stmt { - fn from(payload: StmtWith) -> Self { +impl<'ast> From> for Stmt<'ast> { + fn from(payload: StmtWith<'ast>) -> Self { Stmt::With(payload) } } /// See also [Match](https://docs.python.org/3/library/ast.html#ast.Match) -#[derive(Clone, Debug, PartialEq)] -pub struct StmtMatch { +#[derive(Debug, PartialEq)] +pub struct StmtMatch<'ast> { pub range: TextRange, - pub subject: Box, - pub cases: Vec, + pub subject: Box<'ast, Expr<'ast>>, + pub cases: Vec>, } -impl From for Stmt { - fn from(payload: StmtMatch) -> Self { +impl<'ast> From> for Stmt<'ast> { + fn from(payload: StmtMatch<'ast>) -> Self { Stmt::Match(payload) } } /// See also [Raise](https://docs.python.org/3/library/ast.html#ast.Raise) -#[derive(Clone, Debug, PartialEq)] -pub struct StmtRaise { +#[derive(Debug, PartialEq)] +pub struct StmtRaise<'ast> { pub range: TextRange, - pub exc: Option>, - pub cause: Option>, + pub exc: Option>>, + pub cause: Option>>, } -impl From for Stmt { - fn from(payload: StmtRaise) -> Self { +impl<'ast> From> for Stmt<'ast> { + fn from(payload: StmtRaise<'ast>) -> Self { Stmt::Raise(payload) } } /// See also [Try](https://docs.python.org/3/library/ast.html#ast.Try) and /// [TryStar](https://docs.python.org/3/library/ast.html#ast.TryStar) -#[derive(Clone, Debug, PartialEq)] -pub struct StmtTry { +#[derive(Debug, PartialEq)] +pub struct StmtTry<'ast> { pub range: TextRange, - pub body: Vec, - pub handlers: Vec, - pub orelse: Vec, - pub finalbody: Vec, + pub body: Vec>, + pub handlers: Vec>, + pub orelse: Vec>, + pub finalbody: Vec>, pub is_star: bool, } -impl From for Stmt { - fn from(payload: StmtTry) -> Self { +impl<'ast> From> for Stmt<'ast> { + fn from(payload: StmtTry<'ast>) -> Self { Stmt::Try(payload) } } /// See also [Assert](https://docs.python.org/3/library/ast.html#ast.Assert) -#[derive(Clone, Debug, PartialEq)] -pub struct StmtAssert { +#[derive(Debug, PartialEq)] +pub struct StmtAssert<'ast> { pub range: TextRange, - pub test: Box, - pub msg: Option>, + pub test: Box<'ast, Expr<'ast>>, + pub msg: Option>>, } -impl From for Stmt { - fn from(payload: StmtAssert) -> Self { +impl<'ast> From> for Stmt<'ast> { + fn from(payload: StmtAssert<'ast>) -> Self { Stmt::Assert(payload) } } /// See also [Import](https://docs.python.org/3/library/ast.html#ast.Import) #[derive(Clone, Debug, PartialEq)] -pub struct StmtImport { +pub struct StmtImport<'ast> { pub range: TextRange, - pub names: Vec, + pub names: Vec>, } -impl From for Stmt { - fn from(payload: StmtImport) -> Self { +impl<'ast> From> for Stmt<'ast> { + fn from(payload: StmtImport<'ast>) -> Self { Stmt::Import(payload) } } /// See also [ImportFrom](https://docs.python.org/3/library/ast.html#ast.ImportFrom) #[derive(Clone, Debug, PartialEq)] -pub struct StmtImportFrom { +pub struct StmtImportFrom<'ast> { pub range: TextRange, - pub module: Option, - pub names: Vec, + pub module: Option>, + pub names: Vec>, pub level: u32, } -impl From for Stmt { - fn from(payload: StmtImportFrom) -> Self { +impl<'ast> From> for Stmt<'ast> { + fn from(payload: StmtImportFrom<'ast>) -> Self { Stmt::ImportFrom(payload) } } /// See also [Global](https://docs.python.org/3/library/ast.html#ast.Global) #[derive(Clone, Debug, PartialEq)] -pub struct StmtGlobal { +pub struct StmtGlobal<'ast> { pub range: TextRange, - pub names: Vec, + pub names: Vec>, } -impl From for Stmt { - fn from(payload: StmtGlobal) -> Self { +impl<'ast> From> for Stmt<'ast> { + fn from(payload: StmtGlobal<'ast>) -> Self { Stmt::Global(payload) } } /// See also [Nonlocal](https://docs.python.org/3/library/ast.html#ast.Nonlocal) #[derive(Clone, Debug, PartialEq)] -pub struct StmtNonlocal { +pub struct StmtNonlocal<'ast> { pub range: TextRange, - pub names: Vec, + pub names: Vec>, } -impl From for Stmt { - fn from(payload: StmtNonlocal) -> Self { +impl<'ast> From> for Stmt<'ast> { + fn from(payload: StmtNonlocal<'ast>) -> Self { Stmt::Nonlocal(payload) } } /// See also [Expr](https://docs.python.org/3/library/ast.html#ast.Expr) -#[derive(Clone, Debug, PartialEq)] -pub struct StmtExpr { +#[derive(Debug, PartialEq)] +pub struct StmtExpr<'ast> { pub range: TextRange, - pub value: Box, + pub value: Box<'ast, Expr<'ast>>, } -impl From for Stmt { - fn from(payload: StmtExpr) -> Self { +impl<'ast> From> for Stmt<'ast> { + fn from(payload: StmtExpr<'ast>) -> Self { Stmt::Expr(payload) } } @@ -531,7 +531,7 @@ pub struct StmtPass { pub range: TextRange, } -impl From for Stmt { +impl From for Stmt<'_> { fn from(payload: StmtPass) -> Self { Stmt::Pass(payload) } @@ -543,7 +543,7 @@ pub struct StmtBreak { pub range: TextRange, } -impl From for Stmt { +impl From for Stmt<'_> { fn from(payload: StmtBreak) -> Self { Stmt::Break(payload) } @@ -555,55 +555,55 @@ pub struct StmtContinue { pub range: TextRange, } -impl From for Stmt { +impl From for Stmt<'_> { fn from(payload: StmtContinue) -> Self { Stmt::Continue(payload) } } /// See also [expr](https://docs.python.org/3/library/ast.html#ast.expr) -#[derive(Clone, Debug, PartialEq, is_macro::Is)] -pub enum Expr { +#[derive(Debug, PartialEq, is_macro::Is)] +pub enum Expr<'ast> { #[is(name = "bool_op_expr")] - BoolOp(ExprBoolOp), + BoolOp(ExprBoolOp<'ast>), #[is(name = "named_expr")] - Named(ExprNamed), + Named(ExprNamed<'ast>), #[is(name = "bin_op_expr")] - BinOp(ExprBinOp), + BinOp(ExprBinOp<'ast>), #[is(name = "unary_op_expr")] - UnaryOp(ExprUnaryOp), + UnaryOp(ExprUnaryOp<'ast>), #[is(name = "lambda_expr")] - Lambda(ExprLambda), + Lambda(ExprLambda<'ast>), #[is(name = "if_expr")] - If(ExprIf), + If(ExprIf<'ast>), #[is(name = "dict_expr")] - Dict(ExprDict), + Dict(ExprDict<'ast>), #[is(name = "set_expr")] - Set(ExprSet), + Set(ExprSet<'ast>), #[is(name = "list_comp_expr")] - ListComp(ExprListComp), + ListComp(ExprListComp<'ast>), #[is(name = "set_comp_expr")] - SetComp(ExprSetComp), + SetComp(ExprSetComp<'ast>), #[is(name = "dict_comp_expr")] - DictComp(ExprDictComp), + DictComp(ExprDictComp<'ast>), #[is(name = "generator_expr")] - Generator(ExprGenerator), + Generator(ExprGenerator<'ast>), #[is(name = "await_expr")] - Await(ExprAwait), + Await(ExprAwait<'ast>), #[is(name = "yield_expr")] - Yield(ExprYield), + Yield(ExprYield<'ast>), #[is(name = "yield_from_expr")] - YieldFrom(ExprYieldFrom), + YieldFrom(ExprYieldFrom<'ast>), #[is(name = "compare_expr")] - Compare(ExprCompare), + Compare(ExprCompare<'ast>), #[is(name = "call_expr")] - Call(ExprCall), + Call(ExprCall<'ast>), #[is(name = "f_string_expr")] - FString(ExprFString), + FString(ExprFString<'ast>), #[is(name = "string_literal_expr")] - StringLiteral(ExprStringLiteral), + StringLiteral(ExprStringLiteral<'ast>), #[is(name = "bytes_literal_expr")] - BytesLiteral(ExprBytesLiteral), + BytesLiteral(ExprBytesLiteral<'ast>), #[is(name = "number_literal_expr")] NumberLiteral(ExprNumberLiteral), #[is(name = "boolean_literal_expr")] @@ -613,26 +613,26 @@ pub enum Expr { #[is(name = "ellipsis_literal_expr")] EllipsisLiteral(ExprEllipsisLiteral), #[is(name = "attribute_expr")] - Attribute(ExprAttribute), + Attribute(ExprAttribute<'ast>), #[is(name = "subscript_expr")] - Subscript(ExprSubscript), + Subscript(ExprSubscript<'ast>), #[is(name = "starred_expr")] - Starred(ExprStarred), + Starred(ExprStarred<'ast>), #[is(name = "name_expr")] - Name(ExprName), + Name(ExprName<'ast>), #[is(name = "list_expr")] - List(ExprList), + List(ExprList<'ast>), #[is(name = "tuple_expr")] - Tuple(ExprTuple), + Tuple(ExprTuple<'ast>), #[is(name = "slice_expr")] - Slice(ExprSlice), + Slice(ExprSlice<'ast>), // Jupyter notebook specific #[is(name = "ipy_escape_command_expr")] - IpyEscapeCommand(ExprIpyEscapeCommand), + IpyEscapeCommand(ExprIpyEscapeCommand<'ast>), } -impl Expr { +impl<'ast> Expr<'ast> { /// Returns `true` if the expression is a literal expression. /// /// A literal expression is either a string literal, bytes literal, @@ -650,7 +650,7 @@ impl Expr { } /// Returns [`LiteralExpressionRef`] if the expression is a literal expression. - pub fn as_literal_expr(&self) -> Option> { + pub fn as_literal_expr(&self) -> Option> { match self { Expr::StringLiteral(expr) => Some(LiteralExpressionRef::StringLiteral(expr)), Expr::BytesLiteral(expr) => Some(LiteralExpressionRef::BytesLiteral(expr)), @@ -663,6 +663,12 @@ impl Expr { } } +impl<'ast> CloneIn<'ast> for Expr<'ast> { + fn clone_in(&self, allocator: &'ast Allocator) -> Self { + todo!(); + } +} + /// An AST node used to represent a IPython escape command at the expression level. /// /// For example, @@ -674,101 +680,101 @@ impl Expr { /// /// For more information related to terminology and syntax of escape commands, /// see [`StmtIpyEscapeCommand`]. -#[derive(Clone, Debug, PartialEq)] -pub struct ExprIpyEscapeCommand { +#[derive(Debug, PartialEq)] +pub struct ExprIpyEscapeCommand<'ast> { pub range: TextRange, pub kind: IpyEscapeKind, - pub value: Box, + pub value: &'ast str, } -impl From for Expr { - fn from(payload: ExprIpyEscapeCommand) -> Self { +impl<'ast> From> for Expr<'ast> { + fn from(payload: ExprIpyEscapeCommand<'ast>) -> Self { Expr::IpyEscapeCommand(payload) } } /// See also [BoolOp](https://docs.python.org/3/library/ast.html#ast.BoolOp) -#[derive(Clone, Debug, PartialEq)] -pub struct ExprBoolOp { +#[derive(Debug, PartialEq)] +pub struct ExprBoolOp<'ast> { pub range: TextRange, pub op: BoolOp, - pub values: Vec, + pub values: Vec>, } -impl From for Expr { - fn from(payload: ExprBoolOp) -> Self { +impl<'ast> From> for Expr<'ast> { + fn from(payload: ExprBoolOp<'ast>) -> Self { Expr::BoolOp(payload) } } /// See also [NamedExpr](https://docs.python.org/3/library/ast.html#ast.NamedExpr) -#[derive(Clone, Debug, PartialEq)] -pub struct ExprNamed { +#[derive(Debug, PartialEq)] +pub struct ExprNamed<'ast> { pub range: TextRange, - pub target: Box, - pub value: Box, + pub target: Box<'ast, Expr<'ast>>, + pub value: Box<'ast, Expr<'ast>>, } -impl From for Expr { - fn from(payload: ExprNamed) -> Self { +impl<'ast> From> for Expr<'ast> { + fn from(payload: ExprNamed<'ast>) -> Self { Expr::Named(payload) } } /// See also [BinOp](https://docs.python.org/3/library/ast.html#ast.BinOp) -#[derive(Clone, Debug, PartialEq)] -pub struct ExprBinOp { +#[derive(Debug, PartialEq)] +pub struct ExprBinOp<'ast> { pub range: TextRange, - pub left: Box, + pub left: Box<'ast, Expr<'ast>>, pub op: Operator, - pub right: Box, + pub right: Box<'ast, Expr<'ast>>, } -impl From for Expr { - fn from(payload: ExprBinOp) -> Self { +impl<'ast> From> for Expr<'ast> { + fn from(payload: ExprBinOp<'ast>) -> Self { Expr::BinOp(payload) } } /// See also [UnaryOp](https://docs.python.org/3/library/ast.html#ast.UnaryOp) -#[derive(Clone, Debug, PartialEq)] -pub struct ExprUnaryOp { +#[derive(Debug, PartialEq)] +pub struct ExprUnaryOp<'ast> { pub range: TextRange, pub op: UnaryOp, - pub operand: Box, + pub operand: Box<'ast, Expr<'ast>>, } -impl From for Expr { - fn from(payload: ExprUnaryOp) -> Self { +impl<'ast> From> for Expr<'ast> { + fn from(payload: ExprUnaryOp<'ast>) -> Self { Expr::UnaryOp(payload) } } /// See also [Lambda](https://docs.python.org/3/library/ast.html#ast.Lambda) -#[derive(Clone, Debug, PartialEq)] -pub struct ExprLambda { +#[derive(Debug, PartialEq)] +pub struct ExprLambda<'ast> { pub range: TextRange, - pub parameters: Option>, - pub body: Box, + pub parameters: Option>>, + pub body: Box<'ast, Expr<'ast>>, } -impl From for Expr { - fn from(payload: ExprLambda) -> Self { +impl<'ast> From> for Expr<'ast> { + fn from(payload: ExprLambda<'ast>) -> Self { Expr::Lambda(payload) } } /// See also [IfExp](https://docs.python.org/3/library/ast.html#ast.IfExp) -#[derive(Clone, Debug, PartialEq)] -pub struct ExprIf { +#[derive(Debug, PartialEq)] +pub struct ExprIf<'ast> { pub range: TextRange, - pub test: Box, - pub body: Box, - pub orelse: Box, + pub test: Box<'ast, Expr<'ast>>, + pub body: Box<'ast, Expr<'ast>>, + pub orelse: Box<'ast, Expr<'ast>>, } -impl From for Expr { - fn from(payload: ExprIf) -> Self { +impl<'ast> From> for Expr<'ast> { + fn from(payload: ExprIf<'ast>) -> Self { Expr::If(payload) } } @@ -796,23 +802,23 @@ impl From for Expr { /// ``` /// /// [1]: https://docs.python.org/3/reference/expressions.html#displays-for-lists-sets-and-dictionaries -#[derive(Debug, Clone, PartialEq)] -pub struct DictItem { - pub key: Option, - pub value: Expr, +#[derive(Debug, PartialEq)] +pub struct DictItem<'ast> { + pub key: Option>, + pub value: Expr<'ast>, } -impl DictItem { - fn key(&self) -> Option<&Expr> { +impl<'ast> DictItem<'ast> { + fn key(&self) -> Option<&Expr<'ast>> { self.key.as_ref() } - fn value(&self) -> &Expr { + fn value(&self) -> &Expr<'ast> { &self.value } } -impl Ranged for DictItem { +impl Ranged for DictItem<'_> { fn range(&self) -> TextRange { TextRange::new( self.key.as_ref().map_or(self.value.start(), Ranged::start), @@ -822,22 +828,22 @@ impl Ranged for DictItem { } /// See also [Dict](https://docs.python.org/3/library/ast.html#ast.Dict) -#[derive(Clone, Debug, PartialEq)] -pub struct ExprDict { +#[derive(Debug, PartialEq)] +pub struct ExprDict<'ast> { pub range: TextRange, - pub items: Vec, + pub items: Vec>, } -impl ExprDict { +impl<'ast> ExprDict<'ast> { /// Returns an `Iterator` over the AST nodes representing the /// dictionary's keys. - pub fn iter_keys(&self) -> DictKeyIterator { + pub fn iter_keys(&self) -> DictKeyIterator<'_, 'ast> { DictKeyIterator::new(&self.items) } /// Returns an `Iterator` over the AST nodes representing the /// dictionary's values. - pub fn iter_values(&self) -> DictValueIterator { + pub fn iter_values(&self) -> DictValueIterator<'_, 'ast> { DictValueIterator::new(&self.items) } @@ -845,7 +851,7 @@ impl ExprDict { /// dictionary. /// /// Panics: If the index `n` is out of bounds. - pub fn key(&self, n: usize) -> Option<&Expr> { + pub fn key(&self, n: usize) -> Option<&Expr<'ast>> { self.items[n].key() } @@ -853,24 +859,24 @@ impl ExprDict { /// dictionary. /// /// Panics: If the index `n` is out of bounds. - pub fn value(&self, n: usize) -> &Expr { + pub fn value(&self, n: usize) -> &Expr<'ast> { self.items[n].value() } } -impl From for Expr { - fn from(payload: ExprDict) -> Self { +impl<'ast> From> for Expr<'ast> { + fn from(payload: ExprDict<'ast>) -> Self { Expr::Dict(payload) } } #[derive(Debug, Clone)] -pub struct DictKeyIterator<'a> { - items: Iter<'a, DictItem>, +pub struct DictKeyIterator<'a, 'ast> { + items: Iter<'a, DictItem<'ast>>, } -impl<'a> DictKeyIterator<'a> { - fn new(items: &'a [DictItem]) -> Self { +impl<'a, 'ast> DictKeyIterator<'a, 'ast> { + fn new(items: &'a [DictItem<'ast>]) -> Self { Self { items: items.iter(), } @@ -881,8 +887,8 @@ impl<'a> DictKeyIterator<'a> { } } -impl<'a> Iterator for DictKeyIterator<'a> { - type Item = Option<&'a Expr>; +impl<'a, 'ast> Iterator for DictKeyIterator<'a, 'ast> { + type Item = Option<&'a Expr<'ast>>; fn next(&mut self) -> Option { self.items.next().map(DictItem::key) @@ -897,22 +903,22 @@ impl<'a> Iterator for DictKeyIterator<'a> { } } -impl<'a> DoubleEndedIterator for DictKeyIterator<'a> { +impl DoubleEndedIterator for DictKeyIterator<'_, '_> { fn next_back(&mut self) -> Option { self.items.next_back().map(DictItem::key) } } -impl<'a> FusedIterator for DictKeyIterator<'a> {} -impl<'a> ExactSizeIterator for DictKeyIterator<'a> {} +impl FusedIterator for DictKeyIterator<'_, '_> {} +impl ExactSizeIterator for DictKeyIterator<'_, '_> {} #[derive(Debug, Clone)] -pub struct DictValueIterator<'a> { - items: Iter<'a, DictItem>, +pub struct DictValueIterator<'a, 'ast> { + items: Iter<'a, DictItem<'ast>>, } -impl<'a> DictValueIterator<'a> { - fn new(items: &'a [DictItem]) -> Self { +impl<'a, 'ast> DictValueIterator<'a, 'ast> { + fn new(items: &'a [DictItem<'ast>]) -> Self { Self { items: items.iter(), } @@ -923,8 +929,8 @@ impl<'a> DictValueIterator<'a> { } } -impl<'a> Iterator for DictValueIterator<'a> { - type Item = &'a Expr; +impl<'a, 'ast> Iterator for DictValueIterator<'a, 'ast> { + type Item = &'a Expr<'ast>; fn next(&mut self) -> Option { self.items.next().map(DictItem::value) @@ -939,202 +945,202 @@ impl<'a> Iterator for DictValueIterator<'a> { } } -impl<'a> DoubleEndedIterator for DictValueIterator<'a> { +impl DoubleEndedIterator for DictValueIterator<'_, '_> { fn next_back(&mut self) -> Option { self.items.next_back().map(DictItem::value) } } -impl<'a> FusedIterator for DictValueIterator<'a> {} -impl<'a> ExactSizeIterator for DictValueIterator<'a> {} +impl FusedIterator for DictValueIterator<'_, '_> {} +impl ExactSizeIterator for DictValueIterator<'_, '_> {} /// See also [Set](https://docs.python.org/3/library/ast.html#ast.Set) -#[derive(Clone, Debug, PartialEq)] -pub struct ExprSet { +#[derive(Debug, PartialEq)] +pub struct ExprSet<'ast> { pub range: TextRange, - pub elts: Vec, + pub elts: Vec>, } -impl From for Expr { - fn from(payload: ExprSet) -> Self { +impl<'ast> From> for Expr<'ast> { + fn from(payload: ExprSet<'ast>) -> Self { Expr::Set(payload) } } /// See also [ListComp](https://docs.python.org/3/library/ast.html#ast.ListComp) -#[derive(Clone, Debug, PartialEq)] -pub struct ExprListComp { +#[derive(Debug, PartialEq)] +pub struct ExprListComp<'ast> { pub range: TextRange, - pub elt: Box, - pub generators: Vec, + pub elt: Box<'ast, Expr<'ast>>, + pub generators: Vec>, } -impl From for Expr { - fn from(payload: ExprListComp) -> Self { +impl<'ast> From> for Expr<'ast> { + fn from(payload: ExprListComp<'ast>) -> Self { Expr::ListComp(payload) } } /// See also [SetComp](https://docs.python.org/3/library/ast.html#ast.SetComp) -#[derive(Clone, Debug, PartialEq)] -pub struct ExprSetComp { +#[derive(Debug, PartialEq)] +pub struct ExprSetComp<'ast> { pub range: TextRange, - pub elt: Box, - pub generators: Vec, + pub elt: Box<'ast, Expr<'ast>>, + pub generators: Vec>, } -impl From for Expr { - fn from(payload: ExprSetComp) -> Self { +impl<'ast> From> for Expr<'ast> { + fn from(payload: ExprSetComp<'ast>) -> Self { Expr::SetComp(payload) } } /// See also [DictComp](https://docs.python.org/3/library/ast.html#ast.DictComp) -#[derive(Clone, Debug, PartialEq)] -pub struct ExprDictComp { +#[derive(Debug, PartialEq)] +pub struct ExprDictComp<'ast> { pub range: TextRange, - pub key: Box, - pub value: Box, - pub generators: Vec, + pub key: Box<'ast, Expr<'ast>>, + pub value: Box<'ast, Expr<'ast>>, + pub generators: Vec>, } -impl From for Expr { - fn from(payload: ExprDictComp) -> Self { +impl<'ast> From> for Expr<'ast> { + fn from(payload: ExprDictComp<'ast>) -> Self { Expr::DictComp(payload) } } /// See also [GeneratorExp](https://docs.python.org/3/library/ast.html#ast.GeneratorExp) -#[derive(Clone, Debug, PartialEq)] -pub struct ExprGenerator { +#[derive(Debug, PartialEq)] +pub struct ExprGenerator<'ast> { pub range: TextRange, - pub elt: Box, - pub generators: Vec, + pub elt: Box<'ast, Expr<'ast>>, + pub generators: Vec>, pub parenthesized: bool, } -impl From for Expr { - fn from(payload: ExprGenerator) -> Self { +impl<'ast> From> for Expr<'ast> { + fn from(payload: ExprGenerator<'ast>) -> Self { Expr::Generator(payload) } } /// See also [Await](https://docs.python.org/3/library/ast.html#ast.Await) -#[derive(Clone, Debug, PartialEq)] -pub struct ExprAwait { +#[derive(Debug, PartialEq)] +pub struct ExprAwait<'ast> { pub range: TextRange, - pub value: Box, + pub value: Box<'ast, Expr<'ast>>, } -impl From for Expr { - fn from(payload: ExprAwait) -> Self { +impl<'ast> From> for Expr<'ast> { + fn from(payload: ExprAwait<'ast>) -> Self { Expr::Await(payload) } } /// See also [Yield](https://docs.python.org/3/library/ast.html#ast.Yield) -#[derive(Clone, Debug, PartialEq)] -pub struct ExprYield { +#[derive(Debug, PartialEq)] +pub struct ExprYield<'ast> { pub range: TextRange, - pub value: Option>, + pub value: Option>>, } -impl From for Expr { - fn from(payload: ExprYield) -> Self { +impl<'ast> From> for Expr<'ast> { + fn from(payload: ExprYield<'ast>) -> Self { Expr::Yield(payload) } } /// See also [YieldFrom](https://docs.python.org/3/library/ast.html#ast.YieldFrom) -#[derive(Clone, Debug, PartialEq)] -pub struct ExprYieldFrom { +#[derive(Debug, PartialEq)] +pub struct ExprYieldFrom<'ast> { pub range: TextRange, - pub value: Box, + pub value: Box<'ast, Expr<'ast>>, } -impl From for Expr { - fn from(payload: ExprYieldFrom) -> Self { +impl<'ast> From> for Expr<'ast> { + fn from(payload: ExprYieldFrom<'ast>) -> Self { Expr::YieldFrom(payload) } } /// See also [Compare](https://docs.python.org/3/library/ast.html#ast.Compare) -#[derive(Clone, Debug, PartialEq)] -pub struct ExprCompare { +#[derive(Debug, PartialEq)] +pub struct ExprCompare<'ast> { pub range: TextRange, - pub left: Box, - pub ops: Box<[CmpOp]>, - pub comparators: Box<[Expr]>, + pub left: Box<'ast, Expr<'ast>>, + pub ops: &'ast mut [CmpOp], + pub comparators: &'ast mut [Expr<'ast>], } -impl From for Expr { - fn from(payload: ExprCompare) -> Self { +impl<'ast> From> for Expr<'ast> { + fn from(payload: ExprCompare<'ast>) -> Self { Expr::Compare(payload) } } /// See also [Call](https://docs.python.org/3/library/ast.html#ast.Call) -#[derive(Clone, Debug, PartialEq)] -pub struct ExprCall { +#[derive(Debug, PartialEq)] +pub struct ExprCall<'ast> { pub range: TextRange, - pub func: Box, - pub arguments: Arguments, + pub func: Box<'ast, Expr<'ast>>, + pub arguments: Arguments<'ast>, } -impl From for Expr { - fn from(payload: ExprCall) -> Self { +impl<'ast> From> for Expr<'ast> { + fn from(payload: ExprCall<'ast>) -> Self { Expr::Call(payload) } } -#[derive(Clone, Debug, PartialEq)] -pub struct FStringFormatSpec { +#[derive(Debug, PartialEq)] +pub struct FStringFormatSpec<'ast> { pub range: TextRange, - pub elements: FStringElements, + pub elements: FStringElements<'ast>, } -impl Ranged for FStringFormatSpec { +impl Ranged for FStringFormatSpec<'_> { fn range(&self) -> TextRange { self.range } } /// See also [FormattedValue](https://docs.python.org/3/library/ast.html#ast.FormattedValue) -#[derive(Clone, Debug, PartialEq)] -pub struct FStringExpressionElement { +#[derive(Debug, PartialEq)] +pub struct FStringExpressionElement<'ast> { pub range: TextRange, - pub expression: Box, + pub expression: Box<'ast, Expr<'ast>>, pub debug_text: Option, pub conversion: ConversionFlag, - pub format_spec: Option>, + pub format_spec: Option>>, } -impl Ranged for FStringExpressionElement { +impl Ranged for FStringExpressionElement<'_> { fn range(&self) -> TextRange { self.range } } /// An `FStringLiteralElement` with an empty `value` is an invalid f-string element. -#[derive(Clone, Debug, PartialEq)] -pub struct FStringLiteralElement { +#[derive(Debug, PartialEq)] +pub struct FStringLiteralElement<'ast> { pub range: TextRange, - pub value: Box, + pub value: &'ast str, } -impl FStringLiteralElement { +impl FStringLiteralElement<'_> { pub fn is_valid(&self) -> bool { !self.value.is_empty() } } -impl Ranged for FStringLiteralElement { +impl Ranged for FStringLiteralElement<'_> { fn range(&self) -> TextRange { self.range } } -impl Deref for FStringLiteralElement { +impl Deref for FStringLiteralElement<'_> { type Target = str; fn deref(&self) -> &Self::Target { @@ -1169,7 +1175,7 @@ impl ConversionFlag { } } -#[derive(Clone, Debug, PartialEq, Eq, Hash)] +#[derive(Debug, PartialEq, Eq, Hash)] pub struct DebugText { /// The text between the `{` and the expression node. pub leading: String, @@ -1184,27 +1190,27 @@ pub struct DebugText { /// it keeps them separate and provide various methods to access the parts. /// /// [JoinedStr]: https://docs.python.org/3/library/ast.html#ast.JoinedStr -#[derive(Clone, Debug, PartialEq)] -pub struct ExprFString { +#[derive(Debug, PartialEq)] +pub struct ExprFString<'ast> { pub range: TextRange, - pub value: FStringValue, + pub value: FStringValue<'ast>, } -impl From for Expr { - fn from(payload: ExprFString) -> Self { +impl<'ast> From> for Expr<'ast> { + fn from(payload: ExprFString<'ast>) -> Self { Expr::FString(payload) } } /// The value representing an [`ExprFString`]. -#[derive(Clone, Debug, PartialEq)] -pub struct FStringValue { - inner: FStringValueInner, +#[derive(Debug, PartialEq)] +pub struct FStringValue<'ast> { + inner: FStringValueInner<'ast>, } -impl FStringValue { +impl<'ast> FStringValue<'ast> { /// Creates a new f-string with the given value. - pub fn single(value: FString) -> Self { + pub fn single(value: FString<'ast>) -> Self { Self { inner: FStringValueInner::Single(FStringPart::FString(value)), } @@ -1216,7 +1222,7 @@ impl FStringValue { /// # Panics /// /// Panics if `values` is less than 2. Use [`FStringValue::single`] instead. - pub fn concatenated(values: Vec) -> Self { + pub fn concatenated(values: Vec>) -> Self { assert!(values.len() > 1); Self { inner: FStringValueInner::Concatenated(values), @@ -1229,7 +1235,7 @@ impl FStringValue { } /// Returns a slice of all the [`FStringPart`]s contained in this value. - pub fn as_slice(&self) -> &[FStringPart] { + pub fn as_slice(&self) -> &[FStringPart<'ast>] { match &self.inner { FStringValueInner::Single(part) => std::slice::from_ref(part), FStringValueInner::Concatenated(parts) => parts, @@ -1237,7 +1243,7 @@ impl FStringValue { } /// Returns a mutable slice of all the [`FStringPart`]s contained in this value. - fn as_mut_slice(&mut self) -> &mut [FStringPart] { + fn as_mut_slice(&mut self) -> &mut [FStringPart<'ast>] { match &mut self.inner { FStringValueInner::Single(part) => std::slice::from_mut(part), FStringValueInner::Concatenated(parts) => parts, @@ -1245,13 +1251,13 @@ impl FStringValue { } /// Returns an iterator over all the [`FStringPart`]s contained in this value. - pub fn iter(&self) -> Iter { + pub fn iter(&self) -> Iter> { self.as_slice().iter() } /// Returns an iterator over all the [`FStringPart`]s contained in this value /// that allows modification. - pub(crate) fn iter_mut(&mut self) -> IterMut { + pub(crate) fn iter_mut(&mut self) -> IterMut> { self.as_mut_slice().iter_mut() } @@ -1264,7 +1270,7 @@ impl FStringValue { /// ``` /// /// Here, the string literal parts returned would be `"foo"` and `"baz"`. - pub fn literals(&self) -> impl Iterator { + pub fn literals(&self) -> impl Iterator> { self.iter().filter_map(|part| part.as_literal()) } @@ -1277,7 +1283,7 @@ impl FStringValue { /// ``` /// /// Here, the f-string parts returned would be `f"bar {x}"` and `f"qux"`. - pub fn f_strings(&self) -> impl Iterator { + pub fn f_strings(&self) -> impl Iterator> { self.iter().filter_map(|part| part.as_f_string()) } @@ -1292,49 +1298,49 @@ impl FStringValue { /// /// The f-string elements returned would be string literal (`"bar "`), /// expression (`x`) and string literal (`"qux"`). - pub fn elements(&self) -> impl Iterator { + pub fn elements(&self) -> impl Iterator> { self.f_strings().flat_map(|fstring| fstring.elements.iter()) } } -impl<'a> IntoIterator for &'a FStringValue { - type Item = &'a FStringPart; - type IntoIter = Iter<'a, FStringPart>; +impl<'a, 'ast> IntoIterator for &'a FStringValue<'ast> { + type Item = &'a FStringPart<'ast>; + type IntoIter = Iter<'a, FStringPart<'ast>>; fn into_iter(self) -> Self::IntoIter { self.iter() } } -impl<'a> IntoIterator for &'a mut FStringValue { - type Item = &'a mut FStringPart; - type IntoIter = IterMut<'a, FStringPart>; +impl<'a, 'ast> IntoIterator for &'a mut FStringValue<'ast> { + type Item = &'a mut FStringPart<'ast>; + type IntoIter = IterMut<'a, FStringPart<'ast>>; fn into_iter(self) -> Self::IntoIter { self.iter_mut() } } /// An internal representation of [`FStringValue`]. -#[derive(Clone, Debug, PartialEq)] -enum FStringValueInner { +#[derive(Debug, PartialEq)] +enum FStringValueInner<'ast> { /// A single f-string i.e., `f"foo"`. /// /// This is always going to be `FStringPart::FString` variant which is /// maintained by the `FStringValue::single` constructor. - Single(FStringPart), + Single(FStringPart<'ast>), /// An implicitly concatenated f-string i.e., `"foo" f"bar {x}"`. - Concatenated(Vec), + Concatenated(Vec>), } /// An f-string part which is either a string literal or an f-string. -#[derive(Clone, Debug, PartialEq, is_macro::Is)] -pub enum FStringPart { - Literal(StringLiteral), - FString(FString), +#[derive(Debug, PartialEq, is_macro::Is)] +pub enum FStringPart<'ast> { + Literal(StringLiteral<'ast>), + FString(FString<'ast>), } -impl FStringPart { +impl FStringPart<'_> { pub fn quote_style(&self) -> Quote { match self { Self::Literal(string_literal) => string_literal.flags.quote_style(), @@ -1343,7 +1349,7 @@ impl FStringPart { } } -impl Ranged for FStringPart { +impl Ranged for FStringPart<'_> { fn range(&self) -> TextRange { match self { FStringPart::Literal(string_literal) => string_literal.range(), @@ -1411,7 +1417,7 @@ pub trait StringFlags: Copy { } bitflags! { - #[derive(Default, Copy, Clone, PartialEq, Eq, Hash)] + #[derive(Default, Copy, Clone, PartialEq, Eq, Hash)] struct FStringFlagsInner: u8 { /// The f-string uses double quotes (`"`) for its opener and closer. /// If this flag is not set, the f-string uses single quotes (`'`) @@ -1517,21 +1523,21 @@ impl fmt::Debug for FStringFlags { } /// An AST node that represents a single f-string which is part of an [`ExprFString`]. -#[derive(Clone, Debug, PartialEq)] -pub struct FString { +#[derive(Debug, PartialEq)] +pub struct FString<'ast> { pub range: TextRange, - pub elements: FStringElements, + pub elements: FStringElements<'ast>, pub flags: FStringFlags, } -impl Ranged for FString { +impl Ranged for FString<'_> { fn range(&self) -> TextRange { self.range } } -impl From for Expr { - fn from(payload: FString) -> Self { +impl<'ast> From> for Expr<'ast> { + fn from(payload: FString<'ast>) -> Self { ExprFString { range: payload.range, value: FStringValue::single(payload), @@ -1541,72 +1547,72 @@ impl From for Expr { } /// A newtype wrapper around a list of [`FStringElement`]. -#[derive(Clone, Default, PartialEq)] -pub struct FStringElements(Vec); +#[derive(Default, PartialEq)] +pub struct FStringElements<'ast>(Vec>); -impl FStringElements { +impl<'ast> FStringElements<'ast> { /// Returns an iterator over all the [`FStringLiteralElement`] nodes contained in this f-string. - pub fn literals(&self) -> impl Iterator { + pub fn literals(&self) -> impl Iterator> { self.iter().filter_map(|element| element.as_literal()) } /// Returns an iterator over all the [`FStringExpressionElement`] nodes contained in this f-string. - pub fn expressions(&self) -> impl Iterator { + pub fn expressions(&self) -> impl Iterator> { self.iter().filter_map(|element| element.as_expression()) } } -impl From> for FStringElements { - fn from(elements: Vec) -> Self { +impl<'ast> From>> for FStringElements<'ast> { + fn from(elements: Vec>) -> Self { FStringElements(elements) } } -impl<'a> IntoIterator for &'a FStringElements { - type IntoIter = Iter<'a, FStringElement>; - type Item = &'a FStringElement; +impl<'a, 'ast> IntoIterator for &'a FStringElements<'ast> { + type Item = &'a FStringElement<'ast>; + type IntoIter = Iter<'a, FStringElement<'ast>>; fn into_iter(self) -> Self::IntoIter { self.iter() } } -impl<'a> IntoIterator for &'a mut FStringElements { - type IntoIter = IterMut<'a, FStringElement>; - type Item = &'a mut FStringElement; +impl<'a, 'ast> IntoIterator for &'a mut FStringElements<'ast> { + type Item = &'a mut FStringElement<'ast>; + type IntoIter = IterMut<'a, FStringElement<'ast>>; fn into_iter(self) -> Self::IntoIter { self.iter_mut() } } -impl Deref for FStringElements { - type Target = [FStringElement]; +impl<'ast> Deref for FStringElements<'ast> { + type Target = [FStringElement<'ast>]; fn deref(&self) -> &Self::Target { &self.0 } } -impl DerefMut for FStringElements { +impl DerefMut for FStringElements<'_> { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 } } -impl fmt::Debug for FStringElements { +impl fmt::Debug for FStringElements<'_> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Debug::fmt(&self.0, f) } } -#[derive(Clone, Debug, PartialEq, is_macro::Is)] -pub enum FStringElement { - Literal(FStringLiteralElement), - Expression(FStringExpressionElement), +#[derive(Debug, PartialEq, is_macro::Is)] +pub enum FStringElement<'ast> { + Literal(FStringLiteralElement<'ast>), + Expression(FStringExpressionElement<'ast>), } -impl Ranged for FStringElement { +impl Ranged for FStringElement<'_> { fn range(&self) -> TextRange { match self { FStringElement::Literal(node) => node.range(), @@ -1617,33 +1623,33 @@ impl Ranged for FStringElement { /// An AST node that represents either a single string literal or an implicitly /// concatenated string literals. -#[derive(Clone, Debug, Default, PartialEq)] -pub struct ExprStringLiteral { +#[derive(Debug, Default, PartialEq)] +pub struct ExprStringLiteral<'ast> { pub range: TextRange, - pub value: StringLiteralValue, + pub value: StringLiteralValue<'ast>, } -impl From for Expr { - fn from(payload: ExprStringLiteral) -> Self { +impl<'ast> From> for Expr<'ast> { + fn from(payload: ExprStringLiteral<'ast>) -> Self { Expr::StringLiteral(payload) } } -impl Ranged for ExprStringLiteral { +impl Ranged for ExprStringLiteral<'_> { fn range(&self) -> TextRange { self.range } } /// The value representing a [`ExprStringLiteral`]. -#[derive(Clone, Debug, Default, PartialEq)] -pub struct StringLiteralValue { - inner: StringLiteralValueInner, +#[derive(Debug, Default, PartialEq)] +pub struct StringLiteralValue<'ast> { + inner: StringLiteralValueInner<'ast>, } -impl StringLiteralValue { +impl<'ast> StringLiteralValue<'ast> { /// Creates a new single string literal with the given value. - pub fn single(string: StringLiteral) -> Self { + pub fn single(string: StringLiteral<'ast>) -> Self { Self { inner: StringLiteralValueInner::Single(string), } @@ -1656,7 +1662,7 @@ impl StringLiteralValue { /// /// Panics if `strings` is less than 2. Use [`StringLiteralValue::single`] /// instead. - pub fn concatenated(strings: Vec) -> Self { + pub fn concatenated(strings: Vec>) -> Self { assert!(strings.len() > 1); Self { inner: StringLiteralValueInner::Concatenated(ConcatenatedStringLiteral { @@ -1682,7 +1688,7 @@ impl StringLiteralValue { } /// Returns a slice of all the [`StringLiteral`] parts contained in this value. - pub fn as_slice(&self) -> &[StringLiteral] { + pub fn as_slice(&self) -> &[StringLiteral<'ast>] { match &self.inner { StringLiteralValueInner::Single(value) => std::slice::from_ref(value), StringLiteralValueInner::Concatenated(value) => value.strings.as_slice(), @@ -1690,7 +1696,7 @@ impl StringLiteralValue { } /// Returns a mutable slice of all the [`StringLiteral`] parts contained in this value. - fn as_mut_slice(&mut self) -> &mut [StringLiteral] { + fn as_mut_slice(&mut self) -> &mut [StringLiteral<'ast>] { match &mut self.inner { StringLiteralValueInner::Single(value) => std::slice::from_mut(value), StringLiteralValueInner::Concatenated(value) => value.strings.as_mut_slice(), @@ -1698,13 +1704,13 @@ impl StringLiteralValue { } /// Returns an iterator over all the [`StringLiteral`] parts contained in this value. - pub fn iter(&self) -> Iter { + pub fn iter(&self) -> Iter> { self.as_slice().iter() } /// Returns an iterator over all the [`StringLiteral`] parts contained in this value /// that allows modification. - pub(crate) fn iter_mut(&mut self) -> IterMut { + pub(crate) fn iter_mut(&mut self) -> IterMut> { self.as_mut_slice().iter_mut() } @@ -1736,24 +1742,24 @@ impl StringLiteralValue { } } -impl<'a> IntoIterator for &'a StringLiteralValue { - type Item = &'a StringLiteral; - type IntoIter = Iter<'a, StringLiteral>; +impl<'a, 'ast> IntoIterator for &'a StringLiteralValue<'ast> { + type Item = &'a StringLiteral<'ast>; + type IntoIter = Iter<'a, StringLiteral<'ast>>; fn into_iter(self) -> Self::IntoIter { self.iter() } } -impl<'a> IntoIterator for &'a mut StringLiteralValue { - type Item = &'a mut StringLiteral; - type IntoIter = IterMut<'a, StringLiteral>; +impl<'a, 'ast> IntoIterator for &'a mut StringLiteralValue<'ast> { + type Item = &'a mut StringLiteral<'ast>; + type IntoIter = IterMut<'a, StringLiteral<'ast>>; fn into_iter(self) -> Self::IntoIter { self.iter_mut() } } -impl PartialEq for StringLiteralValue { +impl PartialEq for StringLiteralValue<'_> { fn eq(&self, other: &str) -> bool { if self.len() != other.len() { return false; @@ -1763,23 +1769,23 @@ impl PartialEq for StringLiteralValue { } } -impl fmt::Display for StringLiteralValue { +impl fmt::Display for StringLiteralValue<'_> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str(self.to_str()) } } /// An internal representation of [`StringLiteralValue`]. -#[derive(Clone, Debug, PartialEq)] -enum StringLiteralValueInner { +#[derive(Debug, PartialEq)] +enum StringLiteralValueInner<'ast> { /// A single string literal i.e., `"foo"`. - Single(StringLiteral), + Single(StringLiteral<'ast>), /// An implicitly concatenated string literals i.e., `"foo" "bar"`. - Concatenated(ConcatenatedStringLiteral), + Concatenated(ConcatenatedStringLiteral<'ast>), } -impl Default for StringLiteralValueInner { +impl Default for StringLiteralValueInner<'_> { fn default() -> Self { Self::Single(StringLiteral::default()) } @@ -1928,20 +1934,20 @@ impl fmt::Debug for StringLiteralFlags { /// An AST node that represents a single string literal which is part of an /// [`ExprStringLiteral`]. -#[derive(Clone, Debug, Default, PartialEq)] -pub struct StringLiteral { +#[derive(Default, Debug, PartialEq)] +pub struct StringLiteral<'ast> { pub range: TextRange, - pub value: Box, + pub value: &'ast str, pub flags: StringLiteralFlags, } -impl Ranged for StringLiteral { +impl Ranged for StringLiteral<'_> { fn range(&self) -> TextRange { self.range } } -impl Deref for StringLiteral { +impl Deref for StringLiteral<'_> { type Target = str; fn deref(&self) -> &Self::Target { @@ -1949,7 +1955,7 @@ impl Deref for StringLiteral { } } -impl StringLiteral { +impl<'ast> StringLiteral<'ast> { /// Extracts a string slice containing the entire `String`. pub fn as_str(&self) -> &str { self @@ -1959,14 +1965,14 @@ impl StringLiteral { pub fn invalid(range: TextRange) -> Self { Self { range, - value: "".into(), + value: Default::default(), flags: StringLiteralFlags::default().with_invalid(), } } } -impl From for Expr { - fn from(payload: StringLiteral) -> Self { +impl<'ast> From> for Expr<'ast> { + fn from(payload: StringLiteral<'ast>) -> Self { ExprStringLiteral { range: payload.range, value: StringLiteralValue::single(payload), @@ -1977,25 +1983,24 @@ impl From for Expr { /// An internal representation of [`StringLiteral`] that represents an /// implicitly concatenated string. -#[derive(Clone)] -struct ConcatenatedStringLiteral { +struct ConcatenatedStringLiteral<'ast> { /// Each string literal that makes up the concatenated string. - strings: Vec, + strings: Vec>, /// The concatenated string value. - value: OnceLock>, + value: OnceLock>, } -impl ConcatenatedStringLiteral { +impl<'ast> ConcatenatedStringLiteral<'ast> { /// Extracts a string slice containing the entire concatenated string. fn to_str(&self) -> &str { self.value.get_or_init(|| { let concatenated: String = self.strings.iter().map(StringLiteral::as_str).collect(); - concatenated.into_boxed_str() + std::boxed::Box::from(concatenated) }) } } -impl PartialEq for ConcatenatedStringLiteral { +impl PartialEq for ConcatenatedStringLiteral<'_> { fn eq(&self, other: &Self) -> bool { if self.strings.len() != other.strings.len() { return false; @@ -2008,7 +2013,7 @@ impl PartialEq for ConcatenatedStringLiteral { } } -impl Debug for ConcatenatedStringLiteral { +impl Debug for ConcatenatedStringLiteral<'_> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("ConcatenatedStringLiteral") .field("strings", &self.strings) @@ -2019,33 +2024,33 @@ impl Debug for ConcatenatedStringLiteral { /// An AST node that represents either a single bytes literal or an implicitly /// concatenated bytes literals. -#[derive(Clone, Debug, Default, PartialEq)] -pub struct ExprBytesLiteral { +#[derive(Debug, Default, PartialEq)] +pub struct ExprBytesLiteral<'ast> { pub range: TextRange, - pub value: BytesLiteralValue, + pub value: BytesLiteralValue<'ast>, } -impl From for Expr { - fn from(payload: ExprBytesLiteral) -> Self { +impl<'ast> From> for Expr<'ast> { + fn from(payload: ExprBytesLiteral<'ast>) -> Self { Expr::BytesLiteral(payload) } } -impl Ranged for ExprBytesLiteral { +impl Ranged for ExprBytesLiteral<'_> { fn range(&self) -> TextRange { self.range } } /// The value representing a [`ExprBytesLiteral`]. -#[derive(Clone, Debug, Default, PartialEq)] -pub struct BytesLiteralValue { - inner: BytesLiteralValueInner, +#[derive(Debug, Default, PartialEq)] +pub struct BytesLiteralValue<'ast> { + inner: BytesLiteralValueInner<'ast>, } -impl BytesLiteralValue { +impl<'ast> BytesLiteralValue<'ast> { /// Creates a new single bytes literal with the given value. - pub fn single(value: BytesLiteral) -> Self { + pub fn single(value: BytesLiteral<'ast>) -> Self { Self { inner: BytesLiteralValueInner::Single(value), } @@ -2058,7 +2063,7 @@ impl BytesLiteralValue { /// /// Panics if `values` is less than 2. Use [`BytesLiteralValue::single`] /// instead. - pub fn concatenated(values: Vec) -> Self { + pub fn concatenated(values: Vec>) -> Self { assert!(values.len() > 1); Self { inner: BytesLiteralValueInner::Concatenated(values), @@ -2071,7 +2076,7 @@ impl BytesLiteralValue { } /// Returns a slice of all the [`BytesLiteral`] parts contained in this value. - pub fn as_slice(&self) -> &[BytesLiteral] { + pub fn as_slice(&self) -> &[BytesLiteral<'ast>] { match &self.inner { BytesLiteralValueInner::Single(value) => std::slice::from_ref(value), BytesLiteralValueInner::Concatenated(value) => value.as_slice(), @@ -2079,7 +2084,7 @@ impl BytesLiteralValue { } /// Returns a mutable slice of all the [`BytesLiteral`] parts contained in this value. - fn as_mut_slice(&mut self) -> &mut [BytesLiteral] { + fn as_mut_slice(&mut self) -> &mut [BytesLiteral<'ast>] { match &mut self.inner { BytesLiteralValueInner::Single(value) => std::slice::from_mut(value), BytesLiteralValueInner::Concatenated(value) => value.as_mut_slice(), @@ -2087,13 +2092,13 @@ impl BytesLiteralValue { } /// Returns an iterator over all the [`BytesLiteral`] parts contained in this value. - pub fn iter(&self) -> Iter { + pub fn iter(&self) -> Iter> { self.as_slice().iter() } /// Returns an iterator over all the [`BytesLiteral`] parts contained in this value /// that allows modification. - pub(crate) fn iter_mut(&mut self) -> IterMut { + pub(crate) fn iter_mut(&mut self) -> IterMut> { self.as_mut_slice().iter_mut() } @@ -2113,24 +2118,24 @@ impl BytesLiteralValue { } } -impl<'a> IntoIterator for &'a BytesLiteralValue { - type Item = &'a BytesLiteral; - type IntoIter = Iter<'a, BytesLiteral>; +impl<'a, 'ast> IntoIterator for &'a BytesLiteralValue<'ast> { + type Item = &'a BytesLiteral<'ast>; + type IntoIter = Iter<'a, BytesLiteral<'ast>>; fn into_iter(self) -> Self::IntoIter { self.iter() } } -impl<'a> IntoIterator for &'a mut BytesLiteralValue { - type Item = &'a mut BytesLiteral; - type IntoIter = IterMut<'a, BytesLiteral>; +impl<'a, 'ast> IntoIterator for &'a mut BytesLiteralValue<'ast> { + type Item = &'a mut BytesLiteral<'ast>; + type IntoIter = IterMut<'a, BytesLiteral<'ast>>; fn into_iter(self) -> Self::IntoIter { self.iter_mut() } } -impl PartialEq<[u8]> for BytesLiteralValue { +impl PartialEq<[u8]> for BytesLiteralValue<'_> { fn eq(&self, other: &[u8]) -> bool { if self.len() != other.len() { return false; @@ -2143,16 +2148,16 @@ impl PartialEq<[u8]> for BytesLiteralValue { } /// An internal representation of [`BytesLiteralValue`]. -#[derive(Clone, Debug, PartialEq)] -enum BytesLiteralValueInner { +#[derive(Debug, PartialEq)] +enum BytesLiteralValueInner<'ast> { /// A single bytes literal i.e., `b"foo"`. - Single(BytesLiteral), + Single(BytesLiteral<'ast>), /// An implicitly concatenated bytes literals i.e., `b"foo" b"bar"`. - Concatenated(Vec), + Concatenated(Vec>), } -impl Default for BytesLiteralValueInner { +impl Default for BytesLiteralValueInner<'_> { fn default() -> Self { Self::Single(BytesLiteral::default()) } @@ -2275,20 +2280,20 @@ impl fmt::Debug for BytesLiteralFlags { /// An AST node that represents a single bytes literal which is part of an /// [`ExprBytesLiteral`]. -#[derive(Clone, Debug, Default, PartialEq)] -pub struct BytesLiteral { +#[derive(Default, Debug, PartialEq)] +pub struct BytesLiteral<'ast> { pub range: TextRange, - pub value: Box<[u8]>, + pub value: &'ast [u8], pub flags: BytesLiteralFlags, } -impl Ranged for BytesLiteral { +impl Ranged for BytesLiteral<'_> { fn range(&self) -> TextRange { self.range } } -impl Deref for BytesLiteral { +impl Deref for BytesLiteral<'_> { type Target = [u8]; fn deref(&self) -> &Self::Target { @@ -2296,7 +2301,7 @@ impl Deref for BytesLiteral { } } -impl BytesLiteral { +impl<'ast> BytesLiteral<'ast> { /// Extracts a byte slice containing the entire [`BytesLiteral`]. pub fn as_slice(&self) -> &[u8] { self @@ -2306,14 +2311,14 @@ impl BytesLiteral { pub fn invalid(range: TextRange) -> Self { Self { range, - value: Box::new([]), + value: Default::default(), flags: BytesLiteralFlags::default().with_invalid(), } } } -impl From for Expr { - fn from(payload: BytesLiteral) -> Self { +impl<'ast> From> for Expr<'ast> { + fn from(payload: BytesLiteral<'ast>) -> Self { ExprBytesLiteral { range: payload.range, value: BytesLiteralValue::single(payload), @@ -2616,7 +2621,7 @@ pub struct ExprNumberLiteral { pub value: Number, } -impl From for Expr { +impl From for Expr<'_> { fn from(payload: ExprNumberLiteral) -> Self { Expr::NumberLiteral(payload) } @@ -2641,7 +2646,7 @@ pub struct ExprBooleanLiteral { pub value: bool, } -impl From for Expr { +impl From for Expr<'_> { fn from(payload: ExprBooleanLiteral) -> Self { Expr::BooleanLiteral(payload) } @@ -2658,7 +2663,7 @@ pub struct ExprNoneLiteral { pub range: TextRange, } -impl From for Expr { +impl From for Expr<'_> { fn from(payload: ExprNoneLiteral) -> Self { Expr::NoneLiteral(payload) } @@ -2675,7 +2680,7 @@ pub struct ExprEllipsisLiteral { pub range: TextRange, } -impl From for Expr { +impl From for Expr<'_> { fn from(payload: ExprEllipsisLiteral) -> Self { Expr::EllipsisLiteral(payload) } @@ -2688,111 +2693,111 @@ impl Ranged for ExprEllipsisLiteral { } /// See also [Attribute](https://docs.python.org/3/library/ast.html#ast.Attribute) -#[derive(Clone, Debug, PartialEq)] -pub struct ExprAttribute { +#[derive(Debug, PartialEq)] +pub struct ExprAttribute<'ast> { pub range: TextRange, - pub value: Box, - pub attr: Identifier, + pub value: Box<'ast, Expr<'ast>>, + pub attr: Identifier<'ast>, pub ctx: ExprContext, } -impl From for Expr { - fn from(payload: ExprAttribute) -> Self { +impl<'ast> From> for Expr<'ast> { + fn from(payload: ExprAttribute<'ast>) -> Self { Expr::Attribute(payload) } } /// See also [Subscript](https://docs.python.org/3/library/ast.html#ast.Subscript) -#[derive(Clone, Debug, PartialEq)] -pub struct ExprSubscript { +#[derive(Debug, PartialEq)] +pub struct ExprSubscript<'ast> { pub range: TextRange, - pub value: Box, - pub slice: Box, + pub value: Box<'ast, Expr<'ast>>, + pub slice: Box<'ast, Expr<'ast>>, pub ctx: ExprContext, } -impl From for Expr { - fn from(payload: ExprSubscript) -> Self { +impl<'ast> From> for Expr<'ast> { + fn from(payload: ExprSubscript<'ast>) -> Self { Expr::Subscript(payload) } } /// See also [Starred](https://docs.python.org/3/library/ast.html#ast.Starred) -#[derive(Clone, Debug, PartialEq)] -pub struct ExprStarred { +#[derive(Debug, PartialEq)] +pub struct ExprStarred<'ast> { pub range: TextRange, - pub value: Box, + pub value: Box<'ast, Expr<'ast>>, pub ctx: ExprContext, } -impl From for Expr { - fn from(payload: ExprStarred) -> Self { +impl<'ast> From> for Expr<'ast> { + fn from(payload: ExprStarred<'ast>) -> Self { Expr::Starred(payload) } } /// See also [Name](https://docs.python.org/3/library/ast.html#ast.Name) #[derive(Clone, Debug, PartialEq)] -pub struct ExprName { +pub struct ExprName<'ast> { pub range: TextRange, - pub id: Name, + pub id: &'ast str, pub ctx: ExprContext, } -impl ExprName { - pub fn id(&self) -> &Name { +impl<'ast> ExprName<'ast> { + pub fn id(&self) -> &'ast str { &self.id } } -impl From for Expr { - fn from(payload: ExprName) -> Self { +impl<'ast> From> for Expr<'ast> { + fn from(payload: ExprName<'ast>) -> Self { Expr::Name(payload) } } /// See also [List](https://docs.python.org/3/library/ast.html#ast.List) -#[derive(Clone, Debug, PartialEq)] -pub struct ExprList { +#[derive(Debug, PartialEq)] +pub struct ExprList<'ast> { pub range: TextRange, - pub elts: Vec, + pub elts: Vec>, pub ctx: ExprContext, } -impl From for Expr { - fn from(payload: ExprList) -> Self { +impl<'ast> From> for Expr<'ast> { + fn from(payload: ExprList<'ast>) -> Self { Expr::List(payload) } } /// See also [Tuple](https://docs.python.org/3/library/ast.html#ast.Tuple) -#[derive(Clone, Debug, PartialEq)] -pub struct ExprTuple { +#[derive(Debug, PartialEq)] +pub struct ExprTuple<'ast> { pub range: TextRange, - pub elts: Vec, + pub elts: Vec>, pub ctx: ExprContext, /// Whether the tuple is parenthesized in the source code. pub parenthesized: bool, } -impl From for Expr { - fn from(payload: ExprTuple) -> Self { +impl<'ast> From> for Expr<'ast> { + fn from(payload: ExprTuple<'ast>) -> Self { Expr::Tuple(payload) } } /// See also [Slice](https://docs.python.org/3/library/ast.html#ast.Slice) -#[derive(Clone, Debug, PartialEq)] -pub struct ExprSlice { +#[derive(Debug, PartialEq)] +pub struct ExprSlice<'ast> { pub range: TextRange, - pub lower: Option>, - pub upper: Option>, - pub step: Option>, + pub lower: Option>>, + pub upper: Option>>, + pub step: Option>>, } -impl From for Expr { - fn from(payload: ExprSlice) -> Self { +impl<'ast> From> for Expr<'ast> { + fn from(payload: ExprSlice<'ast>) -> Self { Expr::Slice(payload) } } @@ -2937,91 +2942,91 @@ impl fmt::Display for CmpOp { } /// See also [comprehension](https://docs.python.org/3/library/ast.html#ast.comprehension) -#[derive(Clone, Debug, PartialEq)] -pub struct Comprehension { +#[derive(Debug, PartialEq)] +pub struct Comprehension<'ast> { pub range: TextRange, - pub target: Expr, - pub iter: Expr, - pub ifs: Vec, + pub target: Expr<'ast>, + pub iter: Expr<'ast>, + pub ifs: Vec>, pub is_async: bool, } /// See also [excepthandler](https://docs.python.org/3/library/ast.html#ast.excepthandler) -#[derive(Clone, Debug, PartialEq, is_macro::Is)] -pub enum ExceptHandler { - ExceptHandler(ExceptHandlerExceptHandler), +#[derive(Debug, PartialEq, is_macro::Is)] +pub enum ExceptHandler<'ast> { + ExceptHandler(ExceptHandlerExceptHandler<'ast>), } /// See also [ExceptHandler](https://docs.python.org/3/library/ast.html#ast.ExceptHandler) -#[derive(Clone, Debug, PartialEq)] -pub struct ExceptHandlerExceptHandler { +#[derive(Debug, PartialEq)] +pub struct ExceptHandlerExceptHandler<'ast> { pub range: TextRange, - pub type_: Option>, - pub name: Option, - pub body: Vec, + pub type_: Option>>, + pub name: Option>, + pub body: Vec>, } -impl From for ExceptHandler { - fn from(payload: ExceptHandlerExceptHandler) -> Self { +impl<'ast> From> for ExceptHandler<'ast> { + fn from(payload: ExceptHandlerExceptHandler<'ast>) -> Self { ExceptHandler::ExceptHandler(payload) } } /// See also [arg](https://docs.python.org/3/library/ast.html#ast.arg) -#[derive(Clone, Debug, PartialEq)] -pub struct Parameter { +#[derive(Debug, PartialEq)] +pub struct Parameter<'ast> { pub range: TextRange, - pub name: Identifier, - pub annotation: Option>, + pub name: Identifier<'ast>, + pub annotation: Option>>, } /// See also [keyword](https://docs.python.org/3/library/ast.html#ast.keyword) -#[derive(Clone, Debug, PartialEq)] -pub struct Keyword { +#[derive(Debug, PartialEq)] +pub struct Keyword<'ast> { pub range: TextRange, - pub arg: Option, - pub value: Expr, + pub arg: Option>, + pub value: Expr<'ast>, } /// See also [alias](https://docs.python.org/3/library/ast.html#ast.alias) #[derive(Clone, Debug, PartialEq)] -pub struct Alias { +pub struct Alias<'ast> { pub range: TextRange, - pub name: Identifier, - pub asname: Option, + pub name: Identifier<'ast>, + pub asname: Option>, } /// See also [withitem](https://docs.python.org/3/library/ast.html#ast.withitem) -#[derive(Clone, Debug, PartialEq)] -pub struct WithItem { +#[derive(Debug, PartialEq)] +pub struct WithItem<'ast> { pub range: TextRange, - pub context_expr: Expr, - pub optional_vars: Option>, + pub context_expr: Expr<'ast>, + pub optional_vars: Option>>, } /// See also [match_case](https://docs.python.org/3/library/ast.html#ast.match_case) -#[derive(Clone, Debug, PartialEq)] -pub struct MatchCase { +#[derive(Debug, PartialEq)] +pub struct MatchCase<'ast> { pub range: TextRange, - pub pattern: Pattern, - pub guard: Option>, - pub body: Vec, + pub pattern: Pattern<'ast>, + pub guard: Option>>, + pub body: Vec>, } /// See also [pattern](https://docs.python.org/3/library/ast.html#ast.pattern) -#[derive(Clone, Debug, PartialEq, is_macro::Is)] -pub enum Pattern { - MatchValue(PatternMatchValue), +#[derive(Debug, PartialEq, is_macro::Is)] +pub enum Pattern<'ast> { + MatchValue(PatternMatchValue<'ast>), MatchSingleton(PatternMatchSingleton), - MatchSequence(PatternMatchSequence), - MatchMapping(PatternMatchMapping), - MatchClass(PatternMatchClass), - MatchStar(PatternMatchStar), - MatchAs(PatternMatchAs), - MatchOr(PatternMatchOr), + MatchSequence(PatternMatchSequence<'ast>), + MatchMapping(PatternMatchMapping<'ast>), + MatchClass(PatternMatchClass<'ast>), + MatchStar(PatternMatchStar<'ast>), + MatchAs(PatternMatchAs<'ast>), + MatchOr(PatternMatchOr<'ast>), } -impl Pattern { +impl Pattern<'_> { /// Checks if the [`Pattern`] is an [irrefutable pattern]. /// /// [irrefutable pattern]: https://peps.python.org/pep-0634/#irrefutable-case-blocks @@ -3037,69 +3042,69 @@ impl Pattern { } /// See also [MatchValue](https://docs.python.org/3/library/ast.html#ast.MatchValue) -#[derive(Clone, Debug, PartialEq)] -pub struct PatternMatchValue { +#[derive(Debug, PartialEq)] +pub struct PatternMatchValue<'ast> { pub range: TextRange, - pub value: Box, + pub value: Box<'ast, Expr<'ast>>, } -impl From for Pattern { - fn from(payload: PatternMatchValue) -> Self { +impl<'ast> From> for Pattern<'ast> { + fn from(payload: PatternMatchValue<'ast>) -> Self { Pattern::MatchValue(payload) } } /// See also [MatchSingleton](https://docs.python.org/3/library/ast.html#ast.MatchSingleton) -#[derive(Clone, Debug, PartialEq)] +#[derive(Debug, PartialEq)] pub struct PatternMatchSingleton { pub range: TextRange, pub value: Singleton, } -impl From for Pattern { +impl From for Pattern<'_> { fn from(payload: PatternMatchSingleton) -> Self { Pattern::MatchSingleton(payload) } } /// See also [MatchSequence](https://docs.python.org/3/library/ast.html#ast.MatchSequence) -#[derive(Clone, Debug, PartialEq)] -pub struct PatternMatchSequence { +#[derive(Debug, PartialEq)] +pub struct PatternMatchSequence<'ast> { pub range: TextRange, - pub patterns: Vec, + pub patterns: Vec>, } -impl From for Pattern { - fn from(payload: PatternMatchSequence) -> Self { +impl<'ast> From> for Pattern<'ast> { + fn from(payload: PatternMatchSequence<'ast>) -> Self { Pattern::MatchSequence(payload) } } /// See also [MatchMapping](https://docs.python.org/3/library/ast.html#ast.MatchMapping) -#[derive(Clone, Debug, PartialEq)] -pub struct PatternMatchMapping { +#[derive(Debug, PartialEq)] +pub struct PatternMatchMapping<'ast> { pub range: TextRange, - pub keys: Vec, - pub patterns: Vec, - pub rest: Option, + pub keys: Vec>, + pub patterns: Vec>, + pub rest: Option>, } -impl From for Pattern { - fn from(payload: PatternMatchMapping) -> Self { +impl<'ast> From> for Pattern<'ast> { + fn from(payload: PatternMatchMapping<'ast>) -> Self { Pattern::MatchMapping(payload) } } /// See also [MatchClass](https://docs.python.org/3/library/ast.html#ast.MatchClass) -#[derive(Clone, Debug, PartialEq)] -pub struct PatternMatchClass { +#[derive(Debug, PartialEq)] +pub struct PatternMatchClass<'ast> { pub range: TextRange, - pub cls: Box, - pub arguments: PatternArguments, + pub cls: Box<'ast, Expr<'ast>>, + pub arguments: PatternArguments<'ast>, } -impl From for Pattern { - fn from(payload: PatternMatchClass) -> Self { +impl<'ast> From> for Pattern<'ast> { + fn from(payload: PatternMatchClass<'ast>) -> Self { Pattern::MatchClass(payload) } } @@ -3108,132 +3113,132 @@ impl From for Pattern { /// parenthesized contents in `case Point(1, x=0, y=0)`. /// /// Like [`Arguments`], but for [`PatternMatchClass`]. -#[derive(Clone, Debug, PartialEq)] -pub struct PatternArguments { +#[derive(Debug, PartialEq)] +pub struct PatternArguments<'ast> { pub range: TextRange, - pub patterns: Vec, - pub keywords: Vec, + pub patterns: Vec>, + pub keywords: Vec>, } /// An AST node to represent the keyword arguments to a [`PatternMatchClass`], i.e., the /// `x=0` and `y=0` in `case Point(x=0, y=0)`. /// /// Like [`Keyword`], but for [`PatternMatchClass`]. -#[derive(Clone, Debug, PartialEq)] -pub struct PatternKeyword { +#[derive(Debug, PartialEq)] +pub struct PatternKeyword<'ast> { pub range: TextRange, - pub attr: Identifier, - pub pattern: Pattern, + pub attr: Identifier<'ast>, + pub pattern: Pattern<'ast>, } /// See also [MatchStar](https://docs.python.org/3/library/ast.html#ast.MatchStar) -#[derive(Clone, Debug, PartialEq)] -pub struct PatternMatchStar { +#[derive(Debug, PartialEq)] +pub struct PatternMatchStar<'ast> { pub range: TextRange, - pub name: Option, + pub name: Option>, } -impl From for Pattern { - fn from(payload: PatternMatchStar) -> Self { +impl<'ast> From> for Pattern<'ast> { + fn from(payload: PatternMatchStar<'ast>) -> Self { Pattern::MatchStar(payload) } } /// See also [MatchAs](https://docs.python.org/3/library/ast.html#ast.MatchAs) -#[derive(Clone, Debug, PartialEq)] -pub struct PatternMatchAs { +#[derive(Debug, PartialEq)] +pub struct PatternMatchAs<'ast> { pub range: TextRange, - pub pattern: Option>, - pub name: Option, + pub pattern: Option>>, + pub name: Option>, } -impl From for Pattern { - fn from(payload: PatternMatchAs) -> Self { +impl<'ast> From> for Pattern<'ast> { + fn from(payload: PatternMatchAs<'ast>) -> Self { Pattern::MatchAs(payload) } } /// See also [MatchOr](https://docs.python.org/3/library/ast.html#ast.MatchOr) -#[derive(Clone, Debug, PartialEq)] -pub struct PatternMatchOr { +#[derive(Debug, PartialEq)] +pub struct PatternMatchOr<'ast> { pub range: TextRange, - pub patterns: Vec, + pub patterns: Vec>, } -impl From for Pattern { - fn from(payload: PatternMatchOr) -> Self { +impl<'ast> From> for Pattern<'ast> { + fn from(payload: PatternMatchOr<'ast>) -> Self { Pattern::MatchOr(payload) } } /// See also [type_param](https://docs.python.org/3/library/ast.html#ast.type_param) -#[derive(Clone, Debug, PartialEq, is_macro::Is)] -pub enum TypeParam { - TypeVar(TypeParamTypeVar), - ParamSpec(TypeParamParamSpec), - TypeVarTuple(TypeParamTypeVarTuple), +#[derive(Debug, PartialEq, is_macro::Is)] +pub enum TypeParam<'ast> { + TypeVar(TypeParamTypeVar<'ast>), + ParamSpec(TypeParamParamSpec<'ast>), + TypeVarTuple(TypeParamTypeVarTuple<'ast>), } /// See also [TypeVar](https://docs.python.org/3/library/ast.html#ast.TypeVar) -#[derive(Clone, Debug, PartialEq)] -pub struct TypeParamTypeVar { +#[derive(Debug, PartialEq)] +pub struct TypeParamTypeVar<'ast> { pub range: TextRange, - pub name: Identifier, - pub bound: Option>, - pub default: Option>, + pub name: Identifier<'ast>, + pub bound: Option>>, + pub default: Option>>, } -impl From for TypeParam { - fn from(payload: TypeParamTypeVar) -> Self { +impl<'ast> From> for TypeParam<'ast> { + fn from(payload: TypeParamTypeVar<'ast>) -> Self { TypeParam::TypeVar(payload) } } /// See also [ParamSpec](https://docs.python.org/3/library/ast.html#ast.ParamSpec) -#[derive(Clone, Debug, PartialEq)] -pub struct TypeParamParamSpec { +#[derive(Debug, PartialEq)] +pub struct TypeParamParamSpec<'ast> { pub range: TextRange, - pub name: Identifier, - pub default: Option>, + pub name: Identifier<'ast>, + pub default: Option>>, } -impl From for TypeParam { - fn from(payload: TypeParamParamSpec) -> Self { +impl<'ast> From> for TypeParam<'ast> { + fn from(payload: TypeParamParamSpec<'ast>) -> Self { TypeParam::ParamSpec(payload) } } /// See also [TypeVarTuple](https://docs.python.org/3/library/ast.html#ast.TypeVarTuple) -#[derive(Clone, Debug, PartialEq)] -pub struct TypeParamTypeVarTuple { +#[derive(Debug, PartialEq)] +pub struct TypeParamTypeVarTuple<'ast> { pub range: TextRange, - pub name: Identifier, - pub default: Option>, + pub name: Identifier<'ast>, + pub default: Option>>, } -impl From for TypeParam { - fn from(payload: TypeParamTypeVarTuple) -> Self { +impl<'ast> From> for TypeParam<'ast> { + fn from(payload: TypeParamTypeVarTuple<'ast>) -> Self { TypeParam::TypeVarTuple(payload) } } /// See also [decorator](https://docs.python.org/3/library/ast.html#ast.decorator) -#[derive(Clone, Debug, PartialEq)] -pub struct Decorator { +#[derive(Debug, PartialEq)] +pub struct Decorator<'ast> { pub range: TextRange, - pub expression: Expr, + pub expression: Expr<'ast>, } /// Enumeration of the two kinds of parameter #[derive(Debug, PartialEq, Clone, Copy)] -pub enum AnyParameterRef<'a> { +pub enum AnyParameterRef<'a, 'ast> { /// Variadic parameters cannot have default values, /// e.g. both `*args` and `**kwargs` in the following function: /// /// ```python /// def foo(*args, **kwargs): pass /// ``` - Variadic(&'a Parameter), + Variadic(&'a Parameter<'ast>), /// Non-variadic parameters can have default values, /// though they won't necessarily always have them: @@ -3241,18 +3246,18 @@ pub enum AnyParameterRef<'a> { /// ```python /// def bar(a=1, /, b=2, *, c=3): pass /// ``` - NonVariadic(&'a ParameterWithDefault), + NonVariadic(&'a ParameterWithDefault<'ast>), } -impl<'a> AnyParameterRef<'a> { - pub const fn as_parameter(self) -> &'a Parameter { +impl<'a, 'ast> AnyParameterRef<'a, 'ast> { + pub const fn as_parameter(self) -> &'a Parameter<'ast> { match self { Self::NonVariadic(param) => ¶m.parameter, Self::Variadic(param) => param, } } - pub const fn name(self) -> &'a Identifier { + pub const fn name(self) -> &'a Identifier<'ast> { &self.as_parameter().name } @@ -3260,11 +3265,11 @@ impl<'a> AnyParameterRef<'a> { matches!(self, Self::Variadic(_)) } - pub fn annotation(self) -> Option<&'a Expr> { + pub fn annotation(self) -> Option<&'a Expr<'ast>> { self.as_parameter().annotation.as_deref() } - pub fn default(self) -> Option<&'a Expr> { + pub fn default(self) -> Option<&'a Expr<'ast>> { match self { Self::NonVariadic(param) => param.default.as_deref(), Self::Variadic(_) => None, @@ -3272,7 +3277,7 @@ impl<'a> AnyParameterRef<'a> { } } -impl Ranged for AnyParameterRef<'_> { +impl Ranged for AnyParameterRef<'_, '_> { fn range(&self) -> TextRange { match self { Self::NonVariadic(param) => param.range, @@ -3291,22 +3296,22 @@ impl Ranged for AnyParameterRef<'_> { /// /// NOTE: This type differs from the original Python AST. See: [arguments](https://docs.python.org/3/library/ast.html#ast.arguments). -#[derive(Clone, Debug, PartialEq, Default)] -pub struct Parameters { +#[derive(Debug, PartialEq, Default)] +pub struct Parameters<'ast> { pub range: TextRange, - pub posonlyargs: Vec, - pub args: Vec, - pub vararg: Option>, - pub kwonlyargs: Vec, - pub kwarg: Option>, + pub posonlyargs: Vec>, + pub args: Vec>, + pub vararg: Option>>, + pub kwonlyargs: Vec>, + pub kwarg: Option>>, } -impl Parameters { +impl<'ast> Parameters<'ast> { /// Returns an iterator over all non-variadic parameters included in this [`Parameters`] node. /// /// The variadic parameters (`.vararg` and `.kwarg`) can never have default values; /// non-variadic parameters sometimes will. - pub fn iter_non_variadic_params(&self) -> impl Iterator { + pub fn iter_non_variadic_params(&self) -> impl Iterator> { self.posonlyargs .iter() .chain(&self.args) @@ -3314,13 +3319,13 @@ impl Parameters { } /// Returns the [`ParameterWithDefault`] with the given name, or `None` if no such [`ParameterWithDefault`] exists. - pub fn find(&self, name: &str) -> Option<&ParameterWithDefault> { + pub fn find(&self, name: &str) -> Option<&ParameterWithDefault<'ast>> { self.iter_non_variadic_params() .find(|arg| arg.parameter.name.as_str() == name) } /// Returns an iterator over all parameters included in this [`Parameters`] node. - pub fn iter(&self) -> ParametersIterator { + pub fn iter(&self) -> ParametersIterator<'_, 'ast> { ParametersIterator::new(self) } @@ -3364,16 +3369,16 @@ impl Parameters { } } -pub struct ParametersIterator<'a> { - posonlyargs: Iter<'a, ParameterWithDefault>, - args: Iter<'a, ParameterWithDefault>, - vararg: Option<&'a Parameter>, - kwonlyargs: Iter<'a, ParameterWithDefault>, - kwarg: Option<&'a Parameter>, +pub struct ParametersIterator<'a, 'ast> { + posonlyargs: Iter<'a, ParameterWithDefault<'ast>>, + args: Iter<'a, ParameterWithDefault<'ast>>, + vararg: Option<&'a Parameter<'ast>>, + kwonlyargs: Iter<'a, ParameterWithDefault<'ast>>, + kwarg: Option<&'a Parameter<'ast>>, } -impl<'a> ParametersIterator<'a> { - fn new(parameters: &'a Parameters) -> Self { +impl<'a, 'ast> ParametersIterator<'a, 'ast> { + fn new(parameters: &'a Parameters<'ast>) -> Self { let Parameters { range: _, posonlyargs, @@ -3392,8 +3397,8 @@ impl<'a> ParametersIterator<'a> { } } -impl<'a> Iterator for ParametersIterator<'a> { - type Item = AnyParameterRef<'a>; +impl<'a, 'ast> Iterator for ParametersIterator<'a, 'ast> { + type Item = AnyParameterRef<'a, 'ast>; fn next(&mut self) -> Option { let ParametersIterator { @@ -3454,7 +3459,7 @@ impl<'a> Iterator for ParametersIterator<'a> { } } -impl<'a> DoubleEndedIterator for ParametersIterator<'a> { +impl DoubleEndedIterator for ParametersIterator<'_, '_> { fn next_back(&mut self) -> Option { let ParametersIterator { posonlyargs, @@ -3480,15 +3485,15 @@ impl<'a> DoubleEndedIterator for ParametersIterator<'a> { } } -impl<'a> FusedIterator for ParametersIterator<'a> {} +impl FusedIterator for ParametersIterator<'_, '_> {} /// We rely on the same invariants outlined in the comment above `Parameters::len()` /// in order to implement `ExactSizeIterator` here -impl<'a> ExactSizeIterator for ParametersIterator<'a> {} +impl ExactSizeIterator for ParametersIterator<'_, '_> {} -impl<'a> IntoIterator for &'a Parameters { - type IntoIter = ParametersIterator<'a>; - type Item = AnyParameterRef<'a>; +impl<'a, 'ast> IntoIterator for &'a Parameters<'ast> { + type IntoIter = ParametersIterator<'a, 'ast>; + type Item = AnyParameterRef<'a, 'ast>; fn into_iter(self) -> Self::IntoIter { self.iter() } @@ -3499,11 +3504,11 @@ impl<'a> IntoIterator for &'a Parameters { /// /// NOTE: This type is different from original Python AST. -#[derive(Clone, Debug, PartialEq)] -pub struct ParameterWithDefault { +#[derive(Debug, PartialEq)] +pub struct ParameterWithDefault<'ast> { pub range: TextRange, - pub parameter: Parameter, - pub default: Option>, + pub parameter: Parameter<'ast>, + pub default: Option>>, } /// An AST node used to represent the arguments passed to a function call or class definition. @@ -3528,33 +3533,33 @@ pub struct ParameterWithDefault { /// as they represent the "explicitly specified base classes", while the keyword arguments are /// typically used for `metaclass`, with any additional arguments being passed to the `metaclass`. -#[derive(Clone, Debug, PartialEq)] -pub struct Arguments { +#[derive(Debug, PartialEq)] +pub struct Arguments<'ast> { pub range: TextRange, - pub args: Box<[Expr]>, - pub keywords: Box<[Keyword]>, + pub args: &'ast mut [Expr<'ast>], + pub keywords: &'ast mut [Keyword<'ast>], } /// An entry in the argument list of a function call. -#[derive(Clone, Debug, PartialEq)] -pub enum ArgOrKeyword<'a> { - Arg(&'a Expr), - Keyword(&'a Keyword), +#[derive(Debug, PartialEq)] +pub enum ArgOrKeyword<'a, 'ast> { + Arg(&'a Expr<'ast>), + Keyword(&'a Keyword<'ast>), } -impl<'a> From<&'a Expr> for ArgOrKeyword<'a> { - fn from(arg: &'a Expr) -> Self { +impl<'a, 'ast> From<&'a Expr<'ast>> for ArgOrKeyword<'a, 'ast> { + fn from(arg: &'a Expr<'ast>) -> Self { Self::Arg(arg) } } -impl<'a> From<&'a Keyword> for ArgOrKeyword<'a> { - fn from(keyword: &'a Keyword) -> Self { +impl<'a, 'ast> From<&'a Keyword<'ast>> for ArgOrKeyword<'a, 'ast> { + fn from(keyword: &'a Keyword<'ast>) -> Self { Self::Keyword(keyword) } } -impl Ranged for ArgOrKeyword<'_> { +impl Ranged for ArgOrKeyword<'_, '_> { fn range(&self) -> TextRange { match self { Self::Arg(arg) => arg.range(), @@ -3563,7 +3568,7 @@ impl Ranged for ArgOrKeyword<'_> { } } -impl Arguments { +impl<'ast> Arguments<'ast> { /// Return the number of positional and keyword arguments. pub fn len(&self) -> usize { self.args.len() + self.keywords.len() @@ -3575,7 +3580,7 @@ impl Arguments { } /// Return the [`Keyword`] with the given name, or `None` if no such [`Keyword`] exists. - pub fn find_keyword(&self, keyword_name: &str) -> Option<&Keyword> { + pub fn find_keyword(&self, keyword_name: &str) -> Option<&Keyword<'ast>> { self.keywords.iter().find(|keyword| { let Keyword { arg, .. } = keyword; arg.as_ref().is_some_and(|arg| arg == keyword_name) @@ -3583,7 +3588,7 @@ impl Arguments { } /// Return the positional argument at the given index, or `None` if no such argument exists. - pub fn find_positional(&self, position: usize) -> Option<&Expr> { + pub fn find_positional(&self, position: usize) -> Option<&Expr<'ast>> { self.args .iter() .take_while(|expr| !expr.is_starred_expr()) @@ -3593,7 +3598,7 @@ impl Arguments { /// Return the argument with the given name or at the given position, or `None` if no such /// argument exists. Used to retrieve arguments that can be provided _either_ as keyword or /// positional arguments. - pub fn find_argument(&self, name: &str, position: usize) -> Option<&Expr> { + pub fn find_argument(&self, name: &str, position: usize) -> Option<&Expr<'ast>> { self.find_keyword(name) .map(|keyword| &keyword.value) .or_else(|| self.find_positional(position)) @@ -3633,7 +3638,7 @@ impl Arguments { /// 2 /// {'4': 5} /// ``` - pub fn arguments_source_order(&self) -> impl Iterator> { + pub fn arguments_source_order(&self) -> impl Iterator> { let args = self.args.iter().map(ArgOrKeyword::Arg); let keywords = self.keywords.iter().map(ArgOrKeyword::Keyword); args.merge_by(keywords, |left, right| left.start() < right.start()) @@ -3649,14 +3654,14 @@ impl Arguments { /// The `TypeParams` node would span from the left to right brackets (inclusive), and contain /// the `T`, `U`, and `V` type parameters in the order they appear in the source code. -#[derive(Clone, Debug, PartialEq)] -pub struct TypeParams { +#[derive(Debug, PartialEq)] +pub struct TypeParams<'ast> { pub range: TextRange, - pub type_params: Vec, + pub type_params: Vec>, } -impl Deref for TypeParams { - type Target = [TypeParam]; +impl<'ast> Deref for TypeParams<'ast> { + type Target = [TypeParam<'ast>]; fn deref(&self) -> &Self::Target { &self.type_params @@ -3666,7 +3671,7 @@ impl Deref for TypeParams { /// A suite represents a [Vec] of [Stmt]. /// /// See: -pub type Suite = Vec; +pub type Suite<'ast> = Vec>; /// The kind of escape command as defined in [IPython Syntax] in the IPython codebase. /// @@ -3763,21 +3768,18 @@ impl IpyEscapeKind { /// ... /// ``` #[derive(Clone, Debug, PartialEq, Eq, Hash)] -pub struct Identifier { - pub id: Name, +pub struct Identifier<'ast> { + pub id: &'ast str, pub range: TextRange, } -impl Identifier { +impl<'ast> Identifier<'ast> { #[inline] - pub fn new(id: impl Into, range: TextRange) -> Self { - Self { - id: id.into(), - range, - } + pub fn new(id: &'ast str, range: TextRange) -> Self { + Self { id, range } } - pub fn id(&self) -> &Name { + pub fn id(&self) -> &'ast str { &self.id } @@ -3786,62 +3788,55 @@ impl Identifier { } } -impl Identifier { +impl<'ast> Identifier<'ast> { #[inline] - pub fn as_str(&self) -> &str { - self.id.as_str() + pub fn as_str(&self) -> &'ast str { + self.id } } -impl PartialEq for Identifier { +impl PartialEq for Identifier<'_> { #[inline] fn eq(&self, other: &str) -> bool { self.id == other } } -impl PartialEq for Identifier { +impl PartialEq for Identifier<'_> { #[inline] fn eq(&self, other: &String) -> bool { self.id == other } } -impl std::ops::Deref for Identifier { +impl std::ops::Deref for Identifier<'_> { type Target = str; #[inline] fn deref(&self) -> &Self::Target { - self.id.as_str() + self.id } } -impl AsRef for Identifier { +impl AsRef for Identifier<'_> { #[inline] fn as_ref(&self) -> &str { - self.id.as_str() + self.id } } -impl std::fmt::Display for Identifier { +impl std::fmt::Display for Identifier<'_> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { std::fmt::Display::fmt(&self.id, f) } } -impl From for Name { - #[inline] - fn from(identifier: Identifier) -> Name { - identifier.id - } -} - -impl Ranged for Identifier { +impl Ranged for Identifier<'_> { fn range(&self) -> TextRange { self.range } } -#[derive(Clone, Debug, PartialEq)] +#[derive(Copy, Clone, Debug, PartialEq)] pub enum Singleton { None, True, @@ -3858,17 +3853,17 @@ impl From for Singleton { } } -impl Ranged for crate::nodes::ModModule { +impl Ranged for crate::nodes::ModModule<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::nodes::ModExpression { +impl Ranged for crate::nodes::ModExpression<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::Mod { +impl Ranged for crate::Mod<'_> { fn range(&self) -> TextRange { match self { Self::Module(node) => node.range(), @@ -3877,112 +3872,112 @@ impl Ranged for crate::Mod { } } -impl Ranged for crate::nodes::StmtFunctionDef { +impl Ranged for crate::nodes::StmtFunctionDef<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::nodes::StmtClassDef { +impl Ranged for crate::nodes::StmtClassDef<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::nodes::StmtReturn { +impl Ranged for crate::nodes::StmtReturn<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::nodes::StmtDelete { +impl Ranged for crate::nodes::StmtDelete<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::nodes::StmtTypeAlias { +impl Ranged for crate::nodes::StmtTypeAlias<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::nodes::StmtAssign { +impl Ranged for crate::nodes::StmtAssign<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::nodes::StmtAugAssign { +impl Ranged for crate::nodes::StmtAugAssign<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::nodes::StmtAnnAssign { +impl Ranged for crate::nodes::StmtAnnAssign<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::nodes::StmtFor { +impl Ranged for crate::nodes::StmtFor<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::nodes::StmtWhile { +impl Ranged for crate::nodes::StmtWhile<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::nodes::StmtIf { +impl Ranged for crate::nodes::StmtIf<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::nodes::ElifElseClause { +impl Ranged for crate::nodes::ElifElseClause<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::nodes::StmtWith { +impl Ranged for crate::nodes::StmtWith<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::nodes::StmtMatch { +impl Ranged for crate::nodes::StmtMatch<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::nodes::StmtRaise { +impl Ranged for crate::nodes::StmtRaise<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::nodes::StmtTry { +impl Ranged for crate::nodes::StmtTry<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::nodes::StmtAssert { +impl Ranged for crate::nodes::StmtAssert<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::nodes::StmtImport { +impl Ranged for crate::nodes::StmtImport<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::nodes::StmtImportFrom { +impl Ranged for crate::nodes::StmtImportFrom<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::nodes::StmtGlobal { +impl Ranged for crate::nodes::StmtGlobal<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::nodes::StmtNonlocal { +impl Ranged for crate::nodes::StmtNonlocal<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::nodes::StmtExpr { +impl Ranged for crate::nodes::StmtExpr<'_> { fn range(&self) -> TextRange { self.range } @@ -4002,12 +3997,12 @@ impl Ranged for crate::nodes::StmtContinue { self.range } } -impl Ranged for crate::nodes::StmtIpyEscapeCommand { +impl Ranged for crate::nodes::StmtIpyEscapeCommand<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::Stmt { +impl Ranged for crate::Stmt<'_> { fn range(&self) -> TextRange { match self { Self::FunctionDef(node) => node.range(), @@ -4039,137 +4034,137 @@ impl Ranged for crate::Stmt { } } -impl Ranged for crate::nodes::ExprBoolOp { +impl Ranged for crate::nodes::ExprBoolOp<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::nodes::ExprNamed { +impl Ranged for crate::nodes::ExprNamed<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::nodes::ExprBinOp { +impl Ranged for crate::nodes::ExprBinOp<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::nodes::ExprUnaryOp { +impl Ranged for crate::nodes::ExprUnaryOp<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::nodes::ExprLambda { +impl Ranged for crate::nodes::ExprLambda<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::nodes::ExprIf { +impl Ranged for crate::nodes::ExprIf<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::nodes::ExprDict { +impl Ranged for crate::nodes::ExprDict<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::nodes::ExprSet { +impl Ranged for crate::nodes::ExprSet<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::nodes::ExprListComp { +impl Ranged for crate::nodes::ExprListComp<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::nodes::ExprSetComp { +impl Ranged for crate::nodes::ExprSetComp<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::nodes::ExprDictComp { +impl Ranged for crate::nodes::ExprDictComp<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::nodes::ExprGenerator { +impl Ranged for crate::nodes::ExprGenerator<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::nodes::ExprAwait { +impl Ranged for crate::nodes::ExprAwait<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::nodes::ExprYield { +impl Ranged for crate::nodes::ExprYield<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::nodes::ExprYieldFrom { +impl Ranged for crate::nodes::ExprYieldFrom<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::nodes::ExprCompare { +impl Ranged for crate::nodes::ExprCompare<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::nodes::ExprCall { +impl Ranged for crate::nodes::ExprCall<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::nodes::ExprFString { +impl Ranged for crate::nodes::ExprFString<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::nodes::ExprAttribute { +impl Ranged for crate::nodes::ExprAttribute<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::nodes::ExprSubscript { +impl Ranged for crate::nodes::ExprSubscript<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::nodes::ExprStarred { +impl Ranged for crate::nodes::ExprStarred<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::nodes::ExprName { +impl Ranged for crate::nodes::ExprName<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::nodes::ExprList { +impl Ranged for crate::nodes::ExprList<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::nodes::ExprTuple { +impl Ranged for crate::nodes::ExprTuple<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::nodes::ExprSlice { +impl Ranged for crate::nodes::ExprSlice<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::nodes::ExprIpyEscapeCommand { +impl Ranged for crate::nodes::ExprIpyEscapeCommand<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::Expr { +impl Ranged for crate::Expr<'_> { fn range(&self) -> TextRange { match self { Self::BoolOp(node) => node.range(), @@ -4207,49 +4202,49 @@ impl Ranged for crate::Expr { } } } -impl Ranged for crate::nodes::Comprehension { +impl Ranged for crate::nodes::Comprehension<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::nodes::ExceptHandlerExceptHandler { +impl Ranged for crate::nodes::ExceptHandlerExceptHandler<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::ExceptHandler { +impl Ranged for crate::ExceptHandler<'_> { fn range(&self) -> TextRange { match self { Self::ExceptHandler(node) => node.range(), } } } -impl Ranged for crate::nodes::Parameter { +impl Ranged for crate::nodes::Parameter<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::nodes::Keyword { +impl Ranged for crate::nodes::Keyword<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::nodes::Alias { +impl Ranged for crate::nodes::Alias<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::nodes::WithItem { +impl Ranged for crate::nodes::WithItem<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::nodes::MatchCase { +impl Ranged for crate::nodes::MatchCase<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::nodes::PatternMatchValue { +impl Ranged for crate::nodes::PatternMatchValue<'_> { fn range(&self) -> TextRange { self.range } @@ -4259,37 +4254,37 @@ impl Ranged for crate::nodes::PatternMatchSingleton { self.range } } -impl Ranged for crate::nodes::PatternMatchSequence { +impl Ranged for crate::nodes::PatternMatchSequence<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::nodes::PatternMatchMapping { +impl Ranged for crate::nodes::PatternMatchMapping<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::nodes::PatternMatchClass { +impl Ranged for crate::nodes::PatternMatchClass<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::nodes::PatternMatchStar { +impl Ranged for crate::nodes::PatternMatchStar<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::nodes::PatternMatchAs { +impl Ranged for crate::nodes::PatternMatchAs<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::nodes::PatternMatchOr { +impl Ranged for crate::nodes::PatternMatchOr<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::Pattern { +impl Ranged for crate::Pattern<'_> { fn range(&self) -> TextRange { match self { Self::MatchValue(node) => node.range(), @@ -4303,38 +4298,38 @@ impl Ranged for crate::Pattern { } } } -impl Ranged for crate::nodes::PatternArguments { +impl Ranged for crate::nodes::PatternArguments<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::nodes::PatternKeyword { +impl Ranged for crate::nodes::PatternKeyword<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::nodes::TypeParams { +impl Ranged for crate::nodes::TypeParams<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::nodes::TypeParamTypeVar { +impl Ranged for crate::nodes::TypeParamTypeVar<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::nodes::TypeParamTypeVarTuple { +impl Ranged for crate::nodes::TypeParamTypeVarTuple<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::nodes::TypeParamParamSpec { +impl Ranged for crate::nodes::TypeParamParamSpec<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::TypeParam { +impl Ranged for crate::TypeParam<'_> { fn range(&self) -> TextRange { match self { Self::TypeVar(node) => node.range(), @@ -4343,22 +4338,22 @@ impl Ranged for crate::TypeParam { } } } -impl Ranged for crate::nodes::Decorator { +impl Ranged for crate::nodes::Decorator<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::nodes::Arguments { +impl Ranged for crate::nodes::Arguments<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::nodes::Parameters { +impl Ranged for crate::nodes::Parameters<'_> { fn range(&self) -> TextRange { self.range } } -impl Ranged for crate::nodes::ParameterWithDefault { +impl Ranged for crate::nodes::ParameterWithDefault<'_> { fn range(&self) -> TextRange { self.range } diff --git a/crates/ruff_python_ast/src/parenthesize.rs b/crates/ruff_python_ast/src/parenthesize.rs index 36ec307d73..78ce5e0ccf 100644 --- a/crates/ruff_python_ast/src/parenthesize.rs +++ b/crates/ruff_python_ast/src/parenthesize.rs @@ -11,8 +11,8 @@ use crate::ExpressionRef; /// Note that without a parent the range can be inaccurate, e.g. `f(a)` we falsely return a set of /// parentheses around `a` even if the parentheses actually belong to `f`. That is why you should /// generally prefer [`parenthesized_range`]. -pub fn parentheses_iterator<'a>( - expr: ExpressionRef<'a>, +pub fn parentheses_iterator<'a, 'ast>( + expr: ExpressionRef<'a, 'ast>, parent: Option, comment_ranges: &'a CommentRanges, source: &'a str, diff --git a/crates/ruff_python_ast/src/statement_visitor.rs b/crates/ruff_python_ast/src/statement_visitor.rs index 7ab3ebe06c..de95f239f0 100644 --- a/crates/ruff_python_ast/src/statement_visitor.rs +++ b/crates/ruff_python_ast/src/statement_visitor.rs @@ -3,31 +3,37 @@ use crate::{self as ast, ElifElseClause, ExceptHandler, MatchCase, Stmt}; /// A trait for AST visitors that only need to visit statements. -pub trait StatementVisitor<'a> { - fn visit_body(&mut self, body: &'a [Stmt]) { +pub trait StatementVisitor<'a, 'ast> { + fn visit_body(&mut self, body: &'a [Stmt<'ast>]) { walk_body(self, body); } - fn visit_stmt(&mut self, stmt: &'a Stmt) { + fn visit_stmt(&mut self, stmt: &'a Stmt<'ast>) { walk_stmt(self, stmt); } - fn visit_except_handler(&mut self, except_handler: &'a ExceptHandler) { + fn visit_except_handler(&mut self, except_handler: &'a ExceptHandler<'ast>) { walk_except_handler(self, except_handler); } - fn visit_elif_else_clause(&mut self, elif_else_clause: &'a ElifElseClause) { + fn visit_elif_else_clause(&mut self, elif_else_clause: &'a ElifElseClause<'ast>) { walk_elif_else_clause(self, elif_else_clause); } - fn visit_match_case(&mut self, match_case: &'a MatchCase) { + fn visit_match_case(&mut self, match_case: &'a MatchCase<'ast>) { walk_match_case(self, match_case); } } -pub fn walk_body<'a, V: StatementVisitor<'a> + ?Sized>(visitor: &mut V, body: &'a [Stmt]) { +pub fn walk_body<'a, 'ast, V: StatementVisitor<'a, 'ast> + ?Sized>( + visitor: &mut V, + body: &'a [Stmt<'ast>], +) { for stmt in body { visitor.visit_stmt(stmt); } } -pub fn walk_stmt<'a, V: StatementVisitor<'a> + ?Sized>(visitor: &mut V, stmt: &'a Stmt) { +pub fn walk_stmt<'a, 'ast, V: StatementVisitor<'a, 'ast> + ?Sized>( + visitor: &mut V, + stmt: &'a Stmt<'ast>, +) { match stmt { Stmt::FunctionDef(ast::StmtFunctionDef { body, .. }) => { visitor.visit_body(body); @@ -79,9 +85,9 @@ pub fn walk_stmt<'a, V: StatementVisitor<'a> + ?Sized>(visitor: &mut V, stmt: &' } } -pub fn walk_except_handler<'a, V: StatementVisitor<'a> + ?Sized>( +pub fn walk_except_handler<'a, 'ast, V: StatementVisitor<'a, 'ast> + ?Sized>( visitor: &mut V, - except_handler: &'a ExceptHandler, + except_handler: &'a ExceptHandler<'ast>, ) { match except_handler { ExceptHandler::ExceptHandler(ast::ExceptHandlerExceptHandler { body, .. }) => { @@ -90,16 +96,16 @@ pub fn walk_except_handler<'a, V: StatementVisitor<'a> + ?Sized>( } } -pub fn walk_elif_else_clause<'a, V: StatementVisitor<'a> + ?Sized>( +pub fn walk_elif_else_clause<'a, 'ast, V: StatementVisitor<'a, 'ast> + ?Sized>( visitor: &mut V, - elif_else_clause: &'a ElifElseClause, + elif_else_clause: &'a ElifElseClause<'ast>, ) { visitor.visit_body(&elif_else_clause.body); } -pub fn walk_match_case<'a, V: StatementVisitor<'a> + ?Sized>( +pub fn walk_match_case<'a, 'ast, V: StatementVisitor<'a, 'ast> + ?Sized>( visitor: &mut V, - match_case: &'a MatchCase, + match_case: &'a MatchCase<'ast>, ) { visitor.visit_body(&match_case.body); } diff --git a/crates/ruff_python_ast/src/stmt_if.rs b/crates/ruff_python_ast/src/stmt_if.rs index 08a69fd458..e25e7b235b 100644 --- a/crates/ruff_python_ast/src/stmt_if.rs +++ b/crates/ruff_python_ast/src/stmt_if.rs @@ -20,20 +20,22 @@ pub enum BranchKind { } #[derive(Debug)] -pub struct IfElifBranch<'a> { +pub struct IfElifBranch<'a, 'ast> { pub kind: BranchKind, - pub test: &'a Expr, - pub body: &'a [Stmt], + pub test: &'a Expr<'ast>, + pub body: &'a [Stmt<'ast>], range: TextRange, } -impl Ranged for IfElifBranch<'_> { +impl Ranged for IfElifBranch<'_, '_> { fn range(&self) -> TextRange { self.range } } -pub fn if_elif_branches(stmt_if: &StmtIf) -> impl Iterator { +pub fn if_elif_branches<'a, 'ast>( + stmt_if: &'a StmtIf<'ast>, +) -> impl Iterator> { iter::once(IfElifBranch { kind: BranchKind::If, test: stmt_if.test.as_ref(), diff --git a/crates/ruff_python_ast/src/traversal.rs b/crates/ruff_python_ast/src/traversal.rs index 1e050cfa94..7dc719e534 100644 --- a/crates/ruff_python_ast/src/traversal.rs +++ b/crates/ruff_python_ast/src/traversal.rs @@ -2,7 +2,7 @@ use crate::{self as ast, ExceptHandler, Stmt, Suite}; /// Given a [`Stmt`] and its parent, return the [`Suite`] that contains the [`Stmt`]. -pub fn suite<'a>(stmt: &'a Stmt, parent: &'a Stmt) -> Option<&'a Suite> { +pub fn suite<'a, 'ast>(stmt: &'a Stmt<'ast>, parent: &'a Stmt<'ast>) -> Option<&'a Suite<'ast>> { // TODO: refactor this to work without a parent, ie when `stmt` is at the top level match parent { Stmt::FunctionDef(ast::StmtFunctionDef { body, .. }) => Some(body), @@ -70,7 +70,10 @@ pub fn suite<'a>(stmt: &'a Stmt, parent: &'a Stmt) -> Option<&'a Suite> { } /// Given a [`Stmt`] and its containing [`Suite`], return the next [`Stmt`] in the [`Suite`]. -pub fn next_sibling<'a>(stmt: &'a Stmt, suite: &'a Suite) -> Option<&'a Stmt> { +pub fn next_sibling<'a, 'ast>( + stmt: &'a Stmt<'ast>, + suite: &'a Suite<'ast>, +) -> Option<&'a Stmt<'ast>> { let mut iter = suite.iter(); while let Some(sibling) = iter.next() { if sibling == stmt { diff --git a/crates/ruff_python_ast/src/types.rs b/crates/ruff_python_ast/src/types.rs index 32732ed6cb..ef391211ab 100644 --- a/crates/ruff_python_ast/src/types.rs +++ b/crates/ruff_python_ast/src/types.rs @@ -1,7 +1,7 @@ use crate::{Expr, Stmt}; #[derive(Clone)] -pub enum Node<'a> { - Stmt(&'a Stmt), - Expr(&'a Expr), +pub enum Node<'a, 'ast> { + Stmt(&'a Stmt<'ast>), + Expr(&'a Expr<'ast>), } diff --git a/crates/ruff_python_ast/src/visitor.rs b/crates/ruff_python_ast/src/visitor.rs index 687a4bcf14..0cf70305f7 100644 --- a/crates/ruff_python_ast/src/visitor.rs +++ b/crates/ruff_python_ast/src/visitor.rs @@ -20,17 +20,17 @@ use crate::{ /// in source-order rather than evaluation order. /// /// Use the [`Transformer`](transformer::Transformer) if you want to modify the nodes. -pub trait Visitor<'a> { - fn visit_stmt(&mut self, stmt: &'a Stmt) { +pub trait Visitor<'a, 'ast> { + fn visit_stmt(&mut self, stmt: &'a Stmt<'ast>) { walk_stmt(self, stmt); } - fn visit_annotation(&mut self, expr: &'a Expr) { + fn visit_annotation(&mut self, expr: &'a Expr<'ast>) { walk_annotation(self, expr); } - fn visit_decorator(&mut self, decorator: &'a Decorator) { + fn visit_decorator(&mut self, decorator: &'a Decorator<'ast>) { walk_decorator(self, decorator); } - fn visit_expr(&mut self, expr: &'a Expr) { + fn visit_expr(&mut self, expr: &'a Expr<'ast>) { walk_expr(self, expr); } fn visit_expr_context(&mut self, expr_context: &'a ExprContext) { @@ -48,77 +48,77 @@ pub trait Visitor<'a> { fn visit_cmp_op(&mut self, cmp_op: &'a CmpOp) { walk_cmp_op(self, cmp_op); } - fn visit_comprehension(&mut self, comprehension: &'a Comprehension) { + fn visit_comprehension(&mut self, comprehension: &'a Comprehension<'ast>) { walk_comprehension(self, comprehension); } - fn visit_except_handler(&mut self, except_handler: &'a ExceptHandler) { + fn visit_except_handler(&mut self, except_handler: &'a ExceptHandler<'ast>) { walk_except_handler(self, except_handler); } - fn visit_arguments(&mut self, arguments: &'a Arguments) { + fn visit_arguments(&mut self, arguments: &'a Arguments<'ast>) { walk_arguments(self, arguments); } - fn visit_parameters(&mut self, parameters: &'a Parameters) { + fn visit_parameters(&mut self, parameters: &'a Parameters<'ast>) { walk_parameters(self, parameters); } - fn visit_parameter(&mut self, parameter: &'a Parameter) { + fn visit_parameter(&mut self, parameter: &'a Parameter<'ast>) { walk_parameter(self, parameter); } - fn visit_keyword(&mut self, keyword: &'a Keyword) { + fn visit_keyword(&mut self, keyword: &'a Keyword<'ast>) { walk_keyword(self, keyword); } fn visit_alias(&mut self, alias: &'a Alias) { walk_alias(self, alias); } - fn visit_with_item(&mut self, with_item: &'a WithItem) { + fn visit_with_item(&mut self, with_item: &'a WithItem<'ast>) { walk_with_item(self, with_item); } - fn visit_type_params(&mut self, type_params: &'a TypeParams) { + fn visit_type_params(&mut self, type_params: &'a TypeParams<'ast>) { walk_type_params(self, type_params); } - fn visit_type_param(&mut self, type_param: &'a TypeParam) { + fn visit_type_param(&mut self, type_param: &'a TypeParam<'ast>) { walk_type_param(self, type_param); } - fn visit_match_case(&mut self, match_case: &'a MatchCase) { + fn visit_match_case(&mut self, match_case: &'a MatchCase<'ast>) { walk_match_case(self, match_case); } - fn visit_pattern(&mut self, pattern: &'a Pattern) { + fn visit_pattern(&mut self, pattern: &'a Pattern<'ast>) { walk_pattern(self, pattern); } - fn visit_pattern_arguments(&mut self, pattern_arguments: &'a PatternArguments) { + fn visit_pattern_arguments(&mut self, pattern_arguments: &'a PatternArguments<'ast>) { walk_pattern_arguments(self, pattern_arguments); } - fn visit_pattern_keyword(&mut self, pattern_keyword: &'a PatternKeyword) { + fn visit_pattern_keyword(&mut self, pattern_keyword: &'a PatternKeyword<'ast>) { walk_pattern_keyword(self, pattern_keyword); } - fn visit_body(&mut self, body: &'a [Stmt]) { + fn visit_body(&mut self, body: &'a [Stmt<'ast>]) { walk_body(self, body); } - fn visit_elif_else_clause(&mut self, elif_else_clause: &'a ElifElseClause) { + fn visit_elif_else_clause(&mut self, elif_else_clause: &'a ElifElseClause<'ast>) { walk_elif_else_clause(self, elif_else_clause); } - fn visit_f_string(&mut self, f_string: &'a FString) { + fn visit_f_string(&mut self, f_string: &'a FString<'ast>) { walk_f_string(self, f_string); } - fn visit_f_string_element(&mut self, f_string_element: &'a FStringElement) { + fn visit_f_string_element(&mut self, f_string_element: &'a FStringElement<'ast>) { walk_f_string_element(self, f_string_element); } - fn visit_string_literal(&mut self, string_literal: &'a StringLiteral) { + fn visit_string_literal(&mut self, string_literal: &'a StringLiteral<'ast>) { walk_string_literal(self, string_literal); } - fn visit_bytes_literal(&mut self, bytes_literal: &'a BytesLiteral) { + fn visit_bytes_literal(&mut self, bytes_literal: &'a BytesLiteral<'ast>) { walk_bytes_literal(self, bytes_literal); } } -pub fn walk_body<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, body: &'a [Stmt]) { +pub fn walk_body<'a, 'ast, V: Visitor<'a, 'ast> + ?Sized>(visitor: &mut V, body: &'a [Stmt<'ast>]) { for stmt in body { visitor.visit_stmt(stmt); } } -pub fn walk_elif_else_clause<'a, V: Visitor<'a> + ?Sized>( +pub fn walk_elif_else_clause<'a, 'ast, V: Visitor<'a, 'ast> + ?Sized>( visitor: &mut V, - elif_else_clause: &'a ElifElseClause, + elif_else_clause: &'a ElifElseClause<'ast>, ) { if let Some(test) = &elif_else_clause.test { visitor.visit_expr(test); @@ -126,7 +126,7 @@ pub fn walk_elif_else_clause<'a, V: Visitor<'a> + ?Sized>( visitor.visit_body(&elif_else_clause.body); } -pub fn walk_stmt<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, stmt: &'a Stmt) { +pub fn walk_stmt<'a, 'ast, V: Visitor<'a, 'ast> + ?Sized>(visitor: &mut V, stmt: &'a Stmt<'ast>) { match stmt { Stmt::FunctionDef(ast::StmtFunctionDef { parameters, @@ -323,15 +323,21 @@ pub fn walk_stmt<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, stmt: &'a Stmt) { } } -pub fn walk_annotation<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, expr: &'a Expr) { +pub fn walk_annotation<'a, 'ast, V: Visitor<'a, 'ast> + ?Sized>( + visitor: &mut V, + expr: &'a Expr<'ast>, +) { visitor.visit_expr(expr); } -pub fn walk_decorator<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, decorator: &'a Decorator) { +pub fn walk_decorator<'a, 'ast, V: Visitor<'a, 'ast> + ?Sized>( + visitor: &mut V, + decorator: &'a Decorator<'ast>, +) { visitor.visit_expr(&decorator.expression); } -pub fn walk_expr<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, expr: &'a Expr) { +pub fn walk_expr<'a, 'ast, V: Visitor<'a, 'ast> + ?Sized>(visitor: &mut V, expr: &'a Expr<'ast>) { match expr { Expr::BoolOp(ast::ExprBoolOp { op, @@ -564,9 +570,9 @@ pub fn walk_expr<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, expr: &'a Expr) { } } -pub fn walk_comprehension<'a, V: Visitor<'a> + ?Sized>( +pub fn walk_comprehension<'a, 'ast, V: Visitor<'a, 'ast> + ?Sized>( visitor: &mut V, - comprehension: &'a Comprehension, + comprehension: &'a Comprehension<'ast>, ) { visitor.visit_expr(&comprehension.iter); visitor.visit_expr(&comprehension.target); @@ -575,9 +581,9 @@ pub fn walk_comprehension<'a, V: Visitor<'a> + ?Sized>( } } -pub fn walk_except_handler<'a, V: Visitor<'a> + ?Sized>( +pub fn walk_except_handler<'a, 'ast, V: Visitor<'a, 'ast> + ?Sized>( visitor: &mut V, - except_handler: &'a ExceptHandler, + except_handler: &'a ExceptHandler<'ast>, ) { match except_handler { ExceptHandler::ExceptHandler(ast::ExceptHandlerExceptHandler { type_, body, .. }) => { @@ -589,7 +595,10 @@ pub fn walk_except_handler<'a, V: Visitor<'a> + ?Sized>( } } -pub fn walk_arguments<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, arguments: &'a Arguments) { +pub fn walk_arguments<'a, 'ast, V: Visitor<'a, 'ast> + ?Sized>( + visitor: &mut V, + arguments: &'a Arguments<'ast>, +) { // Note that the there might be keywords before the last arg, e.g. in // f(*args, a=2, *args2, **kwargs)`, but we follow Python in evaluating first `args` and then // `keywords`. See also [Arguments::arguments_source_order`]. @@ -601,7 +610,10 @@ pub fn walk_arguments<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, arguments: & } } -pub fn walk_parameters<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, parameters: &'a Parameters) { +pub fn walk_parameters<'a, 'ast, V: Visitor<'a, 'ast> + ?Sized>( + visitor: &mut V, + parameters: &'a Parameters<'ast>, +) { // Defaults are evaluated before annotations. for default in parameters .iter_non_variadic_params() @@ -615,30 +627,45 @@ pub fn walk_parameters<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, parameters: } } -pub fn walk_parameter<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, parameter: &'a Parameter) { +pub fn walk_parameter<'a, 'ast, V: Visitor<'a, 'ast> + ?Sized>( + visitor: &mut V, + parameter: &'a Parameter<'ast>, +) { if let Some(expr) = ¶meter.annotation { visitor.visit_annotation(expr); } } -pub fn walk_keyword<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, keyword: &'a Keyword) { +pub fn walk_keyword<'a, 'ast, V: Visitor<'a, 'ast> + ?Sized>( + visitor: &mut V, + keyword: &'a Keyword<'ast>, +) { visitor.visit_expr(&keyword.value); } -pub fn walk_with_item<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, with_item: &'a WithItem) { +pub fn walk_with_item<'a, 'ast, V: Visitor<'a, 'ast> + ?Sized>( + visitor: &mut V, + with_item: &'a WithItem<'ast>, +) { visitor.visit_expr(&with_item.context_expr); if let Some(expr) = &with_item.optional_vars { visitor.visit_expr(expr); } } -pub fn walk_type_params<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, type_params: &'a TypeParams) { +pub fn walk_type_params<'a, 'ast, V: Visitor<'a, 'ast> + ?Sized>( + visitor: &mut V, + type_params: &'a TypeParams<'ast>, +) { for type_param in &type_params.type_params { visitor.visit_type_param(type_param); } } -pub fn walk_type_param<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, type_param: &'a TypeParam) { +pub fn walk_type_param<'a, 'ast, V: Visitor<'a, 'ast> + ?Sized>( + visitor: &mut V, + type_param: &'a TypeParam<'ast>, +) { match type_param { TypeParam::TypeVar(TypeParamTypeVar { bound, @@ -674,7 +701,10 @@ pub fn walk_type_param<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, type_param: } } -pub fn walk_match_case<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, match_case: &'a MatchCase) { +pub fn walk_match_case<'a, 'ast, V: Visitor<'a, 'ast> + ?Sized>( + visitor: &mut V, + match_case: &'a MatchCase<'ast>, +) { visitor.visit_pattern(&match_case.pattern); if let Some(expr) = &match_case.guard { visitor.visit_expr(expr); @@ -682,7 +712,10 @@ pub fn walk_match_case<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, match_case: visitor.visit_body(&match_case.body); } -pub fn walk_pattern<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, pattern: &'a Pattern) { +pub fn walk_pattern<'a, 'ast, V: Visitor<'a, 'ast> + ?Sized>( + visitor: &mut V, + pattern: &'a Pattern<'ast>, +) { match pattern { Pattern::MatchValue(ast::PatternMatchValue { value, .. }) => { visitor.visit_expr(value); @@ -719,9 +752,9 @@ pub fn walk_pattern<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, pattern: &'a P } } -pub fn walk_pattern_arguments<'a, V: Visitor<'a> + ?Sized>( +pub fn walk_pattern_arguments<'a, 'ast, V: Visitor<'a, 'ast> + ?Sized>( visitor: &mut V, - pattern_arguments: &'a PatternArguments, + pattern_arguments: &'a PatternArguments<'ast>, ) { for pattern in &pattern_arguments.patterns { visitor.visit_pattern(pattern); @@ -731,22 +764,25 @@ pub fn walk_pattern_arguments<'a, V: Visitor<'a> + ?Sized>( } } -pub fn walk_pattern_keyword<'a, V: Visitor<'a> + ?Sized>( +pub fn walk_pattern_keyword<'a, 'ast, V: Visitor<'a, 'ast> + ?Sized>( visitor: &mut V, - pattern_keyword: &'a PatternKeyword, + pattern_keyword: &'a PatternKeyword<'ast>, ) { visitor.visit_pattern(&pattern_keyword.pattern); } -pub fn walk_f_string<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, f_string: &'a FString) { +pub fn walk_f_string<'a, 'ast, V: Visitor<'a, 'ast> + ?Sized>( + visitor: &mut V, + f_string: &'a FString<'ast>, +) { for f_string_element in &f_string.elements { visitor.visit_f_string_element(f_string_element); } } -pub fn walk_f_string_element<'a, V: Visitor<'a> + ?Sized>( +pub fn walk_f_string_element<'a, 'ast, V: Visitor<'a, 'ast> + ?Sized>( visitor: &mut V, - f_string_element: &'a FStringElement, + f_string_element: &'a FStringElement<'ast>, ) { if let ast::FStringElement::Expression(ast::FStringExpressionElement { expression, @@ -763,30 +799,38 @@ pub fn walk_f_string_element<'a, V: Visitor<'a> + ?Sized>( } } -pub fn walk_expr_context<'a, V: Visitor<'a> + ?Sized>( +pub fn walk_expr_context<'a, 'ast, V: Visitor<'a, 'ast> + ?Sized>( _visitor: &V, _expr_context: &'a ExprContext, ) { } -pub fn walk_bool_op<'a, V: Visitor<'a> + ?Sized>(_visitor: &V, _bool_op: &'a BoolOp) {} +pub fn walk_bool_op<'a, 'ast, V: Visitor<'a, 'ast> + ?Sized>(_visitor: &V, _bool_op: &'a BoolOp) {} -pub fn walk_operator<'a, V: Visitor<'a> + ?Sized>(_visitor: &V, _operator: &'a Operator) {} - -pub fn walk_unary_op<'a, V: Visitor<'a> + ?Sized>(_visitor: &V, _unary_op: &'a UnaryOp) {} - -pub fn walk_cmp_op<'a, V: Visitor<'a> + ?Sized>(_visitor: &V, _cmp_op: &'a CmpOp) {} - -pub fn walk_alias<'a, V: Visitor<'a> + ?Sized>(_visitor: &V, _alias: &'a Alias) {} - -pub fn walk_string_literal<'a, V: Visitor<'a> + ?Sized>( +pub fn walk_operator<'a, 'ast, V: Visitor<'a, 'ast> + ?Sized>( _visitor: &V, - _string_literal: &'a StringLiteral, + _operator: &'a Operator, ) { } -pub fn walk_bytes_literal<'a, V: Visitor<'a> + ?Sized>( +pub fn walk_unary_op<'a, 'ast, V: Visitor<'a, 'ast> + ?Sized>( _visitor: &V, - _bytes_literal: &'a BytesLiteral, + _unary_op: &'a UnaryOp, +) { +} + +pub fn walk_cmp_op<'a, 'ast, V: Visitor<'a, 'ast> + ?Sized>(_visitor: &V, _cmp_op: &'a CmpOp) {} + +pub fn walk_alias<'a, 'ast, V: Visitor<'a, 'ast> + ?Sized>(_visitor: &V, _alias: &'a Alias) {} + +pub fn walk_string_literal<'a, 'ast, V: Visitor<'a, 'ast> + ?Sized>( + _visitor: &V, + _string_literal: &'a StringLiteral<'ast>, +) { +} + +pub fn walk_bytes_literal<'a, 'ast, V: Visitor<'a, 'ast> + ?Sized>( + _visitor: &V, + _bytes_literal: &'a BytesLiteral<'ast>, ) { } diff --git a/crates/ruff_python_ast/src/visitor/source_order.rs b/crates/ruff_python_ast/src/visitor/source_order.rs index 894835b965..6f98fa73cc 100644 --- a/crates/ruff_python_ast/src/visitor/source_order.rs +++ b/crates/ruff_python_ast/src/visitor/source_order.rs @@ -10,37 +10,37 @@ use crate::{AnyNodeRef, AstNode}; /// /// If you need a visitor that visits the nodes in the order they're evaluated at runtime, /// use [`Visitor`](super::Visitor) instead. -pub trait SourceOrderVisitor<'a> { +pub trait SourceOrderVisitor<'a, 'ast> { #[inline] - fn enter_node(&mut self, _node: AnyNodeRef<'a>) -> TraversalSignal { + fn enter_node(&mut self, _node: AnyNodeRef<'a, 'ast>) -> TraversalSignal { TraversalSignal::Traverse } #[inline(always)] - fn leave_node(&mut self, _node: AnyNodeRef<'a>) {} + fn leave_node(&mut self, _node: AnyNodeRef<'a, 'ast>) {} #[inline] - fn visit_mod(&mut self, module: &'a Mod) { + fn visit_mod(&mut self, module: &'a Mod<'ast>) { walk_module(self, module); } #[inline] - fn visit_stmt(&mut self, stmt: &'a Stmt) { + fn visit_stmt(&mut self, stmt: &'a Stmt<'ast>) { walk_stmt(self, stmt); } #[inline] - fn visit_annotation(&mut self, expr: &'a Expr) { + fn visit_annotation(&mut self, expr: &'a Expr<'ast>) { walk_annotation(self, expr); } #[inline] - fn visit_expr(&mut self, expr: &'a Expr) { + fn visit_expr(&mut self, expr: &'a Expr<'ast>) { walk_expr(self, expr); } #[inline] - fn visit_decorator(&mut self, decorator: &'a Decorator) { + fn visit_decorator(&mut self, decorator: &'a Decorator<'ast>) { walk_decorator(self, decorator); } @@ -68,113 +68,116 @@ pub trait SourceOrderVisitor<'a> { } #[inline] - fn visit_comprehension(&mut self, comprehension: &'a Comprehension) { + fn visit_comprehension(&mut self, comprehension: &'a Comprehension<'ast>) { walk_comprehension(self, comprehension); } #[inline] - fn visit_except_handler(&mut self, except_handler: &'a ExceptHandler) { + fn visit_except_handler(&mut self, except_handler: &'a ExceptHandler<'ast>) { walk_except_handler(self, except_handler); } #[inline] - fn visit_arguments(&mut self, arguments: &'a Arguments) { + fn visit_arguments(&mut self, arguments: &'a Arguments<'ast>) { walk_arguments(self, arguments); } #[inline] - fn visit_parameters(&mut self, parameters: &'a Parameters) { + fn visit_parameters(&mut self, parameters: &'a Parameters<'ast>) { walk_parameters(self, parameters); } #[inline] - fn visit_parameter(&mut self, arg: &'a Parameter) { + fn visit_parameter(&mut self, arg: &'a Parameter<'ast>) { walk_parameter(self, arg); } - fn visit_parameter_with_default(&mut self, parameter_with_default: &'a ParameterWithDefault) { + fn visit_parameter_with_default( + &mut self, + parameter_with_default: &'a ParameterWithDefault<'ast>, + ) { walk_parameter_with_default(self, parameter_with_default); } #[inline] - fn visit_keyword(&mut self, keyword: &'a Keyword) { + fn visit_keyword(&mut self, keyword: &'a Keyword<'ast>) { walk_keyword(self, keyword); } #[inline] - fn visit_alias(&mut self, alias: &'a Alias) { + fn visit_alias(&mut self, alias: &'a Alias<'ast>) { walk_alias(self, alias); } #[inline] - fn visit_with_item(&mut self, with_item: &'a WithItem) { + fn visit_with_item(&mut self, with_item: &'a WithItem<'ast>) { walk_with_item(self, with_item); } #[inline] - fn visit_type_params(&mut self, type_params: &'a TypeParams) { + fn visit_type_params(&mut self, type_params: &'a TypeParams<'ast>) { walk_type_params(self, type_params); } #[inline] - fn visit_type_param(&mut self, type_param: &'a TypeParam) { + fn visit_type_param(&mut self, type_param: &'a TypeParam<'ast>) { walk_type_param(self, type_param); } #[inline] - fn visit_match_case(&mut self, match_case: &'a MatchCase) { + fn visit_match_case(&mut self, match_case: &'a MatchCase<'ast>) { walk_match_case(self, match_case); } #[inline] - fn visit_pattern(&mut self, pattern: &'a Pattern) { + fn visit_pattern(&mut self, pattern: &'a Pattern<'ast>) { walk_pattern(self, pattern); } #[inline] - fn visit_pattern_arguments(&mut self, pattern_arguments: &'a PatternArguments) { + fn visit_pattern_arguments(&mut self, pattern_arguments: &'a PatternArguments<'ast>) { walk_pattern_arguments(self, pattern_arguments); } #[inline] - fn visit_pattern_keyword(&mut self, pattern_keyword: &'a PatternKeyword) { + fn visit_pattern_keyword(&mut self, pattern_keyword: &'a PatternKeyword<'ast>) { walk_pattern_keyword(self, pattern_keyword); } #[inline] - fn visit_body(&mut self, body: &'a [Stmt]) { + fn visit_body(&mut self, body: &'a [Stmt<'ast>]) { walk_body(self, body); } #[inline] - fn visit_elif_else_clause(&mut self, elif_else_clause: &'a ElifElseClause) { + fn visit_elif_else_clause(&mut self, elif_else_clause: &'a ElifElseClause<'ast>) { walk_elif_else_clause(self, elif_else_clause); } #[inline] - fn visit_f_string(&mut self, f_string: &'a FString) { + fn visit_f_string(&mut self, f_string: &'a FString<'ast>) { walk_f_string(self, f_string); } #[inline] - fn visit_f_string_element(&mut self, f_string_element: &'a FStringElement) { + fn visit_f_string_element(&mut self, f_string_element: &'a FStringElement<'ast>) { walk_f_string_element(self, f_string_element); } #[inline] - fn visit_string_literal(&mut self, string_literal: &'a StringLiteral) { + fn visit_string_literal(&mut self, string_literal: &'a StringLiteral<'ast>) { walk_string_literal(self, string_literal); } #[inline] - fn visit_bytes_literal(&mut self, bytes_literal: &'a BytesLiteral) { + fn visit_bytes_literal(&mut self, bytes_literal: &'a BytesLiteral<'ast>) { walk_bytes_literal(self, bytes_literal); } } -pub fn walk_module<'a, V>(visitor: &mut V, module: &'a Mod) +pub fn walk_module<'a, 'ast, V>(visitor: &mut V, module: &'a Mod<'ast>) where - V: SourceOrderVisitor<'a> + ?Sized, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let node = AnyNodeRef::from(module); if visitor.enter_node(node).is_traverse() { @@ -187,18 +190,18 @@ where visitor.leave_node(node); } -pub fn walk_body<'a, V>(visitor: &mut V, body: &'a [Stmt]) +pub fn walk_body<'a, 'ast, V>(visitor: &mut V, body: &'a [Stmt<'ast>]) where - V: SourceOrderVisitor<'a> + ?Sized, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { for stmt in body { visitor.visit_stmt(stmt); } } -pub fn walk_stmt<'a, V>(visitor: &mut V, stmt: &'a Stmt) +pub fn walk_stmt<'a, 'ast, V>(visitor: &mut V, stmt: &'a Stmt<'ast>) where - V: SourceOrderVisitor<'a> + ?Sized, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let node = AnyNodeRef::from(stmt); @@ -247,7 +250,10 @@ impl TraversalSignal { } } -pub fn walk_annotation<'a, V: SourceOrderVisitor<'a> + ?Sized>(visitor: &mut V, expr: &'a Expr) { +pub fn walk_annotation<'a, 'ast, V: SourceOrderVisitor<'a, 'ast> + ?Sized>( + visitor: &mut V, + expr: &'a Expr<'ast>, +) { let node = AnyNodeRef::from(expr); if visitor.enter_node(node).is_traverse() { visitor.visit_expr(expr); @@ -256,9 +262,9 @@ pub fn walk_annotation<'a, V: SourceOrderVisitor<'a> + ?Sized>(visitor: &mut V, visitor.leave_node(node); } -pub fn walk_decorator<'a, V>(visitor: &mut V, decorator: &'a Decorator) +pub fn walk_decorator<'a, 'ast, V>(visitor: &mut V, decorator: &'a Decorator<'ast>) where - V: SourceOrderVisitor<'a> + ?Sized, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let node = AnyNodeRef::from(decorator); if visitor.enter_node(node).is_traverse() { @@ -268,9 +274,9 @@ where visitor.leave_node(node); } -pub fn walk_expr<'a, V>(visitor: &mut V, expr: &'a Expr) +pub fn walk_expr<'a, 'ast, V>(visitor: &mut V, expr: &'a Expr<'ast>) where - V: SourceOrderVisitor<'a> + ?Sized, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let node = AnyNodeRef::from(expr); if visitor.enter_node(node).is_traverse() { @@ -313,9 +319,9 @@ where visitor.leave_node(node); } -pub fn walk_comprehension<'a, V>(visitor: &mut V, comprehension: &'a Comprehension) +pub fn walk_comprehension<'a, 'ast, V>(visitor: &mut V, comprehension: &'a Comprehension<'ast>) where - V: SourceOrderVisitor<'a> + ?Sized, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let node = AnyNodeRef::from(comprehension); if visitor.enter_node(node).is_traverse() { @@ -325,9 +331,11 @@ where visitor.leave_node(node); } -pub fn walk_elif_else_clause<'a, V>(visitor: &mut V, elif_else_clause: &'a ElifElseClause) -where - V: SourceOrderVisitor<'a> + ?Sized, +pub fn walk_elif_else_clause<'a, 'ast, V>( + visitor: &mut V, + elif_else_clause: &'a ElifElseClause<'ast>, +) where + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let node = AnyNodeRef::from(elif_else_clause); if visitor.enter_node(node).is_traverse() { @@ -337,9 +345,9 @@ where visitor.leave_node(node); } -pub fn walk_except_handler<'a, V>(visitor: &mut V, except_handler: &'a ExceptHandler) +pub fn walk_except_handler<'a, 'ast, V>(visitor: &mut V, except_handler: &'a ExceptHandler<'ast>) where - V: SourceOrderVisitor<'a> + ?Sized, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let node = AnyNodeRef::from(except_handler); if visitor.enter_node(node).is_traverse() { @@ -352,9 +360,9 @@ where visitor.leave_node(node); } -pub fn walk_format_spec<'a, V: SourceOrderVisitor<'a> + ?Sized>( +pub fn walk_format_spec<'a, 'ast, V: SourceOrderVisitor<'a, 'ast> + ?Sized>( visitor: &mut V, - format_spec: &'a Expr, + format_spec: &'a Expr<'ast>, ) { let node = AnyNodeRef::from(format_spec); if visitor.enter_node(node).is_traverse() { @@ -364,9 +372,9 @@ pub fn walk_format_spec<'a, V: SourceOrderVisitor<'a> + ?Sized>( visitor.leave_node(node); } -pub fn walk_arguments<'a, V>(visitor: &mut V, arguments: &'a Arguments) +pub fn walk_arguments<'a, 'ast, V>(visitor: &mut V, arguments: &'a Arguments<'ast>) where - V: SourceOrderVisitor<'a> + ?Sized, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let node = AnyNodeRef::from(arguments); if visitor.enter_node(node).is_traverse() { @@ -376,9 +384,9 @@ where visitor.leave_node(node); } -pub fn walk_parameters<'a, V>(visitor: &mut V, parameters: &'a Parameters) +pub fn walk_parameters<'a, 'ast, V>(visitor: &mut V, parameters: &'a Parameters<'ast>) where - V: SourceOrderVisitor<'a> + ?Sized, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let node = AnyNodeRef::from(parameters); if visitor.enter_node(node).is_traverse() { @@ -388,9 +396,9 @@ where visitor.leave_node(node); } -pub fn walk_parameter<'a, V>(visitor: &mut V, parameter: &'a Parameter) +pub fn walk_parameter<'a, 'ast, V>(visitor: &mut V, parameter: &'a Parameter<'ast>) where - V: SourceOrderVisitor<'a> + ?Sized, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let node = AnyNodeRef::from(parameter); @@ -400,11 +408,11 @@ where visitor.leave_node(node); } -pub fn walk_parameter_with_default<'a, V>( +pub fn walk_parameter_with_default<'a, 'ast, V>( visitor: &mut V, - parameter_with_default: &'a ParameterWithDefault, + parameter_with_default: &'a ParameterWithDefault<'ast>, ) where - V: SourceOrderVisitor<'a> + ?Sized, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let node = AnyNodeRef::from(parameter_with_default); if visitor.enter_node(node).is_traverse() { @@ -415,9 +423,9 @@ pub fn walk_parameter_with_default<'a, V>( } #[inline] -pub fn walk_keyword<'a, V>(visitor: &mut V, keyword: &'a Keyword) +pub fn walk_keyword<'a, 'ast, V>(visitor: &mut V, keyword: &'a Keyword<'ast>) where - V: SourceOrderVisitor<'a> + ?Sized, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let node = AnyNodeRef::from(keyword); @@ -427,9 +435,9 @@ where visitor.leave_node(node); } -pub fn walk_with_item<'a, V>(visitor: &mut V, with_item: &'a WithItem) +pub fn walk_with_item<'a, 'ast, V>(visitor: &mut V, with_item: &'a WithItem<'ast>) where - V: SourceOrderVisitor<'a> + ?Sized, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let node = AnyNodeRef::from(with_item); if visitor.enter_node(node).is_traverse() { @@ -438,9 +446,9 @@ where visitor.leave_node(node); } -pub fn walk_type_params<'a, V>(visitor: &mut V, type_params: &'a TypeParams) +pub fn walk_type_params<'a, 'ast, V>(visitor: &mut V, type_params: &'a TypeParams<'ast>) where - V: SourceOrderVisitor<'a> + ?Sized, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let node = AnyNodeRef::from(type_params); if visitor.enter_node(node).is_traverse() { @@ -449,9 +457,9 @@ where visitor.leave_node(node); } -pub fn walk_type_param<'a, V>(visitor: &mut V, type_param: &'a TypeParam) +pub fn walk_type_param<'a, 'ast, V>(visitor: &mut V, type_param: &'a TypeParam<'ast>) where - V: SourceOrderVisitor<'a> + ?Sized, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let node = AnyNodeRef::from(type_param); if visitor.enter_node(node).is_traverse() { @@ -464,9 +472,9 @@ where visitor.leave_node(node); } -pub fn walk_match_case<'a, V>(visitor: &mut V, match_case: &'a MatchCase) +pub fn walk_match_case<'a, 'ast, V>(visitor: &mut V, match_case: &'a MatchCase<'ast>) where - V: SourceOrderVisitor<'a> + ?Sized, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let node = AnyNodeRef::from(match_case); if visitor.enter_node(node).is_traverse() { @@ -475,9 +483,9 @@ where visitor.leave_node(node); } -pub fn walk_pattern<'a, V>(visitor: &mut V, pattern: &'a Pattern) +pub fn walk_pattern<'a, 'ast, V>(visitor: &mut V, pattern: &'a Pattern<'ast>) where - V: SourceOrderVisitor<'a> + ?Sized, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let node = AnyNodeRef::from(pattern); if visitor.enter_node(node).is_traverse() { @@ -495,9 +503,11 @@ where visitor.leave_node(node); } -pub fn walk_pattern_arguments<'a, V>(visitor: &mut V, pattern_arguments: &'a PatternArguments) -where - V: SourceOrderVisitor<'a> + ?Sized, +pub fn walk_pattern_arguments<'a, 'ast, V>( + visitor: &mut V, + pattern_arguments: &'a PatternArguments<'ast>, +) where + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let node = AnyNodeRef::from(pattern_arguments); if visitor.enter_node(node).is_traverse() { @@ -511,9 +521,9 @@ where visitor.leave_node(node); } -pub fn walk_pattern_keyword<'a, V>(visitor: &mut V, pattern_keyword: &'a PatternKeyword) +pub fn walk_pattern_keyword<'a, 'ast, V>(visitor: &mut V, pattern_keyword: &'a PatternKeyword<'ast>) where - V: SourceOrderVisitor<'a> + ?Sized, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let node = AnyNodeRef::from(pattern_keyword); if visitor.enter_node(node).is_traverse() { @@ -522,9 +532,9 @@ where visitor.leave_node(node); } -pub fn walk_f_string_element<'a, V: SourceOrderVisitor<'a> + ?Sized>( +pub fn walk_f_string_element<'a, 'ast, V: SourceOrderVisitor<'a, 'ast> + ?Sized>( visitor: &mut V, - f_string_element: &'a FStringElement, + f_string_element: &'a FStringElement<'ast>, ) { let node = AnyNodeRef::from(f_string_element); if visitor.enter_node(node).is_traverse() { @@ -536,37 +546,37 @@ pub fn walk_f_string_element<'a, V: SourceOrderVisitor<'a> + ?Sized>( visitor.leave_node(node); } -pub fn walk_bool_op<'a, V>(_visitor: &mut V, _bool_op: &'a BoolOp) +pub fn walk_bool_op<'a, 'ast, V>(_visitor: &mut V, _bool_op: &'a BoolOp) where - V: SourceOrderVisitor<'a> + ?Sized, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { } #[inline] -pub fn walk_operator<'a, V>(_visitor: &mut V, _operator: &'a Operator) +pub fn walk_operator<'a, 'ast, V>(_visitor: &mut V, _operator: &'a Operator) where - V: SourceOrderVisitor<'a> + ?Sized, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { } #[inline] -pub fn walk_unary_op<'a, V>(_visitor: &mut V, _unary_op: &'a UnaryOp) +pub fn walk_unary_op<'a, 'ast, V>(_visitor: &mut V, _unary_op: &'a UnaryOp) where - V: SourceOrderVisitor<'a> + ?Sized, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { } #[inline] -pub fn walk_cmp_op<'a, V>(_visitor: &mut V, _cmp_op: &'a CmpOp) +pub fn walk_cmp_op<'a, 'ast, V>(_visitor: &mut V, _cmp_op: &'a CmpOp) where - V: SourceOrderVisitor<'a> + ?Sized, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { } #[inline] -pub fn walk_f_string<'a, V>(visitor: &mut V, f_string: &'a FString) +pub fn walk_f_string<'a, 'ast, V>(visitor: &mut V, f_string: &'a FString<'ast>) where - V: SourceOrderVisitor<'a> + ?Sized, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let node = AnyNodeRef::from(f_string); if visitor.enter_node(node).is_traverse() { @@ -576,9 +586,9 @@ where } #[inline] -pub fn walk_string_literal<'a, V>(visitor: &mut V, string_literal: &'a StringLiteral) +pub fn walk_string_literal<'a, 'ast, V>(visitor: &mut V, string_literal: &'a StringLiteral<'ast>) where - V: SourceOrderVisitor<'a> + ?Sized, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let node = AnyNodeRef::from(string_literal); if visitor.enter_node(node).is_traverse() { @@ -588,9 +598,9 @@ where } #[inline] -pub fn walk_bytes_literal<'a, V>(visitor: &mut V, bytes_literal: &'a BytesLiteral) +pub fn walk_bytes_literal<'a, 'ast, V>(visitor: &mut V, bytes_literal: &'a BytesLiteral<'ast>) where - V: SourceOrderVisitor<'a> + ?Sized, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let node = AnyNodeRef::from(bytes_literal); if visitor.enter_node(node).is_traverse() { @@ -600,9 +610,9 @@ where } #[inline] -pub fn walk_alias<'a, V>(visitor: &mut V, alias: &'a Alias) +pub fn walk_alias<'a, 'ast, V>(visitor: &mut V, alias: &'a Alias<'ast>) where - V: SourceOrderVisitor<'a> + ?Sized, + V: SourceOrderVisitor<'a, 'ast> + ?Sized, { let node = AnyNodeRef::from(alias); if visitor.enter_node(node).is_traverse() { diff --git a/crates/ruff_python_parser/Cargo.toml b/crates/ruff_python_parser/Cargo.toml index a74a93143e..6423d8e5d8 100644 --- a/crates/ruff_python_parser/Cargo.toml +++ b/crates/ruff_python_parser/Cargo.toml @@ -13,6 +13,7 @@ license = { workspace = true } [lib] [dependencies] +ruff_allocator = { workspace = true } ruff_python_ast = { workspace = true } ruff_python_trivia = { workspace = true } ruff_text_size = { workspace = true } diff --git a/crates/ruff_python_parser/src/lib.rs b/crates/ruff_python_parser/src/lib.rs index 7569db2ca7..f90f0a378d 100644 --- a/crates/ruff_python_parser/src/lib.rs +++ b/crates/ruff_python_parser/src/lib.rs @@ -64,11 +64,11 @@ //! [parsing]: https://en.wikipedia.org/wiki/Parsing //! [lexer]: crate::lexer -use std::iter::FusedIterator; -use std::ops::Deref; - pub use crate::error::{FStringErrorType, ParseError, ParseErrorType}; pub use crate::token::{Token, TokenKind}; +use ruff_allocator::Allocator; +use std::iter::FusedIterator; +use std::ops::Deref; use crate::parser::Parser; @@ -107,8 +107,11 @@ pub mod typing; /// let module = parse_module(source); /// assert!(module.is_ok()); /// ``` -pub fn parse_module(source: &str) -> Result, ParseError> { - Parser::new(source, Mode::Module) +pub fn parse_module<'ast>( + source: &str, + allocator: &'ast Allocator, +) -> Result>, ParseError> { + Parser::new(source, Mode::Module, allocator) .parse() .try_into_module() .unwrap() @@ -130,8 +133,11 @@ pub fn parse_module(source: &str) -> Result, ParseError> { /// let expr = parse_expression("1 + 2"); /// assert!(expr.is_ok()); /// ``` -pub fn parse_expression(source: &str) -> Result, ParseError> { - Parser::new(source, Mode::Expression) +pub fn parse_expression<'a>( + source: &str, + allocator: &'a Allocator, +) -> Result>, ParseError> { + Parser::new(source, Mode::Expression, allocator) .parse() .try_into_expression() .unwrap() @@ -154,12 +160,13 @@ pub fn parse_expression(source: &str) -> Result, ParseErro /// let parsed = parse_expression_range("11 + 22 + 33", TextRange::new(TextSize::new(5), TextSize::new(7))); /// assert!(parsed.is_ok()); /// ``` -pub fn parse_expression_range( +pub fn parse_expression_range<'ast>( source: &str, range: TextRange, -) -> Result, ParseError> { + allocator: &'ast Allocator, +) -> Result>, ParseError> { let source = &source[..range.end().to_usize()]; - Parser::new_starts_at(source, Mode::Expression, range.start()) + Parser::new_starts_at(source, Mode::Expression, range.start(), allocator) .parse() .try_into_expression() .unwrap() @@ -212,29 +219,41 @@ pub fn parse_expression_range( /// let parsed = parse(source, Mode::Ipython); /// assert!(parsed.is_ok()); /// ``` -pub fn parse(source: &str, mode: Mode) -> Result, ParseError> { - parse_unchecked(source, mode).into_result() +pub fn parse<'ast>( + source: &str, + mode: Mode, + allocator: &'ast Allocator, +) -> Result>, ParseError> { + parse_unchecked(source, mode, allocator).into_result() } /// Parse the given Python source code using the specified [`Mode`]. /// /// This is same as the [`parse`] function except that it doesn't check for any [`ParseError`] /// and returns the [`Parsed`] as is. -pub fn parse_unchecked(source: &str, mode: Mode) -> Parsed { - Parser::new(source, mode).parse() +pub fn parse_unchecked<'ast>( + source: &str, + mode: Mode, + allocator: &'ast Allocator, +) -> Parsed> { + Parser::new(source, mode, allocator).parse() } /// Parse the given Python source code using the specified [`PySourceType`]. -pub fn parse_unchecked_source(source: &str, source_type: PySourceType) -> Parsed { +pub fn parse_unchecked_source<'ast>( + source: &str, + source_type: PySourceType, + allocator: &'ast Allocator, +) -> Parsed> { // SAFETY: Safe because `PySourceType` always parses to a `ModModule` - Parser::new(source, source_type.as_mode()) + Parser::new(source, source_type.as_mode(), allocator) .parse() .try_into_module() .unwrap() } /// Represents the parsed source code. -#[derive(Debug, PartialEq, Clone)] +#[derive(Debug)] pub struct Parsed { syntax: T, tokens: Tokens, @@ -293,7 +312,16 @@ impl Parsed { } } -impl Parsed { +impl PartialEq for Parsed +where + T: PartialEq, +{ + fn eq(&self, other: &Self) -> bool { + self.syntax == other.syntax && self.tokens == other.tokens && self.errors == other.errors + } +} + +impl<'ast> Parsed> { /// Attempts to convert the [`Parsed`] into a [`Parsed`]. /// /// This method checks if the `syntax` field of the output is a [`Mod::Module`]. If it is, the @@ -301,7 +329,7 @@ impl Parsed { /// returns [`None`]. /// /// [`Some(Parsed)`]: Some - pub fn try_into_module(self) -> Option> { + pub fn try_into_module(self) -> Option>> { match self.syntax { Mod::Module(module) => Some(Parsed { syntax: module, @@ -319,7 +347,7 @@ impl Parsed { /// Otherwise, it returns [`None`]. /// /// [`Some(Parsed)`]: Some - pub fn try_into_expression(self) -> Option> { + pub fn try_into_expression(self) -> Option>> { match self.syntax { Mod::Module(_) => None, Mod::Expression(expression) => Some(Parsed { @@ -331,32 +359,32 @@ impl Parsed { } } -impl Parsed { +impl<'ast> Parsed> { /// Returns the module body contained in this parsed output as a [`Suite`]. - pub fn suite(&self) -> &Suite { + pub fn suite(&self) -> &Suite<'ast> { &self.syntax.body } /// Consumes the [`Parsed`] output and returns the module body as a [`Suite`]. - pub fn into_suite(self) -> Suite { + pub fn into_suite(self) -> Suite<'ast> { self.syntax.body } } -impl Parsed { +impl<'ast> Parsed> { /// Returns the expression contained in this parsed output. - pub fn expr(&self) -> &Expr { + pub fn expr(&self) -> &Expr<'ast> { &self.syntax.body } /// Returns a mutable reference to the expression contained in this parsed output. - pub fn expr_mut(&mut self) -> &mut Expr { + pub fn expr_mut(&mut self) -> &mut Expr<'ast> { &mut self.syntax.body } /// Consumes the [`Parsed`] output and returns the contained [`Expr`]. - pub fn into_expr(self) -> Expr { - *self.syntax.body + pub fn into_expr(self) -> Expr<'ast> { + self.syntax.body } } diff --git a/crates/ruff_python_parser/src/parser/expression.rs b/crates/ruff_python_parser/src/parser/expression.rs index 62b42b6c5e..056d42a121 100644 --- a/crates/ruff_python_parser/src/parser/expression.rs +++ b/crates/ruff_python_parser/src/parser/expression.rs @@ -106,7 +106,7 @@ pub(super) const END_EXPR_SET: TokenSet = TokenSet::new([ /// Tokens that can appear at the end of a sequence. const END_SEQUENCE_SET: TokenSet = END_EXPR_SET.remove(TokenKind::Comma); -impl<'src> Parser<'src> { +impl<'src, 'ast> Parser<'src, 'ast> { /// Returns `true` if the parser is at a name or keyword (including soft keyword) token. pub(super) fn at_name_or_keyword(&self) -> bool { self.at(TokenKind::Name) || self.current_token_kind().is_keyword() @@ -138,7 +138,7 @@ impl<'src> Parser<'src> { /// used to match the `star_expressions` rule. /// /// [Python grammar]: https://docs.python.org/3/reference/grammar.html - pub(super) fn parse_expression_list(&mut self, context: ExpressionContext) -> ParsedExpr { + pub(super) fn parse_expression_list(&mut self, context: ExpressionContext) -> ParsedExpr<'ast> { let start = self.node_start(); let parsed_expr = self.parse_conditional_expression_or_higher_impl(context); @@ -167,7 +167,7 @@ impl<'src> Parser<'src> { pub(super) fn parse_named_expression_or_higher( &mut self, context: ExpressionContext, - ) -> ParsedExpr { + ) -> ParsedExpr<'ast> { let start = self.node_start(); let parsed_expr = self.parse_conditional_expression_or_higher_impl(context); @@ -190,14 +190,14 @@ impl<'src> Parser<'src> { /// instead of as a tuple, as done by [`Parser::parse_expression_list`] use this function. /// /// [Python grammar]: https://docs.python.org/3/reference/grammar.html - pub(super) fn parse_conditional_expression_or_higher(&mut self) -> ParsedExpr { + pub(super) fn parse_conditional_expression_or_higher(&mut self) -> ParsedExpr<'ast> { self.parse_conditional_expression_or_higher_impl(ExpressionContext::default()) } pub(super) fn parse_conditional_expression_or_higher_impl( &mut self, context: ExpressionContext, - ) -> ParsedExpr { + ) -> ParsedExpr<'ast> { if self.at(TokenKind::Lambda) { Expr::Lambda(self.parse_lambda_expr()).into() } else { @@ -224,7 +224,7 @@ impl<'src> Parser<'src> { /// specified method to allow parsing lambda expression. /// /// [Python grammar]: https://docs.python.org/3/reference/grammar.html - fn parse_simple_expression(&mut self, context: ExpressionContext) -> ParsedExpr { + fn parse_simple_expression(&mut self, context: ExpressionContext) -> ParsedExpr<'ast> { self.parse_binary_expression_or_higher(OperatorPrecedence::Initial, context) } @@ -235,7 +235,7 @@ impl<'src> Parser<'src> { &mut self, left_precedence: OperatorPrecedence, context: ExpressionContext, - ) -> ParsedExpr { + ) -> ParsedExpr<'ast> { let start = self.node_start(); let lhs = self.parse_lhs_expression(left_precedence, context); self.parse_binary_expression_or_higher_recursive(lhs, left_precedence, context, start) @@ -243,11 +243,11 @@ impl<'src> Parser<'src> { pub(super) fn parse_binary_expression_or_higher_recursive( &mut self, - mut left: ParsedExpr, + mut left: ParsedExpr<'ast>, left_precedence: OperatorPrecedence, context: ExpressionContext, start: TextSize, - ) -> ParsedExpr { + ) -> ParsedExpr<'ast> { let mut progress = ParserProgress::default(); loop { @@ -292,9 +292,9 @@ impl<'src> Parser<'src> { let right = self.parse_binary_expression_or_higher(new_precedence, context); Expr::BinOp(ast::ExprBinOp { - left: Box::new(left.expr), + left: self.alloc_box(left.expr), op: bin_op, - right: Box::new(right.expr), + right: self.alloc_box(right.expr), range: self.node_range(start), }) } @@ -317,7 +317,7 @@ impl<'src> Parser<'src> { &mut self, left_precedence: OperatorPrecedence, context: ExpressionContext, - ) -> ParsedExpr { + ) -> ParsedExpr<'ast> { let start = self.node_start(); let token = self.current_token_kind(); @@ -416,7 +416,7 @@ impl<'src> Parser<'src> { /// sense, it matches the `bitwise_or` rule of the [Python grammar]. /// /// [Python grammar]: https://docs.python.org/3/reference/grammar.html - fn parse_expression_with_bitwise_or_precedence(&mut self) -> ParsedExpr { + fn parse_expression_with_bitwise_or_precedence(&mut self) -> ParsedExpr<'ast> { let parsed_expr = self.parse_conditional_expression_or_higher(); if parsed_expr.is_parenthesized { @@ -450,7 +450,7 @@ impl<'src> Parser<'src> { /// field will be [`ExprContext::Invalid`]. /// /// See: - pub(super) fn parse_name(&mut self) -> ast::ExprName { + pub(super) fn parse_name(&mut self) -> ast::ExprName<'ast> { let identifier = self.parse_identifier(); let ctx = if identifier.is_valid() { @@ -471,20 +471,26 @@ impl<'src> Parser<'src> { /// For an invalid identifier, the `id` field will be an empty string. /// /// See: - pub(super) fn parse_identifier(&mut self) -> ast::Identifier { + pub(super) fn parse_identifier(&mut self) -> ast::Identifier<'ast> { let range = self.current_token_range(); if self.at(TokenKind::Name) { let TokenValue::Name(name) = self.bump_value(TokenKind::Name) else { unreachable!(); }; - return ast::Identifier { id: name, range }; + return ast::Identifier { + id: self.allocator.alloc_str(&name), + range, + }; } if self.current_token_kind().is_soft_keyword() { let id = Name::new(self.src_text(range)); self.bump_soft_keyword_as_name(); - return ast::Identifier { id, range }; + return ast::Identifier { + id: self.allocator.alloc_str(&id), + range, + }; } if self.current_token_kind().is_keyword() { @@ -497,7 +503,7 @@ impl<'src> Parser<'src> { range, ); - let id = Name::new(self.src_text(range)); + let id = self.alloc_str(self.src_text(range)); self.bump_any(); ast::Identifier { id, range } } else { @@ -507,7 +513,7 @@ impl<'src> Parser<'src> { ); ast::Identifier { - id: Name::empty(), + id: "", range: self.missing_node_range(), } } @@ -516,7 +522,7 @@ impl<'src> Parser<'src> { /// Parses an atom. /// /// See: - fn parse_atom(&mut self) -> ParsedExpr { + fn parse_atom(&mut self) -> ParsedExpr<'ast> { let start = self.node_start(); let lhs = match self.current_token_kind() { @@ -595,7 +601,7 @@ impl<'src> Parser<'src> { ); Expr::Name(ast::ExprName { range: self.missing_node_range(), - id: Name::empty(), + id: "", ctx: ExprContext::Invalid, }) } @@ -611,7 +617,11 @@ impl<'src> Parser<'src> { /// expression, `[` for a subscript expression, or `.` for an attribute expression. /// /// This method does nothing if the current token is not a candidate for a postfix expression. - pub(super) fn parse_postfix_expression(&mut self, mut lhs: Expr, start: TextSize) -> Expr { + pub(super) fn parse_postfix_expression( + &mut self, + mut lhs: Expr<'ast>, + start: TextSize, + ) -> Expr<'ast> { loop { lhs = match self.current_token_kind() { TokenKind::Lpar => Expr::Call(self.parse_call_expression(lhs, start)), @@ -632,11 +642,11 @@ impl<'src> Parser<'src> { /// If the parser isn't position at a `(` token. /// /// See: - fn parse_call_expression(&mut self, func: Expr, start: TextSize) -> ast::ExprCall { + fn parse_call_expression(&mut self, func: Expr<'ast>, start: TextSize) -> ast::ExprCall<'ast> { let arguments = self.parse_arguments(); ast::ExprCall { - func: Box::new(func), + func: self.alloc_box(func), arguments, range: self.node_range(start), } @@ -649,12 +659,12 @@ impl<'src> Parser<'src> { /// If the parser isn't positioned at a `(` token. /// /// See: - pub(super) fn parse_arguments(&mut self) -> ast::Arguments { + pub(super) fn parse_arguments(&mut self) -> ast::Arguments<'ast> { let start = self.node_start(); self.bump(TokenKind::Lpar); - let mut args = vec![]; - let mut keywords = vec![]; + let mut args = ruff_allocator::Vec::new_in(&self.allocator); + let mut keywords = ruff_allocator::Vec::new_in(&self.allocator); let mut seen_keyword_argument = false; // foo = 1 let mut seen_keyword_unpacking = false; // **foo @@ -717,7 +727,7 @@ impl<'src> Parser<'src> { &parsed_expr, ); ast::Identifier { - id: Name::empty(), + id: "", range: parsed_expr.range(), } }; @@ -752,8 +762,8 @@ impl<'src> Parser<'src> { let arguments = ast::Arguments { range: self.node_range(start), - args: args.into_boxed_slice(), - keywords: keywords.into_boxed_slice(), + args: args.into_bump_slice_mut(), + keywords: keywords.into_bump_slice_mut(), }; self.validate_arguments(&arguments); @@ -770,9 +780,9 @@ impl<'src> Parser<'src> { /// See: fn parse_subscript_expression( &mut self, - mut value: Expr, + mut value: Expr<'ast>, start: TextSize, - ) -> ast::ExprSubscript { + ) -> ast::ExprSubscript<'ast> { self.bump(TokenKind::Lsqb); // To prevent the `value` context from being `Del` within a `del` statement, @@ -788,10 +798,10 @@ impl<'src> Parser<'src> { self.add_error(ParseErrorType::EmptySlice, slice_range); return ast::ExprSubscript { - value: Box::new(value), - slice: Box::new(Expr::Name(ast::ExprName { + value: self.alloc_box(value), + slice: self.alloc_box(Expr::Name(ast::ExprName { range: slice_range, - id: Name::empty(), + id: "", ctx: ExprContext::Invalid, })), ctx: ExprContext::Load, @@ -831,8 +841,8 @@ impl<'src> Parser<'src> { self.expect(TokenKind::Rsqb); ast::ExprSubscript { - value: Box::new(value), - slice: Box::new(slice), + value: self.alloc_box(value), + slice: self.alloc_box(slice), ctx: ExprContext::Load, range: self.node_range(start), } @@ -841,7 +851,7 @@ impl<'src> Parser<'src> { /// Parses a slice expression. /// /// See: - fn parse_slice(&mut self) -> Expr { + fn parse_slice(&mut self) -> Expr<'ast> { const UPPER_END_SET: TokenSet = TokenSet::new([TokenKind::Comma, TokenKind::Colon, TokenKind::Rsqb]) .union(NEWLINE_EOF_SET); @@ -876,18 +886,20 @@ impl<'src> Parser<'src> { self.expect(TokenKind::Colon); - let lower = lower.map(Box::new); + let lower = lower.map(|lower| self.alloc_box(lower)); let upper = if self.at_ts(UPPER_END_SET) { None } else { - Some(Box::new(self.parse_conditional_expression_or_higher().expr)) + let expression = self.parse_conditional_expression_or_higher().expr; + Some(self.alloc_box(expression)) }; let step = if self.eat(TokenKind::Colon) { if self.at_ts(STEP_END_SET) { None } else { - Some(Box::new(self.parse_conditional_expression_or_higher().expr)) + let expression = self.parse_conditional_expression_or_higher().expr; + Some(self.alloc_box(expression)) } } else { None @@ -915,7 +927,7 @@ impl<'src> Parser<'src> { &mut self, op: UnaryOp, context: ExpressionContext, - ) -> ast::ExprUnaryOp { + ) -> ast::ExprUnaryOp<'ast> { let start = self.node_start(); self.bump(TokenKind::from(op)); @@ -923,7 +935,7 @@ impl<'src> Parser<'src> { ast::ExprUnaryOp { op, - operand: Box::new(operand.expr), + operand: self.alloc_box(operand.expr), range: self.node_range(start), } } @@ -937,15 +949,15 @@ impl<'src> Parser<'src> { /// See: pub(super) fn parse_attribute_expression( &mut self, - value: Expr, + value: Expr<'ast>, start: TextSize, - ) -> ast::ExprAttribute { + ) -> ast::ExprAttribute<'ast> { self.bump(TokenKind::Dot); let attr = self.parse_identifier(); ast::ExprAttribute { - value: Box::new(value), + value: self.alloc_box(value), attr, ctx: ExprContext::Load, range: self.node_range(start), @@ -964,11 +976,11 @@ impl<'src> Parser<'src> { /// See: fn parse_boolean_expression( &mut self, - lhs: Expr, + lhs: Expr<'ast>, start: TextSize, op: BoolOp, context: ExpressionContext, - ) -> ast::ExprBoolOp { + ) -> ast::ExprBoolOp<'ast> { self.bump(TokenKind::from(op)); let mut values = vec![lhs]; @@ -1030,15 +1042,16 @@ impl<'src> Parser<'src> { /// See: fn parse_comparison_expression( &mut self, - lhs: Expr, + lhs: Expr<'ast>, start: TextSize, op: CmpOp, context: ExpressionContext, - ) -> ast::ExprCompare { + ) -> ast::ExprCompare<'ast> { self.bump_cmp_op(op); - let mut comparators = vec![]; - let mut operators = vec![op]; + let mut comparators = ruff_allocator::Vec::new_in(&self.allocator); + let mut operators = ruff_allocator::Vec::new_in(&self.allocator); + operators.push(op); let mut progress = ParserProgress::default(); @@ -1067,9 +1080,9 @@ impl<'src> Parser<'src> { } ast::ExprCompare { - left: Box::new(lhs), - ops: operators.into_boxed_slice(), - comparators: comparators.into_boxed_slice(), + left: self.alloc_box(lhs), + ops: operators.into_bump_slice_mut(), + comparators: comparators.into_bump_slice_mut(), range: self.node_range(start), } } @@ -1081,7 +1094,7 @@ impl<'src> Parser<'src> { /// If the parser isn't positioned at a `String` or `FStringStart` token. /// /// See: (Search "strings:") - pub(super) fn parse_strings(&mut self) -> Expr { + pub(super) fn parse_strings(&mut self) -> Expr<'ast> { const STRING_START_SET: TokenSet = TokenSet::new([TokenKind::String, TokenKind::FStringStart]); @@ -1132,9 +1145,9 @@ impl<'src> Parser<'src> { /// If the length of `strings` is less than 2. fn handle_implicitly_concatenated_strings( &mut self, - strings: Vec, + strings: Vec>, range: TextRange, - ) -> Expr { + ) -> Expr<'ast> { assert!(strings.len() > 1); let mut has_fstring = false; @@ -1251,7 +1264,7 @@ impl<'src> Parser<'src> { /// If the parser isn't positioned at a `String` token. /// /// See: - fn parse_string_or_byte_literal(&mut self) -> StringType { + fn parse_string_or_byte_literal(&mut self) -> StringType<'ast> { let range = self.current_token_range(); let flags = self.tokens.current_flags().as_any_string_flags(); @@ -1259,7 +1272,7 @@ impl<'src> Parser<'src> { unreachable!() }; - match parse_string_literal(value, flags, range) { + match parse_string_literal(&value, flags, range, self.allocator) { Ok(string) => string, Err(error) => { let location = error.location(); @@ -1271,7 +1284,7 @@ impl<'src> Parser<'src> { // rb"a𝐁c123" // b"""123a𝐁c""" StringType::Bytes(ast::BytesLiteral { - value: Box::new([]), + value: &[], range, flags: ast::BytesLiteralFlags::from(flags).with_invalid(), }) @@ -1299,7 +1312,7 @@ impl<'src> Parser<'src> { /// /// See: (Search "fstring:") /// See: - fn parse_fstring(&mut self) -> ast::FString { + fn parse_fstring(&mut self) -> ast::FString<'ast> { let start = self.node_start(); let flags = self.tokens.current_flags().as_any_string_flags(); @@ -1324,7 +1337,7 @@ impl<'src> Parser<'src> { &mut self, flags: ast::AnyStringFlags, kind: FStringElementsKind, - ) -> FStringElements { + ) -> FStringElements<'ast> { let mut elements = vec![]; self.parse_list(RecoveryContextKind::FStringElements(kind), |parser| { @@ -1340,8 +1353,8 @@ impl<'src> Parser<'src> { unreachable!() }; FStringElement::Literal( - parse_fstring_literal_element(value, flags, range).unwrap_or_else( - |lex_error| { + parse_fstring_literal_element(&value, flags, range, self.allocator) + .unwrap_or_else(|lex_error| { // test_err invalid_fstring_literal_element // f'hello \N{INVALID} world' // f"""hello \N{INVALID} world""" @@ -1354,8 +1367,7 @@ impl<'src> Parser<'src> { value: "".into(), range, } - }, - ), + }), ) } // `Invalid` tokens are created when there's a lexical error, so @@ -1388,7 +1400,7 @@ impl<'src> Parser<'src> { fn parse_fstring_expression_element( &mut self, flags: ast::AnyStringFlags, - ) -> ast::FStringExpressionElement { + ) -> ast::FStringExpressionElement<'ast> { let start = self.node_start(); self.bump(TokenKind::Lbrace); @@ -1465,7 +1477,7 @@ impl<'src> Parser<'src> { let format_spec = if self.eat(TokenKind::Colon) { let spec_start = self.node_start(); let elements = self.parse_fstring_elements(flags, FStringElementsKind::FormatSpec); - Some(Box::new(ast::FStringFormatSpec { + Some(self.alloc_box(ast::FStringFormatSpec { range: self.node_range(spec_start), elements, })) @@ -1498,7 +1510,7 @@ impl<'src> Parser<'src> { } ast::FStringExpressionElement { - expression: Box::new(value.expr), + expression: self.alloc_box(value.expr), debug_text, conversion, format_spec, @@ -1513,7 +1525,7 @@ impl<'src> Parser<'src> { /// If the parser isn't positioned at a `[` token. /// /// See: - fn parse_list_like_expression(&mut self) -> Expr { + fn parse_list_like_expression(&mut self) -> Expr<'ast> { let start = self.node_start(); self.bump(TokenKind::Lsqb); @@ -1566,7 +1578,7 @@ impl<'src> Parser<'src> { /// - /// - /// - - fn parse_set_or_dict_like_expression(&mut self) -> Expr { + fn parse_set_or_dict_like_expression(&mut self) -> Expr<'ast> { let start = self.node_start(); self.bump(TokenKind::Lbrace); @@ -1654,7 +1666,7 @@ impl<'src> Parser<'src> { /// Matches the `(tuple | group | genexp)` rule in the [Python grammar]. /// /// [Python grammar]: https://docs.python.org/3/reference/grammar.html - fn parse_parenthesized_expression(&mut self) -> ParsedExpr { + fn parse_parenthesized_expression(&mut self) -> ParsedExpr<'ast> { let start = self.node_start(); self.bump(TokenKind::Lpar); @@ -1735,11 +1747,11 @@ impl<'src> Parser<'src> { /// Uses the `parse_func` to parse each item in the tuple. pub(super) fn parse_tuple_expression( &mut self, - first_element: Expr, + first_element: Expr<'ast>, start: TextSize, parenthesized: Parenthesized, - mut parse_func: impl FnMut(&mut Parser<'src>) -> ParsedExpr, - ) -> ast::ExprTuple { + mut parse_func: impl FnMut(&mut Parser<'src, 'ast>) -> ParsedExpr<'ast>, + ) -> ast::ExprTuple<'ast> { // TODO(dhruvmanila): Can we remove `parse_func` and use `parenthesized` to // determine the parsing function? @@ -1768,7 +1780,11 @@ impl<'src> Parser<'src> { /// Parses a list expression. /// /// See: - fn parse_list_expression(&mut self, first_element: Expr, start: TextSize) -> ast::ExprList { + fn parse_list_expression( + &mut self, + first_element: Expr<'ast>, + start: TextSize, + ) -> ast::ExprList<'ast> { if !self.at_sequence_end() { self.expect(TokenKind::Comma); } @@ -1795,7 +1811,11 @@ impl<'src> Parser<'src> { /// Parses a set expression. /// /// See: - fn parse_set_expression(&mut self, first_element: Expr, start: TextSize) -> ast::ExprSet { + fn parse_set_expression( + &mut self, + first_element: Expr<'ast>, + start: TextSize, + ) -> ast::ExprSet<'ast> { if !self.at_sequence_end() { self.expect(TokenKind::Comma); } @@ -1823,10 +1843,10 @@ impl<'src> Parser<'src> { /// See: fn parse_dictionary_expression( &mut self, - key: Option, - value: Expr, + key: Option>, + value: Expr<'ast>, start: TextSize, - ) -> ast::ExprDict { + ) -> ast::ExprDict<'ast> { if !self.at_sequence_end() { self.expect(TokenKind::Comma); } @@ -1866,7 +1886,7 @@ impl<'src> Parser<'src> { /// followed by `if` clauses. /// /// See: - fn parse_generators(&mut self) -> Vec { + fn parse_generators(&mut self) -> Vec> { const GENERATOR_SET: TokenSet = TokenSet::new([TokenKind::For, TokenKind::Async]); let mut generators = vec![]; @@ -1887,7 +1907,7 @@ impl<'src> Parser<'src> { /// If the parser isn't positioned at an `async` or `for` token. /// /// See: - fn parse_comprehension(&mut self) -> ast::Comprehension { + fn parse_comprehension(&mut self) -> ast::Comprehension<'ast> { let start = self.node_start(); let is_async = self.eat(TokenKind::Async); @@ -1938,10 +1958,10 @@ impl<'src> Parser<'src> { /// See: pub(super) fn parse_generator_expression( &mut self, - element: Expr, + element: Expr<'ast>, start: TextSize, parenthesized: Parenthesized, - ) -> ast::ExprGenerator { + ) -> ast::ExprGenerator<'ast> { let generators = self.parse_generators(); if parenthesized.is_yes() { @@ -1949,7 +1969,7 @@ impl<'src> Parser<'src> { } ast::ExprGenerator { - elt: Box::new(element), + elt: self.alloc_box(element), generators, range: self.node_range(start), parenthesized: parenthesized.is_yes(), @@ -1961,15 +1981,15 @@ impl<'src> Parser<'src> { /// See: fn parse_list_comprehension_expression( &mut self, - element: Expr, + element: Expr<'ast>, start: TextSize, - ) -> ast::ExprListComp { + ) -> ast::ExprListComp<'ast> { let generators = self.parse_generators(); self.expect(TokenKind::Rsqb); ast::ExprListComp { - elt: Box::new(element), + elt: self.alloc_box(element), generators, range: self.node_range(start), } @@ -1980,17 +2000,17 @@ impl<'src> Parser<'src> { /// See: fn parse_dictionary_comprehension_expression( &mut self, - key: Expr, - value: Expr, + key: Expr<'ast>, + value: Expr<'ast>, start: TextSize, - ) -> ast::ExprDictComp { + ) -> ast::ExprDictComp<'ast> { let generators = self.parse_generators(); self.expect(TokenKind::Rbrace); ast::ExprDictComp { - key: Box::new(key), - value: Box::new(value), + key: self.alloc_box(key), + value: self.alloc_box(value), generators, range: self.node_range(start), } @@ -2001,15 +2021,15 @@ impl<'src> Parser<'src> { /// See: fn parse_set_comprehension_expression( &mut self, - element: Expr, + element: Expr<'ast>, start: TextSize, - ) -> ast::ExprSetComp { + ) -> ast::ExprSetComp<'ast> { let generators = self.parse_generators(); self.expect(TokenKind::Rbrace); ast::ExprSetComp { - elt: Box::new(element), + elt: self.alloc_box(element), generators, range: self.node_range(start), } @@ -2031,7 +2051,7 @@ impl<'src> Parser<'src> { /// If the parser isn't positioned at a `*` token. /// /// [Python grammar]: https://docs.python.org/3/reference/grammar.html - fn parse_starred_expression(&mut self, context: ExpressionContext) -> ast::ExprStarred { + fn parse_starred_expression(&mut self, context: ExpressionContext) -> ast::ExprStarred<'ast> { let start = self.node_start(); self.bump(TokenKind::Star); @@ -2045,7 +2065,7 @@ impl<'src> Parser<'src> { }; ast::ExprStarred { - value: Box::new(parsed_expr.expr), + value: self.alloc_box(parsed_expr.expr), ctx: ExprContext::Load, range: self.node_range(start), } @@ -2058,7 +2078,7 @@ impl<'src> Parser<'src> { /// If the parser isn't positioned at an `await` token. /// /// See: - fn parse_await_expression(&mut self) -> ast::ExprAwait { + fn parse_await_expression(&mut self) -> ast::ExprAwait<'ast> { let start = self.node_start(); self.bump(TokenKind::Await); @@ -2068,7 +2088,7 @@ impl<'src> Parser<'src> { ); ast::ExprAwait { - value: Box::new(parsed_expr.expr), + value: self.alloc_box(parsed_expr.expr), range: self.node_range(start), } } @@ -2080,7 +2100,7 @@ impl<'src> Parser<'src> { /// If the parser isn't positioned at a `yield` token. /// /// See: - fn parse_yield_expression(&mut self) -> Expr { + fn parse_yield_expression(&mut self) -> Expr<'ast> { let start = self.node_start(); self.bump(TokenKind::Yield); @@ -2089,10 +2109,10 @@ impl<'src> Parser<'src> { } let value = self.at_expr().then(|| { - Box::new( - self.parse_expression_list(ExpressionContext::starred_bitwise_or()) - .expr, - ) + let expression = self + .parse_expression_list(ExpressionContext::starred_bitwise_or()) + .expr; + self.alloc_box(expression) }); Expr::Yield(ast::ExprYield { @@ -2107,7 +2127,7 @@ impl<'src> Parser<'src> { /// even when parsing a `yield from` expression. /// /// See: - fn parse_yield_from_expression(&mut self, start: TextSize) -> Expr { + fn parse_yield_from_expression(&mut self, start: TextSize) -> Expr<'ast> { // Grammar: // 'yield' 'from' expression // @@ -2135,7 +2155,7 @@ impl<'src> Parser<'src> { } Expr::YieldFrom(ast::ExprYieldFrom { - value: Box::new(expr), + value: self.alloc_box(expr), range: self.node_range(start), }) } @@ -2149,9 +2169,9 @@ impl<'src> Parser<'src> { /// See: pub(super) fn parse_named_expression( &mut self, - mut target: Expr, + mut target: Expr<'ast>, start: TextSize, - ) -> ast::ExprNamed { + ) -> ast::ExprNamed<'ast> { self.bump(TokenKind::ColonEqual); if !target.is_name_expr() { @@ -2162,8 +2182,8 @@ impl<'src> Parser<'src> { let value = self.parse_conditional_expression_or_higher(); ast::ExprNamed { - target: Box::new(target), - value: Box::new(value.expr), + target: self.alloc_box(target), + value: self.alloc_box(value.expr), range: self.node_range(start), } } @@ -2175,7 +2195,7 @@ impl<'src> Parser<'src> { /// If the parser isn't positioned at a `lambda` token. /// /// See: - fn parse_lambda_expr(&mut self) -> ast::ExprLambda { + fn parse_lambda_expr(&mut self) -> ast::ExprLambda<'ast> { let start = self.node_start(); self.bump(TokenKind::Lambda); @@ -2184,7 +2204,8 @@ impl<'src> Parser<'src> { // lambda: 1 None } else { - Some(Box::new(self.parse_parameters(FunctionKind::Lambda))) + let parameters = self.parse_parameters(FunctionKind::Lambda); + Some(self.alloc_box(parameters)) }; self.expect(TokenKind::Colon); @@ -2209,7 +2230,7 @@ impl<'src> Parser<'src> { let body = self.parse_conditional_expression_or_higher(); ast::ExprLambda { - body: Box::new(body.expr), + body: self.alloc_box(body.expr), parameters, range: self.node_range(start), } @@ -2222,7 +2243,11 @@ impl<'src> Parser<'src> { /// If the parser isn't positioned at an `if` token. /// /// See: - pub(super) fn parse_if_expression(&mut self, body: Expr, start: TextSize) -> ast::ExprIf { + pub(super) fn parse_if_expression( + &mut self, + body: Expr<'ast>, + start: TextSize, + ) -> ast::ExprIf<'ast> { self.bump(TokenKind::If); let test = self.parse_simple_expression(ExpressionContext::default()); @@ -2232,9 +2257,9 @@ impl<'src> Parser<'src> { let orelse = self.parse_conditional_expression_or_higher(); ast::ExprIf { - body: Box::new(body), - test: Box::new(test.expr), - orelse: Box::new(orelse.expr), + body: self.alloc_box(body), + test: self.alloc_box(test.expr), + orelse: self.alloc_box(orelse.expr), range: self.node_range(start), } } @@ -2245,7 +2270,7 @@ impl<'src> Parser<'src> { /// /// If the parser isn't positioned at a `IpyEscapeCommand` token. /// If the escape command kind is not `%` or `!`. - fn parse_ipython_escape_command_expression(&mut self) -> ast::ExprIpyEscapeCommand { + fn parse_ipython_escape_command_expression(&mut self) -> ast::ExprIpyEscapeCommand<'ast> { let start = self.node_start(); let TokenValue::IpyEscapeCommand { value, kind } = @@ -2262,7 +2287,7 @@ impl<'src> Parser<'src> { let command = ast::ExprIpyEscapeCommand { range: self.node_range(start), kind, - value, + value: self.alloc_str(&value), }; if self.mode != Mode::Ipython { @@ -2276,7 +2301,7 @@ impl<'src> Parser<'src> { /// 1. There aren't any duplicate keyword argument /// 2. If there are more than one argument (positional or keyword), all generator expressions /// present should be parenthesized. - fn validate_arguments(&mut self, arguments: &ast::Arguments) { + fn validate_arguments(&mut self, arguments: &ast::Arguments<'ast>) { let mut all_arg_names = FxHashSet::with_capacity_and_hasher(arguments.keywords.len(), FxBuildHasher); @@ -2316,21 +2341,21 @@ impl<'src> Parser<'src> { } #[derive(Debug)] -pub(super) struct ParsedExpr { - pub(super) expr: Expr, +pub(super) struct ParsedExpr<'ast> { + pub(super) expr: Expr<'ast>, pub(super) is_parenthesized: bool, } -impl ParsedExpr { +impl ParsedExpr<'_> { #[inline] pub(super) const fn is_unparenthesized_starred_expr(&self) -> bool { !self.is_parenthesized && self.expr.is_starred_expr() } } -impl From for ParsedExpr { +impl<'ast> From> for ParsedExpr<'ast> { #[inline] - fn from(expr: Expr) -> Self { + fn from(expr: Expr<'ast>) -> Self { ParsedExpr { expr, is_parenthesized: false, @@ -2338,15 +2363,15 @@ impl From for ParsedExpr { } } -impl Deref for ParsedExpr { - type Target = Expr; +impl<'ast> Deref for ParsedExpr<'ast> { + type Target = Expr<'ast>; fn deref(&self) -> &Self::Target { &self.expr } } -impl Ranged for ParsedExpr { +impl Ranged for ParsedExpr<'_> { #[inline] fn range(&self) -> TextRange { self.expr.range() diff --git a/crates/ruff_python_parser/src/parser/mod.rs b/crates/ruff_python_parser/src/parser/mod.rs index 08c85f7a07..318a4f3f9f 100644 --- a/crates/ruff_python_parser/src/parser/mod.rs +++ b/crates/ruff_python_parser/src/parser/mod.rs @@ -1,7 +1,7 @@ use std::cmp::Ordering; use bitflags::bitflags; - +use ruff_allocator::Allocator; use ruff_python_ast::{Mod, ModExpression, ModModule}; use ruff_text_size::{Ranged, TextRange, TextSize}; @@ -23,7 +23,7 @@ mod statement; mod tests; #[derive(Debug)] -pub(crate) struct Parser<'src> { +pub(crate) struct Parser<'src, 'ast> { source: &'src str, /// Token source for the parser that skips over any non-trivia token. @@ -47,16 +47,23 @@ pub(crate) struct Parser<'src> { /// The start offset in the source code from which to start parsing at. start_offset: TextSize, + + allocator: &'ast Allocator, } -impl<'src> Parser<'src> { +impl<'src, 'ast> Parser<'src, 'ast> { /// Create a new parser for the given source code. - pub(crate) fn new(source: &'src str, mode: Mode) -> Self { - Parser::new_starts_at(source, mode, TextSize::new(0)) + pub(crate) fn new(source: &'src str, mode: Mode, allocator: &'ast Allocator) -> Self { + Parser::new_starts_at(source, mode, TextSize::new(0), allocator) } /// Create a new parser for the given source code which starts parsing at the given offset. - pub(crate) fn new_starts_at(source: &'src str, mode: Mode, start_offset: TextSize) -> Self { + pub(crate) fn new_starts_at( + source: &'src str, + mode: Mode, + start_offset: TextSize, + allocator: &'ast Allocator, + ) -> Self { let tokens = TokenSource::from_source(source, mode, start_offset); Parser { @@ -68,11 +75,12 @@ impl<'src> Parser<'src> { prev_token_end: TextSize::new(0), start_offset, current_token_id: TokenId::default(), + allocator, } } /// Consumes the [`Parser`] and returns the parsed [`Parsed`]. - pub(crate) fn parse(mut self) -> Parsed { + pub(crate) fn parse(mut self) -> Parsed> { let syntax = match self.mode { Mode::Expression => Mod::Expression(self.parse_single_expression()), Mode::Module | Mode::Ipython => Mod::Module(self.parse_module()), @@ -89,7 +97,7 @@ impl<'src> Parser<'src> { /// /// After parsing a single expression, an error is reported and all remaining tokens are /// dropped by the parser. - fn parse_single_expression(&mut self) -> ModExpression { + fn parse_single_expression(&mut self) -> ModExpression<'ast> { let start = self.node_start(); let parsed_expr = self.parse_expression_list(ExpressionContext::default()); @@ -116,7 +124,7 @@ impl<'src> Parser<'src> { self.bump(TokenKind::EndOfFile); ModExpression { - body: Box::new(parsed_expr.expr), + body: parsed_expr.expr, range: self.node_range(start), } } @@ -124,7 +132,7 @@ impl<'src> Parser<'src> { /// Parses a Python module. /// /// This is to be used for [`Mode::Module`] and [`Mode::Ipython`]. - fn parse_module(&mut self) -> ModModule { + fn parse_module(&mut self) -> ModModule<'ast> { let body = self.parse_list_into_vec( RecoveryContextKind::ModuleStatements, Parser::parse_statement, @@ -138,6 +146,21 @@ impl<'src> Parser<'src> { } } + fn alloc_box(&self, value: T) -> ruff_allocator::Box<'ast, T> { + ruff_allocator::Box::new_in(value, &self.allocator) + } + + fn alloc(&self, value: T) -> &'ast mut T { + self.allocator.alloc(value) + } + + fn alloc_str(&self, value: T) -> &'ast mut str + where + T: AsRef, + { + self.allocator.alloc_str(value.as_ref()) + } + fn finish(self, syntax: Mod) -> Parsed { assert_eq!( self.current_token_kind(), @@ -444,7 +467,7 @@ impl<'src> Parser<'src> { fn parse_list_into_vec( &mut self, recovery_context_kind: RecoveryContextKind, - parse_element: impl Fn(&mut Parser<'src>) -> T, + parse_element: impl Fn(&mut Parser<'src, 'ast>) -> T, ) -> Vec { let mut elements = Vec::new(); self.parse_list(recovery_context_kind, |p| elements.push(parse_element(p))); @@ -461,7 +484,7 @@ impl<'src> Parser<'src> { fn parse_list( &mut self, recovery_context_kind: RecoveryContextKind, - mut parse_element: impl FnMut(&mut Parser<'src>), + mut parse_element: impl FnMut(&mut Parser<'src, 'ast>), ) { let mut progress = ParserProgress::default(); @@ -503,7 +526,7 @@ impl<'src> Parser<'src> { fn parse_comma_separated_list_into_vec( &mut self, recovery_context_kind: RecoveryContextKind, - parse_element: impl Fn(&mut Parser<'src>) -> T, + parse_element: impl Fn(&mut Parser<'src, 'ast>) -> T, ) -> Vec { let mut elements = Vec::new(); self.parse_comma_separated_list(recovery_context_kind, |p| elements.push(parse_element(p))); @@ -520,7 +543,7 @@ impl<'src> Parser<'src> { fn parse_comma_separated_list( &mut self, recovery_context_kind: RecoveryContextKind, - mut parse_element: impl FnMut(&mut Parser<'src>), + mut parse_element: impl FnMut(&mut Parser<'src, 'ast>), ) { let mut progress = ParserProgress::default(); diff --git a/crates/ruff_python_parser/src/parser/pattern.rs b/crates/ruff_python_parser/src/parser/pattern.rs index 0685913e3b..0ca9cfe1e2 100644 --- a/crates/ruff_python_parser/src/parser/pattern.rs +++ b/crates/ruff_python_parser/src/parser/pattern.rs @@ -1,4 +1,3 @@ -use ruff_python_ast::name::Name; use ruff_python_ast::{self as ast, Expr, ExprContext, Number, Operator, Pattern, Singleton}; use ruff_text_size::{Ranged, TextSize}; @@ -49,7 +48,7 @@ const MAPPING_PATTERN_START_SET: TokenSet = TokenSet::new([ ]) .union(LITERAL_PATTERN_START_SET); -impl<'src> Parser<'src> { +impl<'src, 'ast> Parser<'src, 'ast> { /// Returns `true` if the current token is a valid start of a pattern. pub(super) fn at_pattern_start(&self) -> bool { self.at_ts(PATTERN_START_SET) || self.at_soft_keyword() @@ -63,7 +62,7 @@ impl<'src> Parser<'src> { /// Entry point to start parsing a pattern. /// /// See: - pub(super) fn parse_match_patterns(&mut self) -> Pattern { + pub(super) fn parse_match_patterns(&mut self) -> Pattern<'ast> { let start = self.node_start(); // We don't yet know if it's a sequence pattern or a single pattern, so @@ -84,7 +83,7 @@ impl<'src> Parser<'src> { /// Parses an `or_pattern` or an `as_pattern`. /// /// See: - fn parse_match_pattern(&mut self, allow_star_pattern: AllowStarPattern) -> Pattern { + fn parse_match_pattern(&mut self, allow_star_pattern: AllowStarPattern) -> Pattern<'ast> { let start = self.node_start(); // We don't yet know if it's an or pattern or an as pattern, so use whatever @@ -124,7 +123,7 @@ impl<'src> Parser<'src> { lhs = Pattern::MatchAs(ast::PatternMatchAs { range: self.node_range(start), name: Some(ident), - pattern: Some(Box::new(lhs)), + pattern: Some(self.alloc_box(lhs)), }); } @@ -134,7 +133,7 @@ impl<'src> Parser<'src> { /// Parses a pattern. /// /// See: - fn parse_match_pattern_lhs(&mut self, allow_star_pattern: AllowStarPattern) -> Pattern { + fn parse_match_pattern_lhs(&mut self, allow_star_pattern: AllowStarPattern) -> Pattern<'ast> { let start = self.node_start(); let mut lhs = match self.current_token_kind() { @@ -171,7 +170,7 @@ impl<'src> Parser<'src> { /// If the parser isn't positioned at a `{` token. /// /// See: - fn parse_match_pattern_mapping(&mut self) -> ast::PatternMatchMapping { + fn parse_match_pattern_mapping(&mut self) -> ast::PatternMatchMapping<'ast> { let start = self.node_start(); self.bump(TokenKind::Lbrace); @@ -199,7 +198,7 @@ impl<'src> Parser<'src> { rest = Some(identifier); } else { let key = match parser.parse_match_pattern_lhs(AllowStarPattern::No) { - Pattern::MatchValue(ast::PatternMatchValue { value, .. }) => *value, + Pattern::MatchValue(ast::PatternMatchValue { value, .. }) => value.into_inner(), Pattern::MatchSingleton(ast::PatternMatchSingleton { value, range }) => { match value { Singleton::None => Expr::NoneLiteral(ast::ExprNoneLiteral { range }), @@ -217,7 +216,7 @@ impl<'src> Parser<'src> { ParseErrorType::OtherError("Invalid mapping pattern key".to_string()), &pattern, ); - recovery::pattern_to_expr(pattern) + recovery::pattern_to_expr(pattern, &parser.allocator) } }; keys.push(key); @@ -254,7 +253,7 @@ impl<'src> Parser<'src> { /// If the parser isn't positioned at a `*` token. /// /// See: - fn parse_match_pattern_star(&mut self) -> ast::PatternMatchStar { + fn parse_match_pattern_star(&mut self) -> ast::PatternMatchStar<'ast> { let start = self.node_start(); self.bump(TokenKind::Star); @@ -277,7 +276,7 @@ impl<'src> Parser<'src> { /// If the parser isn't positioned at a `(` or `[` token. /// /// See: - fn parse_parenthesized_or_sequence_pattern(&mut self) -> Pattern { + fn parse_parenthesized_or_sequence_pattern(&mut self) -> Pattern<'ast> { let start = self.node_start(); let parentheses = if self.eat(TokenKind::Lpar) { SequenceMatchPatternParentheses::Tuple @@ -333,10 +332,10 @@ impl<'src> Parser<'src> { /// [open sequence pattern]: https://docs.python.org/3/reference/compound_stmts.html#grammar-token-python-grammar-open_sequence_pattern fn parse_sequence_match_pattern( &mut self, - first_element: Pattern, + first_element: Pattern<'ast>, start: TextSize, parentheses: Option, - ) -> ast::PatternMatchSequence { + ) -> ast::PatternMatchSequence<'ast> { if parentheses.is_some_and(|parentheses| { self.at(parentheses.closing_kind()) || self.peek() == parentheses.closing_kind() }) { @@ -366,7 +365,7 @@ impl<'src> Parser<'src> { /// Parses a literal pattern. /// /// See: - fn parse_match_pattern_literal(&mut self) -> Pattern { + fn parse_match_pattern_literal(&mut self) -> Pattern<'ast> { let start = self.node_start(); match self.current_token_kind() { TokenKind::None => { @@ -394,7 +393,7 @@ impl<'src> Parser<'src> { let str = self.parse_strings(); Pattern::MatchValue(ast::PatternMatchValue { - value: Box::new(str), + value: self.alloc_box(str), range: self.node_range(start), }) } @@ -405,7 +404,7 @@ impl<'src> Parser<'src> { let range = self.node_range(start); Pattern::MatchValue(ast::PatternMatchValue { - value: Box::new(Expr::NumberLiteral(ast::ExprNumberLiteral { + value: self.alloc_box(Expr::NumberLiteral(ast::ExprNumberLiteral { value: Number::Complex { real, imag }, range, })), @@ -419,7 +418,7 @@ impl<'src> Parser<'src> { let range = self.node_range(start); Pattern::MatchValue(ast::PatternMatchValue { - value: Box::new(Expr::NumberLiteral(ast::ExprNumberLiteral { + value: self.alloc_box(Expr::NumberLiteral(ast::ExprNumberLiteral { value: Number::Int(value), range, })), @@ -433,7 +432,7 @@ impl<'src> Parser<'src> { let range = self.node_range(start); Pattern::MatchValue(ast::PatternMatchValue { - value: Box::new(Expr::NumberLiteral(ast::ExprNumberLiteral { + value: self.alloc_box(Expr::NumberLiteral(ast::ExprNumberLiteral { value: Number::Float(value), range, })), @@ -462,7 +461,7 @@ impl<'src> Parser<'src> { } return Pattern::MatchValue(ast::PatternMatchValue { - value: Box::new(Expr::UnaryOp(unary_expr)), + value: self.alloc_box(Expr::UnaryOp(unary_expr)), range: self.node_range(start), }); } @@ -481,7 +480,7 @@ impl<'src> Parser<'src> { let attribute = self.parse_attr_expr_for_match_pattern(id, start); Pattern::MatchValue(ast::PatternMatchValue { - value: Box::new(attribute), + value: self.alloc_box(attribute), range: self.node_range(start), }) } else { @@ -511,12 +510,12 @@ impl<'src> Parser<'src> { ); let invalid_node = Expr::Name(ast::ExprName { range: self.missing_node_range(), - id: Name::empty(), + id: "", ctx: ExprContext::Invalid, }); Pattern::MatchValue(ast::PatternMatchValue { range: invalid_node.range(), - value: Box::new(invalid_node), + value: self.alloc_box(invalid_node), }) } } @@ -533,9 +532,9 @@ impl<'src> Parser<'src> { /// See: fn parse_complex_literal_pattern( &mut self, - lhs: Pattern, + lhs: Pattern<'ast>, start: TextSize, - ) -> ast::PatternMatchValue { + ) -> ast::PatternMatchValue<'ast> { let operator = if self.eat(TokenKind::Plus) { Operator::Add } else { @@ -550,7 +549,7 @@ impl<'src> Parser<'src> { lhs.value } else { self.add_error(ParseErrorType::ExpectedRealNumber, &lhs); - Box::new(recovery::pattern_to_expr(lhs)) + self.alloc_box(recovery::pattern_to_expr(lhs, &self.allocator)) }; let rhs_pattern = self.parse_match_pattern_lhs(AllowStarPattern::No); @@ -561,13 +560,13 @@ impl<'src> Parser<'src> { rhs.value } else { self.add_error(ParseErrorType::ExpectedImaginaryNumber, &rhs_pattern); - Box::new(recovery::pattern_to_expr(rhs_pattern)) + self.alloc_box(recovery::pattern_to_expr(rhs_pattern, &self.allocator)) }; let range = self.node_range(start); ast::PatternMatchValue { - value: Box::new(Expr::BinOp(ast::ExprBinOp { + value: self.alloc_box(Expr::BinOp(ast::ExprBinOp { left: lhs_value, op: operator, right: rhs_value, @@ -578,7 +577,11 @@ impl<'src> Parser<'src> { } /// Parses an attribute expression until the current token is not a `.`. - fn parse_attr_expr_for_match_pattern(&mut self, mut lhs: Expr, start: TextSize) -> Expr { + fn parse_attr_expr_for_match_pattern( + &mut self, + mut lhs: Expr<'ast>, + start: TextSize, + ) -> Expr<'ast> { while self.current_token_kind() == TokenKind::Dot { lhs = Expr::Attribute(self.parse_attribute_expression(lhs, start)); } @@ -597,9 +600,9 @@ impl<'src> Parser<'src> { /// [pattern arguments]: https://docs.python.org/3/reference/compound_stmts.html#grammar-token-python-grammar-pattern_arguments fn parse_match_pattern_class( &mut self, - cls: Pattern, + cls: Pattern<'ast>, start: TextSize, - ) -> ast::PatternMatchClass { + ) -> ast::PatternMatchClass<'ast> { let arguments_start = self.node_start(); let cls = match cls { @@ -609,15 +612,15 @@ impl<'src> Parser<'src> { .. }) => { if ident.is_valid() { - Box::new(Expr::Name(ast::ExprName { + self.alloc_box(Expr::Name(ast::ExprName { range: ident.range(), id: ident.id, ctx: ExprContext::Load, })) } else { - Box::new(Expr::Name(ast::ExprName { + self.alloc_box(Expr::Name(ast::ExprName { range: ident.range(), - id: Name::empty(), + id: "", ctx: ExprContext::Invalid, })) } @@ -632,7 +635,7 @@ impl<'src> Parser<'src> { ParseErrorType::OtherError("Invalid value for a class pattern".to_string()), &pattern, ); - Box::new(recovery::pattern_to_expr(pattern)) + self.alloc_box(recovery::pattern_to_expr(pattern, &self.allocator)) } }; @@ -668,7 +671,7 @@ impl<'src> Parser<'src> { &pattern, ); ast::Identifier { - id: Name::empty(), + id: "", range: parser.missing_node_range(), } }; @@ -724,7 +727,7 @@ impl AllowStarPattern { /// Returns `true` if the given expression is a real number literal or a unary /// addition or subtraction of a real number literal. -const fn is_real_number(expr: &Expr) -> bool { +fn is_real_number(expr: &Expr) -> bool { match expr { Expr::NumberLiteral(ast::ExprNumberLiteral { value: ast::Number::Int(_) | ast::Number::Float(_), diff --git a/crates/ruff_python_parser/src/parser/recovery.rs b/crates/ruff_python_parser/src/parser/recovery.rs index 1dd4489a8c..ca14ad1177 100644 --- a/crates/ruff_python_parser/src/parser/recovery.rs +++ b/crates/ruff_python_parser/src/parser/recovery.rs @@ -1,4 +1,4 @@ -use ruff_python_ast::name::Name; +use ruff_allocator::Allocator; use ruff_python_ast::{self as ast, Expr, ExprContext, Pattern}; use ruff_text_size::{Ranged, TextLen, TextRange}; @@ -25,7 +25,10 @@ use ruff_text_size::{Ranged, TextLen, TextRange}; /// This function returns an invalid [`ast::ExprName`] if the given pattern is a [`Pattern::MatchAs`] /// with both the pattern and name present. This is because it cannot be converted to an expression /// without dropping one of them as there's no way to represent `x as y` as a valid expression. -pub(super) fn pattern_to_expr(pattern: Pattern) -> Expr { +pub(super) fn pattern_to_expr<'ast>( + pattern: Pattern<'ast>, + allocator: &'ast Allocator, +) -> Expr<'ast> { match pattern { Pattern::MatchSingleton(ast::PatternMatchSingleton { range, value }) => match value { ast::Singleton::True => { @@ -37,11 +40,14 @@ pub(super) fn pattern_to_expr(pattern: Pattern) -> Expr { }), ast::Singleton::None => Expr::NoneLiteral(ast::ExprNoneLiteral { range }), }, - Pattern::MatchValue(ast::PatternMatchValue { value, .. }) => *value, + Pattern::MatchValue(ast::PatternMatchValue { value, .. }) => value.into_inner(), // We don't know which kind of sequence this is: `case [1, 2]:` or `case (1, 2):`. Pattern::MatchSequence(ast::PatternMatchSequence { range, patterns }) => { Expr::List(ast::ExprList { - elts: patterns.into_iter().map(pattern_to_expr).collect(), + elts: patterns + .into_iter() + .map(|pattern| pattern_to_expr(pattern, allocator)) + .collect(), ctx: ExprContext::Store, range, }) @@ -57,7 +63,7 @@ pub(super) fn pattern_to_expr(pattern: Pattern) -> Expr { .zip(patterns) .map(|(key, pattern)| ast::DictItem { key: Some(key), - value: pattern_to_expr(pattern), + value: pattern_to_expr(pattern, allocator), }) .collect(); if let Some(rest) = rest { @@ -79,41 +85,46 @@ pub(super) fn pattern_to_expr(pattern: Pattern) -> Expr { func: cls, arguments: ast::Arguments { range: arguments.range, - args: arguments - .patterns - .into_iter() - .map(pattern_to_expr) - .collect(), - keywords: arguments - .keywords - .into_iter() - .map(|keyword_pattern| ast::Keyword { + args: allocator.alloc_slice_fill_iter( + arguments + .patterns + .into_iter() + .map(|pattern| pattern_to_expr(pattern, allocator)), + ), + keywords: allocator.alloc_slice_fill_iter(arguments.keywords.into_iter().map( + |keyword_pattern| ast::Keyword { range: keyword_pattern.range, arg: Some(keyword_pattern.attr), - value: pattern_to_expr(keyword_pattern.pattern), - }) - .collect(), + value: pattern_to_expr(keyword_pattern.pattern, allocator), + }, + )), }, }), Pattern::MatchStar(ast::PatternMatchStar { range, name }) => { if let Some(name) = name { Expr::Starred(ast::ExprStarred { range, - value: Box::new(Expr::Name(ast::ExprName { - range: name.range, - id: name.id, - ctx: ExprContext::Store, - })), + value: ruff_allocator::Box::new_in( + Expr::Name(ast::ExprName { + range: name.range, + id: name.id, + ctx: ExprContext::Store, + }), + allocator, + ), ctx: ExprContext::Store, }) } else { Expr::Starred(ast::ExprStarred { range, - value: Box::new(Expr::Name(ast::ExprName { - range: TextRange::new(range.end() - "_".text_len(), range.end()), - id: Name::new_static("_"), - ctx: ExprContext::Store, - })), + value: ruff_allocator::Box::new_in( + Expr::Name(ast::ExprName { + range: TextRange::new(range.end() - "_".text_len(), range.end()), + id: "_", + ctx: ExprContext::Store, + }), + allocator, + ), ctx: ExprContext::Store, }) } @@ -125,10 +136,10 @@ pub(super) fn pattern_to_expr(pattern: Pattern) -> Expr { }) => match (pattern, name) { (Some(_), Some(_)) => Expr::Name(ast::ExprName { range, - id: Name::empty(), + id: "", ctx: ExprContext::Invalid, }), - (Some(pattern), None) => pattern_to_expr(*pattern), + (Some(pattern), None) => pattern_to_expr(pattern.into_inner(), allocator), (None, Some(name)) => Expr::Name(ast::ExprName { range: name.range, id: name.id, @@ -136,16 +147,16 @@ pub(super) fn pattern_to_expr(pattern: Pattern) -> Expr { }), (None, None) => Expr::Name(ast::ExprName { range, - id: Name::new_static("_"), + id: "_", ctx: ExprContext::Store, }), }, Pattern::MatchOr(ast::PatternMatchOr { patterns, .. }) => { - let to_bin_expr = |left: Pattern, right: Pattern| ast::ExprBinOp { + let to_bin_expr = |left: Pattern<'ast>, right: Pattern<'ast>| ast::ExprBinOp { range: TextRange::new(left.start(), right.end()), - left: Box::new(pattern_to_expr(left)), + left: ruff_allocator::Box::new_in(pattern_to_expr(left, allocator), allocator), op: ast::Operator::BitOr, - right: Box::new(pattern_to_expr(right)), + right: ruff_allocator::Box::new_in(pattern_to_expr(right, allocator), allocator), }; let mut iter = patterns.into_iter(); @@ -155,9 +166,12 @@ pub(super) fn pattern_to_expr(pattern: Pattern) -> Expr { Expr::BinOp(iter.fold(to_bin_expr(left, right), |expr_bin_op, pattern| { ast::ExprBinOp { range: TextRange::new(expr_bin_op.start(), pattern.end()), - left: Box::new(Expr::BinOp(expr_bin_op)), + left: ruff_allocator::Box::new_in(Expr::BinOp(expr_bin_op), allocator), op: ast::Operator::BitOr, - right: Box::new(pattern_to_expr(pattern)), + right: ruff_allocator::Box::new_in( + pattern_to_expr(pattern, allocator), + allocator, + ), } })) } diff --git a/crates/ruff_python_parser/src/parser/snapshots/ruff_python_parser__parser__tests__expr_mode_valid_syntax.snap.new b/crates/ruff_python_parser/src/parser/snapshots/ruff_python_parser__parser__tests__expr_mode_valid_syntax.snap.new new file mode 100644 index 0000000000..143d76db68 --- /dev/null +++ b/crates/ruff_python_parser/src/parser/snapshots/ruff_python_parser__parser__tests__expr_mode_valid_syntax.snap.new @@ -0,0 +1,12 @@ +--- +source: crates/ruff_python_parser/src/parser/tests.rs +assertion_line: 56 +expression: parsed.expr() +--- +Name( + ExprName { + range: 0..5, + id: "first", + ctx: Load, + }, +) diff --git a/crates/ruff_python_parser/src/parser/snapshots/ruff_python_parser__parser__tests__ipython_escape_commands.snap.new b/crates/ruff_python_parser/src/parser/snapshots/ruff_python_parser__parser__tests__ipython_escape_commands.snap.new new file mode 100644 index 0000000000..ba1bb6f20e --- /dev/null +++ b/crates/ruff_python_parser/src/parser/snapshots/ruff_python_parser__parser__tests__ipython_escape_commands.snap.new @@ -0,0 +1,400 @@ +--- +source: crates/ruff_python_parser/src/parser/tests.rs +assertion_line: 144 +expression: parsed.syntax() +--- +Module( + ModModule { + range: 0..929, + body: [ + Expr( + StmtExpr { + range: 21..42, + value: BinOp( + ExprBinOp { + range: 27..40, + left: Name( + ExprName { + range: 27..28, + id: "a", + ctx: Load, + }, + ), + op: Mod, + right: Name( + ExprName { + range: 39..40, + id: "b", + ctx: Load, + }, + ), + }, + ), + }, + ), + IpyEscapeCommand( + StmtIpyEscapeCommand { + range: 66..73, + kind: Help2, + value: "a.foo", + }, + ), + IpyEscapeCommand( + StmtIpyEscapeCommand { + range: 74..80, + kind: Help, + value: "a.foo", + }, + ), + IpyEscapeCommand( + StmtIpyEscapeCommand { + range: 81..88, + kind: Help, + value: "a.foo", + }, + ), + IpyEscapeCommand( + StmtIpyEscapeCommand { + range: 89..100, + kind: Help2, + value: "a.foo()", + }, + ), + IpyEscapeCommand( + StmtIpyEscapeCommand { + range: 115..128, + kind: Magic, + value: "timeit a = b", + }, + ), + IpyEscapeCommand( + StmtIpyEscapeCommand { + range: 129..147, + kind: Magic, + value: "timeit foo(b) % 3", + }, + ), + IpyEscapeCommand( + StmtIpyEscapeCommand { + range: 148..176, + kind: Magic, + value: "alias showPath pwd && ls -a", + }, + ), + IpyEscapeCommand( + StmtIpyEscapeCommand { + range: 177..205, + kind: Magic, + value: "timeit a = foo(b); b = 2", + }, + ), + IpyEscapeCommand( + StmtIpyEscapeCommand { + range: 206..226, + kind: Magic, + value: "matplotlib --inline", + }, + ), + IpyEscapeCommand( + StmtIpyEscapeCommand { + range: 227..253, + kind: Magic, + value: "matplotlib --inline", + }, + ), + IpyEscapeCommand( + StmtIpyEscapeCommand { + range: 277..309, + kind: Shell, + value: "pwd && ls -a | sed 's/^/\\ /'", + }, + ), + IpyEscapeCommand( + StmtIpyEscapeCommand { + range: 310..347, + kind: Shell, + value: "pwd && ls -a | sed 's/^/\\\\ /'", + }, + ), + IpyEscapeCommand( + StmtIpyEscapeCommand { + range: 348..393, + kind: ShCap, + value: "cd /Users/foo/Library/Application\\ Support/", + }, + ), + FunctionDef( + StmtFunctionDef { + range: 566..626, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 570..573, + }, + type_params: None, + parameters: Parameters { + range: 573..575, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Return( + StmtReturn { + range: 581..626, + value: Some( + Compare( + ExprCompare { + range: 598..620, + left: Name( + ExprName { + range: 598..599, + id: "a", + ctx: Load, + }, + ), + ops: [ + NotEq, + ], + comparators: [ + Name( + ExprName { + range: 619..620, + id: "b", + ctx: Load, + }, + ), + ], + }, + ), + ), + }, + ), + ], + }, + ), + IpyEscapeCommand( + StmtIpyEscapeCommand { + range: 656..664, + kind: Paren, + value: "foo 1 2", + }, + ), + IpyEscapeCommand( + StmtIpyEscapeCommand { + range: 665..673, + kind: Quote2, + value: "foo 1 2", + }, + ), + IpyEscapeCommand( + StmtIpyEscapeCommand { + range: 674..682, + kind: Quote, + value: "foo 1 2", + }, + ), + For( + StmtFor { + range: 711..737, + is_async: false, + target: Name( + ExprName { + range: 715..716, + id: "a", + ctx: Store, + }, + ), + iter: Call( + ExprCall { + range: 720..728, + func: Name( + ExprName { + range: 720..725, + id: "range", + ctx: Load, + }, + ), + arguments: Arguments { + range: 725..728, + args: [ + NumberLiteral( + ExprNumberLiteral { + range: 726..727, + value: Int( + 5, + ), + }, + ), + ], + keywords: [], + }, + }, + ), + body: [ + IpyEscapeCommand( + StmtIpyEscapeCommand { + range: 734..737, + kind: Shell, + value: "ls", + }, + ), + ], + orelse: [], + }, + ), + Assign( + StmtAssign { + range: 739..748, + targets: [ + Name( + ExprName { + range: 739..741, + id: "p1", + ctx: Store, + }, + ), + ], + value: IpyEscapeCommand( + ExprIpyEscapeCommand { + range: 744..748, + kind: Shell, + value: "pwd", + }, + ), + }, + ), + AnnAssign( + StmtAnnAssign { + range: 749..763, + target: Name( + ExprName { + range: 749..751, + id: "p2", + ctx: Store, + }, + ), + annotation: Name( + ExprName { + range: 753..756, + id: "str", + ctx: Load, + }, + ), + value: Some( + IpyEscapeCommand( + ExprIpyEscapeCommand { + range: 759..763, + kind: Shell, + value: "pwd", + }, + ), + ), + simple: true, + }, + ), + Assign( + StmtAssign { + range: 764..784, + targets: [ + Name( + ExprName { + range: 764..767, + id: "foo", + ctx: Store, + }, + ), + ], + value: IpyEscapeCommand( + ExprIpyEscapeCommand { + range: 770..784, + kind: Magic, + value: "foo bar", + }, + ), + }, + ), + IpyEscapeCommand( + StmtIpyEscapeCommand { + range: 786..791, + kind: Magic, + value: " foo", + }, + ), + Assign( + StmtAssign { + range: 792..813, + targets: [ + Name( + ExprName { + range: 792..795, + id: "foo", + ctx: Store, + }, + ), + ], + value: IpyEscapeCommand( + ExprIpyEscapeCommand { + range: 798..813, + kind: Magic, + value: "foo # comment", + }, + ), + }, + ), + IpyEscapeCommand( + StmtIpyEscapeCommand { + range: 838..842, + kind: Help, + value: "foo", + }, + ), + IpyEscapeCommand( + StmtIpyEscapeCommand { + range: 843..852, + kind: Help2, + value: "foo.bar", + }, + ), + IpyEscapeCommand( + StmtIpyEscapeCommand { + range: 853..865, + kind: Help, + value: "foo.bar.baz", + }, + ), + IpyEscapeCommand( + StmtIpyEscapeCommand { + range: 866..874, + kind: Help2, + value: "foo[0]", + }, + ), + IpyEscapeCommand( + StmtIpyEscapeCommand { + range: 875..885, + kind: Help, + value: "foo[0][1]", + }, + ), + IpyEscapeCommand( + StmtIpyEscapeCommand { + range: 886..905, + kind: Help2, + value: "foo.bar[0].baz[1]", + }, + ), + IpyEscapeCommand( + StmtIpyEscapeCommand { + range: 906..929, + kind: Help2, + value: "foo.bar[0].baz[2].egg", + }, + ), + ], + }, +) diff --git a/crates/ruff_python_parser/src/parser/snapshots/ruff_python_parser__parser__tests__unicode_aliases.snap.new b/crates/ruff_python_parser/src/parser/snapshots/ruff_python_parser__parser__tests__unicode_aliases.snap.new new file mode 100644 index 0000000000..88a0bab7a3 --- /dev/null +++ b/crates/ruff_python_parser/src/parser/snapshots/ruff_python_parser__parser__tests__unicode_aliases.snap.new @@ -0,0 +1,39 @@ +--- +source: crates/ruff_python_parser/src/parser/tests.rs +assertion_line: 66 +expression: suite +--- +[ + Assign( + StmtAssign { + range: 0..37, + targets: [ + Name( + ExprName { + range: 0..1, + id: "x", + ctx: Store, + }, + ), + ], + value: StringLiteral( + ExprStringLiteral { + range: 4..37, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 4..37, + value: "\u{8}another cool trick", + flags: StringLiteralFlags { + quote_style: Double, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + }, + ), +] diff --git a/crates/ruff_python_parser/src/parser/statement.rs b/crates/ruff_python_parser/src/parser/statement.rs index e10487e18c..3e0678595b 100644 --- a/crates/ruff_python_parser/src/parser/statement.rs +++ b/crates/ruff_python_parser/src/parser/statement.rs @@ -1,9 +1,7 @@ -use compact_str::CompactString; use std::fmt::Display; use rustc_hash::{FxBuildHasher, FxHashSet}; -use ruff_python_ast::name::Name; use ruff_python_ast::{ self as ast, ExceptHandler, Expr, ExprContext, IpyEscapeKind, Operator, Stmt, WithItem, }; @@ -77,7 +75,7 @@ const AUGMENTED_ASSIGN_SET: TokenSet = TokenSet::new([ TokenKind::RightShiftEqual, ]); -impl<'src> Parser<'src> { +impl<'src, 'ast> Parser<'src, 'ast> { /// Returns `true` if the current token is the start of a compound statement. pub(super) fn at_compound_stmt(&self) -> bool { self.at_ts(COMPOUND_STMT_SET) @@ -109,7 +107,7 @@ impl<'src> Parser<'src> { /// See: /// - /// - - pub(super) fn parse_statement(&mut self) -> Stmt { + pub(super) fn parse_statement(&mut self) -> Stmt<'ast> { let start = self.node_start(); match self.current_token_kind() { @@ -150,7 +148,7 @@ impl<'src> Parser<'src> { /// This statement must be terminated by a newline or semicolon. /// /// Use [`Parser::parse_simple_statements`] to parse a sequence of simple statements. - fn parse_single_simple_statement(&mut self) -> Stmt { + fn parse_single_simple_statement(&mut self) -> Stmt<'ast> { let stmt = self.parse_simple_statement(); // The order of the token is important here. @@ -189,7 +187,7 @@ impl<'src> Parser<'src> { /// Matches the `simple_stmts` rule in the [Python grammar]. /// /// [Python grammar]: https://docs.python.org/3/reference/grammar.html - fn parse_simple_statements(&mut self) -> Vec { + fn parse_simple_statements(&mut self) -> Vec> { let mut stmts = vec![]; let mut progress = ParserProgress::default(); @@ -259,7 +257,7 @@ impl<'src> Parser<'src> { /// Parses a simple statement. /// /// See: - fn parse_simple_statement(&mut self) -> Stmt { + fn parse_simple_statement(&mut self) -> Stmt<'ast> { match self.current_token_kind() { TokenKind::Return => Stmt::Return(self.parse_return_statement()), TokenKind::Import => Stmt::Import(self.parse_import_statement()), @@ -311,7 +309,7 @@ impl<'src> Parser<'src> { } else { Stmt::Expr(ast::StmtExpr { range: self.node_range(start), - value: Box::new(parsed_expr.expr), + value: self.alloc_box(parsed_expr.expr), }) } } @@ -325,7 +323,7 @@ impl<'src> Parser<'src> { /// If the parser isn't positioned at a `del` token. /// /// See: - fn parse_delete_statement(&mut self) -> ast::StmtDelete { + fn parse_delete_statement(&mut self) -> ast::StmtDelete<'ast> { let start = self.node_start(); self.bump(TokenKind::Del); @@ -377,7 +375,7 @@ impl<'src> Parser<'src> { /// If the parser isn't positioned at a `return` token. /// /// See: - fn parse_return_statement(&mut self) -> ast::StmtReturn { + fn parse_return_statement(&mut self) -> ast::StmtReturn<'ast> { let start = self.node_start(); self.bump(TokenKind::Return); @@ -388,10 +386,10 @@ impl<'src> Parser<'src> { // return x := 1 // return *x and y let value = self.at_expr().then(|| { - Box::new( - self.parse_expression_list(ExpressionContext::starred_bitwise_or()) - .expr, - ) + let value = self + .parse_expression_list(ExpressionContext::starred_bitwise_or()) + .expr; + self.alloc_box(value) }); ast::StmtReturn { @@ -407,7 +405,7 @@ impl<'src> Parser<'src> { /// If the parser isn't positioned at a `raise` token. /// /// See: - fn parse_raise_statement(&mut self) -> ast::StmtRaise { + fn parse_raise_statement(&mut self) -> ast::StmtRaise<'ast> { let start = self.node_start(); self.bump(TokenKind::Raise); @@ -432,7 +430,7 @@ impl<'src> Parser<'src> { self.add_error(ParseErrorType::UnparenthesizedTupleExpression, &exc); } - Some(Box::new(exc.expr)) + Some(self.alloc_box(exc.expr)) }; let cause = (exc.is_some() && self.eat(TokenKind::From)).then(|| { @@ -453,7 +451,7 @@ impl<'src> Parser<'src> { self.add_error(ParseErrorType::UnparenthesizedTupleExpression, &cause); } - Box::new(cause.expr) + self.alloc_box(cause.expr) }); ast::StmtRaise { @@ -470,7 +468,7 @@ impl<'src> Parser<'src> { /// If the parser isn't positioned at an `import` token. /// /// See: - fn parse_import_statement(&mut self) -> ast::StmtImport { + fn parse_import_statement(&mut self) -> ast::StmtImport<'ast> { let start = self.node_start(); self.bump(TokenKind::Import); @@ -510,7 +508,7 @@ impl<'src> Parser<'src> { /// If the parser isn't positioned at a `from` token. /// /// See: - fn parse_from_import_statement(&mut self) -> ast::StmtImportFrom { + fn parse_from_import_statement(&mut self) -> ast::StmtImportFrom<'ast> { let start = self.node_start(); self.bump(TokenKind::From); @@ -619,15 +617,12 @@ impl<'src> Parser<'src> { /// See: /// - /// - - fn parse_alias(&mut self, style: ImportStyle) -> ast::Alias { + fn parse_alias(&mut self, style: ImportStyle) -> ast::Alias<'ast> { let start = self.node_start(); if self.eat(TokenKind::Star) { let range = self.node_range(start); return ast::Alias { - name: ast::Identifier { - id: Name::new_static("*"), - range, - }, + name: ast::Identifier { id: "*", range }, asname: None, range, }; @@ -668,10 +663,11 @@ impl<'src> Parser<'src> { /// Parses a dotted name. /// /// A dotted name is a sequence of identifiers separated by a single dot. - fn parse_dotted_name(&mut self) -> ast::Identifier { + fn parse_dotted_name(&mut self) -> ast::Identifier<'ast> { let start = self.node_start(); - let mut dotted_name: CompactString = self.parse_identifier().id.into(); + let id = self.parse_identifier().id; + let mut dotted_name = ruff_allocator::String::from_str_in(id, &self.allocator); let mut progress = ParserProgress::default(); while self.eat(TokenKind::Dot) { @@ -688,7 +684,7 @@ impl<'src> Parser<'src> { // import a.b.c // import a . b . c ast::Identifier { - id: Name::from(dotted_name), + id: dotted_name.into_bump_str(), range: self.node_range(start), } } @@ -745,7 +741,7 @@ impl<'src> Parser<'src> { /// If the parser isn't positioned at an `assert` token. /// /// See: - fn parse_assert_statement(&mut self) -> ast::StmtAssert { + fn parse_assert_statement(&mut self) -> ast::StmtAssert<'ast> { let start = self.node_start(); self.bump(TokenKind::Assert); @@ -766,7 +762,8 @@ impl<'src> Parser<'src> { // assert False, assert x // assert False, yield x // assert False, x := 1 - Some(Box::new(self.parse_conditional_expression_or_higher().expr)) + let expression = self.parse_conditional_expression_or_higher().expr; + Some(self.alloc_box(expression)) } else { // test_err assert_empty_msg // assert x, @@ -781,7 +778,7 @@ impl<'src> Parser<'src> { }; ast::StmtAssert { - test: Box::new(test.expr), + test: self.alloc_box(test.expr), msg, range: self.node_range(start), } @@ -794,7 +791,7 @@ impl<'src> Parser<'src> { /// If the parser isn't positioned at a `global` token. /// /// See: - fn parse_global_statement(&mut self) -> ast::StmtGlobal { + fn parse_global_statement(&mut self) -> ast::StmtGlobal<'ast> { let start = self.node_start(); self.bump(TokenKind::Global); @@ -832,7 +829,7 @@ impl<'src> Parser<'src> { /// If the parser isn't positioned at a `nonlocal` token. /// /// See: - fn parse_nonlocal_statement(&mut self) -> ast::StmtNonlocal { + fn parse_nonlocal_statement(&mut self) -> ast::StmtNonlocal<'ast> { let start = self.node_start(); self.bump(TokenKind::Nonlocal); @@ -873,7 +870,7 @@ impl<'src> Parser<'src> { /// If the parser isn't positioned at a `type` token. /// /// See: - fn parse_type_alias_statement(&mut self) -> ast::StmtTypeAlias { + fn parse_type_alias_statement(&mut self) -> ast::StmtTypeAlias<'ast> { let start = self.node_start(); self.bump(TokenKind::Type); @@ -897,9 +894,9 @@ impl<'src> Parser<'src> { let value = self.parse_conditional_expression_or_higher(); ast::StmtTypeAlias { - name: Box::new(name), + name: self.alloc_box(name), type_params, - value: Box::new(value.expr), + value: self.alloc_box(value.expr), range: self.node_range(start), } } @@ -909,7 +906,7 @@ impl<'src> Parser<'src> { /// # Panics /// /// If the parser isn't positioned at an `IpyEscapeCommand` token. - fn parse_ipython_escape_command_statement(&mut self) -> ast::StmtIpyEscapeCommand { + fn parse_ipython_escape_command_statement(&mut self) -> ast::StmtIpyEscapeCommand<'ast> { let start = self.node_start(); let TokenValue::IpyEscapeCommand { value, kind } = @@ -923,7 +920,11 @@ impl<'src> Parser<'src> { self.add_error(ParseErrorType::UnexpectedIpythonEscapeCommand, range); } - ast::StmtIpyEscapeCommand { range, kind, value } + ast::StmtIpyEscapeCommand { + range, + kind, + value: self.alloc_str(&value), + } } /// Parses an IPython help end escape command at the statement level. @@ -933,15 +934,19 @@ impl<'src> Parser<'src> { /// If the parser isn't positioned at a `?` token. fn parse_ipython_help_end_escape_command_statement( &mut self, - parsed_expr: &ParsedExpr, - ) -> ast::StmtIpyEscapeCommand { + parsed_expr: &ParsedExpr<'ast>, + ) -> ast::StmtIpyEscapeCommand<'ast> { // We are permissive than the original implementation because we would allow whitespace // between the expression and the suffix while the IPython implementation doesn't allow it. // For example, `foo ?` would be valid in our case but invalid for IPython. - fn unparse_expr(parser: &mut Parser, expr: &Expr, buffer: &mut String) { + fn unparse_expr<'ast>( + parser: &mut Parser<'_, 'ast>, + expr: &Expr<'ast>, + buffer: &mut ruff_allocator::String, + ) { match expr { Expr::Name(ast::ExprName { id, .. }) => { - buffer.push_str(id.as_str()); + buffer.push_str(id); } Expr::Subscript(ast::ExprSubscript { value, slice, .. }) => { unparse_expr(parser, value, buffer); @@ -1012,11 +1017,11 @@ impl<'src> Parser<'src> { ); } - let mut value = String::new(); + let mut value = ruff_allocator::String::new_in(&self.allocator); unparse_expr(self, &parsed_expr.expr, &mut value); ast::StmtIpyEscapeCommand { - value: value.into_boxed_str(), + value: value.into_bump_str(), kind, range: self.node_range(parsed_expr.start()), } @@ -1029,7 +1034,11 @@ impl<'src> Parser<'src> { /// If the parser isn't positioned at an `=` token. /// /// See: - fn parse_assign_statement(&mut self, target: ParsedExpr, start: TextSize) -> ast::StmtAssign { + fn parse_assign_statement( + &mut self, + target: ParsedExpr<'ast>, + start: TextSize, + ) -> ast::StmtAssign<'ast> { self.bump(TokenKind::Equal); let mut targets = vec![target.expr]; @@ -1084,7 +1093,7 @@ impl<'src> Parser<'src> { ast::StmtAssign { targets, - value: Box::new(value.expr), + value: self.alloc_box(value.expr), range: self.node_range(start), } } @@ -1098,9 +1107,9 @@ impl<'src> Parser<'src> { /// See: fn parse_annotated_assignment_statement( &mut self, - mut target: ParsedExpr, + mut target: ParsedExpr<'ast>, start: TextSize, - ) -> ast::StmtAnnAssign { + ) -> ast::StmtAnnAssign<'ast> { self.bump(TokenKind::Colon); // test_err ann_assign_stmt_invalid_target @@ -1142,10 +1151,10 @@ impl<'src> Parser<'src> { // x: Any = *a and b // x: Any = x := 1 // x: list = [x, *a | b, *a or b] - Some(Box::new( - self.parse_expression_list(ExpressionContext::yield_or_starred_bitwise_or()) - .expr, - )) + let expression = self + .parse_expression_list(ExpressionContext::yield_or_starred_bitwise_or()) + .expr; + Some(self.alloc_box(expression)) } else { // test_err ann_assign_stmt_missing_rhs // x: int = @@ -1160,8 +1169,8 @@ impl<'src> Parser<'src> { }; ast::StmtAnnAssign { - target: Box::new(target.expr), - annotation: Box::new(annotation.expr), + target: self.alloc_box(target.expr), + annotation: self.alloc_box(annotation.expr), value, simple, range: self.node_range(start), @@ -1177,10 +1186,10 @@ impl<'src> Parser<'src> { /// See: fn parse_augmented_assignment_statement( &mut self, - mut target: ParsedExpr, + mut target: ParsedExpr<'ast>, op: Operator, start: TextSize, - ) -> ast::StmtAugAssign { + ) -> ast::StmtAugAssign<'ast> { // Consume the operator self.bump_ts(AUGMENTED_ASSIGN_SET); @@ -1215,9 +1224,9 @@ impl<'src> Parser<'src> { let value = self.parse_expression_list(ExpressionContext::yield_or_starred_bitwise_or()); ast::StmtAugAssign { - target: Box::new(target.expr), + target: self.alloc_box(target.expr), op, - value: Box::new(value.expr), + value: self.alloc_box(value.expr), range: self.node_range(start), } } @@ -1229,7 +1238,7 @@ impl<'src> Parser<'src> { /// If the parser isn't positioned at an `if` token. /// /// See: - fn parse_if_statement(&mut self) -> ast::StmtIf { + fn parse_if_statement(&mut self) -> ast::StmtIf<'ast> { let start = self.node_start(); self.bump(TokenKind::If); @@ -1270,7 +1279,7 @@ impl<'src> Parser<'src> { } ast::StmtIf { - test: Box::new(test.expr), + test: self.alloc_box(test.expr), body, elif_else_clauses, range: self.node_range(start), @@ -1282,7 +1291,7 @@ impl<'src> Parser<'src> { /// # Panics /// /// If the parser isn't positioned at an `elif` or `else` token. - fn parse_elif_or_else_clause(&mut self, kind: ElifOrElse) -> ast::ElifElseClause { + fn parse_elif_or_else_clause(&mut self, kind: ElifOrElse) -> ast::ElifElseClause<'ast> { let start = self.node_start(); self.bump(kind.as_token_kind()); @@ -1327,7 +1336,7 @@ impl<'src> Parser<'src> { /// If the parser isn't positioned at a `try` token. /// /// See: - fn parse_try_statement(&mut self) -> ast::StmtTry { + fn parse_try_statement(&mut self) -> ast::StmtTry<'ast> { let try_start = self.node_start(); self.bump(TokenKind::Try); self.expect(TokenKind::Colon); @@ -1436,7 +1445,7 @@ impl<'src> Parser<'src> { /// # Panics /// /// If the parser isn't positioned at an `except` token. - fn parse_except_clause(&mut self) -> (ExceptHandler, ExceptClauseKind) { + fn parse_except_clause(&mut self) -> (ExceptHandler<'ast>, ExceptClauseKind) { let start = self.node_start(); self.bump(TokenKind::Except); @@ -1484,7 +1493,7 @@ impl<'src> Parser<'src> { &parsed_expr, ); } - Some(Box::new(parsed_expr.expr)) + Some(self.alloc_box(parsed_expr.expr)) } else { if block_kind.is_star() || self.at(TokenKind::As) { // test_err except_stmt_missing_exception @@ -1566,7 +1575,7 @@ impl<'src> Parser<'src> { /// If the parser isn't positioned at a `for` token. /// /// See: - fn parse_for_statement(&mut self, start: TextSize) -> ast::StmtFor { + fn parse_for_statement(&mut self, start: TextSize) -> ast::StmtFor<'ast> { self.bump(TokenKind::For); // test_err for_stmt_missing_target @@ -1634,8 +1643,8 @@ impl<'src> Parser<'src> { }; ast::StmtFor { - target: Box::new(target.expr), - iter: Box::new(iter.expr), + target: self.alloc_box(target.expr), + iter: self.alloc_box(iter.expr), is_async: false, body, orelse, @@ -1650,7 +1659,7 @@ impl<'src> Parser<'src> { /// If the parser isn't positioned at a `while` token. /// /// See: - fn parse_while_statement(&mut self) -> ast::StmtWhile { + fn parse_while_statement(&mut self) -> ast::StmtWhile<'ast> { let start = self.node_start(); self.bump(TokenKind::While); @@ -1683,7 +1692,7 @@ impl<'src> Parser<'src> { }; ast::StmtWhile { - test: Box::new(test.expr), + test: self.alloc_box(test.expr), body, orelse, range: self.node_range(start), @@ -1704,9 +1713,9 @@ impl<'src> Parser<'src> { /// See: fn parse_function_definition( &mut self, - decorator_list: Vec, + decorator_list: Vec>, start: TextSize, - ) -> ast::StmtFunctionDef { + ) -> ast::StmtFunctionDef<'ast> { self.bump(TokenKind::Def); // test_err function_def_missing_identifier @@ -1770,7 +1779,7 @@ impl<'src> Parser<'src> { ); } - Some(Box::new(returns.expr)) + Some(self.alloc_box(returns.expr)) } else { // test_err function_def_missing_return_type // def foo() -> : ... @@ -1795,8 +1804,8 @@ impl<'src> Parser<'src> { ast::StmtFunctionDef { name, - type_params: type_params.map(Box::new), - parameters: Box::new(parameters), + type_params: type_params.map(|params| self.alloc_box(params)), + parameters: self.alloc_box(parameters), body, decorator_list, is_async: false, @@ -1817,9 +1826,9 @@ impl<'src> Parser<'src> { /// See: fn parse_class_definition( &mut self, - decorator_list: Vec, + decorator_list: Vec>, start: TextSize, - ) -> ast::StmtClassDef { + ) -> ast::StmtClassDef<'ast> { self.bump(TokenKind::Class); // test_err class_def_missing_name @@ -1837,9 +1846,10 @@ impl<'src> Parser<'src> { // test_ok class_def_arguments // class Foo: ... // class Foo(): ... - let arguments = self - .at(TokenKind::Lpar) - .then(|| Box::new(self.parse_arguments())); + let arguments = self.at(TokenKind::Lpar).then(|| { + let arguments = self.parse_arguments(); + self.alloc_box(arguments) + }); self.expect(TokenKind::Colon); @@ -1853,7 +1863,7 @@ impl<'src> Parser<'src> { range: self.node_range(start), decorator_list, name, - type_params: type_params.map(Box::new), + type_params: type_params.map(|params| self.alloc_box(params)), arguments, body, } @@ -1869,7 +1879,7 @@ impl<'src> Parser<'src> { /// If the parser isn't positioned at a `with` token. /// /// See: - fn parse_with_statement(&mut self, start: TextSize) -> ast::StmtWith { + fn parse_with_statement(&mut self, start: TextSize) -> ast::StmtWith<'ast> { self.bump(TokenKind::With); let items = self.parse_with_items(); @@ -1888,7 +1898,7 @@ impl<'src> Parser<'src> { /// Parses a list of with items. /// /// See: - fn parse_with_items(&mut self) -> Vec { + fn parse_with_items(&mut self) -> Vec> { if !self.at_expr() { self.add_error( ParseErrorType::OtherError( @@ -1959,7 +1969,7 @@ impl<'src> Parser<'src> { /// If the parser isn't positioned at a `(` token. /// /// See: - fn try_parse_parenthesized_with_items(&mut self) -> Option> { + fn try_parse_parenthesized_with_items(&mut self) -> Option>> { let checkpoint = self.checkpoint(); // We'll start with the assumption that the with items are parenthesized. @@ -2067,7 +2077,7 @@ impl<'src> Parser<'src> { /// Parses a single `with` item. /// /// See: - fn parse_with_item(&mut self, state: WithItemParsingState) -> ParsedWithItem { + fn parse_with_item(&mut self, state: WithItemParsingState) -> ParsedWithItem<'ast> { let start = self.node_start(); // The grammar for the context expression of a with item depends on the state @@ -2099,9 +2109,10 @@ impl<'src> Parser<'src> { WithItemParsingState::Regular => self.parse_conditional_expression_or_higher(), }; - let optional_vars = self - .at(TokenKind::As) - .then(|| Box::new(self.parse_with_item_optional_vars().expr)); + let optional_vars = self.at(TokenKind::As).then(|| { + let vars = self.parse_with_item_optional_vars().expr; + self.alloc_box(vars) + }); ParsedWithItem { is_parenthesized: context_expr.is_parenthesized, @@ -2118,7 +2129,7 @@ impl<'src> Parser<'src> { /// # Panics /// /// If the parser isn't positioned at an `as` token. - fn parse_with_item_optional_vars(&mut self) -> ParsedExpr { + fn parse_with_item_optional_vars(&mut self) -> ParsedExpr<'ast> { self.bump(TokenKind::As); let mut target = self @@ -2158,7 +2169,7 @@ impl<'src> Parser<'src> { /// If the parser isn't positioned at a `match` token. /// /// See: - fn try_parse_match_statement(&mut self) -> Option { + fn try_parse_match_statement(&mut self) -> Option> { let checkpoint = self.checkpoint(); let start = self.node_start(); @@ -2174,7 +2185,7 @@ impl<'src> Parser<'src> { let cases = self.parse_match_body(); Some(ast::StmtMatch { - subject: Box::new(subject), + subject: self.alloc_box(subject), cases, range: self.node_range(start), }) @@ -2196,7 +2207,7 @@ impl<'src> Parser<'src> { let cases = self.parse_match_body(); Some(ast::StmtMatch { - subject: Box::new(subject), + subject: self.alloc_box(subject), cases, range: self.node_range(start), }) @@ -2217,7 +2228,7 @@ impl<'src> Parser<'src> { /// If the parser isn't positioned at a `match` token. /// /// See: - fn parse_match_statement(&mut self) -> ast::StmtMatch { + fn parse_match_statement(&mut self) -> ast::StmtMatch<'ast> { let start = self.node_start(); self.bump(TokenKind::Match); @@ -2227,14 +2238,14 @@ impl<'src> Parser<'src> { let cases = self.parse_match_body(); ast::StmtMatch { - subject: Box::new(subject), + subject: self.alloc_box(subject), cases, range: self.node_range(start), } } /// Parses the subject expression for a `match` statement. - fn parse_match_subject_expression(&mut self) -> Expr { + fn parse_match_subject_expression(&mut self) -> Expr<'ast> { let start = self.node_start(); // Subject expression grammar is: @@ -2288,7 +2299,7 @@ impl<'src> Parser<'src> { /// /// This method expects that the parser is positioned at a `Newline` token. If not, it adds a /// syntax error and continues parsing. - fn parse_match_body(&mut self) -> Vec { + fn parse_match_body(&mut self) -> Vec> { // test_err match_stmt_no_newline_before_case // match foo: case _: ... self.expect(TokenKind::Newline); @@ -2315,7 +2326,7 @@ impl<'src> Parser<'src> { } /// Parses a list of match case blocks. - fn parse_match_case_blocks(&mut self) -> Vec { + fn parse_match_case_blocks(&mut self) -> Vec> { let mut cases = vec![]; if !self.at(TokenKind::Case) { @@ -2349,7 +2360,7 @@ impl<'src> Parser<'src> { /// If the parser isn't positioned at a `case` token. /// /// See: - fn parse_match_case(&mut self) -> ast::MatchCase { + fn parse_match_case(&mut self) -> ast::MatchCase<'ast> { let start = self.node_start(); self.bump(TokenKind::Case); @@ -2377,10 +2388,10 @@ impl<'src> Parser<'src> { // case y if (*a): ... // match x: // case y if yield x: ... - Some(Box::new( - self.parse_named_expression_or_higher(ExpressionContext::default()) - .expr, - )) + let guard = self + .parse_named_expression_or_higher(ExpressionContext::default()) + .expr; + Some(self.alloc_box(guard)) } else { // test_err match_stmt_missing_guard_expr // match x: @@ -2420,7 +2431,7 @@ impl<'src> Parser<'src> { /// - /// - /// - - fn parse_async_statement(&mut self) -> Stmt { + fn parse_async_statement(&mut self) -> Stmt<'ast> { let async_start = self.node_start(); self.bump(TokenKind::Async); @@ -2469,7 +2480,7 @@ impl<'src> Parser<'src> { /// Parses a decorator list followed by a class, function or async function definition. /// /// See: - fn parse_decorators(&mut self) -> Stmt { + fn parse_decorators(&mut self) -> Stmt<'ast> { let start = self.node_start(); let mut decorators = vec![]; @@ -2558,7 +2569,7 @@ impl<'src> Parser<'src> { /// /// This could either be a single statement that's on the same line as the /// clause header or an indented block. - fn parse_body(&mut self, parent_clause: Clause) -> Vec { + fn parse_body(&mut self, parent_clause: Clause) -> Vec> { // Note: The test cases in this method chooses a clause at random to test // the error logic. @@ -2604,7 +2615,7 @@ impl<'src> Parser<'src> { /// # Panics /// /// If the parser isn't positioned at an `Indent` token. - fn parse_block(&mut self) -> Vec { + fn parse_block(&mut self) -> Vec> { self.bump(TokenKind::Indent); let statements = @@ -2630,7 +2641,7 @@ impl<'src> Parser<'src> { start: TextSize, function_kind: FunctionKind, allow_star_annotation: AllowStarAnnotation, - ) -> ast::Parameter { + ) -> ast::Parameter<'ast> { let name = self.parse_identifier(); // Annotations are only allowed for function definition. For lambda expression, @@ -2668,7 +2679,7 @@ impl<'src> Parser<'src> { self.parse_conditional_expression_or_higher() } }; - Some(Box::new(parsed_expr.expr)) + Some(self.alloc_box(parsed_expr.expr)) } else { // test_err param_missing_annotation // def foo(x:): ... @@ -2702,7 +2713,7 @@ impl<'src> Parser<'src> { &mut self, start: TextSize, function_kind: FunctionKind, - ) -> ast::ParameterWithDefault { + ) -> ast::ParameterWithDefault<'ast> { let parameter = self.parse_parameter(start, function_kind, AllowStarAnnotation::No); let default = if self.eat(TokenKind::Equal) { @@ -2717,7 +2728,8 @@ impl<'src> Parser<'src> { // def foo(x=*int): ... // def foo(x=(*int)): ... // def foo(x=yield y): ... - Some(Box::new(self.parse_conditional_expression_or_higher().expr)) + let expression = self.parse_conditional_expression_or_higher().expr; + Some(self.alloc_box(expression)) } else { // test_err param_missing_default // def foo(x=): ... @@ -2742,7 +2754,10 @@ impl<'src> Parser<'src> { /// Parses a parameter list for the given function kind. /// /// See: - pub(super) fn parse_parameters(&mut self, function_kind: FunctionKind) -> ast::Parameters { + pub(super) fn parse_parameters( + &mut self, + function_kind: FunctionKind, + ) -> ast::Parameters<'ast> { let start = self.node_start(); if matches!(function_kind, FunctionKind::FunctionDef) { @@ -2813,7 +2828,7 @@ impl<'src> Parser<'src> { // TODO(dhruvmanila): The AST doesn't allow multiple `vararg`, so let's // choose to keep the first one so that the parameters remain in preorder. if parameters.vararg.is_none() { - parameters.vararg = Some(Box::new(param)); + parameters.vararg = Some(parser.alloc_box(param)); } last_keyword_only_separator_range = None; @@ -2887,7 +2902,7 @@ impl<'src> Parser<'src> { ); } - parameters.kwarg = Some(Box::new(param)); + parameters.kwarg = Some(parser.alloc_box(param)); last_keyword_only_separator_range = None; } TokenKind::Slash => { @@ -3009,7 +3024,7 @@ impl<'src> Parser<'src> { /// type parameter list, return `None`. /// /// See: - fn try_parse_type_params(&mut self) -> Option { + fn try_parse_type_params(&mut self) -> Option> { self.at(TokenKind::Lsqb).then(|| self.parse_type_params()) } @@ -3020,7 +3035,7 @@ impl<'src> Parser<'src> { /// If the parser isn't positioned at a `[` token. /// /// See: - fn parse_type_params(&mut self) -> ast::TypeParams { + fn parse_type_params(&mut self) -> ast::TypeParams<'ast> { let start = self.node_start(); self.bump(TokenKind::Lsqb); @@ -3048,7 +3063,7 @@ impl<'src> Parser<'src> { /// Parses a type parameter. /// /// See: - fn parse_type_param(&mut self) -> ast::TypeParam { + fn parse_type_param(&mut self) -> ast::TypeParam<'ast> { let start = self.node_start(); // TODO(dhruvmanila): CPython throws an error if `TypeVarTuple` or `ParamSpec` @@ -3078,12 +3093,12 @@ impl<'src> Parser<'src> { // type X[*Ts = yield x] = int // type X[*Ts = yield from x] = int // type X[*Ts = x := int] = int - Some(Box::new( - self.parse_conditional_expression_or_higher_impl( + let expression = self + .parse_conditional_expression_or_higher_impl( ExpressionContext::starred_bitwise_or(), ) - .expr, - )) + .expr; + Some(self.alloc_box(expression)) } else { // test_err type_param_type_var_tuple_missing_default // type X[*Ts =] = int @@ -3122,7 +3137,8 @@ impl<'src> Parser<'src> { // type X[**P = yield from x] = int // type X[**P = x := int] = int // type X[**P = *int] = int - Some(Box::new(self.parse_conditional_expression_or_higher().expr)) + let expression = self.parse_conditional_expression_or_higher().expr; + Some(self.alloc_box(expression)) } else { // test_err type_param_param_spec_missing_default // type X[**P =] = int @@ -3160,7 +3176,8 @@ impl<'src> Parser<'src> { // type X[T: yield x] = int // type X[T: yield from x] = int // type X[T: x := int] = int - Some(Box::new(self.parse_conditional_expression_or_higher().expr)) + let expression = self.parse_conditional_expression_or_higher().expr; + Some(self.alloc_box(expression)) } else { // test_err type_param_missing_bound // type X[T: ] = int @@ -3184,7 +3201,8 @@ impl<'src> Parser<'src> { // type X[T = yield from x] = int // type X[T = x := int] = int // type X[T: int = *int] = int - Some(Box::new(self.parse_conditional_expression_or_higher().expr)) + let expression = self.parse_conditional_expression_or_higher().expr; + Some(self.alloc_box(expression)) } else { // test_err type_param_type_var_missing_default // type X[T =] = int @@ -3215,7 +3233,7 @@ impl<'src> Parser<'src> { /// If it's a starred expression, then validate the value of the starred expression. /// /// Report an error for each invalid assignment expression found. - pub(super) fn validate_assignment_target(&mut self, expr: &Expr) { + pub(super) fn validate_assignment_target(&mut self, expr: &Expr<'ast>) { match expr { Expr::Starred(ast::ExprStarred { value, .. }) => self.validate_assignment_target(value), Expr::List(ast::ExprList { elts, .. }) | Expr::Tuple(ast::ExprTuple { elts, .. }) => { @@ -3232,7 +3250,7 @@ impl<'src> Parser<'src> { /// /// Unlike [`Parser::validate_assignment_target`], starred, list and tuple /// expressions aren't allowed here. - fn validate_annotated_assignment_target(&mut self, expr: &Expr) { + fn validate_annotated_assignment_target(&mut self, expr: &Expr<'ast>) { match expr { Expr::List(_) => self.add_error( ParseErrorType::OtherError( @@ -3256,7 +3274,7 @@ impl<'src> Parser<'src> { /// If the expression is a list or tuple, then validate each element in the list. /// /// See: - fn validate_delete_target(&mut self, expr: &Expr) { + fn validate_delete_target(&mut self, expr: &Expr<'ast>) { match expr { Expr::List(ast::ExprList { elts, .. }) | Expr::Tuple(ast::ExprTuple { elts, .. }) => { for expr in elts { @@ -3271,7 +3289,7 @@ impl<'src> Parser<'src> { /// Validate that the given parameters doesn't have any duplicate names. /// /// Report errors for all the duplicate names found. - fn validate_parameters(&mut self, parameters: &ast::Parameters) { + fn validate_parameters(&mut self, parameters: &ast::Parameters<'ast>) { let mut all_arg_names = FxHashSet::with_capacity_and_hasher(parameters.len(), FxBuildHasher); @@ -3431,7 +3449,7 @@ impl<'src> Parser<'src> { fn parse_clauses( &mut self, clause: Clause, - mut parse_clause: impl FnMut(&mut Parser<'src>) -> T, + mut parse_clause: impl FnMut(&mut Parser<'src, 'ast>) -> T, ) -> Vec { let mut clauses = Vec::new(); let mut progress = ParserProgress::default(); @@ -3544,9 +3562,9 @@ enum WithItemParsingState { } #[derive(Debug)] -struct ParsedWithItem { +struct ParsedWithItem<'ast> { /// The contained with item. - item: WithItem, + item: WithItem<'ast>, /// If the context expression of the item is parenthesized. is_parenthesized: bool, } diff --git a/crates/ruff_python_parser/src/parser/tests.rs b/crates/ruff_python_parser/src/parser/tests.rs index 09bc41e7f7..e244e52853 100644 --- a/crates/ruff_python_parser/src/parser/tests.rs +++ b/crates/ruff_python_parser/src/parser/tests.rs @@ -1,51 +1,57 @@ use crate::{parse, parse_expression, parse_module, Mode}; +use ruff_allocator::Allocator; #[test] fn test_modes() { + let allocator = Allocator::new(); let source = "a[0][1][2][3][4]"; - assert!(parse(source, Mode::Expression).is_ok()); - assert!(parse(source, Mode::Module).is_ok()); + assert!(parse(source, Mode::Expression, &allocator).is_ok()); + assert!(parse(source, Mode::Module, &allocator).is_ok()); } #[test] fn test_expr_mode_invalid_syntax1() { + let allocator = Allocator::new(); let source = "first second"; - let error = parse_expression(source).unwrap_err(); + let error = parse_expression(source, &allocator).unwrap_err(); insta::assert_debug_snapshot!(error); } #[test] fn test_expr_mode_invalid_syntax2() { + let allocator = Allocator::new(); let source = r"first second "; - let error = parse_expression(source).unwrap_err(); + let error = parse_expression(source, &allocator).unwrap_err(); insta::assert_debug_snapshot!(error); } #[test] fn test_expr_mode_invalid_syntax3() { + let allocator = Allocator::new(); let source = r"first second third "; - let error = parse_expression(source).unwrap_err(); + let error = parse_expression(source, &allocator).unwrap_err(); insta::assert_debug_snapshot!(error); } #[test] fn test_expr_mode_valid_syntax() { + let allocator = Allocator::new(); let source = "first "; - let parsed = parse_expression(source).unwrap(); + let parsed = parse_expression(source, &allocator).unwrap(); insta::assert_debug_snapshot!(parsed.expr()); } @@ -53,14 +59,16 @@ fn test_expr_mode_valid_syntax() { #[test] fn test_unicode_aliases() { // https://github.com/RustPython/RustPython/issues/4566 + let allocator = Allocator::new(); let source = r#"x = "\N{BACKSPACE}another cool trick""#; - let suite = parse_module(source).unwrap().into_suite(); + let suite = parse_module(source, &allocator).unwrap().into_suite(); insta::assert_debug_snapshot!(suite); } #[test] fn test_ipython_escape_commands() { + let allocator = Allocator::new(); let parsed = parse( r" # Normal Python code @@ -130,6 +138,7 @@ foo.bar[0].baz[2].egg?? " .trim(), Mode::Ipython, + &allocator, ) .unwrap(); insta::assert_debug_snapshot!(parsed.syntax()); diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__string__tests__dont_panic_on_8_in_octal_escape.snap.new b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__string__tests__dont_panic_on_8_in_octal_escape.snap.new new file mode 100644 index 0000000000..393b0b355a --- /dev/null +++ b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__string__tests__dont_panic_on_8_in_octal_escape.snap.new @@ -0,0 +1,39 @@ +--- +source: crates/ruff_python_parser/src/string.rs +assertion_line: 869 +expression: suite +--- +[ + Assign( + StmtAssign { + range: 0..16, + targets: [ + Name( + ExprName { + range: 0..4, + id: "bold", + ctx: Store, + }, + ), + ], + value: StringLiteral( + ExprStringLiteral { + range: 7..16, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 7..16, + value: "\u{3}8[1m", + flags: StringLiteralFlags { + quote_style: Single, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + }, + ), +] diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__string__tests__fstring_constant_range.snap.new b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__string__tests__fstring_constant_range.snap.new new file mode 100644 index 0000000000..a4a8bfc11f --- /dev/null +++ b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__string__tests__fstring_constant_range.snap.new @@ -0,0 +1,81 @@ +--- +source: crates/ruff_python_parser/src/string.rs +assertion_line: 802 +expression: suite +--- +[ + Expr( + StmtExpr { + range: 0..22, + value: FString( + ExprFString { + range: 0..22, + value: FStringValue { + inner: Single( + FString( + FString { + range: 0..22, + elements: [ + Literal( + FStringLiteralElement { + range: 2..5, + value: "aaa", + }, + ), + Expression( + FStringExpressionElement { + range: 5..10, + expression: Name( + ExprName { + range: 6..9, + id: "bbb", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + Literal( + FStringLiteralElement { + range: 10..13, + value: "ccc", + }, + ), + Expression( + FStringExpressionElement { + range: 13..18, + expression: Name( + ExprName { + range: 14..17, + id: "ddd", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + Literal( + FStringLiteralElement { + range: 18..21, + value: "eee", + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + }, + ), +] diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__string__tests__fstring_escaped_character.snap.new b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__string__tests__fstring_escaped_character.snap.new new file mode 100644 index 0000000000..6efd6fd15b --- /dev/null +++ b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__string__tests__fstring_escaped_character.snap.new @@ -0,0 +1,54 @@ +--- +source: crates/ruff_python_parser/src/string.rs +assertion_line: 819 +expression: suite +--- +[ + Expr( + StmtExpr { + range: 0..8, + value: FString( + ExprFString { + range: 0..8, + value: FStringValue { + inner: Single( + FString( + FString { + range: 0..8, + elements: [ + Literal( + FStringLiteralElement { + range: 2..4, + value: "\\", + }, + ), + Expression( + FStringExpressionElement { + range: 4..7, + expression: Name( + ExprName { + range: 5..6, + id: "x", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + }, + ), +] diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__string__tests__fstring_escaped_newline.snap.new b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__string__tests__fstring_escaped_newline.snap.new new file mode 100644 index 0000000000..cbff234971 --- /dev/null +++ b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__string__tests__fstring_escaped_newline.snap.new @@ -0,0 +1,54 @@ +--- +source: crates/ruff_python_parser/src/string.rs +assertion_line: 794 +expression: suite +--- +[ + Expr( + StmtExpr { + range: 0..8, + value: FString( + ExprFString { + range: 0..8, + value: FStringValue { + inner: Single( + FString( + FString { + range: 0..8, + elements: [ + Literal( + FStringLiteralElement { + range: 2..4, + value: "\n", + }, + ), + Expression( + FStringExpressionElement { + range: 4..7, + expression: Name( + ExprName { + range: 5..6, + id: "x", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + }, + ), +] diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__string__tests__fstring_line_continuation.snap.new b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__string__tests__fstring_line_continuation.snap.new new file mode 100644 index 0000000000..5085b3cbc9 --- /dev/null +++ b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__string__tests__fstring_line_continuation.snap.new @@ -0,0 +1,56 @@ +--- +source: crates/ruff_python_parser/src/string.rs +assertion_line: 844 +expression: suite +--- +[ + Expr( + StmtExpr { + range: 0..9, + value: FString( + ExprFString { + range: 0..9, + value: FStringValue { + inner: Single( + FString( + FString { + range: 0..9, + elements: [ + Literal( + FStringLiteralElement { + range: 3..5, + value: "\\\n", + }, + ), + Expression( + FStringExpressionElement { + range: 5..8, + expression: Name( + ExprName { + range: 6..7, + id: "x", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Raw { + uppercase_r: false, + }, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + }, + ), +] diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__string__tests__fstring_parse_self_documenting_base.snap.new b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__string__tests__fstring_parse_self_documenting_base.snap.new new file mode 100644 index 0000000000..fc2c4eddf8 --- /dev/null +++ b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__string__tests__fstring_parse_self_documenting_base.snap.new @@ -0,0 +1,53 @@ +--- +source: crates/ruff_python_parser/src/string.rs +assertion_line: 569 +expression: suite +--- +[ + Expr( + StmtExpr { + range: 0..10, + value: FString( + ExprFString { + range: 0..10, + value: FStringValue { + inner: Single( + FString( + FString { + range: 0..10, + elements: [ + Expression( + FStringExpressionElement { + range: 2..9, + expression: Name( + ExprName { + range: 3..7, + id: "user", + ctx: Load, + }, + ), + debug_text: Some( + DebugText { + leading: "", + trailing: "=", + }, + ), + conversion: None, + format_spec: None, + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + }, + ), +] diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__string__tests__fstring_parse_self_documenting_base_more.snap.new b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__string__tests__fstring_parse_self_documenting_base_more.snap.new new file mode 100644 index 0000000000..a5322a0e11 --- /dev/null +++ b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__string__tests__fstring_parse_self_documenting_base_more.snap.new @@ -0,0 +1,85 @@ +--- +source: crates/ruff_python_parser/src/string.rs +assertion_line: 577 +expression: suite +--- +[ + Expr( + StmtExpr { + range: 0..38, + value: FString( + ExprFString { + range: 0..38, + value: FStringValue { + inner: Single( + FString( + FString { + range: 0..38, + elements: [ + Literal( + FStringLiteralElement { + range: 2..6, + value: "mix ", + }, + ), + Expression( + FStringExpressionElement { + range: 6..13, + expression: Name( + ExprName { + range: 7..11, + id: "user", + ctx: Load, + }, + ), + debug_text: Some( + DebugText { + leading: "", + trailing: "=", + }, + ), + conversion: None, + format_spec: None, + }, + ), + Literal( + FStringLiteralElement { + range: 13..28, + value: " with text and ", + }, + ), + Expression( + FStringExpressionElement { + range: 28..37, + expression: Name( + ExprName { + range: 29..35, + id: "second", + ctx: Load, + }, + ), + debug_text: Some( + DebugText { + leading: "", + trailing: "=", + }, + ), + conversion: None, + format_spec: None, + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + }, + ), +] diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__string__tests__fstring_parse_self_documenting_format.snap.new b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__string__tests__fstring_parse_self_documenting_format.snap.new new file mode 100644 index 0000000000..7db3a82718 --- /dev/null +++ b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__string__tests__fstring_parse_self_documenting_format.snap.new @@ -0,0 +1,65 @@ +--- +source: crates/ruff_python_parser/src/string.rs +assertion_line: 585 +expression: suite +--- +[ + Expr( + StmtExpr { + range: 0..14, + value: FString( + ExprFString { + range: 0..14, + value: FStringValue { + inner: Single( + FString( + FString { + range: 0..14, + elements: [ + Expression( + FStringExpressionElement { + range: 2..13, + expression: Name( + ExprName { + range: 3..7, + id: "user", + ctx: Load, + }, + ), + debug_text: Some( + DebugText { + leading: "", + trailing: "=", + }, + ), + conversion: None, + format_spec: Some( + FStringFormatSpec { + range: 9..12, + elements: [ + Literal( + FStringLiteralElement { + range: 9..12, + value: ">10", + }, + ), + ], + }, + ), + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + }, + ), +] diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__string__tests__fstring_unescaped_newline.snap.new b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__string__tests__fstring_unescaped_newline.snap.new new file mode 100644 index 0000000000..d5170ea741 --- /dev/null +++ b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__string__tests__fstring_unescaped_newline.snap.new @@ -0,0 +1,54 @@ +--- +source: crates/ruff_python_parser/src/string.rs +assertion_line: 811 +expression: suite +--- +[ + Expr( + StmtExpr { + range: 0..11, + value: FString( + ExprFString { + range: 0..11, + value: FStringValue { + inner: Single( + FString( + FString { + range: 0..11, + elements: [ + Literal( + FStringLiteralElement { + range: 4..5, + value: "\n", + }, + ), + Expression( + FStringExpressionElement { + range: 5..8, + expression: Name( + ExprName { + range: 6..7, + id: "x", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: true, + }, + }, + ), + ), + }, + }, + ), + }, + ), +] diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__string__tests__parse_fstring.snap.new b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__string__tests__parse_fstring.snap.new new file mode 100644 index 0000000000..40aecfea9f --- /dev/null +++ b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__string__tests__parse_fstring.snap.new @@ -0,0 +1,69 @@ +--- +source: crates/ruff_python_parser/src/string.rs +assertion_line: 537 +expression: suite +--- +[ + Expr( + StmtExpr { + range: 0..18, + value: FString( + ExprFString { + range: 0..18, + value: FStringValue { + inner: Single( + FString( + FString { + range: 0..18, + elements: [ + Expression( + FStringExpressionElement { + range: 2..5, + expression: Name( + ExprName { + range: 3..4, + id: "a", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + Expression( + FStringExpressionElement { + range: 5..10, + expression: Name( + ExprName { + range: 7..8, + id: "b", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + Literal( + FStringLiteralElement { + range: 10..17, + value: "{foo}", + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + }, + ), +] diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__string__tests__parse_fstring_nested_concatenation_string_spec.snap.new b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__string__tests__parse_fstring_nested_concatenation_string_spec.snap.new new file mode 100644 index 0000000000..0237a13e78 --- /dev/null +++ b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__string__tests__parse_fstring_nested_concatenation_string_spec.snap.new @@ -0,0 +1,94 @@ +--- +source: crates/ruff_python_parser/src/string.rs +assertion_line: 860 +expression: suite +--- +[ + Expr( + StmtExpr { + range: 0..16, + value: FString( + ExprFString { + range: 0..16, + value: FStringValue { + inner: Single( + FString( + FString { + range: 0..16, + elements: [ + Expression( + FStringExpressionElement { + range: 2..15, + expression: Name( + ExprName { + range: 3..6, + id: "foo", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: Some( + FStringFormatSpec { + range: 7..14, + elements: [ + Expression( + FStringExpressionElement { + range: 7..14, + expression: StringLiteral( + ExprStringLiteral { + range: 8..13, + value: StringLiteralValue { + inner: Concatenated( + ConcatenatedStringLiteral { + strings: [ + StringLiteral { + range: 8..10, + value: "", + flags: StringLiteralFlags { + quote_style: Single, + prefix: Empty, + triple_quoted: false, + }, + }, + StringLiteral { + range: 11..13, + value: "", + flags: StringLiteralFlags { + quote_style: Single, + prefix: Empty, + triple_quoted: false, + }, + }, + ], + value: "", + }, + ), + }, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + ], + }, + ), + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + }, + ), +] diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__string__tests__parse_fstring_nested_spec.snap.new b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__string__tests__parse_fstring_nested_spec.snap.new new file mode 100644 index 0000000000..5550aa56d4 --- /dev/null +++ b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__string__tests__parse_fstring_nested_spec.snap.new @@ -0,0 +1,69 @@ +--- +source: crates/ruff_python_parser/src/string.rs +assertion_line: 545 +expression: suite +--- +[ + Expr( + StmtExpr { + range: 0..15, + value: FString( + ExprFString { + range: 0..15, + value: FStringValue { + inner: Single( + FString( + FString { + range: 0..15, + elements: [ + Expression( + FStringExpressionElement { + range: 2..14, + expression: Name( + ExprName { + range: 3..6, + id: "foo", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: Some( + FStringFormatSpec { + range: 7..13, + elements: [ + Expression( + FStringExpressionElement { + range: 7..13, + expression: Name( + ExprName { + range: 8..12, + id: "spec", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + ], + }, + ), + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + }, + ), +] diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__string__tests__parse_fstring_nested_string_spec.snap.new b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__string__tests__parse_fstring_nested_string_spec.snap.new new file mode 100644 index 0000000000..2ca00d9792 --- /dev/null +++ b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__string__tests__parse_fstring_nested_string_spec.snap.new @@ -0,0 +1,80 @@ +--- +source: crates/ruff_python_parser/src/string.rs +assertion_line: 852 +expression: suite +--- +[ + Expr( + StmtExpr { + range: 0..13, + value: FString( + ExprFString { + range: 0..13, + value: FStringValue { + inner: Single( + FString( + FString { + range: 0..13, + elements: [ + Expression( + FStringExpressionElement { + range: 2..12, + expression: Name( + ExprName { + range: 3..6, + id: "foo", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: Some( + FStringFormatSpec { + range: 7..11, + elements: [ + Expression( + FStringExpressionElement { + range: 7..11, + expression: StringLiteral( + ExprStringLiteral { + range: 8..10, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 8..10, + value: "", + flags: StringLiteralFlags { + quote_style: Single, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + ], + }, + ), + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + }, + ), +] diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__string__tests__parse_fstring_not_nested_spec.snap.new b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__string__tests__parse_fstring_not_nested_spec.snap.new new file mode 100644 index 0000000000..b55bdb6580 --- /dev/null +++ b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__string__tests__parse_fstring_not_nested_spec.snap.new @@ -0,0 +1,60 @@ +--- +source: crates/ruff_python_parser/src/string.rs +assertion_line: 553 +expression: suite +--- +[ + Expr( + StmtExpr { + range: 0..13, + value: FString( + ExprFString { + range: 0..13, + value: FStringValue { + inner: Single( + FString( + FString { + range: 0..13, + elements: [ + Expression( + FStringExpressionElement { + range: 2..12, + expression: Name( + ExprName { + range: 3..6, + id: "foo", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: Some( + FStringFormatSpec { + range: 7..11, + elements: [ + Literal( + FStringLiteralElement { + range: 7..11, + value: "spec", + }, + ), + ], + }, + ), + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + }, + ), +] diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__string__tests__parse_fstring_self_doc_prec_space.snap.new b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__string__tests__parse_fstring_self_doc_prec_space.snap.new new file mode 100644 index 0000000000..270b40d17a --- /dev/null +++ b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__string__tests__parse_fstring_self_doc_prec_space.snap.new @@ -0,0 +1,53 @@ +--- +source: crates/ruff_python_parser/src/string.rs +assertion_line: 639 +expression: suite +--- +[ + Expr( + StmtExpr { + range: 0..10, + value: FString( + ExprFString { + range: 0..10, + value: FStringValue { + inner: Single( + FString( + FString { + range: 0..10, + elements: [ + Expression( + FStringExpressionElement { + range: 2..9, + expression: Name( + ExprName { + range: 3..4, + id: "x", + ctx: Load, + }, + ), + debug_text: Some( + DebugText { + leading: "", + trailing: " =", + }, + ), + conversion: None, + format_spec: None, + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + }, + ), +] diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__string__tests__parse_fstring_self_doc_trailing_space.snap.new b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__string__tests__parse_fstring_self_doc_trailing_space.snap.new new file mode 100644 index 0000000000..57f0a35148 --- /dev/null +++ b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__string__tests__parse_fstring_self_doc_trailing_space.snap.new @@ -0,0 +1,53 @@ +--- +source: crates/ruff_python_parser/src/string.rs +assertion_line: 647 +expression: suite +--- +[ + Expr( + StmtExpr { + range: 0..10, + value: FString( + ExprFString { + range: 0..10, + value: FStringValue { + inner: Single( + FString( + FString { + range: 0..10, + elements: [ + Expression( + FStringExpressionElement { + range: 2..9, + expression: Name( + ExprName { + range: 3..4, + id: "x", + ctx: Load, + }, + ), + debug_text: Some( + DebugText { + leading: "", + trailing: "= ", + }, + ), + conversion: None, + format_spec: None, + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + }, + ), +] diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__string__tests__raw_fstring.snap.new b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__string__tests__raw_fstring.snap.new new file mode 100644 index 0000000000..6ce128d78f --- /dev/null +++ b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__string__tests__raw_fstring.snap.new @@ -0,0 +1,50 @@ +--- +source: crates/ruff_python_parser/src/string.rs +assertion_line: 827 +expression: suite +--- +[ + Expr( + StmtExpr { + range: 0..7, + value: FString( + ExprFString { + range: 0..7, + value: FStringValue { + inner: Single( + FString( + FString { + range: 0..7, + elements: [ + Expression( + FStringExpressionElement { + range: 3..6, + expression: Name( + ExprName { + range: 4..5, + id: "x", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Raw { + uppercase_r: false, + }, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + }, + ), +] diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__string__tests__triple_quoted_raw_fstring.snap.new b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__string__tests__triple_quoted_raw_fstring.snap.new new file mode 100644 index 0000000000..0f7b97ae04 --- /dev/null +++ b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__string__tests__triple_quoted_raw_fstring.snap.new @@ -0,0 +1,50 @@ +--- +source: crates/ruff_python_parser/src/string.rs +assertion_line: 835 +expression: suite +--- +[ + Expr( + StmtExpr { + range: 0..11, + value: FString( + ExprFString { + range: 0..11, + value: FStringValue { + inner: Single( + FString( + FString { + range: 0..11, + elements: [ + Expression( + FStringExpressionElement { + range: 5..8, + expression: Name( + ExprName { + range: 6..7, + id: "x", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Raw { + uppercase_r: false, + }, + triple_quoted: true, + }, + }, + ), + ), + }, + }, + ), + }, + ), +] diff --git a/crates/ruff_python_parser/src/string.rs b/crates/ruff_python_parser/src/string.rs index 8c9d61ba91..1cdcdeb5de 100644 --- a/crates/ruff_python_parser/src/string.rs +++ b/crates/ruff_python_parser/src/string.rs @@ -1,20 +1,20 @@ //! Parsing of string literals, bytes literals, and implicit string concatenation. use bstr::ByteSlice; - +use ruff_allocator::Allocator; use ruff_python_ast::{self as ast, AnyStringFlags, Expr, StringFlags}; use ruff_text_size::{Ranged, TextRange, TextSize}; use crate::error::{LexicalError, LexicalErrorType}; #[derive(Debug)] -pub(crate) enum StringType { - Str(ast::StringLiteral), - Bytes(ast::BytesLiteral), - FString(ast::FString), +pub(crate) enum StringType<'ast> { + Str(ast::StringLiteral<'ast>), + Bytes(ast::BytesLiteral<'ast>), + FString(ast::FString<'ast>), } -impl Ranged for StringType { +impl Ranged for StringType<'_> { fn range(&self) -> TextRange { match self { Self::Str(node) => node.range(), @@ -24,8 +24,8 @@ impl Ranged for StringType { } } -impl From for Expr { - fn from(string: StringType) -> Self { +impl<'ast> From> for Expr<'ast> { + fn from(string: StringType<'ast>) -> Self { match string { StringType::Str(node) => Expr::from(node), StringType::Bytes(node) => Expr::from(node), @@ -39,9 +39,9 @@ enum EscapedChar { Escape(char), } -struct StringParser { +struct StringParser<'source, 'ast> { /// The raw content of the string e.g., the `foo` part in `"foo"`. - source: Box, + source: &'source str, /// Current position of the parser in the source. cursor: usize, /// Flags that can be used to query information about the string. @@ -50,21 +50,29 @@ struct StringParser { offset: TextSize, /// The range of the string literal. range: TextRange, + allocator: &'ast Allocator, } -impl StringParser { - fn new(source: Box, flags: AnyStringFlags, offset: TextSize, range: TextRange) -> Self { +impl<'source, 'ast> StringParser<'source, 'ast> { + fn new( + source: &'source str, + flags: AnyStringFlags, + offset: TextSize, + range: TextRange, + allocator: &'ast Allocator, + ) -> Self { Self { source, cursor: 0, flags, offset, range, + allocator, } } #[inline] - fn skip_bytes(&mut self, bytes: usize) -> &str { + fn skip_bytes(&mut self, bytes: usize) -> &'source str { let skipped_str = &self.source[self.cursor..self.cursor + bytes]; self.cursor += bytes; skipped_str @@ -232,16 +240,16 @@ impl StringParser { Ok(Some(EscapedChar::Literal(new_char))) } - fn parse_fstring_middle(mut self) -> Result { + fn parse_fstring_middle(mut self) -> Result, LexicalError> { // Fast-path: if the f-string doesn't contain any escape sequences, return the literal. let Some(mut index) = memchr::memchr3(b'{', b'}', b'\\', self.source.as_bytes()) else { return Ok(ast::FStringLiteralElement { - value: self.source, + value: self.allocator.alloc_str(&self.source), range: self.range, }); }; - let mut value = String::with_capacity(self.source.len()); + let mut value = ruff_allocator::String::with_capacity_in(self.source.len(), self.allocator); loop { // Add the characters before the escape sequence (or curly brace) to the string. let before_with_slash_or_brace = self.skip_bytes(index + 1); @@ -313,12 +321,12 @@ impl StringParser { } Ok(ast::FStringLiteralElement { - value: value.into_boxed_str(), + value: value.into_bump_str(), range: self.range, }) } - fn parse_bytes(mut self) -> Result { + fn parse_bytes(mut self) -> Result, LexicalError> { if let Some(index) = self.source.as_bytes().find_non_ascii_byte() { let ch = self.source.chars().nth(index).unwrap(); return Err(LexicalError::new( @@ -333,7 +341,7 @@ impl StringParser { if self.flags.is_raw_string() { // For raw strings, no escaping is necessary. return Ok(StringType::Bytes(ast::BytesLiteral { - value: self.source.into_boxed_bytes(), + value: self.allocator.alloc_str(self.source).as_bytes(), range: self.range, flags: self.flags.into(), })); @@ -342,14 +350,14 @@ impl StringParser { let Some(mut escape) = memchr::memchr(b'\\', self.source.as_bytes()) else { // If the string doesn't contain any escape sequences, return the owned string. return Ok(StringType::Bytes(ast::BytesLiteral { - value: self.source.into_boxed_bytes(), + value: self.allocator.alloc_str(self.source).as_bytes(), range: self.range, flags: self.flags.into(), })); }; // If the string contains escape sequences, we need to parse them. - let mut value = Vec::with_capacity(self.source.len()); + let mut value = ruff_allocator::Vec::with_capacity_in(self.source.len(), self.allocator); loop { // Add the characters before the escape sequence to the string. let before_with_slash = self.skip_bytes(escape + 1); @@ -379,17 +387,17 @@ impl StringParser { } Ok(StringType::Bytes(ast::BytesLiteral { - value: value.into_boxed_slice(), + value: value.into_bump_slice(), range: self.range, flags: self.flags.into(), })) } - fn parse_string(mut self) -> Result { + fn parse_string(mut self) -> Result, LexicalError> { if self.flags.is_raw_string() { // For raw strings, no escaping is necessary. return Ok(StringType::Str(ast::StringLiteral { - value: self.source, + value: self.allocator.alloc_str(self.source), range: self.range, flags: self.flags.into(), })); @@ -398,14 +406,14 @@ impl StringParser { let Some(mut escape) = memchr::memchr(b'\\', self.source.as_bytes()) else { // If the string doesn't contain any escape sequences, return the owned string. return Ok(StringType::Str(ast::StringLiteral { - value: self.source, + value: self.allocator.alloc_str(self.source), range: self.range, flags: self.flags.into(), })); }; // If the string contains escape sequences, we need to parse them. - let mut value = String::with_capacity(self.source.len()); + let mut value = ruff_allocator::String::with_capacity_in(self.source.len(), self.allocator); loop { // Add the characters before the escape sequence to the string. @@ -435,13 +443,13 @@ impl StringParser { } Ok(StringType::Str(ast::StringLiteral { - value: value.into_boxed_str(), + value: value.into_bump_str(), range: self.range, flags: self.flags.into(), })) } - fn parse(self) -> Result { + fn parse(self) -> Result, LexicalError> { if self.flags.is_byte_string() { self.parse_bytes() } else { @@ -450,25 +458,35 @@ impl StringParser { } } -pub(crate) fn parse_string_literal( - source: Box, +pub(crate) fn parse_string_literal<'source, 'ast>( + source: &'source str, flags: AnyStringFlags, range: TextRange, -) -> Result { - StringParser::new(source, flags, range.start() + flags.opener_len(), range).parse() + allocator: &'ast Allocator, +) -> Result, LexicalError> { + StringParser::new( + source, + flags, + range.start() + flags.opener_len(), + range, + allocator, + ) + .parse() } // TODO(dhruvmanila): Move this to the new parser -pub(crate) fn parse_fstring_literal_element( - source: Box, +pub(crate) fn parse_fstring_literal_element<'ast>( + source: &str, flags: AnyStringFlags, range: TextRange, -) -> Result { - StringParser::new(source, flags, range.start(), range).parse_fstring_middle() + allocator: &'ast Allocator, +) -> Result, LexicalError> { + StringParser::new(source, flags, range.start(), range, allocator).parse_fstring_middle() } #[cfg(test)] mod tests { + use ruff_allocator::Allocator; use ruff_python_ast::Suite; use crate::error::LexicalErrorType; @@ -478,84 +496,98 @@ mod tests { const MAC_EOL: &str = "\r"; const UNIX_EOL: &str = "\n"; - fn parse_suite(source: &str) -> Result { - parse_module(source).map(Parsed::into_suite) + fn parse_suite<'ast>( + source: &str, + allocator: &'ast Allocator, + ) -> Result, ParseError> { + parse_module(source, allocator).map(Parsed::into_suite) } - fn string_parser_escaped_eol(eol: &str) -> Suite { + fn string_parser_escaped_eol<'ast>(eol: &str, allocator: &'ast Allocator) -> Suite<'ast> { let source = format!(r"'text \{eol}more text'"); - parse_suite(&source).unwrap() + parse_suite(&source, &allocator).unwrap() } #[test] fn test_string_parser_escaped_unix_eol() { - let suite = string_parser_escaped_eol(UNIX_EOL); + let allocator = Allocator::new(); + let suite = string_parser_escaped_eol(UNIX_EOL, &allocator); insta::assert_debug_snapshot!(suite); } #[test] fn test_string_parser_escaped_mac_eol() { - let suite = string_parser_escaped_eol(MAC_EOL); + let allocator = Allocator::new(); + let suite = string_parser_escaped_eol(MAC_EOL, &allocator); insta::assert_debug_snapshot!(suite); } #[test] fn test_string_parser_escaped_windows_eol() { - let suite = string_parser_escaped_eol(WINDOWS_EOL); + let allocator = Allocator::new(); + let suite = string_parser_escaped_eol(WINDOWS_EOL, &allocator); insta::assert_debug_snapshot!(suite); } #[test] fn test_parse_fstring() { let source = r#"f"{a}{ b }{{foo}}""#; - let suite = parse_suite(source).unwrap(); + let allocator = Allocator::new(); + let suite = parse_suite(source, &allocator).unwrap(); insta::assert_debug_snapshot!(suite); } #[test] fn test_parse_fstring_nested_spec() { let source = r#"f"{foo:{spec}}""#; - let suite = parse_suite(source).unwrap(); + let allocator = Allocator::new(); + let suite = parse_suite(source, &allocator).unwrap(); insta::assert_debug_snapshot!(suite); } #[test] fn test_parse_fstring_not_nested_spec() { let source = r#"f"{foo:spec}""#; - let suite = parse_suite(source).unwrap(); + let allocator = Allocator::new(); + let suite = parse_suite(source, &allocator).unwrap(); insta::assert_debug_snapshot!(suite); } #[test] fn test_parse_empty_fstring() { let source = r#"f"""#; - let suite = parse_suite(source).unwrap(); + let allocator = Allocator::new(); + let suite = parse_suite(source, &allocator).unwrap(); insta::assert_debug_snapshot!(suite); } #[test] fn test_fstring_parse_self_documenting_base() { let source = r#"f"{user=}""#; - let suite = parse_suite(source).unwrap(); + let allocator = Allocator::new(); + let suite = parse_suite(source, &allocator).unwrap(); insta::assert_debug_snapshot!(suite); } #[test] fn test_fstring_parse_self_documenting_base_more() { let source = r#"f"mix {user=} with text and {second=}""#; - let suite = parse_suite(source).unwrap(); + let allocator = Allocator::new(); + let suite = parse_suite(source, &allocator).unwrap(); insta::assert_debug_snapshot!(suite); } #[test] fn test_fstring_parse_self_documenting_format() { let source = r#"f"{user=:>10}""#; - let suite = parse_suite(source).unwrap(); + let allocator = Allocator::new(); + let suite = parse_suite(source, &allocator).unwrap(); insta::assert_debug_snapshot!(suite); } fn parse_fstring_error(source: &str) -> FStringErrorType { - parse_suite(source) + let allocator = Allocator::new(); + parse_suite(source, &allocator) .map_err(|e| match e.error { ParseErrorType::Lexical(LexicalErrorType::FStringError(e)) => e, ParseErrorType::FStringError(e) => e, @@ -579,111 +611,127 @@ mod tests { // error appears after the unexpected `FStringMiddle` token, which is between the // `:` and the `{`. // assert_eq!(parse_fstring_error("f'{lambda x: {x}}'"), LambdaWithoutParentheses); - assert!(parse_suite(r#"f"{class}""#).is_err()); + let allocator = Allocator::new(); + assert!(parse_suite(r#"f"{class}""#, &allocator).is_err()); } #[test] fn test_parse_fstring_not_equals() { let source = r#"f"{1 != 2}""#; - let suite = parse_suite(source).unwrap(); + let allocator = Allocator::new(); + let suite = parse_suite(source, &allocator).unwrap(); insta::assert_debug_snapshot!(suite); } #[test] fn test_parse_fstring_equals() { let source = r#"f"{42 == 42}""#; - let suite = parse_suite(source).unwrap(); + let allocator = Allocator::new(); + let suite = parse_suite(source, &allocator).unwrap(); insta::assert_debug_snapshot!(suite); } #[test] fn test_parse_fstring_self_doc_prec_space() { let source = r#"f"{x =}""#; - let suite = parse_suite(source).unwrap(); + let allocator = Allocator::new(); + let suite = parse_suite(source, &allocator).unwrap(); insta::assert_debug_snapshot!(suite); } #[test] fn test_parse_fstring_self_doc_trailing_space() { let source = r#"f"{x= }""#; - let suite = parse_suite(source).unwrap(); + let allocator = Allocator::new(); + let suite = parse_suite(source, &allocator).unwrap(); insta::assert_debug_snapshot!(suite); } #[test] fn test_parse_fstring_yield_expr() { let source = r#"f"{yield}""#; - let suite = parse_suite(source).unwrap(); + let allocator = Allocator::new(); + let suite = parse_suite(source, &allocator).unwrap(); insta::assert_debug_snapshot!(suite); } #[test] fn test_parse_string_concat() { let source = "'Hello ' 'world'"; - let suite = parse_suite(source).unwrap(); + let allocator = Allocator::new(); + let suite = parse_suite(source, &allocator).unwrap(); insta::assert_debug_snapshot!(suite); } #[test] fn test_parse_u_string_concat_1() { let source = "'Hello ' u'world'"; - let suite = parse_suite(source).unwrap(); + let allocator = Allocator::new(); + let suite = parse_suite(source, &allocator).unwrap(); insta::assert_debug_snapshot!(suite); } #[test] fn test_parse_u_string_concat_2() { let source = "u'Hello ' 'world'"; - let suite = parse_suite(source).unwrap(); + let allocator = Allocator::new(); + let suite = parse_suite(source, &allocator).unwrap(); insta::assert_debug_snapshot!(suite); } #[test] fn test_parse_f_string_concat_1() { let source = "'Hello ' f'world'"; - let suite = parse_suite(source).unwrap(); + let allocator = Allocator::new(); + let suite = parse_suite(source, &allocator).unwrap(); insta::assert_debug_snapshot!(suite); } #[test] fn test_parse_f_string_concat_2() { let source = "'Hello ' f'world'"; - let suite = parse_suite(source).unwrap(); + let allocator = Allocator::new(); + let suite = parse_suite(source, &allocator).unwrap(); insta::assert_debug_snapshot!(suite); } #[test] fn test_parse_f_string_concat_3() { let source = "'Hello ' f'world{\"!\"}'"; - let suite = parse_suite(source).unwrap(); + let allocator = Allocator::new(); + let suite = parse_suite(source, &allocator).unwrap(); insta::assert_debug_snapshot!(suite); } #[test] fn test_parse_f_string_concat_4() { let source = "'Hello ' f'world{\"!\"}' 'again!'"; - let suite = parse_suite(source).unwrap(); + let allocator = Allocator::new(); + let suite = parse_suite(source, &allocator).unwrap(); insta::assert_debug_snapshot!(suite); } #[test] fn test_parse_u_f_string_concat_1() { let source = "u'Hello ' f'world'"; - let suite = parse_suite(source).unwrap(); + let allocator = Allocator::new(); + let suite = parse_suite(source, &allocator).unwrap(); insta::assert_debug_snapshot!(suite); } #[test] fn test_parse_u_f_string_concat_2() { let source = "u'Hello ' f'world' '!'"; - let suite = parse_suite(source).unwrap(); + let allocator = Allocator::new(); + let suite = parse_suite(source, &allocator).unwrap(); insta::assert_debug_snapshot!(suite); } #[test] fn test_parse_string_triple_quotes_with_kind() { let source = "u'''Hello, world!'''"; - let suite = parse_suite(source).unwrap(); + let allocator = Allocator::new(); + let suite = parse_suite(source, &allocator).unwrap(); insta::assert_debug_snapshot!(suite); } @@ -691,7 +739,8 @@ mod tests { fn test_single_quoted_byte() { // single quote let source = r##"b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\x7f\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff'"##; - let suite = parse_suite(source).unwrap(); + let allocator = Allocator::new(); + let suite = parse_suite(source, &allocator).unwrap(); insta::assert_debug_snapshot!(suite); } @@ -699,7 +748,8 @@ mod tests { fn test_double_quoted_byte() { // double quote let source = r##"b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\x7f\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff""##; - let suite = parse_suite(source).unwrap(); + let allocator = Allocator::new(); + let suite = parse_suite(source, &allocator).unwrap(); insta::assert_debug_snapshot!(suite); } @@ -707,42 +757,48 @@ mod tests { fn test_escape_char_in_byte_literal() { // backslash does not escape let source = r#"b"omkmok\Xaa""#; // spell-checker:ignore omkmok - let suite = parse_suite(source).unwrap(); + let allocator = Allocator::new(); + let suite = parse_suite(source, &allocator).unwrap(); insta::assert_debug_snapshot!(suite); } #[test] fn test_raw_byte_literal_1() { let source = r"rb'\x1z'"; - let suite = parse_suite(source).unwrap(); + let allocator = Allocator::new(); + let suite = parse_suite(source, &allocator).unwrap(); insta::assert_debug_snapshot!(suite); } #[test] fn test_raw_byte_literal_2() { let source = r"rb'\\'"; - let suite = parse_suite(source).unwrap(); + let allocator = Allocator::new(); + let suite = parse_suite(source, &allocator).unwrap(); insta::assert_debug_snapshot!(suite); } #[test] fn test_escape_octet() { let source = r"b'\43a\4\1234'"; - let suite = parse_suite(source).unwrap(); + let allocator = Allocator::new(); + let suite = parse_suite(source, &allocator).unwrap(); insta::assert_debug_snapshot!(suite); } #[test] fn test_fstring_escaped_newline() { let source = r#"f"\n{x}""#; - let suite = parse_suite(source).unwrap(); + let allocator = Allocator::new(); + let suite = parse_suite(source, &allocator).unwrap(); insta::assert_debug_snapshot!(suite); } #[test] fn test_fstring_constant_range() { let source = r#"f"aaa{bbb}ccc{ddd}eee""#; - let suite = parse_suite(source).unwrap(); + let allocator = Allocator::new(); + let suite = parse_suite(source, &allocator).unwrap(); insta::assert_debug_snapshot!(suite); } @@ -750,28 +806,32 @@ mod tests { fn test_fstring_unescaped_newline() { let source = r#"f""" {x}""""#; - let suite = parse_suite(source).unwrap(); + let allocator = Allocator::new(); + let suite = parse_suite(source, &allocator).unwrap(); insta::assert_debug_snapshot!(suite); } #[test] fn test_fstring_escaped_character() { let source = r#"f"\\{x}""#; - let suite = parse_suite(source).unwrap(); + let allocator = Allocator::new(); + let suite = parse_suite(source, &allocator).unwrap(); insta::assert_debug_snapshot!(suite); } #[test] fn test_raw_fstring() { let source = r#"rf"{x}""#; - let suite = parse_suite(source).unwrap(); + let allocator = Allocator::new(); + let suite = parse_suite(source, &allocator).unwrap(); insta::assert_debug_snapshot!(suite); } #[test] fn test_triple_quoted_raw_fstring() { let source = r#"rf"""{x}""""#; - let suite = parse_suite(source).unwrap(); + let allocator = Allocator::new(); + let suite = parse_suite(source, &allocator).unwrap(); insta::assert_debug_snapshot!(suite); } @@ -779,21 +839,24 @@ mod tests { fn test_fstring_line_continuation() { let source = r#"rf"\ {x}""#; - let suite = parse_suite(source).unwrap(); + let allocator = Allocator::new(); + let suite = parse_suite(source, &allocator).unwrap(); insta::assert_debug_snapshot!(suite); } #[test] fn test_parse_fstring_nested_string_spec() { let source = r#"f"{foo:{''}}""#; - let suite = parse_suite(source).unwrap(); + let allocator = Allocator::new(); + let suite = parse_suite(source, &allocator).unwrap(); insta::assert_debug_snapshot!(suite); } #[test] fn test_parse_fstring_nested_concatenation_string_spec() { let source = r#"f"{foo:{'' ''}}""#; - let suite = parse_suite(source).unwrap(); + let allocator = Allocator::new(); + let suite = parse_suite(source, &allocator).unwrap(); insta::assert_debug_snapshot!(suite); } @@ -801,42 +864,48 @@ mod tests { #[test] fn test_dont_panic_on_8_in_octal_escape() { let source = r"bold = '\038[1m'"; - let suite = parse_suite(source).unwrap(); + let allocator = Allocator::new(); + let suite = parse_suite(source, &allocator).unwrap(); insta::assert_debug_snapshot!(suite); } #[test] fn test_invalid_unicode_literal() { let source = r"'\x1ó34'"; - let error = parse_suite(source).unwrap_err(); + let allocator = Allocator::new(); + let error = parse_suite(source, &allocator).unwrap_err(); insta::assert_debug_snapshot!(error); } #[test] fn test_missing_unicode_lbrace_error() { let source = r"'\N '"; - let error = parse_suite(source).unwrap_err(); + let allocator = Allocator::new(); + let error = parse_suite(source, &allocator).unwrap_err(); insta::assert_debug_snapshot!(error); } #[test] fn test_missing_unicode_rbrace_error() { let source = r"'\N{SPACE'"; - let error = parse_suite(source).unwrap_err(); + let allocator = Allocator::new(); + let error = parse_suite(source, &allocator).unwrap_err(); insta::assert_debug_snapshot!(error); } #[test] fn test_invalid_unicode_name_error() { let source = r"'\N{INVALID}'"; - let error = parse_suite(source).unwrap_err(); + let allocator = Allocator::new(); + let error = parse_suite(source, &allocator).unwrap_err(); insta::assert_debug_snapshot!(error); } #[test] fn test_invalid_byte_literal_error() { let source = r"b'123a𝐁c'"; - let error = parse_suite(source).unwrap_err(); + let allocator = Allocator::new(); + let error = parse_suite(source, &allocator).unwrap_err(); insta::assert_debug_snapshot!(error); } @@ -846,7 +915,8 @@ mod tests { #[test] fn $name() { let source = format!(r#""\N{{{0}}}""#, $alias); - let suite = parse_suite(&source).unwrap(); + let allocator = Allocator::new(); + let suite = parse_suite(&source, &allocator).unwrap(); insta::assert_debug_snapshot!(suite); } )* diff --git a/crates/ruff_python_parser/src/typing.rs b/crates/ruff_python_parser/src/typing.rs index 273091515b..75ec9086bf 100644 --- a/crates/ruff_python_parser/src/typing.rs +++ b/crates/ruff_python_parser/src/typing.rs @@ -1,5 +1,6 @@ //! This module takes care of parsing a type annotation. +use ruff_allocator::Allocator; use ruff_python_ast::relocate::relocate_expr; use ruff_python_ast::str::raw_contents; use ruff_python_ast::{ExprStringLiteral, ModExpression, StringFlags, StringLiteral}; @@ -31,10 +32,11 @@ impl AnnotationKind { /// Parses the given string expression node as a type annotation. The given `source` is the entire /// source code. -pub fn parse_type_annotation( - string_expr: &ExprStringLiteral, - source: &str, -) -> Result<(Parsed, AnnotationKind), ParseError> { +pub fn parse_type_annotation<'a>( + string_expr: &ExprStringLiteral<'a>, + source: &'a str, + allocator: &'a Allocator, +) -> Result<(Parsed>, AnnotationKind), ParseError> { let expr_text = &source[string_expr.range()]; if let [string_literal] = string_expr.value.as_slice() { @@ -43,22 +45,23 @@ pub fn parse_type_annotation( if raw_contents(expr_text) .is_some_and(|raw_contents| raw_contents == string_literal.as_str()) { - parse_simple_type_annotation(string_literal, source) + parse_simple_type_annotation(string_literal, source, allocator) } else { // The raw contents of the string doesn't match the parsed content. This could be the // case for annotations that contain escaped quotes. - parse_complex_type_annotation(string_expr) + parse_complex_type_annotation(string_expr, allocator) } } else { // String is implicitly concatenated. - parse_complex_type_annotation(string_expr) + parse_complex_type_annotation(string_expr, allocator) } } -fn parse_simple_type_annotation( - string_literal: &StringLiteral, - source: &str, -) -> Result<(Parsed, AnnotationKind), ParseError> { +fn parse_simple_type_annotation<'a>( + string_literal: &StringLiteral<'a>, + source: &'a str, + allocator: &'a Allocator, +) -> Result<(Parsed>, AnnotationKind), ParseError> { Ok(( parse_expression_range( source, @@ -66,15 +69,17 @@ fn parse_simple_type_annotation( .range() .add_start(string_literal.flags.opener_len()) .sub_end(string_literal.flags.closer_len()), + allocator, )?, AnnotationKind::Simple, )) } -fn parse_complex_type_annotation( - string_expr: &ExprStringLiteral, -) -> Result<(Parsed, AnnotationKind), ParseError> { - let mut parsed = parse_expression(string_expr.value.to_str())?; +fn parse_complex_type_annotation<'ast>( + string_expr: &ExprStringLiteral<'_>, + allocator: &'ast Allocator, +) -> Result<(Parsed>, AnnotationKind), ParseError> { + let mut parsed = parse_expression(string_expr.value.to_str(), allocator)?; relocate_expr(parsed.expr_mut(), string_expr.range()); Ok((parsed, AnnotationKind::Complex)) } diff --git a/crates/ruff_python_parser/tests/fixtures.rs b/crates/ruff_python_parser/tests/fixtures.rs index 893695fa94..4cf92fda28 100644 --- a/crates/ruff_python_parser/tests/fixtures.rs +++ b/crates/ruff_python_parser/tests/fixtures.rs @@ -5,7 +5,7 @@ use std::path::Path; use annotate_snippets::display_list::{DisplayList, FormatOptions}; use annotate_snippets::snippet::{AnnotationType, Slice, Snippet, SourceAnnotation}; - +use ruff_allocator::Allocator; use ruff_python_ast::visitor::source_order::{walk_module, SourceOrderVisitor, TraversalSignal}; use ruff_python_ast::{AnyNodeRef, Mod}; use ruff_python_parser::{parse_unchecked, Mode, ParseErrorType, Token}; @@ -36,7 +36,8 @@ fn inline_err() { /// Snapshots the AST. fn test_valid_syntax(input_path: &Path) { let source = fs::read_to_string(input_path).expect("Expected test file to exist"); - let parsed = parse_unchecked(&source, Mode::Module); + let allocator = Allocator::new(); + let parsed = parse_unchecked(&source, Mode::Module, &allocator); if !parsed.is_valid() { let line_index = LineIndex::from_source_text(&source); @@ -80,7 +81,8 @@ fn test_valid_syntax(input_path: &Path) { /// Snapshots the AST and the error messages. fn test_invalid_syntax(input_path: &Path) { let source = fs::read_to_string(input_path).expect("Expected test file to exist"); - let parsed = parse_unchecked(&source, Mode::Module); + let allocator = Allocator::new(); + let parsed = parse_unchecked(&source, Mode::Module, &allocator); assert!( !parsed.is_valid(), @@ -132,7 +134,8 @@ f'{' f'{foo!r' "; - let parsed = parse_unchecked(source, Mode::Module); + let allocator = Allocator::new(); + let parsed = parse_unchecked(source, Mode::Module, &allocator); println!("AST:\n----\n{:#?}", parsed.syntax()); println!("Tokens:\n-------\n{:#?}", parsed.tokens()); @@ -272,14 +275,14 @@ fn validate_ast(root: &Mod, source_len: TextSize, test_path: &Path) { } #[derive(Debug)] -struct ValidateAstVisitor<'a> { - parents: Vec>, - previous: Option>, +struct ValidateAstVisitor<'a, 'ast> { + parents: Vec>, + previous: Option>, source_length: TextSize, test_path: &'a Path, } -impl<'a> ValidateAstVisitor<'a> { +impl<'a, 'ast> ValidateAstVisitor<'a, 'ast> { fn new(source_length: TextSize, test_path: &'a Path) -> Self { Self { parents: Vec::new(), @@ -290,8 +293,8 @@ impl<'a> ValidateAstVisitor<'a> { } } -impl<'ast> SourceOrderVisitor<'ast> for ValidateAstVisitor<'ast> { - fn enter_node(&mut self, node: AnyNodeRef<'ast>) -> TraversalSignal { +impl<'a, 'ast> SourceOrderVisitor<'a, 'ast> for ValidateAstVisitor<'a, 'ast> { + fn enter_node(&mut self, node: AnyNodeRef<'a, 'ast>) -> TraversalSignal { assert!( node.end() <= self.source_length, "{path}: The range of the node exceeds the length of the source code. Node: {node:#?}", @@ -319,7 +322,7 @@ impl<'ast> SourceOrderVisitor<'ast> for ValidateAstVisitor<'ast> { TraversalSignal::Traverse } - fn leave_node(&mut self, node: AnyNodeRef<'ast>) { + fn leave_node(&mut self, node: AnyNodeRef<'a, 'ast>) { self.parents.pop().expect("Expected tree to be balanced"); self.previous = Some(node); diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@ann_assign_stmt_invalid_annotation.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@ann_assign_stmt_invalid_annotation.py.snap.new new file mode 100644 index 0000000000..3ddbcf9d04 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@ann_assign_stmt_invalid_annotation.py.snap.new @@ -0,0 +1,201 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/ann_assign_stmt_invalid_annotation.py +--- +## AST + +``` +Module( + ModModule { + range: 0..63, + body: [ + AnnAssign( + StmtAnnAssign { + range: 0..11, + target: Name( + ExprName { + range: 0..1, + id: "x", + ctx: Store, + }, + ), + annotation: Starred( + ExprStarred { + range: 3..7, + value: Name( + ExprName { + range: 4..7, + id: "int", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + value: Some( + NumberLiteral( + ExprNumberLiteral { + range: 10..11, + value: Int( + 1, + ), + }, + ), + ), + simple: true, + }, + ), + AnnAssign( + StmtAnnAssign { + range: 12..26, + target: Name( + ExprName { + range: 12..13, + id: "x", + ctx: Store, + }, + ), + annotation: Yield( + ExprYield { + range: 15..22, + value: Some( + Name( + ExprName { + range: 21..22, + id: "a", + ctx: Load, + }, + ), + ), + }, + ), + value: Some( + NumberLiteral( + ExprNumberLiteral { + range: 25..26, + value: Int( + 1, + ), + }, + ), + ), + simple: true, + }, + ), + AnnAssign( + StmtAnnAssign { + range: 27..46, + target: Name( + ExprName { + range: 27..28, + id: "x", + ctx: Store, + }, + ), + annotation: YieldFrom( + ExprYieldFrom { + range: 30..42, + value: Name( + ExprName { + range: 41..42, + id: "b", + ctx: Load, + }, + ), + }, + ), + value: Some( + NumberLiteral( + ExprNumberLiteral { + range: 45..46, + value: Int( + 1, + ), + }, + ), + ), + simple: true, + }, + ), + AnnAssign( + StmtAnnAssign { + range: 47..51, + target: Name( + ExprName { + range: 47..48, + id: "x", + ctx: Store, + }, + ), + annotation: Name( + ExprName { + range: 50..51, + id: "y", + ctx: Load, + }, + ), + value: None, + simple: true, + }, + ), + Assign( + StmtAssign { + range: 55..62, + targets: [ + Name( + ExprName { + range: 55..58, + id: "int", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 61..62, + value: Int( + 1, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | x: *int = 1 + | ^^^^ Syntax Error: Starred expression cannot be used here +2 | x: yield a = 1 +3 | x: yield from b = 1 + | + + + | +1 | x: *int = 1 +2 | x: yield a = 1 + | ^^^^^^^ Syntax Error: Yield expression cannot be used here +3 | x: yield from b = 1 +4 | x: y := int = 1 + | + + + | +1 | x: *int = 1 +2 | x: yield a = 1 +3 | x: yield from b = 1 + | ^^^^^^^^^^^^ Syntax Error: Yield expression cannot be used here +4 | x: y := int = 1 + | + + + | +2 | x: yield a = 1 +3 | x: yield from b = 1 +4 | x: y := int = 1 + | ^^ Syntax Error: Expected a statement + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@ann_assign_stmt_invalid_target.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@ann_assign_stmt_invalid_target.py.snap.new new file mode 100644 index 0000000000..f7b07253a8 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@ann_assign_stmt_invalid_target.py.snap.new @@ -0,0 +1,509 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/ann_assign_stmt_invalid_target.py +--- +## AST + +``` +Module( + ModModule { + range: 0..170, + body: [ + AnnAssign( + StmtAnnAssign { + range: 0..18, + target: StringLiteral( + ExprStringLiteral { + range: 0..5, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 0..5, + value: "abc", + flags: StringLiteralFlags { + quote_style: Double, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + annotation: Name( + ExprName { + range: 7..10, + id: "str", + ctx: Load, + }, + ), + value: Some( + StringLiteral( + ExprStringLiteral { + range: 13..18, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 13..18, + value: "def", + flags: StringLiteralFlags { + quote_style: Double, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + ), + simple: false, + }, + ), + AnnAssign( + StmtAnnAssign { + range: 19..37, + target: Call( + ExprCall { + range: 19..25, + func: Name( + ExprName { + range: 19..23, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 23..25, + args: [], + keywords: [], + }, + }, + ), + annotation: Name( + ExprName { + range: 27..30, + id: "str", + ctx: Load, + }, + ), + value: Some( + StringLiteral( + ExprStringLiteral { + range: 33..37, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 33..37, + value: "no", + flags: StringLiteralFlags { + quote_style: Double, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + ), + simple: false, + }, + ), + AnnAssign( + StmtAnnAssign { + range: 38..52, + target: Starred( + ExprStarred { + range: 38..40, + value: Name( + ExprName { + range: 39..40, + id: "x", + ctx: Store, + }, + ), + ctx: Store, + }, + ), + annotation: Name( + ExprName { + range: 42..45, + id: "int", + ctx: Load, + }, + ), + value: Some( + Tuple( + ExprTuple { + range: 48..52, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 48..49, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 51..52, + value: Int( + 2, + ), + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + ), + simple: false, + }, + ), + AnnAssign( + StmtAnnAssign { + range: 72..83, + target: Tuple( + ExprTuple { + range: 72..74, + elts: [ + Name( + ExprName { + range: 72..73, + id: "x", + ctx: Store, + }, + ), + ], + ctx: Store, + parenthesized: false, + }, + ), + annotation: Name( + ExprName { + range: 76..79, + id: "int", + ctx: Load, + }, + ), + value: Some( + NumberLiteral( + ExprNumberLiteral { + range: 82..83, + value: Int( + 1, + ), + }, + ), + ), + simple: false, + }, + ), + AnnAssign( + StmtAnnAssign { + range: 84..100, + target: Tuple( + ExprTuple { + range: 84..88, + elts: [ + Name( + ExprName { + range: 84..85, + id: "x", + ctx: Store, + }, + ), + Name( + ExprName { + range: 87..88, + id: "y", + ctx: Store, + }, + ), + ], + ctx: Store, + parenthesized: false, + }, + ), + annotation: Name( + ExprName { + range: 90..93, + id: "int", + ctx: Load, + }, + ), + value: Some( + Tuple( + ExprTuple { + range: 96..100, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 96..97, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 99..100, + value: Int( + 2, + ), + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + ), + simple: false, + }, + ), + AnnAssign( + StmtAnnAssign { + range: 101..119, + target: Tuple( + ExprTuple { + range: 101..107, + elts: [ + Name( + ExprName { + range: 102..103, + id: "x", + ctx: Store, + }, + ), + Name( + ExprName { + range: 105..106, + id: "y", + ctx: Store, + }, + ), + ], + ctx: Store, + parenthesized: true, + }, + ), + annotation: Name( + ExprName { + range: 109..112, + id: "int", + ctx: Load, + }, + ), + value: Some( + Tuple( + ExprTuple { + range: 115..119, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 115..116, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 118..119, + value: Int( + 2, + ), + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + ), + simple: false, + }, + ), + AnnAssign( + StmtAnnAssign { + range: 138..150, + target: List( + ExprList { + range: 138..141, + elts: [ + Name( + ExprName { + range: 139..140, + id: "x", + ctx: Store, + }, + ), + ], + ctx: Store, + }, + ), + annotation: Name( + ExprName { + range: 143..146, + id: "int", + ctx: Load, + }, + ), + value: Some( + NumberLiteral( + ExprNumberLiteral { + range: 149..150, + value: Int( + 1, + ), + }, + ), + ), + simple: false, + }, + ), + AnnAssign( + StmtAnnAssign { + range: 151..169, + target: List( + ExprList { + range: 151..157, + elts: [ + Name( + ExprName { + range: 152..153, + id: "x", + ctx: Store, + }, + ), + Name( + ExprName { + range: 155..156, + id: "y", + ctx: Store, + }, + ), + ], + ctx: Store, + }, + ), + annotation: Name( + ExprName { + range: 159..162, + id: "int", + ctx: Load, + }, + ), + value: Some( + Tuple( + ExprTuple { + range: 165..169, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 165..166, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 168..169, + value: Int( + 2, + ), + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + ), + simple: false, + }, + ), + ], + }, +) +``` +## Errors + + | +1 | "abc": str = "def" + | ^^^^^ Syntax Error: Invalid annotated assignment target +2 | call(): str = "no" +3 | *x: int = 1, 2 + | + + + | +1 | "abc": str = "def" +2 | call(): str = "no" + | ^^^^^^ Syntax Error: Invalid annotated assignment target +3 | *x: int = 1, 2 +4 | # Tuple assignment + | + + + | +1 | "abc": str = "def" +2 | call(): str = "no" +3 | *x: int = 1, 2 + | ^^ Syntax Error: Invalid annotated assignment target +4 | # Tuple assignment +5 | x,: int = 1 + | + + + | +3 | *x: int = 1, 2 +4 | # Tuple assignment +5 | x,: int = 1 + | ^^ Syntax Error: Only single target (not tuple) can be annotated +6 | x, y: int = 1, 2 +7 | (x, y): int = 1, 2 + | + + + | +4 | # Tuple assignment +5 | x,: int = 1 +6 | x, y: int = 1, 2 + | ^^^^ Syntax Error: Only single target (not tuple) can be annotated +7 | (x, y): int = 1, 2 +8 | # List assignment + | + + + | +5 | x,: int = 1 +6 | x, y: int = 1, 2 +7 | (x, y): int = 1, 2 + | ^^^^^^ Syntax Error: Only single target (not tuple) can be annotated +8 | # List assignment +9 | [x]: int = 1 + | + + + | + 7 | (x, y): int = 1, 2 + 8 | # List assignment + 9 | [x]: int = 1 + | ^^^ Syntax Error: Only single target (not list) can be annotated +10 | [x, y]: int = 1, 2 + | + + + | + 8 | # List assignment + 9 | [x]: int = 1 +10 | [x, y]: int = 1, 2 + | ^^^^^^ Syntax Error: Only single target (not list) can be annotated + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@ann_assign_stmt_invalid_value.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@ann_assign_stmt_invalid_value.py.snap.new new file mode 100644 index 0000000000..465db77ee4 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@ann_assign_stmt_invalid_value.py.snap.new @@ -0,0 +1,223 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/ann_assign_stmt_invalid_value.py +--- +## AST + +``` +Module( + ModModule { + range: 0..65, + body: [ + AnnAssign( + StmtAnnAssign { + range: 0..17, + target: Name( + ExprName { + range: 0..1, + id: "x", + ctx: Store, + }, + ), + annotation: Name( + ExprName { + range: 3..6, + id: "Any", + ctx: Load, + }, + ), + value: Some( + Starred( + ExprStarred { + range: 9..17, + value: BoolOp( + ExprBoolOp { + range: 10..17, + op: And, + values: [ + Name( + ExprName { + range: 10..11, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 16..17, + id: "b", + ctx: Load, + }, + ), + ], + }, + ), + ctx: Load, + }, + ), + ), + simple: true, + }, + ), + AnnAssign( + StmtAnnAssign { + range: 18..28, + target: Name( + ExprName { + range: 18..19, + id: "x", + ctx: Store, + }, + ), + annotation: Name( + ExprName { + range: 21..24, + id: "Any", + ctx: Load, + }, + ), + value: Some( + Name( + ExprName { + range: 27..28, + id: "x", + ctx: Load, + }, + ), + ), + simple: true, + }, + ), + Expr( + StmtExpr { + range: 32..33, + value: NumberLiteral( + ExprNumberLiteral { + range: 32..33, + value: Int( + 1, + ), + }, + ), + }, + ), + AnnAssign( + StmtAnnAssign { + range: 34..64, + target: Name( + ExprName { + range: 34..35, + id: "x", + ctx: Store, + }, + ), + annotation: Name( + ExprName { + range: 37..41, + id: "list", + ctx: Load, + }, + ), + value: Some( + List( + ExprList { + range: 44..64, + elts: [ + Name( + ExprName { + range: 45..46, + id: "x", + ctx: Load, + }, + ), + Starred( + ExprStarred { + range: 48..54, + value: BinOp( + ExprBinOp { + range: 49..54, + left: Name( + ExprName { + range: 49..50, + id: "a", + ctx: Load, + }, + ), + op: BitOr, + right: Name( + ExprName { + range: 53..54, + id: "b", + ctx: Load, + }, + ), + }, + ), + ctx: Load, + }, + ), + Starred( + ExprStarred { + range: 56..63, + value: BoolOp( + ExprBoolOp { + range: 57..63, + op: Or, + values: [ + Name( + ExprName { + range: 57..58, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 62..63, + id: "b", + ctx: Load, + }, + ), + ], + }, + ), + ctx: Load, + }, + ), + ], + ctx: Load, + }, + ), + ), + simple: true, + }, + ), + ], + }, +) +``` +## Errors + + | +1 | x: Any = *a and b + | ^^^^^^^ Syntax Error: Boolean expression cannot be used here +2 | x: Any = x := 1 +3 | x: list = [x, *a | b, *a or b] + | + + + | +1 | x: Any = *a and b +2 | x: Any = x := 1 + | ^^ Syntax Error: Expected a statement +3 | x: list = [x, *a | b, *a or b] + | + + + | +1 | x: Any = *a and b +2 | x: Any = x := 1 +3 | x: list = [x, *a | b, *a or b] + | ^^^^^^ Syntax Error: Boolean expression cannot be used here + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@ann_assign_stmt_missing_rhs.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@ann_assign_stmt_missing_rhs.py.snap.new new file mode 100644 index 0000000000..869ded04eb --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@ann_assign_stmt_missing_rhs.py.snap.new @@ -0,0 +1,43 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/ann_assign_stmt_missing_rhs.py +--- +## AST + +``` +Module( + ModModule { + range: 0..9, + body: [ + AnnAssign( + StmtAnnAssign { + range: 0..8, + target: Name( + ExprName { + range: 0..1, + id: "x", + ctx: Store, + }, + ), + annotation: Name( + ExprName { + range: 3..6, + id: "int", + ctx: Load, + }, + ), + value: None, + simple: true, + }, + ), + ], + }, +) +``` +## Errors + + | +1 | x: int = + | ^ Syntax Error: Expected an expression + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@ann_assign_stmt_type_alias_annotation.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@ann_assign_stmt_type_alias_annotation.py.snap.new new file mode 100644 index 0000000000..a7f256d99c --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@ann_assign_stmt_type_alias_annotation.py.snap.new @@ -0,0 +1,111 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/ann_assign_stmt_type_alias_annotation.py +--- +## AST + +``` +Module( + ModModule { + range: 0..37, + body: [ + AnnAssign( + StmtAnnAssign { + range: 0..7, + target: Name( + ExprName { + range: 0..1, + id: "a", + ctx: Store, + }, + ), + annotation: Name( + ExprName { + range: 3..7, + id: "type", + ctx: Load, + }, + ), + value: None, + simple: true, + }, + ), + Assign( + StmtAssign { + range: 8..15, + targets: [ + Name( + ExprName { + range: 8..9, + id: "X", + ctx: Store, + }, + ), + ], + value: Name( + ExprName { + range: 12..15, + id: "int", + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 16..28, + value: Lambda( + ExprLambda { + range: 16..28, + parameters: None, + body: Name( + ExprName { + range: 24..28, + id: "type", + ctx: Load, + }, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 29..36, + targets: [ + Name( + ExprName { + range: 29..30, + id: "X", + ctx: Store, + }, + ), + ], + value: Name( + ExprName { + range: 33..36, + id: "int", + ctx: Load, + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | a: type X = int + | ^ Syntax Error: Simple statements must be separated by newlines or semicolons +2 | lambda: type X = int + | + + + | +1 | a: type X = int +2 | lambda: type X = int + | ^ Syntax Error: Simple statements must be separated by newlines or semicolons + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@assert_empty_msg.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@assert_empty_msg.py.snap.new new file mode 100644 index 0000000000..325bd9d6a5 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@assert_empty_msg.py.snap.new @@ -0,0 +1,35 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/assert_empty_msg.py +--- +## AST + +``` +Module( + ModModule { + range: 0..10, + body: [ + Assert( + StmtAssert { + range: 0..9, + test: Name( + ExprName { + range: 7..8, + id: "x", + ctx: Load, + }, + ), + msg: None, + }, + ), + ], + }, +) +``` +## Errors + + | +1 | assert x, + | ^ Syntax Error: Expected an expression + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@assert_empty_test.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@assert_empty_test.py.snap.new new file mode 100644 index 0000000000..c9174fa079 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@assert_empty_test.py.snap.new @@ -0,0 +1,35 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/assert_empty_test.py +--- +## AST + +``` +Module( + ModModule { + range: 0..7, + body: [ + Assert( + StmtAssert { + range: 0..6, + test: Name( + ExprName { + range: 6..6, + id: "", + ctx: Invalid, + }, + ), + msg: None, + }, + ), + ], + }, +) +``` +## Errors + + | +1 | assert + | ^ Syntax Error: Expected an expression + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@assert_invalid_msg_expr.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@assert_invalid_msg_expr.py.snap.new new file mode 100644 index 0000000000..549a6d9600 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@assert_invalid_msg_expr.py.snap.new @@ -0,0 +1,161 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/assert_invalid_msg_expr.py +--- +## AST + +``` +Module( + ModModule { + range: 0..83, + body: [ + Assert( + StmtAssert { + range: 0..16, + test: BooleanLiteral( + ExprBooleanLiteral { + range: 7..12, + value: false, + }, + ), + msg: Some( + Starred( + ExprStarred { + range: 14..16, + value: Name( + ExprName { + range: 15..16, + id: "x", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ), + }, + ), + Assert( + StmtAssert { + range: 17..30, + test: BooleanLiteral( + ExprBooleanLiteral { + range: 24..29, + value: false, + }, + ), + msg: None, + }, + ), + Assert( + StmtAssert { + range: 31..39, + test: Name( + ExprName { + range: 38..39, + id: "x", + ctx: Load, + }, + ), + msg: None, + }, + ), + Assert( + StmtAssert { + range: 40..61, + test: BooleanLiteral( + ExprBooleanLiteral { + range: 47..52, + value: false, + }, + ), + msg: Some( + Yield( + ExprYield { + range: 54..61, + value: Some( + Name( + ExprName { + range: 60..61, + id: "x", + ctx: Load, + }, + ), + ), + }, + ), + ), + }, + ), + Assert( + StmtAssert { + range: 62..77, + test: BooleanLiteral( + ExprBooleanLiteral { + range: 69..74, + value: false, + }, + ), + msg: Some( + Name( + ExprName { + range: 76..77, + id: "x", + ctx: Load, + }, + ), + ), + }, + ), + Expr( + StmtExpr { + range: 81..82, + value: NumberLiteral( + ExprNumberLiteral { + range: 81..82, + value: Int( + 1, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | assert False, *x + | ^^ Syntax Error: Starred expression cannot be used here +2 | assert False, assert x +3 | assert False, yield x + | + + + | +1 | assert False, *x +2 | assert False, assert x + | ^^^^^^ Syntax Error: Expected an expression +3 | assert False, yield x +4 | assert False, x := 1 + | + + + | +1 | assert False, *x +2 | assert False, assert x +3 | assert False, yield x + | ^^^^^^^ Syntax Error: Yield expression cannot be used here +4 | assert False, x := 1 + | + + + | +2 | assert False, assert x +3 | assert False, yield x +4 | assert False, x := 1 + | ^^ Syntax Error: Expected a statement + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@assert_invalid_test_expr.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@assert_invalid_test_expr.py.snap.new new file mode 100644 index 0000000000..78b1ae6043 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@assert_invalid_test_expr.py.snap.new @@ -0,0 +1,149 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/assert_invalid_test_expr.py +--- +## AST + +``` +Module( + ModModule { + range: 0..55, + body: [ + Assert( + StmtAssert { + range: 0..9, + test: Starred( + ExprStarred { + range: 7..9, + value: Name( + ExprName { + range: 8..9, + id: "x", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + msg: None, + }, + ), + Assert( + StmtAssert { + range: 10..23, + test: Name( + ExprName { + range: 17..23, + id: "assert", + ctx: Load, + }, + ), + msg: None, + }, + ), + Expr( + StmtExpr { + range: 24..25, + value: Name( + ExprName { + range: 24..25, + id: "x", + ctx: Load, + }, + ), + }, + ), + Assert( + StmtAssert { + range: 26..40, + test: Yield( + ExprYield { + range: 33..40, + value: Some( + Name( + ExprName { + range: 39..40, + id: "x", + ctx: Load, + }, + ), + ), + }, + ), + msg: None, + }, + ), + Assert( + StmtAssert { + range: 41..49, + test: Name( + ExprName { + range: 48..49, + id: "x", + ctx: Load, + }, + ), + msg: None, + }, + ), + Expr( + StmtExpr { + range: 53..54, + value: NumberLiteral( + ExprNumberLiteral { + range: 53..54, + value: Int( + 1, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | assert *x + | ^^ Syntax Error: Starred expression cannot be used here +2 | assert assert x +3 | assert yield x + | + + + | +1 | assert *x +2 | assert assert x + | ^^^^^^ Syntax Error: Expected an identifier, but found a keyword 'assert' that cannot be used here +3 | assert yield x +4 | assert x := 1 + | + + + | +1 | assert *x +2 | assert assert x + | ^ Syntax Error: Simple statements must be separated by newlines or semicolons +3 | assert yield x +4 | assert x := 1 + | + + + | +1 | assert *x +2 | assert assert x +3 | assert yield x + | ^^^^^^^ Syntax Error: Yield expression cannot be used here +4 | assert x := 1 + | + + + | +2 | assert assert x +3 | assert yield x +4 | assert x := 1 + | ^^ Syntax Error: Expected a statement + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@assign_stmt_invalid_target.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@assign_stmt_invalid_target.py.snap.new new file mode 100644 index 0000000000..9a89066d6b --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@assign_stmt_invalid_target.py.snap.new @@ -0,0 +1,259 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/assign_stmt_invalid_target.py +--- +## AST + +``` +Module( + ModModule { + range: 0..58, + body: [ + Assign( + StmtAssign { + range: 0..5, + targets: [ + NumberLiteral( + ExprNumberLiteral { + range: 0..1, + value: Int( + 1, + ), + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 4..5, + value: Int( + 1, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 6..15, + targets: [ + Name( + ExprName { + range: 6..7, + id: "x", + ctx: Store, + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 10..11, + value: Int( + 1, + ), + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 14..15, + value: Int( + 2, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 16..33, + targets: [ + Name( + ExprName { + range: 16..17, + id: "x", + ctx: Store, + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 20..21, + value: Int( + 1, + ), + }, + ), + Name( + ExprName { + range: 24..25, + id: "y", + ctx: Store, + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 28..29, + value: Int( + 2, + ), + }, + ), + ], + value: Name( + ExprName { + range: 32..33, + id: "z", + ctx: Load, + }, + ), + }, + ), + Assign( + StmtAssign { + range: 34..57, + targets: [ + List( + ExprList { + range: 34..44, + elts: [ + StringLiteral( + ExprStringLiteral { + range: 35..38, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 35..38, + value: "a", + flags: StringLiteralFlags { + quote_style: Double, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + StringLiteral( + ExprStringLiteral { + range: 40..43, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 40..43, + value: "b", + flags: StringLiteralFlags { + quote_style: Double, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + ], + ctx: Store, + }, + ), + ], + value: List( + ExprList { + range: 47..57, + elts: [ + StringLiteral( + ExprStringLiteral { + range: 48..51, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 48..51, + value: "a", + flags: StringLiteralFlags { + quote_style: Double, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + StringLiteral( + ExprStringLiteral { + range: 53..56, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 53..56, + value: "b", + flags: StringLiteralFlags { + quote_style: Double, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + ], + ctx: Load, + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | 1 = 1 + | ^ Syntax Error: Invalid assignment target +2 | x = 1 = 2 +3 | x = 1 = y = 2 = z + | + + + | +1 | 1 = 1 +2 | x = 1 = 2 + | ^ Syntax Error: Invalid assignment target +3 | x = 1 = y = 2 = z +4 | ["a", "b"] = ["a", "b"] + | + + + | +1 | 1 = 1 +2 | x = 1 = 2 +3 | x = 1 = y = 2 = z + | ^ Syntax Error: Invalid assignment target +4 | ["a", "b"] = ["a", "b"] + | + + + | +1 | 1 = 1 +2 | x = 1 = 2 +3 | x = 1 = y = 2 = z + | ^ Syntax Error: Invalid assignment target +4 | ["a", "b"] = ["a", "b"] + | + + + | +2 | x = 1 = 2 +3 | x = 1 = y = 2 = z +4 | ["a", "b"] = ["a", "b"] + | ^^^ Syntax Error: Invalid assignment target + | + + + | +2 | x = 1 = 2 +3 | x = 1 = y = 2 = z +4 | ["a", "b"] = ["a", "b"] + | ^^^ Syntax Error: Invalid assignment target + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@assign_stmt_invalid_value_expr.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@assign_stmt_invalid_value_expr.py.snap.new new file mode 100644 index 0000000000..a5f9d28b95 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@assign_stmt_invalid_value_expr.py.snap.new @@ -0,0 +1,257 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/assign_stmt_invalid_value_expr.py +--- +## AST + +``` +Module( + ModModule { + range: 0..72, + body: [ + Assign( + StmtAssign { + range: 0..12, + targets: [ + Name( + ExprName { + range: 0..1, + id: "x", + ctx: Store, + }, + ), + ], + value: Starred( + ExprStarred { + range: 4..12, + value: BoolOp( + ExprBoolOp { + range: 5..12, + op: And, + values: [ + Name( + ExprName { + range: 5..6, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 11..12, + id: "b", + ctx: Load, + }, + ), + ], + }, + ), + ctx: Load, + }, + ), + }, + ), + Assign( + StmtAssign { + range: 13..25, + targets: [ + Name( + ExprName { + range: 13..14, + id: "x", + ctx: Store, + }, + ), + ], + value: Starred( + ExprStarred { + range: 17..25, + value: Yield( + ExprYield { + range: 18..25, + value: Some( + Name( + ExprName { + range: 24..25, + id: "x", + ctx: Load, + }, + ), + ), + }, + ), + ctx: Load, + }, + ), + }, + ), + Assign( + StmtAssign { + range: 26..43, + targets: [ + Name( + ExprName { + range: 26..27, + id: "x", + ctx: Store, + }, + ), + ], + value: Starred( + ExprStarred { + range: 30..43, + value: YieldFrom( + ExprYieldFrom { + range: 31..43, + value: Name( + ExprName { + range: 42..43, + id: "x", + ctx: Load, + }, + ), + }, + ), + ctx: Load, + }, + ), + }, + ), + Assign( + StmtAssign { + range: 44..60, + targets: [ + Name( + ExprName { + range: 44..45, + id: "x", + ctx: Store, + }, + ), + ], + value: Starred( + ExprStarred { + range: 48..60, + value: Lambda( + ExprLambda { + range: 49..60, + parameters: Some( + Parameters { + range: 56..57, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 56..57, + parameter: Parameter { + range: 56..57, + name: Identifier { + id: "x", + range: 56..57, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: Name( + ExprName { + range: 59..60, + id: "x", + ctx: Load, + }, + ), + }, + ), + ctx: Load, + }, + ), + }, + ), + Assign( + StmtAssign { + range: 61..66, + targets: [ + Name( + ExprName { + range: 61..62, + id: "x", + ctx: Store, + }, + ), + ], + value: Name( + ExprName { + range: 65..66, + id: "x", + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 70..71, + value: NumberLiteral( + ExprNumberLiteral { + range: 70..71, + value: Int( + 1, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | x = *a and b + | ^^^^^^^ Syntax Error: Boolean expression cannot be used here +2 | x = *yield x +3 | x = *yield from x + | + + + | +1 | x = *a and b +2 | x = *yield x + | ^^^^^^^ Syntax Error: Yield expression cannot be used here +3 | x = *yield from x +4 | x = *lambda x: x + | + + + | +1 | x = *a and b +2 | x = *yield x +3 | x = *yield from x + | ^^^^^^^^^^^^ Syntax Error: Yield expression cannot be used here +4 | x = *lambda x: x +5 | x = x := 1 + | + + + | +2 | x = *yield x +3 | x = *yield from x +4 | x = *lambda x: x + | ^^^^^^^^^^^ Syntax Error: Lambda expression cannot be used here +5 | x = x := 1 + | + + + | +3 | x = *yield from x +4 | x = *lambda x: x +5 | x = x := 1 + | ^^ Syntax Error: Expected a statement + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@assign_stmt_keyword_target.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@assign_stmt_keyword_target.py.snap.new new file mode 100644 index 0000000000..32f2a8c662 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@assign_stmt_keyword_target.py.snap.new @@ -0,0 +1,146 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/assign_stmt_keyword_target.py +--- +## AST + +``` +Module( + ModModule { + range: 0..42, + body: [ + Assign( + StmtAssign { + range: 0..12, + targets: [ + Name( + ExprName { + range: 0..1, + id: "a", + ctx: Store, + }, + ), + Name( + ExprName { + range: 4..8, + id: "pass", + ctx: Store, + }, + ), + ], + value: Name( + ExprName { + range: 11..12, + id: "c", + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 13..18, + value: BinOp( + ExprBinOp { + range: 13..18, + left: Name( + ExprName { + range: 13..14, + id: "a", + ctx: Load, + }, + ), + op: Add, + right: Name( + ExprName { + range: 17..18, + id: "b", + ctx: Load, + }, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 19..35, + targets: [ + Name( + ExprName { + range: 19..20, + id: "a", + ctx: Store, + }, + ), + Name( + ExprName { + range: 23..24, + id: "b", + ctx: Store, + }, + ), + Name( + ExprName { + range: 27..31, + id: "pass", + ctx: Store, + }, + ), + ], + value: Name( + ExprName { + range: 34..35, + id: "c", + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 36..41, + value: BinOp( + ExprBinOp { + range: 36..41, + left: Name( + ExprName { + range: 36..37, + id: "a", + ctx: Load, + }, + ), + op: Add, + right: Name( + ExprName { + range: 40..41, + id: "b", + ctx: Load, + }, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | a = pass = c + | ^^^^ Syntax Error: Expected an identifier, but found a keyword 'pass' that cannot be used here +2 | a + b +3 | a = b = pass = c + | + + + | +1 | a = pass = c +2 | a + b +3 | a = b = pass = c + | ^^^^ Syntax Error: Expected an identifier, but found a keyword 'pass' that cannot be used here +4 | a + b + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@assign_stmt_missing_rhs.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@assign_stmt_missing_rhs.py.snap.new new file mode 100644 index 0000000000..84ee6df61f --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@assign_stmt_missing_rhs.py.snap.new @@ -0,0 +1,203 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/assign_stmt_missing_rhs.py +--- +## AST + +``` +Module( + ModModule { + range: 0..38, + body: [ + Assign( + StmtAssign { + range: 0..3, + targets: [ + Name( + ExprName { + range: 0..1, + id: "x", + ctx: Store, + }, + ), + ], + value: Name( + ExprName { + range: 3..3, + id: "", + ctx: Invalid, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 4..9, + value: BinOp( + ExprBinOp { + range: 4..9, + left: NumberLiteral( + ExprNumberLiteral { + range: 4..5, + value: Int( + 1, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 8..9, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 10..17, + targets: [ + Name( + ExprName { + range: 10..11, + id: "x", + ctx: Store, + }, + ), + Name( + ExprName { + range: 14..15, + id: "y", + ctx: Store, + }, + ), + ], + value: Name( + ExprName { + range: 17..17, + id: "", + ctx: Invalid, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 18..23, + value: BinOp( + ExprBinOp { + range: 18..23, + left: NumberLiteral( + ExprNumberLiteral { + range: 18..19, + value: Int( + 2, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 22..23, + value: Int( + 2, + ), + }, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 24..31, + targets: [ + Name( + ExprName { + range: 24..25, + id: "x", + ctx: Store, + }, + ), + Name( + ExprName { + range: 27..27, + id: "", + ctx: Store, + }, + ), + ], + value: Name( + ExprName { + range: 30..31, + id: "y", + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 32..37, + value: BinOp( + ExprBinOp { + range: 32..37, + left: NumberLiteral( + ExprNumberLiteral { + range: 32..33, + value: Int( + 3, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 36..37, + value: Int( + 3, + ), + }, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | x = + | ^ Syntax Error: Expected an expression +2 | 1 + 1 +3 | x = y = +4 | 2 + 2 + | + + + | +1 | x = +2 | 1 + 1 +3 | x = y = + | ^ Syntax Error: Expected an expression +4 | 2 + 2 +5 | x = = y +6 | 3 + 3 + | + + + | +3 | x = y = +4 | 2 + 2 +5 | x = = y + | ^ Syntax Error: Expected an expression +6 | 3 + 3 + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@async_unexpected_token.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@async_unexpected_token.py.snap.new new file mode 100644 index 0000000000..bd10f4e301 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@async_unexpected_token.py.snap.new @@ -0,0 +1,203 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/async_unexpected_token.py +--- +## AST + +``` +Module( + ModModule { + range: 0..116, + body: [ + ClassDef( + StmtClassDef { + range: 6..20, + decorator_list: [], + name: Identifier { + id: "Foo", + range: 12..15, + }, + type_params: None, + arguments: None, + body: [ + Expr( + StmtExpr { + range: 17..20, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 17..20, + }, + ), + }, + ), + ], + }, + ), + While( + StmtWhile { + range: 27..42, + test: Name( + ExprName { + range: 33..37, + id: "test", + ctx: Load, + }, + ), + body: [ + Expr( + StmtExpr { + range: 39..42, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 39..42, + }, + ), + }, + ), + ], + orelse: [], + }, + ), + Assign( + StmtAssign { + range: 49..54, + targets: [ + Name( + ExprName { + range: 49..50, + id: "x", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 53..54, + value: Int( + 1, + ), + }, + ), + }, + ), + FunctionDef( + StmtFunctionDef { + range: 61..81, + is_async: true, + decorator_list: [], + name: Identifier { + id: "foo", + range: 71..74, + }, + type_params: None, + parameters: Parameters { + range: 74..76, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 78..81, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 78..81, + }, + ), + }, + ), + ], + }, + ), + Match( + StmtMatch { + range: 88..115, + subject: Name( + ExprName { + range: 94..98, + id: "test", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 104..115, + pattern: MatchAs( + PatternMatchAs { + range: 109..110, + pattern: None, + name: None, + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 112..115, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 112..115, + }, + ), + }, + ), + ], + }, + ], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | async class Foo: ... + | ^^^^^ Syntax Error: Expected 'def', 'with' or 'for' to follow 'async', found 'class' +2 | async while test: ... +3 | async x = 1 + | + + + | +1 | async class Foo: ... +2 | async while test: ... + | ^^^^^ Syntax Error: Expected 'def', 'with' or 'for' to follow 'async', found 'while' +3 | async x = 1 +4 | async async def foo(): ... + | + + + | +1 | async class Foo: ... +2 | async while test: ... +3 | async x = 1 + | ^ Syntax Error: Expected 'def', 'with' or 'for' to follow 'async', found name +4 | async async def foo(): ... +5 | async match test: + | + + + | +2 | async while test: ... +3 | async x = 1 +4 | async async def foo(): ... + | ^^^^^ Syntax Error: Expected 'def', 'with' or 'for' to follow 'async', found 'async' +5 | async match test: +6 | case _: ... + | + + + | +3 | async x = 1 +4 | async async def foo(): ... +5 | async match test: + | ^^^^^ Syntax Error: Expected 'def', 'with' or 'for' to follow 'async', found 'match' +6 | case _: ... + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@aug_assign_stmt_invalid_target.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@aug_assign_stmt_invalid_target.py.snap.new new file mode 100644 index 0000000000..a8d62f8bcb --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@aug_assign_stmt_invalid_target.py.snap.new @@ -0,0 +1,233 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/aug_assign_stmt_invalid_target.py +--- +## AST + +``` +Module( + ModModule { + range: 0..59, + body: [ + AugAssign( + StmtAugAssign { + range: 0..6, + target: NumberLiteral( + ExprNumberLiteral { + range: 0..1, + value: Int( + 1, + ), + }, + ), + op: Add, + value: NumberLiteral( + ExprNumberLiteral { + range: 5..6, + value: Int( + 1, + ), + }, + ), + }, + ), + AugAssign( + StmtAugAssign { + range: 7..17, + target: StringLiteral( + ExprStringLiteral { + range: 7..10, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 7..10, + value: "a", + flags: StringLiteralFlags { + quote_style: Double, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + op: Add, + value: StringLiteral( + ExprStringLiteral { + range: 14..17, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 14..17, + value: "b", + flags: StringLiteralFlags { + quote_style: Double, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + }, + ), + AugAssign( + StmtAugAssign { + range: 18..25, + target: Starred( + ExprStarred { + range: 18..20, + value: Name( + ExprName { + range: 19..20, + id: "x", + ctx: Store, + }, + ), + ctx: Store, + }, + ), + op: Add, + value: NumberLiteral( + ExprNumberLiteral { + range: 24..25, + value: Int( + 1, + ), + }, + ), + }, + ), + Pass( + StmtPass { + range: 26..30, + }, + ), + Expr( + StmtExpr { + range: 34..35, + value: NumberLiteral( + ExprNumberLiteral { + range: 34..35, + value: Int( + 1, + ), + }, + ), + }, + ), + AugAssign( + StmtAugAssign { + range: 36..45, + target: Name( + ExprName { + range: 36..37, + id: "x", + ctx: Store, + }, + ), + op: Add, + value: Name( + ExprName { + range: 41..45, + id: "pass", + ctx: Load, + }, + ), + }, + ), + AugAssign( + StmtAugAssign { + range: 46..58, + target: BinOp( + ExprBinOp { + range: 47..52, + left: Name( + ExprName { + range: 47..48, + id: "x", + ctx: Load, + }, + ), + op: Add, + right: Name( + ExprName { + range: 51..52, + id: "y", + ctx: Load, + }, + ), + }, + ), + op: Add, + value: NumberLiteral( + ExprNumberLiteral { + range: 57..58, + value: Int( + 1, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | 1 += 1 + | ^ Syntax Error: Invalid augmented assignment target +2 | "a" += "b" +3 | *x += 1 + | + + + | +1 | 1 += 1 +2 | "a" += "b" + | ^^^ Syntax Error: Invalid augmented assignment target +3 | *x += 1 +4 | pass += 1 + | + + + | +1 | 1 += 1 +2 | "a" += "b" +3 | *x += 1 + | ^^ Syntax Error: Invalid augmented assignment target +4 | pass += 1 +5 | x += pass + | + + + | +2 | "a" += "b" +3 | *x += 1 +4 | pass += 1 + | ^^ Syntax Error: Expected a statement +5 | x += pass +6 | (x + y) += 1 + | + + + | +3 | *x += 1 +4 | pass += 1 +5 | x += pass + | ^^^^ Syntax Error: Expected an identifier, but found a keyword 'pass' that cannot be used here +6 | (x + y) += 1 + | + + + | +4 | pass += 1 +5 | x += pass +6 | (x + y) += 1 + | ^^^^^ Syntax Error: Invalid augmented assignment target + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@aug_assign_stmt_invalid_value.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@aug_assign_stmt_invalid_value.py.snap.new new file mode 100644 index 0000000000..f3cddf98b8 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@aug_assign_stmt_invalid_value.py.snap.new @@ -0,0 +1,252 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/aug_assign_stmt_invalid_value.py +--- +## AST + +``` +Module( + ModModule { + range: 0..77, + body: [ + AugAssign( + StmtAugAssign { + range: 0..13, + target: Name( + ExprName { + range: 0..1, + id: "x", + ctx: Store, + }, + ), + op: Add, + value: Starred( + ExprStarred { + range: 5..13, + value: BoolOp( + ExprBoolOp { + range: 6..13, + op: And, + values: [ + Name( + ExprName { + range: 6..7, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 12..13, + id: "b", + ctx: Load, + }, + ), + ], + }, + ), + ctx: Load, + }, + ), + }, + ), + AugAssign( + StmtAugAssign { + range: 14..27, + target: Name( + ExprName { + range: 14..15, + id: "x", + ctx: Store, + }, + ), + op: Add, + value: Starred( + ExprStarred { + range: 19..27, + value: Yield( + ExprYield { + range: 20..27, + value: Some( + Name( + ExprName { + range: 26..27, + id: "x", + ctx: Load, + }, + ), + ), + }, + ), + ctx: Load, + }, + ), + }, + ), + AugAssign( + StmtAugAssign { + range: 28..46, + target: Name( + ExprName { + range: 28..29, + id: "x", + ctx: Store, + }, + ), + op: Add, + value: Starred( + ExprStarred { + range: 33..46, + value: YieldFrom( + ExprYieldFrom { + range: 34..46, + value: Name( + ExprName { + range: 45..46, + id: "x", + ctx: Load, + }, + ), + }, + ), + ctx: Load, + }, + ), + }, + ), + AugAssign( + StmtAugAssign { + range: 47..64, + target: Name( + ExprName { + range: 47..48, + id: "x", + ctx: Store, + }, + ), + op: Add, + value: Starred( + ExprStarred { + range: 52..64, + value: Lambda( + ExprLambda { + range: 53..64, + parameters: Some( + Parameters { + range: 60..61, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 60..61, + parameter: Parameter { + range: 60..61, + name: Identifier { + id: "x", + range: 60..61, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: Name( + ExprName { + range: 63..64, + id: "x", + ctx: Load, + }, + ), + }, + ), + ctx: Load, + }, + ), + }, + ), + AugAssign( + StmtAugAssign { + range: 65..71, + target: Name( + ExprName { + range: 65..66, + id: "x", + ctx: Store, + }, + ), + op: Add, + value: Name( + ExprName { + range: 70..71, + id: "y", + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 75..76, + value: NumberLiteral( + ExprNumberLiteral { + range: 75..76, + value: Int( + 1, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | x += *a and b + | ^^^^^^^ Syntax Error: Boolean expression cannot be used here +2 | x += *yield x +3 | x += *yield from x + | + + + | +1 | x += *a and b +2 | x += *yield x + | ^^^^^^^ Syntax Error: Yield expression cannot be used here +3 | x += *yield from x +4 | x += *lambda x: x + | + + + | +1 | x += *a and b +2 | x += *yield x +3 | x += *yield from x + | ^^^^^^^^^^^^ Syntax Error: Yield expression cannot be used here +4 | x += *lambda x: x +5 | x += y := 1 + | + + + | +2 | x += *yield x +3 | x += *yield from x +4 | x += *lambda x: x + | ^^^^^^^^^^^ Syntax Error: Lambda expression cannot be used here +5 | x += y := 1 + | + + + | +3 | x += *yield from x +4 | x += *lambda x: x +5 | x += y := 1 + | ^^ Syntax Error: Expected a statement + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@aug_assign_stmt_missing_rhs.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@aug_assign_stmt_missing_rhs.py.snap.new new file mode 100644 index 0000000000..46481509e3 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@aug_assign_stmt_missing_rhs.py.snap.new @@ -0,0 +1,137 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/aug_assign_stmt_missing_rhs.py +--- +## AST + +``` +Module( + ModModule { + range: 0..27, + body: [ + AugAssign( + StmtAugAssign { + range: 0..4, + target: Name( + ExprName { + range: 0..1, + id: "x", + ctx: Store, + }, + ), + op: Add, + value: Name( + ExprName { + range: 4..4, + id: "", + ctx: Invalid, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 5..10, + value: BinOp( + ExprBinOp { + range: 5..10, + left: NumberLiteral( + ExprNumberLiteral { + range: 5..6, + value: Int( + 1, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 9..10, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + AugAssign( + StmtAugAssign { + range: 11..17, + target: Name( + ExprName { + range: 11..12, + id: "x", + ctx: Store, + }, + ), + op: Add, + value: Name( + ExprName { + range: 16..17, + id: "y", + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 21..26, + value: BinOp( + ExprBinOp { + range: 21..26, + left: NumberLiteral( + ExprNumberLiteral { + range: 21..22, + value: Int( + 2, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 25..26, + value: Int( + 2, + ), + }, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | x += + | ^ Syntax Error: Expected an expression +2 | 1 + 1 +3 | x += y += +4 | 2 + 2 + | + + + | +1 | x += +2 | 1 + 1 +3 | x += y += + | ^^ Syntax Error: Expected a statement +4 | 2 + 2 + | + + + | +1 | x += +2 | 1 + 1 +3 | x += y += + | ^ Syntax Error: Expected a statement +4 | 2 + 2 + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@case_expect_indented_block.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@case_expect_indented_block.py.snap.new new file mode 100644 index 0000000000..d09ecc7e05 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@case_expect_indented_block.py.snap.new @@ -0,0 +1,85 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/case_expect_indented_block.py +--- +## AST + +``` +Module( + ModModule { + range: 0..43, + body: [ + Match( + StmtMatch { + range: 0..42, + subject: Name( + ExprName { + range: 6..13, + id: "subject", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 19..26, + pattern: MatchValue( + PatternMatchValue { + range: 24..25, + value: NumberLiteral( + ExprNumberLiteral { + range: 24..25, + value: Int( + 1, + ), + }, + ), + }, + ), + guard: None, + body: [], + }, + MatchCase { + range: 31..42, + pattern: MatchValue( + PatternMatchValue { + range: 36..37, + value: NumberLiteral( + ExprNumberLiteral { + range: 36..37, + value: Int( + 2, + ), + }, + ), + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 39..42, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 39..42, + }, + ), + }, + ), + ], + }, + ], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | match subject: +2 | case 1: +3 | case 2: ... + | ^^^^ Syntax Error: Expected an indented block after `case` block + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@class_def_empty_body.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@class_def_empty_body.py.snap.new new file mode 100644 index 0000000000..3d601527c6 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@class_def_empty_body.py.snap.new @@ -0,0 +1,86 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/class_def_empty_body.py +--- +## AST + +``` +Module( + ModModule { + range: 0..31, + body: [ + ClassDef( + StmtClassDef { + range: 0..10, + decorator_list: [], + name: Identifier { + id: "Foo", + range: 6..9, + }, + type_params: None, + arguments: None, + body: [], + }, + ), + ClassDef( + StmtClassDef { + range: 11..23, + decorator_list: [], + name: Identifier { + id: "Foo", + range: 17..20, + }, + type_params: None, + arguments: Some( + Arguments { + range: 20..22, + args: [], + keywords: [], + }, + ), + body: [], + }, + ), + Assign( + StmtAssign { + range: 24..30, + targets: [ + Name( + ExprName { + range: 24..25, + id: "x", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 28..30, + value: Int( + 42, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | class Foo: +2 | class Foo(): + | ^^^^^ Syntax Error: Expected an indented block after `class` definition +3 | x = 42 + | + + + | +1 | class Foo: +2 | class Foo(): +3 | x = 42 + | ^ Syntax Error: Expected an indented block after `class` definition + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@class_def_missing_name.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@class_def_missing_name.py.snap.new new file mode 100644 index 0000000000..23a31082d3 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@class_def_missing_name.py.snap.new @@ -0,0 +1,141 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/class_def_missing_name.py +--- +## AST + +``` +Module( + ModModule { + range: 0..53, + body: [ + ClassDef( + StmtClassDef { + range: 0..11, + decorator_list: [], + name: Identifier { + id: "", + range: 5..5, + }, + type_params: None, + arguments: None, + body: [ + Expr( + StmtExpr { + range: 8..11, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 8..11, + }, + ), + }, + ), + ], + }, + ), + ClassDef( + StmtClassDef { + range: 12..25, + decorator_list: [], + name: Identifier { + id: "", + range: 17..17, + }, + type_params: None, + arguments: Some( + Arguments { + range: 18..20, + args: [], + keywords: [], + }, + ), + body: [ + Expr( + StmtExpr { + range: 22..25, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 22..25, + }, + ), + }, + ), + ], + }, + ), + ClassDef( + StmtClassDef { + range: 26..52, + decorator_list: [], + name: Identifier { + id: "", + range: 31..31, + }, + type_params: None, + arguments: Some( + Arguments { + range: 32..47, + args: [], + keywords: [ + Keyword { + range: 33..46, + arg: Some( + Identifier { + id: "metaclass", + range: 33..42, + }, + ), + value: Name( + ExprName { + range: 43..46, + id: "ABC", + ctx: Load, + }, + ), + }, + ], + }, + ), + body: [ + Expr( + StmtExpr { + range: 49..52, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 49..52, + }, + ), + }, + ), + ], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | class : ... + | ^ Syntax Error: Expected an identifier +2 | class (): ... +3 | class (metaclass=ABC): ... + | + + + | +1 | class : ... +2 | class (): ... + | ^ Syntax Error: Expected an identifier +3 | class (metaclass=ABC): ... + | + + + | +1 | class : ... +2 | class (): ... +3 | class (metaclass=ABC): ... + | ^ Syntax Error: Expected an identifier + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@class_def_unclosed_type_param_list.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@class_def_unclosed_type_param_list.py.snap.new new file mode 100644 index 0000000000..745d18d95f --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@class_def_unclosed_type_param_list.py.snap.new @@ -0,0 +1,113 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/class_def_unclosed_type_param_list.py +--- +## AST + +``` +Module( + ModModule { + range: 0..41, + body: [ + ClassDef( + StmtClassDef { + range: 0..33, + decorator_list: [], + name: Identifier { + id: "Foo", + range: 6..9, + }, + type_params: Some( + TypeParams { + range: 9..17, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 10..12, + name: Identifier { + id: "T1", + range: 10..12, + }, + bound: None, + default: None, + }, + ), + TypeVarTuple( + TypeParamTypeVarTuple { + range: 14..17, + name: Identifier { + id: "T2", + range: 15..17, + }, + default: None, + }, + ), + ], + }, + ), + arguments: Some( + Arguments { + range: 17..23, + args: [ + Name( + ExprName { + range: 18..19, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 21..22, + id: "b", + ctx: Load, + }, + ), + ], + keywords: [], + }, + ), + body: [ + Pass( + StmtPass { + range: 29..33, + }, + ), + ], + }, + ), + Assign( + StmtAssign { + range: 34..40, + targets: [ + Name( + ExprName { + range: 34..35, + id: "x", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 38..40, + value: Int( + 10, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | class Foo[T1, *T2(a, b): + | ^ Syntax Error: Expected ']', found '(' +2 | pass +3 | x = 10 + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@comma_separated_missing_comma.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@comma_separated_missing_comma.py.snap.new new file mode 100644 index 0000000000..3fd56eb850 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@comma_separated_missing_comma.py.snap.new @@ -0,0 +1,71 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/comma_separated_missing_comma.py +--- +## AST + +``` +Module( + ModModule { + range: 0..15, + body: [ + Expr( + StmtExpr { + range: 0..14, + value: Call( + ExprCall { + range: 0..14, + func: Name( + ExprName { + range: 0..4, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 4..14, + args: [ + NumberLiteral( + ExprNumberLiteral { + range: 12..13, + value: Int( + 1, + ), + }, + ), + ], + keywords: [ + Keyword { + range: 5..8, + arg: None, + value: Name( + ExprName { + range: 7..8, + id: "x", + ctx: Load, + }, + ), + }, + ], + }, + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | call(**x := 1) + | ^^ Syntax Error: Expected ',', found ':=' + | + + + | +1 | call(**x := 1) + | ^ Syntax Error: Positional argument cannot follow keyword argument unpacking + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@comma_separated_missing_first_element.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@comma_separated_missing_first_element.py.snap.new new file mode 100644 index 0000000000..302c3ce449 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@comma_separated_missing_first_element.py.snap.new @@ -0,0 +1,53 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/comma_separated_missing_first_element.py +--- +## AST + +``` +Module( + ModModule { + range: 0..10, + body: [ + Expr( + StmtExpr { + range: 0..9, + value: Call( + ExprCall { + range: 0..9, + func: Name( + ExprName { + range: 0..4, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 4..9, + args: [ + NumberLiteral( + ExprNumberLiteral { + range: 7..8, + value: Int( + 1, + ), + }, + ), + ], + keywords: [], + }, + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | call(= 1) + | ^ Syntax Error: Expected an expression or a ')' + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@comprehension_missing_for_after_async.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@comprehension_missing_for_after_async.py.snap.new new file mode 100644 index 0000000000..8c16e1e992 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@comprehension_missing_for_after_async.py.snap.new @@ -0,0 +1,81 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/comprehension_missing_for_after_async.py +--- +## AST + +``` +Module( + ModModule { + range: 0..28, + body: [ + Expr( + StmtExpr { + range: 0..7, + value: Name( + ExprName { + range: 1..6, + id: "async", + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 8..27, + value: Generator( + ExprGenerator { + range: 8..27, + elt: Name( + ExprName { + range: 9..10, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 11..26, + target: Name( + ExprName { + range: 17..18, + id: "x", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 22..26, + id: "iter", + ctx: Load, + }, + ), + ifs: [], + is_async: true, + }, + ], + parenthesized: true, + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | (async) + | ^^^^^ Syntax Error: Expected an identifier, but found a keyword 'async' that cannot be used here +2 | (x async x in iter) + | + + + | +1 | (async) +2 | (x async x in iter) + | ^ Syntax Error: Expected 'for', found name + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@decorator_invalid_expression.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@decorator_invalid_expression.py.snap.new new file mode 100644 index 0000000000..e4bc0434c7 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@decorator_invalid_expression.py.snap.new @@ -0,0 +1,176 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/decorator_invalid_expression.py +--- +## AST + +``` +Module( + ModModule { + range: 0..56, + body: [ + FunctionDef( + StmtFunctionDef { + range: 0..55, + is_async: false, + decorator_list: [ + Decorator { + range: 0..3, + expression: Starred( + ExprStarred { + range: 1..3, + value: Name( + ExprName { + range: 2..3, + id: "x", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + }, + Decorator { + range: 4..9, + expression: Starred( + ExprStarred { + range: 6..8, + value: Name( + ExprName { + range: 7..8, + id: "x", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + }, + Decorator { + range: 10..17, + expression: Starred( + ExprStarred { + range: 13..15, + value: Name( + ExprName { + range: 14..15, + id: "x", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + }, + Decorator { + range: 18..26, + expression: Yield( + ExprYield { + range: 19..26, + value: Some( + Name( + ExprName { + range: 25..26, + id: "x", + ctx: Load, + }, + ), + ), + }, + ), + }, + Decorator { + range: 27..40, + expression: YieldFrom( + ExprYieldFrom { + range: 28..40, + value: Name( + ExprName { + range: 39..40, + id: "x", + ctx: Load, + }, + ), + }, + ), + }, + ], + name: Identifier { + id: "foo", + range: 45..48, + }, + type_params: None, + parameters: Parameters { + range: 48..50, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 52..55, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 52..55, + }, + ), + }, + ), + ], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | @*x + | ^^ Syntax Error: Starred expression cannot be used here +2 | @(*x) +3 | @((*x)) + | + + + | +1 | @*x +2 | @(*x) + | ^^ Syntax Error: Starred expression cannot be used here +3 | @((*x)) +4 | @yield x + | + + + | +1 | @*x +2 | @(*x) +3 | @((*x)) + | ^^ Syntax Error: Starred expression cannot be used here +4 | @yield x +5 | @yield from x + | + + + | +2 | @(*x) +3 | @((*x)) +4 | @yield x + | ^^^^^^^ Syntax Error: Yield expression cannot be used here +5 | @yield from x +6 | def foo(): ... + | + + + | +3 | @((*x)) +4 | @yield x +5 | @yield from x + | ^^^^^^^^^^^^ Syntax Error: Yield expression cannot be used here +6 | def foo(): ... + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@decorator_missing_expression.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@decorator_missing_expression.py.snap.new new file mode 100644 index 0000000000..bf4b099b11 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@decorator_missing_expression.py.snap.new @@ -0,0 +1,190 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/decorator_missing_expression.py +--- +## AST + +``` +Module( + ModModule { + range: 0..51, + body: [ + AnnAssign( + StmtAnnAssign { + range: 5..15, + target: Call( + ExprCall { + range: 5..10, + func: Name( + ExprName { + range: 5..8, + id: "foo", + ctx: Load, + }, + ), + arguments: Arguments { + range: 8..10, + args: [], + keywords: [], + }, + }, + ), + annotation: EllipsisLiteral( + ExprEllipsisLiteral { + range: 12..15, + }, + ), + value: None, + simple: false, + }, + ), + FunctionDef( + StmtFunctionDef { + range: 16..32, + is_async: false, + decorator_list: [ + Decorator { + range: 16..17, + expression: Name( + ExprName { + range: 17..17, + id: "", + ctx: Invalid, + }, + ), + }, + ], + name: Identifier { + id: "foo", + range: 22..25, + }, + type_params: None, + parameters: Parameters { + range: 25..27, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 29..32, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 29..32, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 33..50, + is_async: false, + decorator_list: [ + Decorator { + range: 33..35, + expression: BinOp( + ExprBinOp { + range: 34..35, + left: Name( + ExprName { + range: 34..34, + id: "", + ctx: Invalid, + }, + ), + op: MatMult, + right: Name( + ExprName { + range: 35..35, + id: "", + ctx: Invalid, + }, + ), + }, + ), + }, + ], + name: Identifier { + id: "foo", + range: 40..43, + }, + type_params: None, + parameters: Parameters { + range: 43..45, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 47..50, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 47..50, + }, + ), + }, + ), + ], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | @def foo(): ... + | ^^^ Syntax Error: Expected an identifier, but found a keyword 'def' that cannot be used here +2 | @ +3 | def foo(): ... + | + + + | +1 | @def foo(): ... + | ^^^ Syntax Error: Expected newline, found name +2 | @ +3 | def foo(): ... + | + + + | +1 | @def foo(): ... +2 | @ + | ^ Syntax Error: Expected an expression +3 | def foo(): ... +4 | @@ +5 | def foo(): ... + | + + + | +2 | @ +3 | def foo(): ... +4 | @@ + | ^ Syntax Error: Expected an expression +5 | def foo(): ... + | + + + | +2 | @ +3 | def foo(): ... +4 | @@ + | ^ Syntax Error: Expected an expression +5 | def foo(): ... + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@decorator_missing_newline.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@decorator_missing_newline.py.snap.new new file mode 100644 index 0000000000..a72e950cb6 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@decorator_missing_newline.py.snap.new @@ -0,0 +1,163 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/decorator_missing_newline.py +--- +## AST + +``` +Module( + ModModule { + range: 0..60, + body: [ + FunctionDef( + StmtFunctionDef { + range: 0..17, + is_async: false, + decorator_list: [ + Decorator { + range: 0..2, + expression: Name( + ExprName { + range: 1..2, + id: "x", + ctx: Load, + }, + ), + }, + ], + name: Identifier { + id: "foo", + range: 7..10, + }, + type_params: None, + parameters: Parameters { + range: 10..12, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 14..17, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 14..17, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 18..41, + is_async: true, + decorator_list: [ + Decorator { + range: 18..20, + expression: Name( + ExprName { + range: 19..20, + id: "x", + ctx: Load, + }, + ), + }, + ], + name: Identifier { + id: "foo", + range: 31..34, + }, + type_params: None, + parameters: Parameters { + range: 34..36, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 38..41, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 38..41, + }, + ), + }, + ), + ], + }, + ), + ClassDef( + StmtClassDef { + range: 42..59, + decorator_list: [ + Decorator { + range: 42..44, + expression: Name( + ExprName { + range: 43..44, + id: "x", + ctx: Load, + }, + ), + }, + ], + name: Identifier { + id: "Foo", + range: 51..54, + }, + type_params: None, + arguments: None, + body: [ + Expr( + StmtExpr { + range: 56..59, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 56..59, + }, + ), + }, + ), + ], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | @x def foo(): ... + | ^^^ Syntax Error: Expected newline, found 'def' +2 | @x async def foo(): ... +3 | @x class Foo: ... + | + + + | +1 | @x def foo(): ... +2 | @x async def foo(): ... + | ^^^^^ Syntax Error: Expected newline, found 'async' +3 | @x class Foo: ... + | + + + | +1 | @x def foo(): ... +2 | @x async def foo(): ... +3 | @x class Foo: ... + | ^^^^^ Syntax Error: Expected newline, found 'class' + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@decorator_unexpected_token.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@decorator_unexpected_token.py.snap.new new file mode 100644 index 0000000000..1d329e2332 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@decorator_unexpected_token.py.snap.new @@ -0,0 +1,86 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/decorator_unexpected_token.py +--- +## AST + +``` +Module( + ModModule { + range: 0..34, + body: [ + With( + StmtWith { + range: 5..22, + is_async: true, + items: [ + WithItem { + range: 16..17, + context_expr: Name( + ExprName { + range: 16..17, + id: "x", + ctx: Load, + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 19..22, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 19..22, + }, + ), + }, + ), + ], + }, + ), + Assign( + StmtAssign { + range: 28..33, + targets: [ + Name( + ExprName { + range: 28..29, + id: "x", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 32..33, + value: Int( + 1, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | @foo +2 | async with x: ... + | ^^^^^ Syntax Error: Expected class, function definition or async function definition after decorator +3 | @foo +4 | x = 1 + | + + + | +2 | async with x: ... +3 | @foo +4 | x = 1 + | ^ Syntax Error: Expected class, function definition or async function definition after decorator + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@del_incomplete_target.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@del_incomplete_target.py.snap.new new file mode 100644 index 0000000000..05a6d6f43a --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@del_incomplete_target.py.snap.new @@ -0,0 +1,125 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/del_incomplete_target.py +--- +## AST + +``` +Module( + ModModule { + range: 0..24, + body: [ + Delete( + StmtDelete { + range: 0..9, + targets: [ + Name( + ExprName { + range: 4..5, + id: "x", + ctx: Del, + }, + ), + Attribute( + ExprAttribute { + range: 7..9, + value: Name( + ExprName { + range: 7..8, + id: "y", + ctx: Load, + }, + ), + attr: Identifier { + id: "", + range: 9..9, + }, + ctx: Del, + }, + ), + ], + }, + ), + Expr( + StmtExpr { + range: 10..11, + value: Name( + ExprName { + range: 10..11, + id: "z", + ctx: Load, + }, + ), + }, + ), + Delete( + StmtDelete { + range: 12..24, + targets: [ + Name( + ExprName { + range: 16..17, + id: "x", + ctx: Del, + }, + ), + Subscript( + ExprSubscript { + range: 19..23, + value: Name( + ExprName { + range: 19..20, + id: "y", + ctx: Load, + }, + ), + slice: Slice( + ExprSlice { + range: 22..23, + lower: Some( + Name( + ExprName { + range: 22..23, + id: "z", + ctx: Load, + }, + ), + ), + upper: Some( + Name( + ExprName { + range: 23..23, + id: "", + ctx: Invalid, + }, + ), + ), + step: None, + }, + ), + ctx: Del, + }, + ), + ], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | del x, y. + | ^ Syntax Error: Expected an identifier +2 | z +3 | del x, y[ +4 | z + | + + + | +3 | del x, y[ +4 | z + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@dotted_name_multiple_dots.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@dotted_name_multiple_dots.py.snap.new new file mode 100644 index 0000000000..e5b2d546fe --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@dotted_name_multiple_dots.py.snap.new @@ -0,0 +1,89 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/dotted_name_multiple_dots.py +--- +## AST + +``` +Module( + ModModule { + range: 0..25, + body: [ + Import( + StmtImport { + range: 0..11, + names: [ + Alias { + range: 7..11, + name: Identifier { + id: "a..b", + range: 7..11, + }, + asname: None, + }, + ], + }, + ), + Import( + StmtImport { + range: 12..20, + names: [ + Alias { + range: 19..20, + name: Identifier { + id: "a", + range: 19..20, + }, + asname: None, + }, + ], + }, + ), + Expr( + StmtExpr { + range: 20..23, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 20..23, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 23..24, + value: Name( + ExprName { + range: 23..24, + id: "b", + ctx: Load, + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | import a..b + | ^ Syntax Error: Expected an identifier +2 | import a...b + | + + + | +1 | import a..b +2 | import a...b + | ^^^ Syntax Error: Simple statements must be separated by newlines or semicolons + | + + + | +1 | import a..b +2 | import a...b + | ^ Syntax Error: Simple statements must be separated by newlines or semicolons + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@except_stmt_invalid_expression.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@except_stmt_invalid_expression.py.snap.new new file mode 100644 index 0000000000..fe6747f223 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@except_stmt_invalid_expression.py.snap.new @@ -0,0 +1,126 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/except_stmt_invalid_expression.py +--- +## AST + +``` +Module( + ModModule { + range: 0..74, + body: [ + Try( + StmtTry { + range: 0..38, + body: [ + Pass( + StmtPass { + range: 9..13, + }, + ), + ], + handlers: [ + ExceptHandler( + ExceptHandlerExceptHandler { + range: 14..38, + type_: Some( + Yield( + ExprYield { + range: 21..28, + value: Some( + Name( + ExprName { + range: 27..28, + id: "x", + ctx: Load, + }, + ), + ), + }, + ), + ), + name: None, + body: [ + Pass( + StmtPass { + range: 34..38, + }, + ), + ], + }, + ), + ], + orelse: [], + finalbody: [], + is_star: false, + }, + ), + Try( + StmtTry { + range: 39..73, + body: [ + Pass( + StmtPass { + range: 48..52, + }, + ), + ], + handlers: [ + ExceptHandler( + ExceptHandlerExceptHandler { + range: 53..73, + type_: Some( + Starred( + ExprStarred { + range: 61..63, + value: Name( + ExprName { + range: 62..63, + id: "x", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ), + name: None, + body: [ + Pass( + StmtPass { + range: 69..73, + }, + ), + ], + }, + ), + ], + orelse: [], + finalbody: [], + is_star: true, + }, + ), + ], + }, +) +``` +## Errors + + | +1 | try: +2 | pass +3 | except yield x: + | ^^^^^^^ Syntax Error: Yield expression cannot be used here +4 | pass +5 | try: + | + + + | +5 | try: +6 | pass +7 | except* *x: + | ^^ Syntax Error: Starred expression cannot be used here +8 | pass + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@except_stmt_missing_as_name.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@except_stmt_missing_as_name.py.snap.new new file mode 100644 index 0000000000..7273e97409 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@except_stmt_missing_as_name.py.snap.new @@ -0,0 +1,96 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/except_stmt_missing_as_name.py +--- +## AST + +``` +Module( + ModModule { + range: 0..73, + body: [ + Try( + StmtTry { + range: 0..72, + body: [ + Pass( + StmtPass { + range: 9..13, + }, + ), + ], + handlers: [ + ExceptHandler( + ExceptHandlerExceptHandler { + range: 14..43, + type_: Some( + Name( + ExprName { + range: 21..30, + id: "Exception", + ctx: Load, + }, + ), + ), + name: None, + body: [ + Pass( + StmtPass { + range: 39..43, + }, + ), + ], + }, + ), + ExceptHandler( + ExceptHandlerExceptHandler { + range: 44..72, + type_: Some( + Name( + ExprName { + range: 51..60, + id: "Exception", + ctx: Load, + }, + ), + ), + name: None, + body: [ + Pass( + StmtPass { + range: 68..72, + }, + ), + ], + }, + ), + ], + orelse: [], + finalbody: [], + is_star: false, + }, + ), + ], + }, +) +``` +## Errors + + | +1 | try: +2 | pass +3 | except Exception as: + | ^ Syntax Error: Expected name after `as` +4 | pass +5 | except Exception as + | + + + | +3 | except Exception as: +4 | pass +5 | except Exception as + | ^ Syntax Error: Expected name after `as` +6 | pass + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@except_stmt_missing_exception.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@except_stmt_missing_exception.py.snap.new new file mode 100644 index 0000000000..9a7a1289fb --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@except_stmt_missing_exception.py.snap.new @@ -0,0 +1,156 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/except_stmt_missing_exception.py +--- +## AST + +``` +Module( + ModModule { + range: 0..166, + body: [ + Try( + StmtTry { + range: 0..37, + body: [ + Pass( + StmtPass { + range: 9..13, + }, + ), + ], + handlers: [ + ExceptHandler( + ExceptHandlerExceptHandler { + range: 14..37, + type_: None, + name: Some( + Identifier { + id: "exc", + range: 24..27, + }, + ), + body: [ + Pass( + StmtPass { + range: 33..37, + }, + ), + ], + }, + ), + ], + orelse: [], + finalbody: [], + is_star: false, + }, + ), + Try( + StmtTry { + range: 92..165, + body: [ + Pass( + StmtPass { + range: 101..105, + }, + ), + ], + handlers: [ + ExceptHandler( + ExceptHandlerExceptHandler { + range: 106..123, + type_: None, + name: None, + body: [ + Pass( + StmtPass { + range: 119..123, + }, + ), + ], + }, + ), + ExceptHandler( + ExceptHandlerExceptHandler { + range: 124..140, + type_: None, + name: None, + body: [ + Pass( + StmtPass { + range: 136..140, + }, + ), + ], + }, + ), + ExceptHandler( + ExceptHandlerExceptHandler { + range: 141..165, + type_: None, + name: Some( + Identifier { + id: "exc", + range: 152..155, + }, + ), + body: [ + Pass( + StmtPass { + range: 161..165, + }, + ), + ], + }, + ), + ], + orelse: [], + finalbody: [], + is_star: true, + }, + ), + ], + }, +) +``` +## Errors + + | +1 | try: +2 | pass +3 | except as exc: + | ^^ Syntax Error: Expected one or more exception types +4 | pass +5 | # If a '*' is present then exception type is required + | + + + | + 6 | try: + 7 | pass + 8 | except*: + | ^ Syntax Error: Expected one or more exception types + 9 | pass +10 | except* + | + + + | + 8 | except*: + 9 | pass +10 | except* + | ^ Syntax Error: Expected one or more exception types +11 | pass +12 | except* as exc: +13 | pass + | + + + | +10 | except* +11 | pass +12 | except* as exc: + | ^^ Syntax Error: Expected one or more exception types +13 | pass + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@except_stmt_unparenthesized_tuple.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@except_stmt_unparenthesized_tuple.py.snap.new new file mode 100644 index 0000000000..d202c552ba --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@except_stmt_unparenthesized_tuple.py.snap.new @@ -0,0 +1,251 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/except_stmt_unparenthesized_tuple.py +--- +## AST + +``` +Module( + ModModule { + range: 0..131, + body: [ + Try( + StmtTry { + range: 0..64, + body: [ + Pass( + StmtPass { + range: 9..13, + }, + ), + ], + handlers: [ + ExceptHandler( + ExceptHandlerExceptHandler { + range: 14..35, + type_: Some( + Tuple( + ExprTuple { + range: 21..25, + elts: [ + Name( + ExprName { + range: 21..22, + id: "x", + ctx: Load, + }, + ), + Name( + ExprName { + range: 24..25, + id: "y", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + ), + name: None, + body: [ + Pass( + StmtPass { + range: 31..35, + }, + ), + ], + }, + ), + ExceptHandler( + ExceptHandlerExceptHandler { + range: 36..64, + type_: Some( + Tuple( + ExprTuple { + range: 43..47, + elts: [ + Name( + ExprName { + range: 43..44, + id: "x", + ctx: Load, + }, + ), + Name( + ExprName { + range: 46..47, + id: "y", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + ), + name: Some( + Identifier { + id: "exc", + range: 51..54, + }, + ), + body: [ + Pass( + StmtPass { + range: 60..64, + }, + ), + ], + }, + ), + ], + orelse: [], + finalbody: [], + is_star: false, + }, + ), + Try( + StmtTry { + range: 65..130, + body: [ + Pass( + StmtPass { + range: 74..78, + }, + ), + ], + handlers: [ + ExceptHandler( + ExceptHandlerExceptHandler { + range: 79..101, + type_: Some( + Tuple( + ExprTuple { + range: 87..91, + elts: [ + Name( + ExprName { + range: 87..88, + id: "x", + ctx: Load, + }, + ), + Name( + ExprName { + range: 90..91, + id: "y", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + ), + name: None, + body: [ + Pass( + StmtPass { + range: 97..101, + }, + ), + ], + }, + ), + ExceptHandler( + ExceptHandlerExceptHandler { + range: 102..130, + type_: Some( + Tuple( + ExprTuple { + range: 110..114, + elts: [ + Name( + ExprName { + range: 110..111, + id: "x", + ctx: Load, + }, + ), + Name( + ExprName { + range: 113..114, + id: "y", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + ), + name: Some( + Identifier { + id: "eg", + range: 118..120, + }, + ), + body: [ + Pass( + StmtPass { + range: 126..130, + }, + ), + ], + }, + ), + ], + orelse: [], + finalbody: [], + is_star: true, + }, + ), + ], + }, +) +``` +## Errors + + | +1 | try: +2 | pass +3 | except x, y: + | ^^^^ Syntax Error: Multiple exception types must be parenthesized +4 | pass +5 | except x, y as exc: + | + + + | +3 | except x, y: +4 | pass +5 | except x, y as exc: + | ^^^^ Syntax Error: Multiple exception types must be parenthesized +6 | pass +7 | try: + | + + + | + 7 | try: + 8 | pass + 9 | except* x, y: + | ^^^^ Syntax Error: Multiple exception types must be parenthesized +10 | pass +11 | except* x, y as eg: + | + + + | + 9 | except* x, y: +10 | pass +11 | except* x, y as eg: + | ^^^^ Syntax Error: Multiple exception types must be parenthesized +12 | pass + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__arguments__double_starred.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__arguments__double_starred.py.snap.new new file mode 100644 index 0000000000..e2926cf14d --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__arguments__double_starred.py.snap.new @@ -0,0 +1,222 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/arguments/double_starred.py +--- +## AST + +``` +Module( + ModModule { + range: 0..55, + body: [ + Expr( + StmtExpr { + range: 0..15, + value: Call( + ExprCall { + range: 0..15, + func: Name( + ExprName { + range: 0..4, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 4..15, + args: [], + keywords: [ + Keyword { + range: 5..14, + arg: None, + value: Yield( + ExprYield { + range: 7..14, + value: Some( + Name( + ExprName { + range: 13..14, + id: "x", + ctx: Load, + }, + ), + ), + }, + ), + }, + ], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 16..27, + value: Call( + ExprCall { + range: 16..27, + func: Name( + ExprName { + range: 16..20, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 20..27, + args: [], + keywords: [ + Keyword { + range: 21..26, + arg: None, + value: Starred( + ExprStarred { + range: 24..26, + value: Name( + ExprName { + range: 25..26, + id: "x", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + }, + ], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 28..38, + value: Call( + ExprCall { + range: 28..38, + func: Name( + ExprName { + range: 28..32, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 32..38, + args: [], + keywords: [ + Keyword { + range: 33..37, + arg: None, + value: Starred( + ExprStarred { + range: 35..37, + value: Name( + ExprName { + range: 36..37, + id: "x", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + }, + ], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 40..54, + value: Call( + ExprCall { + range: 40..54, + func: Name( + ExprName { + range: 40..44, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 44..54, + args: [ + NumberLiteral( + ExprNumberLiteral { + range: 52..53, + value: Int( + 1, + ), + }, + ), + ], + keywords: [ + Keyword { + range: 45..48, + arg: None, + value: Name( + ExprName { + range: 47..48, + id: "x", + ctx: Load, + }, + ), + }, + ], + }, + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | call(**yield x) + | ^^^^^^^ Syntax Error: Yield expression cannot be used here +2 | call(** *x) +3 | call(***x) + | + + + | +1 | call(**yield x) +2 | call(** *x) + | ^^ Syntax Error: Starred expression cannot be used here +3 | call(***x) + | + + + | +1 | call(**yield x) +2 | call(** *x) +3 | call(***x) + | ^^ Syntax Error: Starred expression cannot be used here +4 | +5 | call(**x := 1) + | + + + | +3 | call(***x) +4 | +5 | call(**x := 1) + | ^^ Syntax Error: Expected ',', found ':=' + | + + + | +3 | call(***x) +4 | +5 | call(**x := 1) + | ^ Syntax Error: Positional argument cannot follow keyword argument unpacking + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__arguments__duplicate_keyword_arguments.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__arguments__duplicate_keyword_arguments.py.snap.new new file mode 100644 index 0000000000..724540963a --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__arguments__duplicate_keyword_arguments.py.snap.new @@ -0,0 +1,136 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/arguments/duplicate_keyword_arguments.py +--- +## AST + +``` +Module( + ModModule { + range: 0..28, + body: [ + Expr( + StmtExpr { + range: 0..28, + value: Call( + ExprCall { + range: 0..28, + func: Name( + ExprName { + range: 0..3, + id: "foo", + ctx: Load, + }, + ), + arguments: Arguments { + range: 3..28, + args: [], + keywords: [ + Keyword { + range: 4..7, + arg: Some( + Identifier { + id: "a", + range: 4..5, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 6..7, + value: Int( + 1, + ), + }, + ), + }, + Keyword { + range: 9..12, + arg: Some( + Identifier { + id: "b", + range: 9..10, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 11..12, + value: Int( + 2, + ), + }, + ), + }, + Keyword { + range: 14..17, + arg: Some( + Identifier { + id: "c", + range: 14..15, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 16..17, + value: Int( + 3, + ), + }, + ), + }, + Keyword { + range: 19..22, + arg: Some( + Identifier { + id: "b", + range: 19..20, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 21..22, + value: Int( + 4, + ), + }, + ), + }, + Keyword { + range: 24..27, + arg: Some( + Identifier { + id: "a", + range: 24..25, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 26..27, + value: Int( + 5, + ), + }, + ), + }, + ], + }, + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | foo(a=1, b=2, c=3, b=4, a=5) + | ^^^ Syntax Error: Duplicate keyword argument "b" + | + + + | +1 | foo(a=1, b=2, c=3, b=4, a=5) + | ^^^ Syntax Error: Duplicate keyword argument "a" + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__arguments__invalid_expression.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__arguments__invalid_expression.py.snap.new new file mode 100644 index 0000000000..ce4db56e33 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__arguments__invalid_expression.py.snap.new @@ -0,0 +1,200 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/arguments/invalid_expression.py +--- +## AST + +``` +Module( + ModModule { + range: 0..67, + body: [ + Expr( + StmtExpr { + range: 0..15, + value: Call( + ExprCall { + range: 0..15, + func: Name( + ExprName { + range: 0..4, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 4..15, + args: [], + keywords: [ + Keyword { + range: 5..14, + arg: Some( + Identifier { + id: "", + range: 5..10, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 13..14, + value: Int( + 1, + ), + }, + ), + }, + ], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 16..32, + value: Call( + ExprCall { + range: 16..32, + func: Name( + ExprName { + range: 16..20, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 20..32, + args: [], + keywords: [ + Keyword { + range: 21..31, + arg: Some( + Identifier { + id: "", + range: 21..27, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 30..31, + value: Int( + 1, + ), + }, + ), + }, + ], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 34..47, + value: Call( + ExprCall { + range: 34..47, + func: Name( + ExprName { + range: 34..38, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 38..47, + args: [ + Yield( + ExprYield { + range: 39..46, + value: Some( + Name( + ExprName { + range: 45..46, + id: "x", + ctx: Load, + }, + ), + ), + }, + ), + ], + keywords: [], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 48..66, + value: Call( + ExprCall { + range: 48..66, + func: Name( + ExprName { + range: 48..52, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 52..66, + args: [ + YieldFrom( + ExprYieldFrom { + range: 53..65, + value: Name( + ExprName { + range: 64..65, + id: "x", + ctx: Load, + }, + ), + }, + ), + ], + keywords: [], + }, + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | call(x + y = 1) + | ^^^^^ Syntax Error: Expected a parameter name +2 | call(x := 1 = 1) + | + + + | +1 | call(x + y = 1) +2 | call(x := 1 = 1) + | ^^^^^^ Syntax Error: Expected a parameter name +3 | +4 | call(yield x) + | + + + | +2 | call(x := 1 = 1) +3 | +4 | call(yield x) + | ^^^^^^^ Syntax Error: Yield expression cannot be used here +5 | call(yield from x) + | + + + | +4 | call(yield x) +5 | call(yield from x) + | ^^^^^^^^^^^^ Syntax Error: Yield expression cannot be used here + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__arguments__invalid_keyword_expression.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__arguments__invalid_keyword_expression.py.snap.new new file mode 100644 index 0000000000..018f760327 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__arguments__invalid_keyword_expression.py.snap.new @@ -0,0 +1,230 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/arguments/invalid_keyword_expression.py +--- +## AST + +``` +Module( + ModModule { + range: 0..69, + body: [ + Expr( + StmtExpr { + range: 0..17, + value: Call( + ExprCall { + range: 0..17, + func: Name( + ExprName { + range: 0..4, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 4..17, + args: [], + keywords: [ + Keyword { + range: 5..16, + arg: Some( + Identifier { + id: "x", + range: 5..6, + }, + ), + value: Yield( + ExprYield { + range: 9..16, + value: Some( + Name( + ExprName { + range: 15..16, + id: "y", + ctx: Load, + }, + ), + ), + }, + ), + }, + ], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 18..40, + value: Call( + ExprCall { + range: 18..40, + func: Name( + ExprName { + range: 18..22, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 22..40, + args: [], + keywords: [ + Keyword { + range: 23..39, + arg: Some( + Identifier { + id: "x", + range: 23..24, + }, + ), + value: YieldFrom( + ExprYieldFrom { + range: 27..39, + value: Name( + ExprName { + range: 38..39, + id: "y", + ctx: Load, + }, + ), + }, + ), + }, + ], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 41..53, + value: Call( + ExprCall { + range: 41..53, + func: Name( + ExprName { + range: 41..45, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 45..53, + args: [], + keywords: [ + Keyword { + range: 46..52, + arg: Some( + Identifier { + id: "x", + range: 46..47, + }, + ), + value: Starred( + ExprStarred { + range: 50..52, + value: Name( + ExprName { + range: 51..52, + id: "y", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + }, + ], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 54..68, + value: Call( + ExprCall { + range: 54..68, + func: Name( + ExprName { + range: 54..58, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 58..68, + args: [], + keywords: [ + Keyword { + range: 59..67, + arg: Some( + Identifier { + id: "x", + range: 59..60, + }, + ), + value: Starred( + ExprStarred { + range: 64..66, + value: Name( + ExprName { + range: 65..66, + id: "y", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + }, + ], + }, + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | call(x = yield y) + | ^^^^^^^ Syntax Error: Yield expression cannot be used here +2 | call(x = yield from y) +3 | call(x = *y) + | + + + | +1 | call(x = yield y) +2 | call(x = yield from y) + | ^^^^^^^^^^^^ Syntax Error: Yield expression cannot be used here +3 | call(x = *y) +4 | call(x = (*y)) + | + + + | +1 | call(x = yield y) +2 | call(x = yield from y) +3 | call(x = *y) + | ^^ Syntax Error: Starred expression cannot be used here +4 | call(x = (*y)) + | + + + | +2 | call(x = yield from y) +3 | call(x = *y) +4 | call(x = (*y)) + | ^^ Syntax Error: Starred expression cannot be used here + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__arguments__invalid_order.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__arguments__invalid_order.py.snap.new new file mode 100644 index 0000000000..2c32eb2136 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__arguments__invalid_order.py.snap.new @@ -0,0 +1,305 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/arguments/invalid_order.py +--- +## AST + +``` +Module( + ModModule { + range: 0..100, + body: [ + Expr( + StmtExpr { + range: 0..17, + value: Call( + ExprCall { + range: 0..17, + func: Name( + ExprName { + range: 0..4, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 4..17, + args: [ + Name( + ExprName { + range: 15..16, + id: "x", + ctx: Load, + }, + ), + ], + keywords: [ + Keyword { + range: 5..13, + arg: None, + value: Name( + ExprName { + range: 7..13, + id: "kwargs", + ctx: Load, + }, + ), + }, + ], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 18..30, + value: Call( + ExprCall { + range: 18..30, + func: Name( + ExprName { + range: 18..22, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 22..30, + args: [ + Name( + ExprName { + range: 28..29, + id: "y", + ctx: Load, + }, + ), + ], + keywords: [ + Keyword { + range: 23..26, + arg: Some( + Identifier { + id: "x", + range: 23..24, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 25..26, + value: Int( + 1, + ), + }, + ), + }, + ], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 31..53, + value: Call( + ExprCall { + range: 31..53, + func: Name( + ExprName { + range: 31..35, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 35..53, + args: [ + Name( + ExprName { + range: 51..52, + id: "y", + ctx: Load, + }, + ), + ], + keywords: [ + Keyword { + range: 36..39, + arg: Some( + Identifier { + id: "x", + range: 36..37, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 38..39, + value: Int( + 1, + ), + }, + ), + }, + Keyword { + range: 41..49, + arg: None, + value: Name( + ExprName { + range: 43..49, + id: "kwargs", + ctx: Load, + }, + ), + }, + ], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 54..75, + value: Call( + ExprCall { + range: 54..75, + func: Name( + ExprName { + range: 54..58, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 58..75, + args: [ + Starred( + ExprStarred { + range: 69..74, + value: Name( + ExprName { + range: 70..74, + id: "args", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ], + keywords: [ + Keyword { + range: 59..67, + arg: None, + value: Name( + ExprName { + range: 61..67, + id: "kwargs", + ctx: Load, + }, + ), + }, + ], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 76..99, + value: Call( + ExprCall { + range: 76..99, + func: Name( + ExprName { + range: 76..80, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 80..99, + args: [ + Starred( + ExprStarred { + range: 92..97, + value: Name( + ExprName { + range: 93..97, + id: "args", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ], + keywords: [ + Keyword { + range: 81..89, + arg: None, + value: Name( + ExprName { + range: 83..89, + id: "kwargs", + ctx: Load, + }, + ), + }, + ], + }, + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | call(**kwargs, x) + | ^ Syntax Error: Positional argument cannot follow keyword argument unpacking +2 | call(x=1, y) +3 | call(x=1, **kwargs, y) + | + + + | +1 | call(**kwargs, x) +2 | call(x=1, y) + | ^ Syntax Error: Positional argument cannot follow keyword argument +3 | call(x=1, **kwargs, y) +4 | call(**kwargs, *args) + | + + + | +1 | call(**kwargs, x) +2 | call(x=1, y) +3 | call(x=1, **kwargs, y) + | ^ Syntax Error: Positional argument cannot follow keyword argument unpacking +4 | call(**kwargs, *args) +5 | call(**kwargs, (*args)) + | + + + | +2 | call(x=1, y) +3 | call(x=1, **kwargs, y) +4 | call(**kwargs, *args) + | ^^^^^ Syntax Error: Iterable argument unpacking cannot follow keyword argument unpacking +5 | call(**kwargs, (*args)) + | + + + | +3 | call(x=1, **kwargs, y) +4 | call(**kwargs, *args) +5 | call(**kwargs, (*args)) + | ^^^^^ Syntax Error: Starred expression cannot be used here + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__arguments__missing_argument.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__arguments__missing_argument.py.snap.new new file mode 100644 index 0000000000..1889fb72d4 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__arguments__missing_argument.py.snap.new @@ -0,0 +1,59 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/arguments/missing_argument.py +--- +## AST + +``` +Module( + ModModule { + range: 0..10, + body: [ + Expr( + StmtExpr { + range: 0..10, + value: Call( + ExprCall { + range: 0..10, + func: Name( + ExprName { + range: 0..4, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 4..10, + args: [ + Name( + ExprName { + range: 5..6, + id: "x", + ctx: Load, + }, + ), + Name( + ExprName { + range: 8..9, + id: "y", + ctx: Load, + }, + ), + ], + keywords: [], + }, + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | call(x,,y) + | ^ Syntax Error: Expected an expression or a ')' + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__arguments__missing_comma.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__arguments__missing_comma.py.snap.new new file mode 100644 index 0000000000..6578ebd69d --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__arguments__missing_comma.py.snap.new @@ -0,0 +1,59 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/arguments/missing_comma.py +--- +## AST + +``` +Module( + ModModule { + range: 0..9, + body: [ + Expr( + StmtExpr { + range: 0..9, + value: Call( + ExprCall { + range: 0..9, + func: Name( + ExprName { + range: 0..4, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 4..9, + args: [ + Name( + ExprName { + range: 5..6, + id: "x", + ctx: Load, + }, + ), + Name( + ExprName { + range: 7..8, + id: "y", + ctx: Load, + }, + ), + ], + keywords: [], + }, + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | call(x y) + | ^ Syntax Error: Expected ',', found name + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__arguments__missing_expression.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__arguments__missing_expression.py.snap.new new file mode 100644 index 0000000000..a0f2d5d727 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__arguments__missing_expression.py.snap.new @@ -0,0 +1,167 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/arguments/missing_expression.py +--- +## AST + +``` +Module( + ModModule { + range: 0..38, + body: [ + Expr( + StmtExpr { + range: 0..10, + value: Call( + ExprCall { + range: 0..10, + func: Name( + ExprName { + range: 0..4, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 4..10, + args: [ + NumberLiteral( + ExprNumberLiteral { + range: 8..9, + value: Int( + 1, + ), + }, + ), + ], + keywords: [], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 11..21, + value: Call( + ExprCall { + range: 11..21, + func: Name( + ExprName { + range: 11..15, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 15..21, + args: [], + keywords: [ + Keyword { + range: 16..19, + arg: Some( + Identifier { + id: "x", + range: 16..17, + }, + ), + value: Name( + ExprName { + range: 19..19, + id: "", + ctx: Invalid, + }, + ), + }, + ], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 22..32, + value: Call( + ExprCall { + range: 22..32, + func: Name( + ExprName { + range: 22..26, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 26..32, + args: [ + Starred( + ExprStarred { + range: 27..28, + value: Name( + ExprName { + range: 28..28, + id: "", + ctx: Invalid, + }, + ), + ctx: Load, + }, + ), + Name( + ExprName { + range: 30..31, + id: "y", + ctx: Load, + }, + ), + ], + keywords: [], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 34..37, + value: Name( + ExprName { + range: 34..37, + id: "foo", + ctx: Load, + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | call( = 1) + | ^ Syntax Error: Expected an expression or a ')' +2 | call(x = ) +3 | call(*, y) + | + + + | +1 | call( = 1) +2 | call(x = ) + | ^ Syntax Error: Expected an expression +3 | call(*, y) + | + + + | +1 | call( = 1) +2 | call(x = ) +3 | call(*, y) + | ^ Syntax Error: Expected an expression +4 | +5 | foo + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__arguments__starred.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__arguments__starred.py.snap.new new file mode 100644 index 0000000000..6baaa53d95 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__arguments__starred.py.snap.new @@ -0,0 +1,187 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/arguments/starred.py +--- +## AST + +``` +Module( + ModModule { + range: 0..64, + body: [ + Expr( + StmtExpr { + range: 0..28, + value: Call( + ExprCall { + range: 0..28, + func: Name( + ExprName { + range: 0..4, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 4..28, + args: [ + Generator( + ExprGenerator { + range: 5..27, + elt: Starred( + ExprStarred { + range: 5..10, + value: Name( + ExprName { + range: 6..10, + id: "data", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 11..27, + target: Name( + ExprName { + range: 15..19, + id: "data", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 23..27, + id: "iter", + ctx: Load, + }, + ), + ifs: [], + is_async: false, + }, + ], + parenthesized: false, + }, + ), + ], + keywords: [], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 29..43, + value: Call( + ExprCall { + range: 29..43, + func: Name( + ExprName { + range: 29..33, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 33..43, + args: [ + Starred( + ExprStarred { + range: 34..42, + value: Yield( + ExprYield { + range: 35..42, + value: Some( + Name( + ExprName { + range: 41..42, + id: "x", + ctx: Load, + }, + ), + ), + }, + ), + ctx: Load, + }, + ), + ], + keywords: [], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 44..63, + value: Call( + ExprCall { + range: 44..63, + func: Name( + ExprName { + range: 44..48, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 48..63, + args: [ + Starred( + ExprStarred { + range: 49..62, + value: YieldFrom( + ExprYieldFrom { + range: 50..62, + value: Name( + ExprName { + range: 61..62, + id: "x", + ctx: Load, + }, + ), + }, + ), + ctx: Load, + }, + ), + ], + keywords: [], + }, + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | call(*data for data in iter) + | ^^^^^ Syntax Error: Iterable unpacking cannot be used in a comprehension +2 | call(*yield x) +3 | call(*yield from x) + | + + + | +1 | call(*data for data in iter) +2 | call(*yield x) + | ^^^^^^^ Syntax Error: Yield expression cannot be used here +3 | call(*yield from x) + | + + + | +1 | call(*data for data in iter) +2 | call(*yield x) +3 | call(*yield from x) + | ^^^^^^^^^^^^ Syntax Error: Yield expression cannot be used here + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__arguments__unclosed_0.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__arguments__unclosed_0.py.snap.new new file mode 100644 index 0000000000..9e82d2885c --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__arguments__unclosed_0.py.snap.new @@ -0,0 +1,75 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/arguments/unclosed_0.py +--- +## AST + +``` +Module( + ModModule { + range: 0..26, + body: [ + Expr( + StmtExpr { + range: 0..5, + value: Call( + ExprCall { + range: 0..5, + func: Name( + ExprName { + range: 0..4, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 4..5, + args: [], + keywords: [], + }, + }, + ), + }, + ), + FunctionDef( + StmtFunctionDef { + range: 7..26, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 11..14, + }, + type_params: None, + parameters: Parameters { + range: 14..16, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 22..26, + }, + ), + ], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | call( + | ^ Syntax Error: Expected ')', found newline +2 | +3 | def foo(): +4 | pass + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__arguments__unclosed_1.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__arguments__unclosed_1.py.snap.new new file mode 100644 index 0000000000..800c4be1dc --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__arguments__unclosed_1.py.snap.new @@ -0,0 +1,83 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/arguments/unclosed_1.py +--- +## AST + +``` +Module( + ModModule { + range: 0..27, + body: [ + Expr( + StmtExpr { + range: 0..6, + value: Call( + ExprCall { + range: 0..6, + func: Name( + ExprName { + range: 0..4, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 4..6, + args: [ + Name( + ExprName { + range: 5..6, + id: "x", + ctx: Load, + }, + ), + ], + keywords: [], + }, + }, + ), + }, + ), + FunctionDef( + StmtFunctionDef { + range: 8..27, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 12..15, + }, + type_params: None, + parameters: Parameters { + range: 15..17, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 23..27, + }, + ), + ], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | call(x + | ^ Syntax Error: Expected ')', found newline +2 | +3 | def foo(): +4 | pass + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__arguments__unclosed_2.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__arguments__unclosed_2.py.snap.new new file mode 100644 index 0000000000..1e496038dd --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__arguments__unclosed_2.py.snap.new @@ -0,0 +1,83 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/arguments/unclosed_2.py +--- +## AST + +``` +Module( + ModModule { + range: 0..28, + body: [ + Expr( + StmtExpr { + range: 0..7, + value: Call( + ExprCall { + range: 0..7, + func: Name( + ExprName { + range: 0..4, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 4..7, + args: [ + Name( + ExprName { + range: 5..6, + id: "x", + ctx: Load, + }, + ), + ], + keywords: [], + }, + }, + ), + }, + ), + FunctionDef( + StmtFunctionDef { + range: 9..28, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 13..16, + }, + type_params: None, + parameters: Parameters { + range: 16..18, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 24..28, + }, + ), + ], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | call(x, + | ^ Syntax Error: Expected ')', found newline +2 | +3 | def foo(): +4 | pass + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__attribute__invalid_member.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__attribute__invalid_member.py.snap.new new file mode 100644 index 0000000000..01b8a595d8 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__attribute__invalid_member.py.snap.new @@ -0,0 +1,147 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/attribute/invalid_member.py +--- +## AST + +``` +Module( + ModModule { + range: 0..16, + body: [ + Expr( + StmtExpr { + range: 0..1, + value: Name( + ExprName { + range: 0..1, + id: "x", + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 1..3, + value: NumberLiteral( + ExprNumberLiteral { + range: 1..3, + value: Float( + 0.1, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 4..5, + value: Name( + ExprName { + range: 4..5, + id: "x", + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 5..7, + value: NumberLiteral( + ExprNumberLiteral { + range: 5..7, + value: Float( + 0.1, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 7..9, + value: NumberLiteral( + ExprNumberLiteral { + range: 7..9, + value: Float( + 0.0, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 10..15, + value: Subscript( + ExprSubscript { + range: 10..15, + value: Attribute( + ExprAttribute { + range: 10..12, + value: Name( + ExprName { + range: 10..11, + id: "x", + ctx: Load, + }, + ), + attr: Identifier { + id: "", + range: 12..12, + }, + ctx: Load, + }, + ), + slice: NumberLiteral( + ExprNumberLiteral { + range: 13..14, + value: Int( + 0, + ), + }, + ), + ctx: Load, + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | x.1 + | ^^ Syntax Error: Simple statements must be separated by newlines or semicolons +2 | x.1.0 +3 | x.[0] + | + + + | +1 | x.1 +2 | x.1.0 + | ^^ Syntax Error: Simple statements must be separated by newlines or semicolons +3 | x.[0] + | + + + | +1 | x.1 +2 | x.1.0 + | ^^ Syntax Error: Simple statements must be separated by newlines or semicolons +3 | x.[0] + | + + + | +1 | x.1 +2 | x.1.0 +3 | x.[0] + | ^ Syntax Error: Expected an identifier + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__attribute__multiple_dots.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__attribute__multiple_dots.py.snap.new new file mode 100644 index 0000000000..76b9792ca1 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__attribute__multiple_dots.py.snap.new @@ -0,0 +1,154 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/attribute/multiple_dots.py +--- +## AST + +``` +Module( + ModModule { + range: 0..46, + body: [ + Expr( + StmtExpr { + range: 0..10, + value: Attribute( + ExprAttribute { + range: 0..10, + value: Attribute( + ExprAttribute { + range: 0..6, + value: Name( + ExprName { + range: 0..5, + id: "extra", + ctx: Load, + }, + ), + attr: Identifier { + id: "", + range: 6..6, + }, + ctx: Load, + }, + ), + attr: Identifier { + id: "dot", + range: 7..10, + }, + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 11..19, + value: Name( + ExprName { + range: 11..19, + id: "multiple", + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 19..27, + value: Attribute( + ExprAttribute { + range: 19..27, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 19..22, + }, + ), + attr: Identifier { + id: "dots", + range: 23..27, + }, + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 28..36, + value: Name( + ExprName { + range: 28..36, + id: "multiple", + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 36..45, + value: Attribute( + ExprAttribute { + range: 36..45, + value: Attribute( + ExprAttribute { + range: 36..40, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 36..39, + }, + ), + attr: Identifier { + id: "", + range: 40..40, + }, + ctx: Load, + }, + ), + attr: Identifier { + id: "dots", + range: 41..45, + }, + ctx: Load, + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | extra..dot + | ^ Syntax Error: Expected an identifier +2 | multiple....dots +3 | multiple.....dots + | + + + | +1 | extra..dot +2 | multiple....dots + | ^^^ Syntax Error: Simple statements must be separated by newlines or semicolons +3 | multiple.....dots + | + + + | +1 | extra..dot +2 | multiple....dots +3 | multiple.....dots + | ^^^ Syntax Error: Simple statements must be separated by newlines or semicolons + | + + + | +1 | extra..dot +2 | multiple....dots +3 | multiple.....dots + | ^ Syntax Error: Expected an identifier + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__attribute__no_member.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__attribute__no_member.py.snap.new new file mode 100644 index 0000000000..a6b01d10fc --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__attribute__no_member.py.snap.new @@ -0,0 +1,89 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/attribute/no_member.py +--- +## AST + +``` +Module( + ModModule { + range: 0..141, + body: [ + Expr( + StmtExpr { + range: 87..93, + value: Attribute( + ExprAttribute { + range: 87..93, + value: Name( + ExprName { + range: 87..92, + id: "first", + ctx: Load, + }, + ), + attr: Identifier { + id: "", + range: 93..93, + }, + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 94..100, + value: Name( + ExprName { + range: 94..100, + id: "second", + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 136..141, + value: Attribute( + ExprAttribute { + range: 136..141, + value: Name( + ExprName { + range: 136..140, + id: "last", + ctx: Load, + }, + ), + attr: Identifier { + id: "", + range: 141..141, + }, + ctx: Load, + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | # The `second` is a variable on another line and not part of the attribute expression. +2 | first. + | ^ Syntax Error: Expected an identifier +3 | second +4 | +5 | # No member access after the dot. + | + + + | +5 | # No member access after the dot. +6 | last. + | Syntax Error: Expected an identifier + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__await__no_expression_0.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__await__no_expression_0.py.snap.new new file mode 100644 index 0000000000..e79611fb3c --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__await__no_expression_0.py.snap.new @@ -0,0 +1,67 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/await/no_expression_0.py +--- +## AST + +``` +Module( + ModModule { + range: 0..73, + body: [ + Expr( + StmtExpr { + range: 61..66, + value: Await( + ExprAwait { + range: 61..66, + value: Name( + ExprName { + range: 66..66, + id: "", + ctx: Invalid, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 68..73, + value: BinOp( + ExprBinOp { + range: 68..73, + left: Name( + ExprName { + range: 68..69, + id: "x", + ctx: Load, + }, + ), + op: Add, + right: Name( + ExprName { + range: 72..73, + id: "y", + ctx: Load, + }, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | # No expression after `await`, an expression on another line +2 | await + | ^ Syntax Error: Expected an expression +3 | +4 | x + y + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__await__no_expression_1.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__await__no_expression_1.py.snap.new new file mode 100644 index 0000000000..37bd077bd6 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__await__no_expression_1.py.snap.new @@ -0,0 +1,71 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/await/no_expression_1.py +--- +## AST + +``` +Module( + ModModule { + range: 0..85, + body: [ + Expr( + StmtExpr { + range: 59..64, + value: Await( + ExprAwait { + range: 59..64, + value: Name( + ExprName { + range: 64..64, + id: "", + ctx: Invalid, + }, + ), + }, + ), + }, + ), + FunctionDef( + StmtFunctionDef { + range: 66..85, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 70..73, + }, + type_params: None, + parameters: Parameters { + range: 73..75, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 81..85, + }, + ), + ], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | # No expression after `await`, a statement on another line +2 | await + | ^ Syntax Error: Expected an expression +3 | +4 | def foo(): +5 | pass + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__await__recover.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__await__recover.py.snap.new new file mode 100644 index 0000000000..3acf38365f --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__await__recover.py.snap.new @@ -0,0 +1,328 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/await/recover.py +--- +## AST + +``` +Module( + ModModule { + range: 0..284, + body: [ + Expr( + StmtExpr { + range: 117..130, + value: Await( + ExprAwait { + range: 117..130, + value: Await( + ExprAwait { + range: 123..130, + value: Name( + ExprName { + range: 129..130, + id: "x", + ctx: Load, + }, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 154..162, + value: Await( + ExprAwait { + range: 154..162, + value: Starred( + ExprStarred { + range: 160..162, + value: Name( + ExprName { + range: 161..162, + id: "x", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 163..173, + value: Await( + ExprAwait { + range: 163..173, + value: Starred( + ExprStarred { + range: 170..172, + value: Name( + ExprName { + range: 171..172, + id: "x", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 214..227, + value: Await( + ExprAwait { + range: 214..227, + value: Yield( + ExprYield { + range: 220..227, + value: Some( + Name( + ExprName { + range: 226..227, + id: "x", + ctx: Load, + }, + ), + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 228..245, + value: Await( + ExprAwait { + range: 228..245, + value: Lambda( + ExprLambda { + range: 234..245, + parameters: Some( + Parameters { + range: 241..242, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 241..242, + parameter: Parameter { + range: 241..242, + name: Identifier { + id: "x", + range: 241..242, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: Name( + ExprName { + range: 244..245, + id: "x", + ctx: Load, + }, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 246..254, + value: Await( + ExprAwait { + range: 246..254, + value: UnaryOp( + ExprUnaryOp { + range: 252..254, + op: UAdd, + operand: Name( + ExprName { + range: 253..254, + id: "x", + ctx: Load, + }, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 255..263, + value: Await( + ExprAwait { + range: 255..263, + value: UnaryOp( + ExprUnaryOp { + range: 261..263, + op: USub, + operand: Name( + ExprName { + range: 262..263, + id: "x", + ctx: Load, + }, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 264..272, + value: Await( + ExprAwait { + range: 264..272, + value: UnaryOp( + ExprUnaryOp { + range: 270..272, + op: Invert, + operand: Name( + ExprName { + range: 271..272, + id: "x", + ctx: Load, + }, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 273..284, + value: Await( + ExprAwait { + range: 273..284, + value: UnaryOp( + ExprUnaryOp { + range: 279..284, + op: Not, + operand: Name( + ExprName { + range: 283..284, + id: "x", + ctx: Load, + }, + ), + }, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +4 | # Nested await +5 | await await x + | ^^^^^^^ Syntax Error: Await expression cannot be used here +6 | +7 | # Starred expressions + | + + + | +7 | # Starred expressions +8 | await *x + | ^^ Syntax Error: Starred expression cannot be used here +9 | await (*x) + | + + + | + 7 | # Starred expressions + 8 | await *x + 9 | await (*x) + | ^^ Syntax Error: Starred expression cannot be used here +10 | +11 | # Invalid expression as per precedence + | + + + | +11 | # Invalid expression as per precedence +12 | await yield x + | ^^^^^^^ Syntax Error: Yield expression cannot be used here +13 | await lambda x: x +14 | await +x + | + + + | +11 | # Invalid expression as per precedence +12 | await yield x +13 | await lambda x: x + | ^^^^^^^^^^^ Syntax Error: Lambda expression cannot be used here +14 | await +x +15 | await -x + | + + + | +12 | await yield x +13 | await lambda x: x +14 | await +x + | ^^ Syntax Error: Unary '+' expression cannot be used here +15 | await -x +16 | await ~x + | + + + | +13 | await lambda x: x +14 | await +x +15 | await -x + | ^^ Syntax Error: Unary '-' expression cannot be used here +16 | await ~x +17 | await not x + | + + + | +14 | await +x +15 | await -x +16 | await ~x + | ^^ Syntax Error: Unary '~' expression cannot be used here +17 | await not x + | + + + | +15 | await -x +16 | await ~x +17 | await not x + | ^^^^^ Syntax Error: Boolean 'not' expression cannot be used here + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__bin_op__invalid_rhs_expression.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__bin_op__invalid_rhs_expression.py.snap.new new file mode 100644 index 0000000000..191ee32490 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__bin_op__invalid_rhs_expression.py.snap.new @@ -0,0 +1,117 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/bin_op/invalid_rhs_expression.py +--- +## AST + +``` +Module( + ModModule { + range: 0..28, + body: [ + Expr( + StmtExpr { + range: 0..15, + value: BinOp( + ExprBinOp { + range: 0..15, + left: Name( + ExprName { + range: 0..1, + id: "x", + ctx: Load, + }, + ), + op: Add, + right: Lambda( + ExprLambda { + range: 4..15, + parameters: Some( + Parameters { + range: 11..12, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 11..12, + parameter: Parameter { + range: 11..12, + name: Identifier { + id: "y", + range: 11..12, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: Name( + ExprName { + range: 14..15, + id: "y", + ctx: Load, + }, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 17..28, + value: BinOp( + ExprBinOp { + range: 17..28, + left: Name( + ExprName { + range: 17..18, + id: "x", + ctx: Load, + }, + ), + op: Sub, + right: Yield( + ExprYield { + range: 21..28, + value: Some( + Name( + ExprName { + range: 27..28, + id: "y", + ctx: Load, + }, + ), + ), + }, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | x + lambda y: y + | ^^^^^^^^^^^ Syntax Error: Lambda expression cannot be used here +2 | +3 | x - yield y + | + + + | +1 | x + lambda y: y +2 | +3 | x - yield y + | ^^^^^^^ Syntax Error: Yield expression cannot be used here + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__bin_op__missing_lhs.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__bin_op__missing_lhs.py.snap.new new file mode 100644 index 0000000000..c21b758aa7 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__bin_op__missing_lhs.py.snap.new @@ -0,0 +1,63 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/bin_op/missing_lhs.py +--- +## AST + +``` +Module( + ModModule { + range: 0..10, + body: [ + Expr( + StmtExpr { + range: 2..3, + value: Name( + ExprName { + range: 2..3, + id: "y", + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 5..10, + value: BinOp( + ExprBinOp { + range: 5..10, + left: NumberLiteral( + ExprNumberLiteral { + range: 5..6, + value: Int( + 1, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 9..10, + value: Int( + 2, + ), + }, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | / y + | ^ Syntax Error: Expected a statement +2 | +3 | 1 + 2 + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__bin_op__missing_rhs_0.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__bin_op__missing_rhs_0.py.snap.new new file mode 100644 index 0000000000..639cd5e88a --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__bin_op__missing_rhs_0.py.snap.new @@ -0,0 +1,77 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/bin_op/missing_rhs_0.py +--- +## AST + +``` +Module( + ModModule { + range: 0..10, + body: [ + Expr( + StmtExpr { + range: 0..3, + value: BinOp( + ExprBinOp { + range: 0..3, + left: NumberLiteral( + ExprNumberLiteral { + range: 0..1, + value: Int( + 0, + ), + }, + ), + op: Add, + right: Name( + ExprName { + range: 3..3, + id: "", + ctx: Invalid, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 5..10, + value: BinOp( + ExprBinOp { + range: 5..10, + left: NumberLiteral( + ExprNumberLiteral { + range: 5..6, + value: Int( + 1, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 9..10, + value: Int( + 2, + ), + }, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | 0 + + | ^ Syntax Error: Expected an expression +2 | +3 | 1 + 2 + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__bin_op__missing_rhs_1.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__bin_op__missing_rhs_1.py.snap.new new file mode 100644 index 0000000000..b0d0488be2 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__bin_op__missing_rhs_1.py.snap.new @@ -0,0 +1,105 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/bin_op/missing_rhs_1.py +--- +## AST + +``` +Module( + ModModule { + range: 0..18, + body: [ + Expr( + StmtExpr { + range: 0..11, + value: BinOp( + ExprBinOp { + range: 0..11, + left: BinOp( + ExprBinOp { + range: 0..5, + left: NumberLiteral( + ExprNumberLiteral { + range: 0..1, + value: Int( + 1, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 4..5, + value: Int( + 2, + ), + }, + ), + }, + ), + op: Sub, + right: BinOp( + ExprBinOp { + range: 8..11, + left: NumberLiteral( + ExprNumberLiteral { + range: 8..9, + value: Int( + 3, + ), + }, + ), + op: Mult, + right: Name( + ExprName { + range: 11..11, + id: "", + ctx: Invalid, + }, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 13..18, + value: BinOp( + ExprBinOp { + range: 13..18, + left: NumberLiteral( + ExprNumberLiteral { + range: 13..14, + value: Int( + 4, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 17..18, + value: Int( + 5, + ), + }, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | 1 + 2 - 3 * + | ^ Syntax Error: Expected an expression +2 | +3 | 4 + 5 + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__bin_op__multiple_ops.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__bin_op__multiple_ops.py.snap.new new file mode 100644 index 0000000000..ff531cca01 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__bin_op__multiple_ops.py.snap.new @@ -0,0 +1,150 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/bin_op/multiple_ops.py +--- +## AST + +``` +Module( + ModModule { + range: 0..19, + body: [ + Expr( + StmtExpr { + range: 0..3, + value: BinOp( + ExprBinOp { + range: 0..3, + left: Name( + ExprName { + range: 0..1, + id: "x", + ctx: Load, + }, + ), + op: Add, + right: UnaryOp( + ExprUnaryOp { + range: 2..3, + op: UAdd, + operand: Name( + ExprName { + range: 3..3, + id: "", + ctx: Invalid, + }, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 4..9, + value: BinOp( + ExprBinOp { + range: 4..9, + left: NumberLiteral( + ExprNumberLiteral { + range: 4..5, + value: Int( + 1, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 8..9, + value: Int( + 2, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 10..13, + value: BinOp( + ExprBinOp { + range: 10..13, + left: Name( + ExprName { + range: 10..11, + id: "x", + ctx: Load, + }, + ), + op: Sub, + right: UnaryOp( + ExprUnaryOp { + range: 12..13, + op: USub, + operand: Name( + ExprName { + range: 13..13, + id: "", + ctx: Invalid, + }, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 14..19, + value: BinOp( + ExprBinOp { + range: 14..19, + left: NumberLiteral( + ExprNumberLiteral { + range: 14..15, + value: Int( + 1, + ), + }, + ), + op: Sub, + right: NumberLiteral( + ExprNumberLiteral { + range: 18..19, + value: Int( + 2, + ), + }, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | x++ + | ^ Syntax Error: Expected an expression +2 | 1 + 2 +3 | x-- +4 | 1 - 2 + | + + + | +1 | x++ +2 | 1 + 2 +3 | x-- + | ^ Syntax Error: Expected an expression +4 | 1 - 2 + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__bin_op__named_expression.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__bin_op__named_expression.py.snap.new new file mode 100644 index 0000000000..9c54a5157f --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__bin_op__named_expression.py.snap.new @@ -0,0 +1,123 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/bin_op/named_expression.py +--- +## AST + +``` +Module( + ModModule { + range: 0..26, + body: [ + Expr( + StmtExpr { + range: 0..5, + value: BinOp( + ExprBinOp { + range: 0..5, + left: Name( + ExprName { + range: 0..1, + id: "x", + ctx: Load, + }, + ), + op: Sub, + right: Name( + ExprName { + range: 4..5, + id: "y", + ctx: Load, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 9..15, + value: Tuple( + ExprTuple { + range: 9..15, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 10..11, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 13..14, + value: Int( + 2, + ), + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 16..21, + value: BinOp( + ExprBinOp { + range: 16..21, + left: Name( + ExprName { + range: 16..17, + id: "x", + ctx: Load, + }, + ), + op: Div, + right: Name( + ExprName { + range: 20..21, + id: "y", + ctx: Load, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 25..26, + value: NumberLiteral( + ExprNumberLiteral { + range: 25..26, + value: Int( + 2, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | x - y := (1, 2) + | ^^ Syntax Error: Expected a statement +2 | x / y := 2 + | + + + | +1 | x - y := (1, 2) +2 | x / y := 2 + | ^^ Syntax Error: Expected a statement + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__bin_op__starred_expression.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__bin_op__starred_expression.py.snap.new new file mode 100644 index 0000000000..a2bf66a886 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__bin_op__starred_expression.py.snap.new @@ -0,0 +1,92 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/bin_op/starred_expression.py +--- +## AST + +``` +Module( + ModModule { + range: 0..14, + body: [ + Expr( + StmtExpr { + range: 0..6, + value: BinOp( + ExprBinOp { + range: 0..6, + left: Name( + ExprName { + range: 0..1, + id: "x", + ctx: Load, + }, + ), + op: Add, + right: Starred( + ExprStarred { + range: 4..6, + value: Name( + ExprName { + range: 5..6, + id: "y", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 7..14, + value: BinOp( + ExprBinOp { + range: 7..14, + left: Name( + ExprName { + range: 7..8, + id: "x", + ctx: Load, + }, + ), + op: Pow, + right: Starred( + ExprStarred { + range: 12..14, + value: Name( + ExprName { + range: 13..14, + id: "y", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | x + *y + | ^^ Syntax Error: Starred expression cannot be used here +2 | x ** *y + | + + + | +1 | x + *y +2 | x ** *y + | ^^ Syntax Error: Starred expression cannot be used here + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__bool_op__invalid_rhs_expression.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__bool_op__invalid_rhs_expression.py.snap.new new file mode 100644 index 0000000000..8b958db8fd --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__bool_op__invalid_rhs_expression.py.snap.new @@ -0,0 +1,121 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/bool_op/invalid_rhs_expression.py +--- +## AST + +``` +Module( + ModModule { + range: 0..31, + body: [ + Expr( + StmtExpr { + range: 0..17, + value: BoolOp( + ExprBoolOp { + range: 0..17, + op: And, + values: [ + Name( + ExprName { + range: 0..1, + id: "x", + ctx: Load, + }, + ), + Lambda( + ExprLambda { + range: 6..17, + parameters: Some( + Parameters { + range: 13..14, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 13..14, + parameter: Parameter { + range: 13..14, + name: Identifier { + id: "y", + range: 13..14, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: Name( + ExprName { + range: 16..17, + id: "y", + ctx: Load, + }, + ), + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 19..31, + value: BoolOp( + ExprBoolOp { + range: 19..31, + op: Or, + values: [ + Name( + ExprName { + range: 19..20, + id: "x", + ctx: Load, + }, + ), + Yield( + ExprYield { + range: 24..31, + value: Some( + Name( + ExprName { + range: 30..31, + id: "y", + ctx: Load, + }, + ), + ), + }, + ), + ], + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | x and lambda y: y + | ^^^^^^^^^^^ Syntax Error: Lambda expression cannot be used here +2 | +3 | x or yield y + | + + + | +1 | x and lambda y: y +2 | +3 | x or yield y + | ^^^^^^^ Syntax Error: Yield expression cannot be used here + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__bool_op__missing_lhs.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__bool_op__missing_lhs.py.snap.new new file mode 100644 index 0000000000..c1c20dd520 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__bool_op__missing_lhs.py.snap.new @@ -0,0 +1,34 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/bool_op/missing_lhs.py +--- +## AST + +``` +Module( + ModModule { + range: 0..5, + body: [ + Expr( + StmtExpr { + range: 4..5, + value: Name( + ExprName { + range: 4..5, + id: "y", + ctx: Load, + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | and y + | ^^^ Syntax Error: Expected a statement + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__bool_op__missing_rhs.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__bool_op__missing_rhs.py.snap.new new file mode 100644 index 0000000000..c580c4df86 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__bool_op__missing_rhs.py.snap.new @@ -0,0 +1,78 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/bool_op/missing_rhs.py +--- +## AST + +``` +Module( + ModModule { + range: 0..12, + body: [ + Expr( + StmtExpr { + range: 0..5, + value: BoolOp( + ExprBoolOp { + range: 0..5, + op: And, + values: [ + Name( + ExprName { + range: 0..1, + id: "x", + ctx: Load, + }, + ), + Name( + ExprName { + range: 5..5, + id: "", + ctx: Invalid, + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 7..12, + value: BinOp( + ExprBinOp { + range: 7..12, + left: NumberLiteral( + ExprNumberLiteral { + range: 7..8, + value: Int( + 1, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 11..12, + value: Int( + 2, + ), + }, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | x and + | ^ Syntax Error: Expected an expression +2 | +3 | 1 + 2 + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__bool_op__named_expression.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__bool_op__named_expression.py.snap.new new file mode 100644 index 0000000000..44a8ef65c3 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__bool_op__named_expression.py.snap.new @@ -0,0 +1,108 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/bool_op/named_expression.py +--- +## AST + +``` +Module( + ModModule { + range: 0..24, + body: [ + Expr( + StmtExpr { + range: 0..7, + value: BoolOp( + ExprBoolOp { + range: 0..7, + op: And, + values: [ + Name( + ExprName { + range: 0..1, + id: "x", + ctx: Load, + }, + ), + Name( + ExprName { + range: 6..7, + id: "a", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 11..12, + value: Name( + ExprName { + range: 11..12, + id: "b", + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 13..19, + value: BoolOp( + ExprBoolOp { + range: 13..19, + op: Or, + values: [ + Name( + ExprName { + range: 13..14, + id: "x", + ctx: Load, + }, + ), + Name( + ExprName { + range: 18..19, + id: "a", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 23..24, + value: Name( + ExprName { + range: 23..24, + id: "b", + ctx: Load, + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | x and a := b + | ^^ Syntax Error: Expected a statement +2 | x or a := b + | + + + | +1 | x and a := b +2 | x or a := b + | ^^ Syntax Error: Expected a statement + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__bool_op__starred_expression.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__bool_op__starred_expression.py.snap.new new file mode 100644 index 0000000000..072f3c2b77 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__bool_op__starred_expression.py.snap.new @@ -0,0 +1,96 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/bool_op/starred_expression.py +--- +## AST + +``` +Module( + ModModule { + range: 0..16, + body: [ + Expr( + StmtExpr { + range: 0..8, + value: BoolOp( + ExprBoolOp { + range: 0..8, + op: And, + values: [ + Name( + ExprName { + range: 0..1, + id: "x", + ctx: Load, + }, + ), + Starred( + ExprStarred { + range: 6..8, + value: Name( + ExprName { + range: 7..8, + id: "y", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 9..16, + value: BoolOp( + ExprBoolOp { + range: 9..16, + op: Or, + values: [ + Name( + ExprName { + range: 9..10, + id: "x", + ctx: Load, + }, + ), + Starred( + ExprStarred { + range: 14..16, + value: Name( + ExprName { + range: 15..16, + id: "y", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | x and *y + | ^^ Syntax Error: Starred expression cannot be used here +2 | x or *y + | + + + | +1 | x and *y +2 | x or *y + | ^^ Syntax Error: Starred expression cannot be used here + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__compare__invalid_order.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__compare__invalid_order.py.snap.new new file mode 100644 index 0000000000..aae59a7df5 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__compare__invalid_order.py.snap.new @@ -0,0 +1,169 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/compare/invalid_order.py +--- +## AST + +``` +Module( + ModModule { + range: 0..131, + body: [ + Expr( + StmtExpr { + range: 0..10, + value: Compare( + ExprCompare { + range: 0..10, + left: Name( + ExprName { + range: 0..1, + id: "x", + ctx: Load, + }, + ), + ops: [ + In, + ], + comparators: [ + UnaryOp( + ExprUnaryOp { + range: 5..10, + op: Not, + operand: Name( + ExprName { + range: 9..10, + id: "y", + ctx: Load, + }, + ), + }, + ), + ], + }, + ), + }, + ), + Assign( + StmtAssign { + range: 35..41, + targets: [ + Name( + ExprName { + range: 35..36, + id: "x", + ctx: Store, + }, + ), + ], + value: Compare( + ExprCompare { + range: 38..41, + left: Name( + ExprName { + range: 38..38, + id: "", + ctx: Invalid, + }, + ), + ops: [ + Gt, + ], + comparators: [ + Name( + ExprName { + range: 40..41, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 120..121, + value: Name( + ExprName { + range: 120..121, + id: "x", + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 122..128, + value: UnaryOp( + ExprUnaryOp { + range: 122..128, + op: Not, + operand: Name( + ExprName { + range: 126..128, + id: "is", + ctx: Load, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 129..130, + value: Name( + ExprName { + range: 129..130, + id: "y", + ctx: Load, + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | x in not y + | ^^^^^ Syntax Error: Boolean 'not' expression cannot be used here +2 | +3 | # `=>` instead of `>=` + | + + + | +3 | # `=>` instead of `>=` +4 | x => y + | ^ Syntax Error: Expected an expression +5 | +6 | # Same here as well, `not` without `in` is considered to be a unary operator + | + + + | +6 | # Same here as well, `not` without `in` is considered to be a unary operator +7 | x not is y + | ^^^ Syntax Error: Simple statements must be separated by newlines or semicolons + | + + + | +6 | # Same here as well, `not` without `in` is considered to be a unary operator +7 | x not is y + | ^^ Syntax Error: Expected an identifier, but found a keyword 'is' that cannot be used here + | + + + | +6 | # Same here as well, `not` without `in` is considered to be a unary operator +7 | x not is y + | ^ Syntax Error: Simple statements must be separated by newlines or semicolons + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__compare__invalid_rhs_expression.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__compare__invalid_rhs_expression.py.snap.new new file mode 100644 index 0000000000..237cad295d --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__compare__invalid_rhs_expression.py.snap.new @@ -0,0 +1,125 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/compare/invalid_rhs_expression.py +--- +## AST + +``` +Module( + ModModule { + range: 0..34, + body: [ + Expr( + StmtExpr { + range: 0..20, + value: Compare( + ExprCompare { + range: 0..20, + left: Name( + ExprName { + range: 0..1, + id: "x", + ctx: Load, + }, + ), + ops: [ + NotIn, + ], + comparators: [ + Lambda( + ExprLambda { + range: 9..20, + parameters: Some( + Parameters { + range: 16..17, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 16..17, + parameter: Parameter { + range: 16..17, + name: Identifier { + id: "y", + range: 16..17, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: Name( + ExprName { + range: 19..20, + id: "y", + ctx: Load, + }, + ), + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 22..34, + value: Compare( + ExprCompare { + range: 22..34, + left: Name( + ExprName { + range: 22..23, + id: "x", + ctx: Load, + }, + ), + ops: [ + Eq, + ], + comparators: [ + Yield( + ExprYield { + range: 27..34, + value: Some( + Name( + ExprName { + range: 33..34, + id: "y", + ctx: Load, + }, + ), + ), + }, + ), + ], + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | x not in lambda y: y + | ^^^^^^^^^^^ Syntax Error: Lambda expression cannot be used here +2 | +3 | x == yield y + | + + + | +1 | x not in lambda y: y +2 | +3 | x == yield y + | ^^^^^^^ Syntax Error: Yield expression cannot be used here + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__compare__missing_lhs.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__compare__missing_lhs.py.snap.new new file mode 100644 index 0000000000..96a2345a7f --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__compare__missing_lhs.py.snap.new @@ -0,0 +1,63 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/compare/missing_lhs.py +--- +## AST + +``` +Module( + ModModule { + range: 0..10, + body: [ + Expr( + StmtExpr { + range: 2..3, + value: Name( + ExprName { + range: 2..3, + id: "y", + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 5..10, + value: BinOp( + ExprBinOp { + range: 5..10, + left: NumberLiteral( + ExprNumberLiteral { + range: 5..6, + value: Int( + 1, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 9..10, + value: Int( + 2, + ), + }, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | > y + | ^ Syntax Error: Expected a statement +2 | +3 | 1 + 2 + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__compare__missing_rhs_0.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__compare__missing_rhs_0.py.snap.new new file mode 100644 index 0000000000..048fa60fed --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__compare__missing_rhs_0.py.snap.new @@ -0,0 +1,80 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/compare/missing_rhs_0.py +--- +## AST + +``` +Module( + ModModule { + range: 0..10, + body: [ + Expr( + StmtExpr { + range: 0..3, + value: Compare( + ExprCompare { + range: 0..3, + left: Name( + ExprName { + range: 0..1, + id: "x", + ctx: Load, + }, + ), + ops: [ + Gt, + ], + comparators: [ + Name( + ExprName { + range: 3..3, + id: "", + ctx: Invalid, + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 5..10, + value: BinOp( + ExprBinOp { + range: 5..10, + left: NumberLiteral( + ExprNumberLiteral { + range: 5..6, + value: Int( + 1, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 9..10, + value: Int( + 2, + ), + }, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | x > + | ^ Syntax Error: Expected an expression +2 | +3 | 1 + 2 + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__compare__missing_rhs_1.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__compare__missing_rhs_1.py.snap.new new file mode 100644 index 0000000000..8c44bafe23 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__compare__missing_rhs_1.py.snap.new @@ -0,0 +1,91 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/compare/missing_rhs_1.py +--- +## AST + +``` +Module( + ModModule { + range: 0..71, + body: [ + Expr( + StmtExpr { + range: 59..60, + value: Name( + ExprName { + range: 59..60, + id: "x", + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 61..64, + value: UnaryOp( + ExprUnaryOp { + range: 61..64, + op: Not, + operand: Name( + ExprName { + range: 64..64, + id: "", + ctx: Invalid, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 66..71, + value: BinOp( + ExprBinOp { + range: 66..71, + left: NumberLiteral( + ExprNumberLiteral { + range: 66..67, + value: Int( + 1, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 70..71, + value: Int( + 2, + ), + }, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | # Without the `in`, this is considered to be a unary `not` +2 | x not + | ^^^ Syntax Error: Simple statements must be separated by newlines or semicolons +3 | +4 | 1 + 2 + | + + + | +1 | # Without the `in`, this is considered to be a unary `not` +2 | x not + | ^ Syntax Error: Expected an expression +3 | +4 | 1 + 2 + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__compare__missing_rhs_2.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__compare__missing_rhs_2.py.snap.new new file mode 100644 index 0000000000..e6b944c403 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__compare__missing_rhs_2.py.snap.new @@ -0,0 +1,80 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/compare/missing_rhs_2.py +--- +## AST + +``` +Module( + ModModule { + range: 0..15, + body: [ + Expr( + StmtExpr { + range: 0..8, + value: Compare( + ExprCompare { + range: 0..8, + left: Name( + ExprName { + range: 0..1, + id: "x", + ctx: Load, + }, + ), + ops: [ + IsNot, + ], + comparators: [ + Name( + ExprName { + range: 8..8, + id: "", + ctx: Invalid, + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 10..15, + value: BinOp( + ExprBinOp { + range: 10..15, + left: NumberLiteral( + ExprNumberLiteral { + range: 10..11, + value: Int( + 1, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 14..15, + value: Int( + 2, + ), + }, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | x is not + | ^ Syntax Error: Expected an expression +2 | +3 | 1 + 2 + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__compare__multiple_equals.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__compare__multiple_equals.py.snap.new new file mode 100644 index 0000000000..00fcd44f56 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__compare__multiple_equals.py.snap.new @@ -0,0 +1,124 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/compare/multiple_equals.py +--- +## AST + +``` +Module( + ModModule { + range: 0..41, + body: [ + Assign( + StmtAssign { + range: 25..32, + targets: [ + Compare( + ExprCompare { + range: 25..29, + left: Name( + ExprName { + range: 25..26, + id: "x", + ctx: Load, + }, + ), + ops: [ + Eq, + ], + comparators: [ + Name( + ExprName { + range: 29..29, + id: "", + ctx: Invalid, + }, + ), + ], + }, + ), + ], + value: Name( + ExprName { + range: 31..32, + id: "y", + ctx: Load, + }, + ), + }, + ), + Assign( + StmtAssign { + range: 33..40, + targets: [ + Compare( + ExprCompare { + range: 33..37, + left: Name( + ExprName { + range: 33..34, + id: "x", + ctx: Load, + }, + ), + ops: [ + NotEq, + ], + comparators: [ + Name( + ExprName { + range: 37..37, + id: "", + ctx: Invalid, + }, + ), + ], + }, + ), + ], + value: Name( + ExprName { + range: 39..40, + id: "y", + ctx: Load, + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | # This is not JavaScript +2 | x === y + | ^ Syntax Error: Expected an expression +3 | x !== y + | + + + | +1 | # This is not JavaScript +2 | x === y + | ^^^^ Syntax Error: Invalid assignment target +3 | x !== y + | + + + | +1 | # This is not JavaScript +2 | x === y +3 | x !== y + | ^ Syntax Error: Expected an expression + | + + + | +1 | # This is not JavaScript +2 | x === y +3 | x !== y + | ^^^^ Syntax Error: Invalid assignment target + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__compare__named_expression.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__compare__named_expression.py.snap.new new file mode 100644 index 0000000000..57c8bf28ee --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__compare__named_expression.py.snap.new @@ -0,0 +1,131 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/compare/named_expression.py +--- +## AST + +``` +Module( + ModModule { + range: 0..31, + body: [ + Expr( + StmtExpr { + range: 0..10, + value: Compare( + ExprCompare { + range: 0..10, + left: Name( + ExprName { + range: 0..1, + id: "x", + ctx: Load, + }, + ), + ops: [ + NotIn, + ], + comparators: [ + Name( + ExprName { + range: 9..10, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 14..20, + value: Tuple( + ExprTuple { + range: 14..20, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 15..16, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 18..19, + value: Int( + 2, + ), + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 21..26, + value: Compare( + ExprCompare { + range: 21..26, + left: Name( + ExprName { + range: 21..22, + id: "x", + ctx: Load, + }, + ), + ops: [ + Gt, + ], + comparators: [ + Name( + ExprName { + range: 25..26, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 30..31, + value: NumberLiteral( + ExprNumberLiteral { + range: 30..31, + value: Int( + 2, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | x not in y := (1, 2) + | ^^ Syntax Error: Expected a statement +2 | x > y := 2 + | + + + | +1 | x not in y := (1, 2) +2 | x > y := 2 + | ^^ Syntax Error: Expected a statement + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__compare__starred_expression.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__compare__starred_expression.py.snap.new new file mode 100644 index 0000000000..a78f9e7108 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__compare__starred_expression.py.snap.new @@ -0,0 +1,188 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/compare/starred_expression.py +--- +## AST + +``` +Module( + ModModule { + range: 0..39, + body: [ + Expr( + StmtExpr { + range: 0..7, + value: Compare( + ExprCompare { + range: 0..7, + left: Name( + ExprName { + range: 0..1, + id: "x", + ctx: Load, + }, + ), + ops: [ + GtE, + ], + comparators: [ + Starred( + ExprStarred { + range: 5..7, + value: Name( + ExprName { + range: 6..7, + id: "y", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 8..19, + value: Compare( + ExprCompare { + range: 8..19, + left: Name( + ExprName { + range: 8..9, + id: "x", + ctx: Load, + }, + ), + ops: [ + NotIn, + ], + comparators: [ + Starred( + ExprStarred { + range: 17..19, + value: Name( + ExprName { + range: 18..19, + id: "y", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 21..27, + value: Starred( + ExprStarred { + range: 21..27, + value: Compare( + ExprCompare { + range: 22..27, + left: Name( + ExprName { + range: 22..23, + id: "x", + ctx: Load, + }, + ), + ops: [ + Lt, + ], + comparators: [ + Name( + ExprName { + range: 26..27, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 28..39, + value: Starred( + ExprStarred { + range: 28..39, + value: Compare( + ExprCompare { + range: 29..39, + left: Name( + ExprName { + range: 29..30, + id: "x", + ctx: Load, + }, + ), + ops: [ + IsNot, + ], + comparators: [ + Name( + ExprName { + range: 38..39, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + ctx: Load, + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | x >= *y + | ^^ Syntax Error: Starred expression cannot be used here +2 | x not in *y + | + + + | +1 | x >= *y +2 | x not in *y + | ^^ Syntax Error: Starred expression cannot be used here +3 | +4 | *x < y + | + + + | +2 | x not in *y +3 | +4 | *x < y + | ^^^^^ Syntax Error: Comparison expression cannot be used here +5 | *x is not y + | + + + | +4 | *x < y +5 | *x is not y + | ^^^^^^^^^^ Syntax Error: Comparison expression cannot be used here + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__dict__comprehension.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__dict__comprehension.py.snap.new new file mode 100644 index 0000000000..1119e1fb4a --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__dict__comprehension.py.snap.new @@ -0,0 +1,828 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/dict/comprehension.py +--- +## AST + +``` +Module( + ModModule { + range: 0..362, + body: [ + Expr( + StmtExpr { + range: 17..34, + value: DictComp( + ExprDictComp { + range: 17..34, + key: Name( + ExprName { + range: 18..19, + id: "x", + ctx: Load, + }, + ), + value: Name( + ExprName { + range: 21..22, + id: "y", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 23..33, + target: NumberLiteral( + ExprNumberLiteral { + range: 27..28, + value: Int( + 1, + ), + }, + ), + iter: Name( + ExprName { + range: 32..33, + id: "y", + ctx: Load, + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 35..54, + value: DictComp( + ExprDictComp { + range: 35..54, + key: Name( + ExprName { + range: 36..37, + id: "x", + ctx: Load, + }, + ), + value: Name( + ExprName { + range: 39..40, + id: "y", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 41..53, + target: StringLiteral( + ExprStringLiteral { + range: 45..48, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 45..48, + value: "a", + flags: StringLiteralFlags { + quote_style: Single, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + iter: Name( + ExprName { + range: 52..53, + id: "y", + ctx: Load, + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 55..77, + value: DictComp( + ExprDictComp { + range: 55..77, + key: Name( + ExprName { + range: 56..57, + id: "x", + ctx: Load, + }, + ), + value: Name( + ExprName { + range: 59..60, + id: "y", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 61..76, + target: Call( + ExprCall { + range: 65..71, + func: Name( + ExprName { + range: 65..69, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 69..71, + args: [], + keywords: [], + }, + }, + ), + iter: Name( + ExprName { + range: 75..76, + id: "y", + ctx: Load, + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 78..100, + value: DictComp( + ExprDictComp { + range: 78..100, + key: Name( + ExprName { + range: 79..80, + id: "x", + ctx: Load, + }, + ), + value: Name( + ExprName { + range: 82..83, + id: "y", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 84..99, + target: Set( + ExprSet { + range: 88..94, + elts: [ + Name( + ExprName { + range: 89..90, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 92..93, + id: "b", + ctx: Load, + }, + ), + ], + }, + ), + iter: Name( + ExprName { + range: 98..99, + id: "y", + ctx: Load, + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 117..135, + value: DictComp( + ExprDictComp { + range: 117..135, + key: Name( + ExprName { + range: 118..119, + id: "x", + ctx: Load, + }, + ), + value: Name( + ExprName { + range: 121..122, + id: "y", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 123..134, + target: Name( + ExprName { + range: 127..128, + id: "x", + ctx: Store, + }, + ), + iter: Starred( + ExprStarred { + range: 132..134, + value: Name( + ExprName { + range: 133..134, + id: "y", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 136..159, + value: DictComp( + ExprDictComp { + range: 136..159, + key: Name( + ExprName { + range: 137..138, + id: "x", + ctx: Load, + }, + ), + value: Name( + ExprName { + range: 140..141, + id: "y", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 142..158, + target: Name( + ExprName { + range: 146..147, + id: "x", + ctx: Store, + }, + ), + iter: Yield( + ExprYield { + range: 151..158, + value: Some( + Name( + ExprName { + range: 157..158, + id: "y", + ctx: Load, + }, + ), + ), + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 160..188, + value: DictComp( + ExprDictComp { + range: 160..188, + key: Name( + ExprName { + range: 161..162, + id: "x", + ctx: Load, + }, + ), + value: Name( + ExprName { + range: 164..165, + id: "y", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 166..187, + target: Name( + ExprName { + range: 170..171, + id: "x", + ctx: Store, + }, + ), + iter: YieldFrom( + ExprYieldFrom { + range: 175..187, + value: Name( + ExprName { + range: 186..187, + id: "y", + ctx: Load, + }, + ), + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 189..216, + value: DictComp( + ExprDictComp { + range: 189..216, + key: Name( + ExprName { + range: 190..191, + id: "x", + ctx: Load, + }, + ), + value: Name( + ExprName { + range: 193..194, + id: "y", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 195..215, + target: Name( + ExprName { + range: 199..200, + id: "x", + ctx: Store, + }, + ), + iter: Lambda( + ExprLambda { + range: 204..215, + parameters: Some( + Parameters { + range: 211..212, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 211..212, + parameter: Parameter { + range: 211..212, + name: Identifier { + id: "y", + range: 211..212, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: Name( + ExprName { + range: 214..215, + id: "y", + ctx: Load, + }, + ), + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 231..257, + value: DictComp( + ExprDictComp { + range: 231..257, + key: Name( + ExprName { + range: 232..233, + id: "x", + ctx: Load, + }, + ), + value: Name( + ExprName { + range: 235..236, + id: "y", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 237..256, + target: Name( + ExprName { + range: 241..242, + id: "x", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 246..250, + id: "data", + ctx: Load, + }, + ), + ifs: [ + Starred( + ExprStarred { + range: 254..256, + value: Name( + ExprName { + range: 255..256, + id: "y", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 258..289, + value: DictComp( + ExprDictComp { + range: 258..289, + key: Name( + ExprName { + range: 259..260, + id: "x", + ctx: Load, + }, + ), + value: Name( + ExprName { + range: 262..263, + id: "y", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 264..288, + target: Name( + ExprName { + range: 268..269, + id: "x", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 273..277, + id: "data", + ctx: Load, + }, + ), + ifs: [ + Yield( + ExprYield { + range: 281..288, + value: Some( + Name( + ExprName { + range: 287..288, + id: "y", + ctx: Load, + }, + ), + ), + }, + ), + ], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 290..326, + value: DictComp( + ExprDictComp { + range: 290..326, + key: Name( + ExprName { + range: 291..292, + id: "x", + ctx: Load, + }, + ), + value: Name( + ExprName { + range: 294..295, + id: "y", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 296..325, + target: Name( + ExprName { + range: 300..301, + id: "x", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 305..309, + id: "data", + ctx: Load, + }, + ), + ifs: [ + YieldFrom( + ExprYieldFrom { + range: 313..325, + value: Name( + ExprName { + range: 324..325, + id: "y", + ctx: Load, + }, + ), + }, + ), + ], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 327..362, + value: DictComp( + ExprDictComp { + range: 327..362, + key: Name( + ExprName { + range: 328..329, + id: "x", + ctx: Load, + }, + ), + value: Name( + ExprName { + range: 331..332, + id: "y", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 333..361, + target: Name( + ExprName { + range: 337..338, + id: "x", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 342..346, + id: "data", + ctx: Load, + }, + ), + ifs: [ + Lambda( + ExprLambda { + range: 350..361, + parameters: Some( + Parameters { + range: 357..358, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 357..358, + parameter: Parameter { + range: 357..358, + name: Identifier { + id: "y", + range: 357..358, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: Name( + ExprName { + range: 360..361, + id: "y", + ctx: Load, + }, + ), + }, + ), + ], + is_async: false, + }, + ], + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | # Invalid target +2 | {x: y for 1 in y} + | ^ Syntax Error: Invalid assignment target +3 | {x: y for 'a' in y} +4 | {x: y for call() in y} + | + + + | +1 | # Invalid target +2 | {x: y for 1 in y} +3 | {x: y for 'a' in y} + | ^^^ Syntax Error: Invalid assignment target +4 | {x: y for call() in y} +5 | {x: y for {a, b} in y} + | + + + | +2 | {x: y for 1 in y} +3 | {x: y for 'a' in y} +4 | {x: y for call() in y} + | ^^^^^^ Syntax Error: Invalid assignment target +5 | {x: y for {a, b} in y} + | + + + | +3 | {x: y for 'a' in y} +4 | {x: y for call() in y} +5 | {x: y for {a, b} in y} + | ^^^^^^ Syntax Error: Invalid assignment target +6 | +7 | # Invalid iter + | + + + | + 7 | # Invalid iter + 8 | {x: y for x in *y} + | ^^ Syntax Error: Starred expression cannot be used here + 9 | {x: y for x in yield y} +10 | {x: y for x in yield from y} + | + + + | + 7 | # Invalid iter + 8 | {x: y for x in *y} + 9 | {x: y for x in yield y} + | ^^^^^^^ Syntax Error: Yield expression cannot be used here +10 | {x: y for x in yield from y} +11 | {x: y for x in lambda y: y} + | + + + | + 8 | {x: y for x in *y} + 9 | {x: y for x in yield y} +10 | {x: y for x in yield from y} + | ^^^^^^^^^^^^ Syntax Error: Yield expression cannot be used here +11 | {x: y for x in lambda y: y} + | + + + | + 9 | {x: y for x in yield y} +10 | {x: y for x in yield from y} +11 | {x: y for x in lambda y: y} + | ^^^^^^^^^^^ Syntax Error: Lambda expression cannot be used here +12 | +13 | # Invalid if + | + + + | +13 | # Invalid if +14 | {x: y for x in data if *y} + | ^^ Syntax Error: Starred expression cannot be used here +15 | {x: y for x in data if yield y} +16 | {x: y for x in data if yield from y} + | + + + | +13 | # Invalid if +14 | {x: y for x in data if *y} +15 | {x: y for x in data if yield y} + | ^^^^^^^ Syntax Error: Yield expression cannot be used here +16 | {x: y for x in data if yield from y} +17 | {x: y for x in data if lambda y: y} + | + + + | +14 | {x: y for x in data if *y} +15 | {x: y for x in data if yield y} +16 | {x: y for x in data if yield from y} + | ^^^^^^^^^^^^ Syntax Error: Yield expression cannot be used here +17 | {x: y for x in data if lambda y: y} + | + + + | +15 | {x: y for x in data if yield y} +16 | {x: y for x in data if yield from y} +17 | {x: y for x in data if lambda y: y} + | ^^^^^^^^^^^ Syntax Error: Lambda expression cannot be used here + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__dict__double_star.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__dict__double_star.py.snap.new new file mode 100644 index 0000000000..fcf9674767 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__dict__double_star.py.snap.new @@ -0,0 +1,576 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/dict/double_star.py +--- +## AST + +``` +Module( + ModModule { + range: 0..278, + body: [ + Expr( + StmtExpr { + range: 125..135, + value: Dict( + ExprDict { + range: 125..135, + items: [ + DictItem { + key: None, + value: Name( + ExprName { + range: 128..129, + id: "x", + ctx: Load, + }, + ), + }, + DictItem { + key: Some( + NumberLiteral( + ExprNumberLiteral { + range: 133..134, + value: Int( + 1, + ), + }, + ), + ), + value: Name( + ExprName { + range: 134..134, + id: "", + ctx: Invalid, + }, + ), + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 136..162, + value: Dict( + ExprDict { + range: 136..162, + items: [ + DictItem { + key: Some( + Name( + ExprName { + range: 137..138, + id: "a", + ctx: Load, + }, + ), + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 140..141, + value: Int( + 1, + ), + }, + ), + }, + DictItem { + key: None, + value: If( + ExprIf { + range: 145..161, + test: BooleanLiteral( + ExprBooleanLiteral { + range: 150..154, + value: true, + }, + ), + body: Name( + ExprName { + range: 145..146, + id: "x", + ctx: Load, + }, + ), + orelse: Name( + ExprName { + range: 160..161, + id: "y", + ctx: Load, + }, + ), + }, + ), + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 163..184, + value: Dict( + ExprDict { + range: 163..184, + items: [ + DictItem { + key: None, + value: Lambda( + ExprLambda { + range: 166..177, + parameters: Some( + Parameters { + range: 173..174, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 173..174, + parameter: Parameter { + range: 173..174, + name: Identifier { + id: "x", + range: 173..174, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: Name( + ExprName { + range: 176..177, + id: "x", + ctx: Load, + }, + ), + }, + ), + }, + DictItem { + key: Some( + Name( + ExprName { + range: 179..180, + id: "b", + ctx: Load, + }, + ), + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 182..183, + value: Int( + 2, + ), + }, + ), + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 185..201, + value: Dict( + ExprDict { + range: 185..201, + items: [ + DictItem { + key: Some( + Name( + ExprName { + range: 186..187, + id: "a", + ctx: Load, + }, + ), + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 189..190, + value: Int( + 1, + ), + }, + ), + }, + DictItem { + key: None, + value: BoolOp( + ExprBoolOp { + range: 194..200, + op: Or, + values: [ + Name( + ExprName { + range: 194..195, + id: "x", + ctx: Load, + }, + ), + Name( + ExprName { + range: 199..200, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 202..219, + value: Dict( + ExprDict { + range: 202..219, + items: [ + DictItem { + key: None, + value: BoolOp( + ExprBoolOp { + range: 205..212, + op: And, + values: [ + Name( + ExprName { + range: 205..206, + id: "x", + ctx: Load, + }, + ), + Name( + ExprName { + range: 211..212, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + }, + DictItem { + key: Some( + Name( + ExprName { + range: 214..215, + id: "b", + ctx: Load, + }, + ), + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 217..218, + value: Int( + 2, + ), + }, + ), + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 220..241, + value: Dict( + ExprDict { + range: 220..241, + items: [ + DictItem { + key: Some( + Name( + ExprName { + range: 221..222, + id: "a", + ctx: Load, + }, + ), + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 224..225, + value: Int( + 1, + ), + }, + ), + }, + DictItem { + key: None, + value: UnaryOp( + ExprUnaryOp { + range: 229..234, + op: Not, + operand: Name( + ExprName { + range: 233..234, + id: "x", + ctx: Load, + }, + ), + }, + ), + }, + DictItem { + key: Some( + Name( + ExprName { + range: 236..237, + id: "b", + ctx: Load, + }, + ), + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 239..240, + value: Int( + 2, + ), + }, + ), + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 242..252, + value: Dict( + ExprDict { + range: 242..252, + items: [ + DictItem { + key: None, + value: Compare( + ExprCompare { + range: 245..251, + left: Name( + ExprName { + range: 245..246, + id: "x", + ctx: Load, + }, + ), + ops: [ + In, + ], + comparators: [ + Name( + ExprName { + range: 250..251, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 253..267, + value: Dict( + ExprDict { + range: 253..267, + items: [ + DictItem { + key: None, + value: Compare( + ExprCompare { + range: 256..266, + left: Name( + ExprName { + range: 256..257, + id: "x", + ctx: Load, + }, + ), + ops: [ + NotIn, + ], + comparators: [ + Name( + ExprName { + range: 265..266, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 268..277, + value: Dict( + ExprDict { + range: 268..277, + items: [ + DictItem { + key: None, + value: Compare( + ExprCompare { + range: 271..276, + left: Name( + ExprName { + range: 271..272, + id: "x", + ctx: Load, + }, + ), + ops: [ + Lt, + ], + comparators: [ + Name( + ExprName { + range: 275..276, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + }, + ], + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +2 | # the ones which are higher than that. +3 | +4 | {**x := 1} + | ^^ Syntax Error: Expected ',', found ':=' +5 | {a: 1, **x if True else y} +6 | {**lambda x: x, b: 2} + | + + + | +2 | # the ones which are higher than that. +3 | +4 | {**x := 1} + | ^ Syntax Error: Expected ':', found '}' +5 | {a: 1, **x if True else y} +6 | {**lambda x: x, b: 2} + | + + + | +4 | {**x := 1} +5 | {a: 1, **x if True else y} + | ^^^^^^^^^^^^^^^^ Syntax Error: Conditional expression cannot be used here +6 | {**lambda x: x, b: 2} +7 | {a: 1, **x or y} + | + + + | +4 | {**x := 1} +5 | {a: 1, **x if True else y} +6 | {**lambda x: x, b: 2} + | ^^^^^^^^^^^ Syntax Error: Lambda expression cannot be used here +7 | {a: 1, **x or y} +8 | {**x and y, b: 2} + | + + + | +5 | {a: 1, **x if True else y} +6 | {**lambda x: x, b: 2} +7 | {a: 1, **x or y} + | ^^^^^^ Syntax Error: Boolean expression cannot be used here +8 | {**x and y, b: 2} +9 | {a: 1, **not x, b: 2} + | + + + | + 6 | {**lambda x: x, b: 2} + 7 | {a: 1, **x or y} + 8 | {**x and y, b: 2} + | ^^^^^^^ Syntax Error: Boolean expression cannot be used here + 9 | {a: 1, **not x, b: 2} +10 | {**x in y} + | + + + | + 7 | {a: 1, **x or y} + 8 | {**x and y, b: 2} + 9 | {a: 1, **not x, b: 2} + | ^^^^^ Syntax Error: Boolean expression cannot be used here +10 | {**x in y} +11 | {**x not in y} + | + + + | + 8 | {**x and y, b: 2} + 9 | {a: 1, **not x, b: 2} +10 | {**x in y} + | ^^^^^^ Syntax Error: Comparison expression cannot be used here +11 | {**x not in y} +12 | {**x < y} + | + + + | + 9 | {a: 1, **not x, b: 2} +10 | {**x in y} +11 | {**x not in y} + | ^^^^^^^^^^ Syntax Error: Comparison expression cannot be used here +12 | {**x < y} + | + + + | +10 | {**x in y} +11 | {**x not in y} +12 | {**x < y} + | ^^^^^ Syntax Error: Comparison expression cannot be used here + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__dict__double_star_comprehension.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__dict__double_star_comprehension.py.snap.new new file mode 100644 index 0000000000..37ce4468d3 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__dict__double_star_comprehension.py.snap.new @@ -0,0 +1,159 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/dict/double_star_comprehension.py +--- +## AST + +``` +Module( + ModModule { + range: 0..358, + body: [ + Expr( + StmtExpr { + range: 122..147, + value: Dict( + ExprDict { + range: 122..147, + items: [ + DictItem { + key: None, + value: Name( + ExprName { + range: 125..126, + id: "x", + ctx: Load, + }, + ), + }, + DictItem { + key: Some( + Name( + ExprName { + range: 128..129, + id: "y", + ctx: Load, + }, + ), + ), + value: Name( + ExprName { + range: 130..133, + id: "for", + ctx: Load, + }, + ), + }, + DictItem { + key: Some( + Name( + ExprName { + range: 134..135, + id: "x", + ctx: Load, + }, + ), + ), + value: Name( + ExprName { + range: 135..135, + id: "", + ctx: Invalid, + }, + ), + }, + DictItem { + key: Some( + Compare( + ExprCompare { + range: 137..146, + left: Name( + ExprName { + range: 137..138, + id: "y", + ctx: Load, + }, + ), + ops: [ + In, + ], + comparators: [ + Name( + ExprName { + range: 142..146, + id: "data", + ctx: Load, + }, + ), + ], + }, + ), + ), + value: Name( + ExprName { + range: 146..146, + id: "", + ctx: Invalid, + }, + ), + }, + ], + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +2 | # it's actually a comprehension. +3 | +4 | {**x: y for x, y in data} + | ^ Syntax Error: Expected an expression or a '}' +5 | +6 | # TODO(dhruvmanila): This test case fails because there's no way to represent `**y` + | + + + | +2 | # it's actually a comprehension. +3 | +4 | {**x: y for x, y in data} + | ^^^ Syntax Error: Expected ':', found 'for' +5 | +6 | # TODO(dhruvmanila): This test case fails because there's no way to represent `**y` + | + + + | +2 | # it's actually a comprehension. +3 | +4 | {**x: y for x, y in data} + | ^ Syntax Error: Expected ',', found name +5 | +6 | # TODO(dhruvmanila): This test case fails because there's no way to represent `**y` + | + + + | +2 | # it's actually a comprehension. +3 | +4 | {**x: y for x, y in data} + | ^ Syntax Error: Expected ':', found ',' +5 | +6 | # TODO(dhruvmanila): This test case fails because there's no way to represent `**y` + | + + + | +2 | # it's actually a comprehension. +3 | +4 | {**x: y for x, y in data} + | ^ Syntax Error: Expected ':', found '}' +5 | +6 | # TODO(dhruvmanila): This test case fails because there's no way to represent `**y` + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__dict__missing_closing_brace_0.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__dict__missing_closing_brace_0.py.snap.new new file mode 100644 index 0000000000..7ce4ac564f --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__dict__missing_closing_brace_0.py.snap.new @@ -0,0 +1,106 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/dict/missing_closing_brace_0.py +--- +## AST + +``` +Module( + ModModule { + range: 0..24, + body: [ + Expr( + StmtExpr { + range: 0..24, + value: Dict( + ExprDict { + range: 0..24, + items: [ + DictItem { + key: Some( + Name( + ExprName { + range: 1..2, + id: "x", + ctx: Load, + }, + ), + ), + value: Name( + ExprName { + range: 5..8, + id: "def", + ctx: Load, + }, + ), + }, + DictItem { + key: Some( + Call( + ExprCall { + range: 9..14, + func: Name( + ExprName { + range: 9..12, + id: "foo", + ctx: Load, + }, + ), + arguments: Arguments { + range: 12..14, + args: [], + keywords: [], + }, + }, + ), + ), + value: Name( + ExprName { + range: 20..24, + id: "pass", + ctx: Load, + }, + ), + }, + ], + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | {x: +2 | +3 | def foo(): + | ^^^ Syntax Error: Expected an identifier, but found a keyword 'def' that cannot be used here +4 | pass + | + + + | +1 | {x: +2 | +3 | def foo(): + | ^^^ Syntax Error: Expected ',', found name +4 | pass + | + + + | +3 | def foo(): +4 | pass + | ^^^^ Syntax Error: Expected an identifier, but found a keyword 'pass' that cannot be used here + | + + + | +3 | def foo(): +4 | pass + | Syntax Error: unexpected EOF while parsing + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__dict__missing_closing_brace_1.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__dict__missing_closing_brace_1.py.snap.new new file mode 100644 index 0000000000..0b9616167a --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__dict__missing_closing_brace_1.py.snap.new @@ -0,0 +1,69 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/dict/missing_closing_brace_1.py +--- +## AST + +``` +Module( + ModModule { + range: 0..10, + body: [ + Expr( + StmtExpr { + range: 0..10, + value: Dict( + ExprDict { + range: 0..10, + items: [ + DictItem { + key: Some( + Name( + ExprName { + range: 1..2, + id: "x", + ctx: Load, + }, + ), + ), + value: BinOp( + ExprBinOp { + range: 5..10, + left: NumberLiteral( + ExprNumberLiteral { + range: 5..6, + value: Int( + 1, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 9..10, + value: Int( + 2, + ), + }, + ), + }, + ), + }, + ], + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | {x: +2 | +3 | 1 + 2 + | Syntax Error: unexpected EOF while parsing + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__dict__missing_closing_brace_2.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__dict__missing_closing_brace_2.py.snap.new new file mode 100644 index 0000000000..955207508e --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__dict__missing_closing_brace_2.py.snap.new @@ -0,0 +1,84 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/dict/missing_closing_brace_2.py +--- +## AST + +``` +Module( + ModModule { + range: 0..27, + body: [ + Expr( + StmtExpr { + range: 0..6, + value: Dict( + ExprDict { + range: 0..6, + items: [ + DictItem { + key: Some( + Name( + ExprName { + range: 1..2, + id: "x", + ctx: Load, + }, + ), + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 4..5, + value: Int( + 1, + ), + }, + ), + }, + ], + }, + ), + }, + ), + FunctionDef( + StmtFunctionDef { + range: 8..27, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 12..15, + }, + type_params: None, + parameters: Parameters { + range: 15..17, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 23..27, + }, + ), + ], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | {x: 1, + | ^ Syntax Error: Expected '}', found newline +2 | +3 | def foo(): +4 | pass + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__dict__named_expression_0.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__dict__named_expression_0.py.snap.new new file mode 100644 index 0000000000..1ffa5fe711 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__dict__named_expression_0.py.snap.new @@ -0,0 +1,141 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/dict/named_expression_0.py +--- +## AST + +``` +Module( + ModModule { + range: 0..84, + body: [ + Expr( + StmtExpr { + range: 55..77, + value: Dict( + ExprDict { + range: 55..77, + items: [ + DictItem { + key: Some( + Named( + ExprNamed { + range: 56..62, + target: Name( + ExprName { + range: 56..57, + id: "x", + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 61..62, + value: Int( + 1, + ), + }, + ), + }, + ), + ), + value: Name( + ExprName { + range: 64..65, + id: "y", + ctx: Load, + }, + ), + }, + DictItem { + key: Some( + Name( + ExprName { + range: 67..68, + id: "z", + ctx: Load, + }, + ), + ), + value: Name( + ExprName { + range: 68..68, + id: "", + ctx: Invalid, + }, + ), + }, + DictItem { + key: Some( + NumberLiteral( + ExprNumberLiteral { + range: 72..73, + value: Int( + 2, + ), + }, + ), + ), + value: Name( + ExprName { + range: 75..76, + id: "a", + ctx: Load, + }, + ), + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 79..84, + value: BinOp( + ExprBinOp { + range: 79..84, + left: Name( + ExprName { + range: 79..80, + id: "x", + ctx: Load, + }, + ), + op: Add, + right: Name( + ExprName { + range: 83..84, + id: "y", + ctx: Load, + }, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | # Unparenthesized named expression not allowed in key +2 | +3 | {x := 1: y, z := 2: a} + | ^^^^^^ Syntax Error: Unparenthesized named expression cannot be used here +4 | +5 | x + y + | + + + | +1 | # Unparenthesized named expression not allowed in key +2 | +3 | {x := 1: y, z := 2: a} + | ^^ Syntax Error: Expected ':', found ':=' +4 | +5 | x + y + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__dict__named_expression_1.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__dict__named_expression_1.py.snap.new new file mode 100644 index 0000000000..b7efe2bd36 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__dict__named_expression_1.py.snap.new @@ -0,0 +1,167 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/dict/named_expression_1.py +--- +## AST + +``` +Module( + ModModule { + range: 0..86, + body: [ + Expr( + StmtExpr { + range: 57..79, + value: Dict( + ExprDict { + range: 57..79, + items: [ + DictItem { + key: Some( + Name( + ExprName { + range: 58..59, + id: "x", + ctx: Load, + }, + ), + ), + value: Name( + ExprName { + range: 61..62, + id: "y", + ctx: Load, + }, + ), + }, + DictItem { + key: Some( + NumberLiteral( + ExprNumberLiteral { + range: 66..67, + value: Int( + 1, + ), + }, + ), + ), + value: Name( + ExprName { + range: 67..67, + id: "", + ctx: Invalid, + }, + ), + }, + DictItem { + key: Some( + Name( + ExprName { + range: 69..70, + id: "z", + ctx: Load, + }, + ), + ), + value: Name( + ExprName { + range: 72..73, + id: "a", + ctx: Load, + }, + ), + }, + DictItem { + key: Some( + NumberLiteral( + ExprNumberLiteral { + range: 77..78, + value: Int( + 2, + ), + }, + ), + ), + value: Name( + ExprName { + range: 78..78, + id: "", + ctx: Invalid, + }, + ), + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 81..86, + value: BinOp( + ExprBinOp { + range: 81..86, + left: Name( + ExprName { + range: 81..82, + id: "x", + ctx: Load, + }, + ), + op: Add, + right: Name( + ExprName { + range: 85..86, + id: "y", + ctx: Load, + }, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | # Unparenthesized named expression not allowed in value +2 | +3 | {x: y := 1, z: a := 2} + | ^^ Syntax Error: Expected ',', found ':=' +4 | +5 | x + y + | + + + | +1 | # Unparenthesized named expression not allowed in value +2 | +3 | {x: y := 1, z: a := 2} + | ^ Syntax Error: Expected ':', found ',' +4 | +5 | x + y + | + + + | +1 | # Unparenthesized named expression not allowed in value +2 | +3 | {x: y := 1, z: a := 2} + | ^^ Syntax Error: Expected ',', found ':=' +4 | +5 | x + y + | + + + | +1 | # Unparenthesized named expression not allowed in value +2 | +3 | {x: y := 1, z: a := 2} + | ^ Syntax Error: Expected ':', found '}' +4 | +5 | x + y + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__dict__recover.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__dict__recover.py.snap.new new file mode 100644 index 0000000000..7d844143e7 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__dict__recover.py.snap.new @@ -0,0 +1,518 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/dict/recover.py +--- +## AST + +``` +Module( + ModModule { + range: 0..346, + body: [ + Expr( + StmtExpr { + range: 88..91, + value: Set( + ExprSet { + range: 88..91, + elts: [ + Name( + ExprName { + range: 89..89, + id: "", + ctx: Invalid, + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 93..105, + value: Dict( + ExprDict { + range: 93..105, + items: [ + DictItem { + key: Some( + NumberLiteral( + ExprNumberLiteral { + range: 94..95, + value: Int( + 1, + ), + }, + ), + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 97..98, + value: Int( + 2, + ), + }, + ), + }, + DictItem { + key: Some( + NumberLiteral( + ExprNumberLiteral { + range: 100..101, + value: Int( + 3, + ), + }, + ), + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 103..104, + value: Int( + 4, + ), + }, + ), + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 107..115, + value: Dict( + ExprDict { + range: 107..115, + items: [ + DictItem { + key: Some( + NumberLiteral( + ExprNumberLiteral { + range: 108..109, + value: Int( + 1, + ), + }, + ), + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 111..112, + value: Int( + 2, + ), + }, + ), + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 133..144, + value: Dict( + ExprDict { + range: 133..144, + items: [ + DictItem { + key: Some( + NumberLiteral( + ExprNumberLiteral { + range: 134..135, + value: Int( + 1, + ), + }, + ), + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 137..138, + value: Int( + 2, + ), + }, + ), + }, + DictItem { + key: Some( + NumberLiteral( + ExprNumberLiteral { + range: 139..140, + value: Int( + 3, + ), + }, + ), + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 142..143, + value: Int( + 4, + ), + }, + ), + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 157..162, + value: Dict( + ExprDict { + range: 157..162, + items: [ + DictItem { + key: Some( + NumberLiteral( + ExprNumberLiteral { + range: 158..159, + value: Int( + 1, + ), + }, + ), + ), + value: Name( + ExprName { + range: 160..160, + id: "", + ctx: Invalid, + }, + ), + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 201..205, + value: Dict( + ExprDict { + range: 201..205, + items: [ + DictItem { + key: None, + value: Name( + ExprName { + range: 204..204, + id: "", + ctx: Invalid, + }, + ), + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 206..222, + value: Dict( + ExprDict { + range: 206..222, + items: [ + DictItem { + key: Some( + Name( + ExprName { + range: 207..208, + id: "x", + ctx: Load, + }, + ), + ), + value: Name( + ExprName { + range: 210..211, + id: "y", + ctx: Load, + }, + ), + }, + DictItem { + key: None, + value: Name( + ExprName { + range: 215..215, + id: "", + ctx: Invalid, + }, + ), + }, + DictItem { + key: Some( + Name( + ExprName { + range: 217..218, + id: "a", + ctx: Load, + }, + ), + ), + value: Name( + ExprName { + range: 220..221, + id: "b", + ctx: Load, + }, + ), + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 310..330, + value: Dict( + ExprDict { + range: 310..330, + items: [ + DictItem { + key: Some( + Starred( + ExprStarred { + range: 311..313, + value: Name( + ExprName { + range: 312..313, + id: "x", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ), + value: Name( + ExprName { + range: 315..316, + id: "y", + ctx: Load, + }, + ), + }, + DictItem { + key: Some( + Name( + ExprName { + range: 318..319, + id: "z", + ctx: Load, + }, + ), + ), + value: Name( + ExprName { + range: 321..322, + id: "a", + ctx: Load, + }, + ), + }, + DictItem { + key: Some( + Starred( + ExprStarred { + range: 324..326, + value: Name( + ExprName { + range: 325..326, + id: "b", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ), + value: Name( + ExprName { + range: 328..329, + id: "c", + ctx: Load, + }, + ), + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 331..345, + value: Dict( + ExprDict { + range: 331..345, + items: [ + DictItem { + key: Some( + Name( + ExprName { + range: 332..333, + id: "x", + ctx: Load, + }, + ), + ), + value: Starred( + ExprStarred { + range: 335..337, + value: Name( + ExprName { + range: 336..337, + id: "y", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + }, + DictItem { + key: Some( + Name( + ExprName { + range: 339..340, + id: "z", + ctx: Load, + }, + ), + ), + value: Starred( + ExprStarred { + range: 342..344, + value: Name( + ExprName { + range: 343..344, + id: "a", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + }, + ], + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | # Test cases for dictionary expressions where the parser recovers from a syntax error. +2 | +3 | {,} + | ^ Syntax Error: Expected an expression +4 | +5 | {1: 2,,3: 4} + | + + + | +3 | {,} +4 | +5 | {1: 2,,3: 4} + | ^ Syntax Error: Expected an expression or a '}' +6 | +7 | {1: 2,,} + | + + + | +5 | {1: 2,,3: 4} +6 | +7 | {1: 2,,} + | ^ Syntax Error: Expected an expression or a '}' +8 | +9 | # Missing comma + | + + + | + 9 | # Missing comma +10 | {1: 2 3: 4} + | ^ Syntax Error: Expected ',', found int +11 | +12 | # No value + | + + + | +12 | # No value +13 | {1: } + | ^ Syntax Error: Expected an expression +14 | +15 | # No value for double star unpacking + | + + + | +15 | # No value for double star unpacking +16 | {**} + | ^ Syntax Error: Expected an expression +17 | {x: y, **, a: b} + | + + + | +15 | # No value for double star unpacking +16 | {**} +17 | {x: y, **, a: b} + | ^ Syntax Error: Expected an expression +18 | +19 | # This is not a double star unpacking + | + + + | +22 | # Star expression not allowed here +23 | {*x: y, z: a, *b: c} + | ^^ Syntax Error: Starred expression cannot be used here +24 | {x: *y, z: *a} + | + + + | +22 | # Star expression not allowed here +23 | {*x: y, z: a, *b: c} + | ^^ Syntax Error: Starred expression cannot be used here +24 | {x: *y, z: *a} + | + + + | +22 | # Star expression not allowed here +23 | {*x: y, z: a, *b: c} +24 | {x: *y, z: *a} + | ^^ Syntax Error: Starred expression cannot be used here + | + + + | +22 | # Star expression not allowed here +23 | {*x: y, z: a, *b: c} +24 | {x: *y, z: *a} + | ^^ Syntax Error: Starred expression cannot be used here + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__emoji_identifiers.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__emoji_identifiers.py.snap.new new file mode 100644 index 0000000000..67a101cdb3 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__emoji_identifiers.py.snap.new @@ -0,0 +1,139 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/emoji_identifiers.py +--- +## AST + +``` +Module( + ModModule { + range: 0..64, + body: [ + Assign( + StmtAssign { + range: 0..5, + targets: [ + Name( + ExprName { + range: 0..1, + id: "a", + ctx: Store, + }, + ), + ], + value: Name( + ExprName { + range: 5..5, + id: "", + ctx: Invalid, + }, + ), + }, + ), + Assign( + StmtAssign { + range: 32..37, + targets: [ + Name( + ExprName { + range: 32..33, + id: "a", + ctx: Store, + }, + ), + ], + value: Name( + ExprName { + range: 37..37, + id: "", + ctx: Invalid, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 42..43, + value: UnaryOp( + ExprUnaryOp { + range: 42..43, + op: UAdd, + operand: Name( + ExprName { + range: 43..43, + id: "", + ctx: Invalid, + }, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | a = (🐶 + | ^^ Syntax Error: Got unexpected token 🐶 +2 | # comment 🐶 +3 | ) + | + + + | +1 | a = (🐶 +2 | # comment 🐶 +3 | ) + | ^ Syntax Error: Expected a statement +4 | +5 | a = (🐶 + + | + + + | +1 | a = (🐶 +2 | # comment 🐶 +3 | ) + | ^ Syntax Error: Expected a statement +4 | +5 | a = (🐶 + +6 | # comment + | + + + | +3 | ) +4 | +5 | a = (🐶 + + | ^^ Syntax Error: Got unexpected token 🐶 +6 | # comment +7 | 🐶) + | + + + | +5 | a = (🐶 + +6 | # comment +7 | 🐶) + | ^^ Syntax Error: Got unexpected token 🐶 + | + + + | +5 | a = (🐶 + +6 | # comment +7 | 🐶) + | ^ Syntax Error: Expected a statement + | + + + | +5 | a = (🐶 + +6 | # comment +7 | 🐶) + | ^ Syntax Error: Expected a statement + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__if__missing_orelse_expr_0.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__if__missing_orelse_expr_0.py.snap.new new file mode 100644 index 0000000000..adf2b1ba1d --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__if__missing_orelse_expr_0.py.snap.new @@ -0,0 +1,85 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/if/missing_orelse_expr_0.py +--- +## AST + +``` +Module( + ModModule { + range: 0..88, + body: [ + Expr( + StmtExpr { + range: 53..67, + value: If( + ExprIf { + range: 53..67, + test: Name( + ExprName { + range: 58..62, + id: "expr", + ctx: Load, + }, + ), + body: Name( + ExprName { + range: 53..54, + id: "x", + ctx: Load, + }, + ), + orelse: Name( + ExprName { + range: 67..67, + id: "", + ctx: Invalid, + }, + ), + }, + ), + }, + ), + FunctionDef( + StmtFunctionDef { + range: 69..88, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 73..76, + }, + type_params: None, + parameters: Parameters { + range: 76..78, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 84..88, + }, + ), + ], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | # Missing orelse expression, followed by a statement +2 | x if expr else + | ^ Syntax Error: Expected an expression +3 | +4 | def foo(): +5 | pass + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__if__missing_orelse_expr_1.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__if__missing_orelse_expr_1.py.snap.new new file mode 100644 index 0000000000..93d7d48d65 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__if__missing_orelse_expr_1.py.snap.new @@ -0,0 +1,83 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/if/missing_orelse_expr_1.py +--- +## AST + +``` +Module( + ModModule { + range: 0..76, + body: [ + Expr( + StmtExpr { + range: 55..69, + value: If( + ExprIf { + range: 55..69, + test: Name( + ExprName { + range: 60..64, + id: "expr", + ctx: Load, + }, + ), + body: Name( + ExprName { + range: 55..56, + id: "x", + ctx: Load, + }, + ), + orelse: Name( + ExprName { + range: 69..69, + id: "", + ctx: Invalid, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 71..76, + value: BinOp( + ExprBinOp { + range: 71..76, + left: NumberLiteral( + ExprNumberLiteral { + range: 71..72, + value: Int( + 1, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 75..76, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | # Missing orelse expression, followed by an expression +2 | x if expr else + | ^ Syntax Error: Expected an expression +3 | +4 | 1 + 1 + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__if__missing_test_expr_0.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__if__missing_test_expr_0.py.snap.new new file mode 100644 index 0000000000..b7f7c6fa82 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__if__missing_test_expr_0.py.snap.new @@ -0,0 +1,85 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/if/missing_test_expr_0.py +--- +## AST + +``` +Module( + ModModule { + range: 0..76, + body: [ + Expr( + StmtExpr { + range: 51..55, + value: If( + ExprIf { + range: 51..55, + test: Name( + ExprName { + range: 55..55, + id: "", + ctx: Invalid, + }, + ), + body: Name( + ExprName { + range: 51..52, + id: "x", + ctx: Load, + }, + ), + orelse: Name( + ExprName { + range: 55..55, + id: "", + ctx: Invalid, + }, + ), + }, + ), + }, + ), + FunctionDef( + StmtFunctionDef { + range: 57..76, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 61..64, + }, + type_params: None, + parameters: Parameters { + range: 64..66, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 72..76, + }, + ), + ], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | # Missing test expression, followed by a statement +2 | x if + | ^ Syntax Error: Expected an expression +3 | +4 | def foo(): +5 | pass + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__if__missing_test_expr_1.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__if__missing_test_expr_1.py.snap.new new file mode 100644 index 0000000000..5e41946610 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__if__missing_test_expr_1.py.snap.new @@ -0,0 +1,83 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/if/missing_test_expr_1.py +--- +## AST + +``` +Module( + ModModule { + range: 0..64, + body: [ + Expr( + StmtExpr { + range: 53..57, + value: If( + ExprIf { + range: 53..57, + test: Name( + ExprName { + range: 57..57, + id: "", + ctx: Invalid, + }, + ), + body: Name( + ExprName { + range: 53..54, + id: "x", + ctx: Load, + }, + ), + orelse: Name( + ExprName { + range: 57..57, + id: "", + ctx: Invalid, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 59..64, + value: BinOp( + ExprBinOp { + range: 59..64, + left: NumberLiteral( + ExprNumberLiteral { + range: 59..60, + value: Int( + 1, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 63..64, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | # Missing test expression, followed by an expression +2 | x if + | ^ Syntax Error: Expected an expression +3 | +4 | 1 + 1 + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__if__recover.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__if__recover.py.snap.new new file mode 100644 index 0000000000..0fe3e25873 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__if__recover.py.snap.new @@ -0,0 +1,361 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/if/recover.py +--- +## AST + +``` +Module( + ModModule { + range: 0..215, + body: [ + Expr( + StmtExpr { + range: 26..43, + value: If( + ExprIf { + range: 26..43, + test: Starred( + ExprStarred { + range: 31..36, + value: Name( + ExprName { + range: 32..36, + id: "expr", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + body: Name( + ExprName { + range: 26..27, + id: "x", + ctx: Load, + }, + ), + orelse: Name( + ExprName { + range: 42..43, + id: "y", + ctx: Load, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 44..67, + value: If( + ExprIf { + range: 44..67, + test: Lambda( + ExprLambda { + range: 49..60, + parameters: Some( + Parameters { + range: 56..57, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 56..57, + parameter: Parameter { + range: 56..57, + name: Identifier { + id: "x", + range: 56..57, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: Name( + ExprName { + range: 59..60, + id: "x", + ctx: Load, + }, + ), + }, + ), + body: Name( + ExprName { + range: 44..45, + id: "x", + ctx: Load, + }, + ), + orelse: Name( + ExprName { + range: 66..67, + id: "y", + ctx: Load, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 68..87, + value: If( + ExprIf { + range: 68..87, + test: Yield( + ExprYield { + range: 73..80, + value: Some( + Name( + ExprName { + range: 79..80, + id: "x", + ctx: Load, + }, + ), + ), + }, + ), + body: Name( + ExprName { + range: 68..69, + id: "x", + ctx: Load, + }, + ), + orelse: Name( + ExprName { + range: 86..87, + id: "y", + ctx: Load, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 88..112, + value: If( + ExprIf { + range: 88..112, + test: YieldFrom( + ExprYieldFrom { + range: 93..105, + value: Name( + ExprName { + range: 104..105, + id: "x", + ctx: Load, + }, + ), + }, + ), + body: Name( + ExprName { + range: 88..89, + id: "x", + ctx: Load, + }, + ), + orelse: Name( + ExprName { + range: 111..112, + id: "y", + ctx: Load, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 142..164, + value: If( + ExprIf { + range: 142..164, + test: Name( + ExprName { + range: 147..151, + id: "expr", + ctx: Load, + }, + ), + body: Name( + ExprName { + range: 142..143, + id: "x", + ctx: Load, + }, + ), + orelse: Starred( + ExprStarred { + range: 157..164, + value: Name( + ExprName { + range: 158..164, + id: "orelse", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 165..187, + value: If( + ExprIf { + range: 165..187, + test: Name( + ExprName { + range: 170..174, + id: "expr", + ctx: Load, + }, + ), + body: Name( + ExprName { + range: 165..166, + id: "x", + ctx: Load, + }, + ), + orelse: Yield( + ExprYield { + range: 180..187, + value: Some( + Name( + ExprName { + range: 186..187, + id: "y", + ctx: Load, + }, + ), + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 188..215, + value: If( + ExprIf { + range: 188..215, + test: Name( + ExprName { + range: 193..197, + id: "expr", + ctx: Load, + }, + ), + body: Name( + ExprName { + range: 188..189, + id: "x", + ctx: Load, + }, + ), + orelse: YieldFrom( + ExprYieldFrom { + range: 203..215, + value: Name( + ExprName { + range: 214..215, + id: "y", + ctx: Load, + }, + ), + }, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | # Invalid test expression +2 | x if *expr else y + | ^^^^^ Syntax Error: Starred expression cannot be used here +3 | x if lambda x: x else y +4 | x if yield x else y + | + + + | +1 | # Invalid test expression +2 | x if *expr else y +3 | x if lambda x: x else y + | ^^^^^^^^^^^ Syntax Error: Lambda expression cannot be used here +4 | x if yield x else y +5 | x if yield from x else y + | + + + | +2 | x if *expr else y +3 | x if lambda x: x else y +4 | x if yield x else y + | ^^^^^^^ Syntax Error: Yield expression cannot be used here +5 | x if yield from x else y + | + + + | +3 | x if lambda x: x else y +4 | x if yield x else y +5 | x if yield from x else y + | ^^^^^^^^^^^^ Syntax Error: Yield expression cannot be used here +6 | +7 | # Invalid orelse expression + | + + + | + 7 | # Invalid orelse expression + 8 | x if expr else *orelse + | ^^^^^^^ Syntax Error: Starred expression cannot be used here + 9 | x if expr else yield y +10 | x if expr else yield from y + | + + + | + 7 | # Invalid orelse expression + 8 | x if expr else *orelse + 9 | x if expr else yield y + | ^^^^^^^ Syntax Error: Yield expression cannot be used here +10 | x if expr else yield from y + | + + + | + 8 | x if expr else *orelse + 9 | x if expr else yield y +10 | x if expr else yield from y + | ^^^^^^^^^^^^ Syntax Error: Yield expression cannot be used here + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__lambda_default_parameters.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__lambda_default_parameters.py.snap.new new file mode 100644 index 0000000000..593428af9d --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__lambda_default_parameters.py.snap.new @@ -0,0 +1,96 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/lambda_default_parameters.py +--- +## AST + +``` +Module( + ModModule { + range: 0..20, + body: [ + Expr( + StmtExpr { + range: 0..20, + value: Lambda( + ExprLambda { + range: 0..20, + parameters: Some( + Parameters { + range: 7..17, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 7..8, + parameter: Parameter { + range: 7..8, + name: Identifier { + id: "a", + range: 7..8, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 10..14, + parameter: Parameter { + range: 10..11, + name: Identifier { + id: "b", + range: 10..11, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 12..14, + value: Int( + 20, + ), + }, + ), + ), + }, + ParameterWithDefault { + range: 16..17, + parameter: Parameter { + range: 16..17, + name: Identifier { + id: "c", + range: 16..17, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: NumberLiteral( + ExprNumberLiteral { + range: 19..20, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | lambda a, b=20, c: 1 + | ^ Syntax Error: Parameter without a default cannot follow a parameter with a default + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__lambda_duplicate_parameters.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__lambda_duplicate_parameters.py.snap.new new file mode 100644 index 0000000000..247eae4307 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__lambda_duplicate_parameters.py.snap.new @@ -0,0 +1,339 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/lambda_duplicate_parameters.py +--- +## AST + +``` +Module( + ModModule { + range: 0..91, + body: [ + Expr( + StmtExpr { + range: 0..14, + value: Lambda( + ExprLambda { + range: 0..14, + parameters: Some( + Parameters { + range: 7..11, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 7..8, + parameter: Parameter { + range: 7..8, + name: Identifier { + id: "a", + range: 7..8, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 10..11, + parameter: Parameter { + range: 10..11, + name: Identifier { + id: "a", + range: 10..11, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: NumberLiteral( + ExprNumberLiteral { + range: 13..14, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 16..33, + value: Lambda( + ExprLambda { + range: 16..33, + parameters: Some( + Parameters { + range: 23..30, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 23..24, + parameter: Parameter { + range: 23..24, + name: Identifier { + id: "a", + range: 23..24, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [ + ParameterWithDefault { + range: 29..30, + parameter: Parameter { + range: 29..30, + name: Identifier { + id: "a", + range: 29..30, + }, + annotation: None, + }, + default: None, + }, + ], + kwarg: None, + }, + ), + body: NumberLiteral( + ExprNumberLiteral { + range: 32..33, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 35..52, + value: Lambda( + ExprLambda { + range: 35..52, + parameters: Some( + Parameters { + range: 42..49, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 42..43, + parameter: Parameter { + range: 42..43, + name: Identifier { + id: "a", + range: 42..43, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 45..49, + parameter: Parameter { + range: 45..46, + name: Identifier { + id: "a", + range: 45..46, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 47..49, + value: Int( + 20, + ), + }, + ), + ), + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: NumberLiteral( + ExprNumberLiteral { + range: 51..52, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 54..69, + value: Lambda( + ExprLambda { + range: 54..69, + parameters: Some( + Parameters { + range: 61..66, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 61..62, + parameter: Parameter { + range: 61..62, + name: Identifier { + id: "a", + range: 61..62, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: Some( + Parameter { + range: 64..66, + name: Identifier { + id: "a", + range: 65..66, + }, + annotation: None, + }, + ), + kwonlyargs: [], + kwarg: None, + }, + ), + body: NumberLiteral( + ExprNumberLiteral { + range: 68..69, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 71..90, + value: Lambda( + ExprLambda { + range: 71..90, + parameters: Some( + Parameters { + range: 78..87, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 78..79, + parameter: Parameter { + range: 78..79, + name: Identifier { + id: "a", + range: 78..79, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: Some( + Parameter { + range: 84..87, + name: Identifier { + id: "a", + range: 86..87, + }, + annotation: None, + }, + ), + }, + ), + body: NumberLiteral( + ExprNumberLiteral { + range: 89..90, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | lambda a, a: 1 + | ^ Syntax Error: Duplicate parameter "a" +2 | +3 | lambda a, *, a: 1 + | + + + | +1 | lambda a, a: 1 +2 | +3 | lambda a, *, a: 1 + | ^ Syntax Error: Duplicate parameter "a" +4 | +5 | lambda a, a=20: 1 + | + + + | +3 | lambda a, *, a: 1 +4 | +5 | lambda a, a=20: 1 + | ^ Syntax Error: Duplicate parameter "a" +6 | +7 | lambda a, *a: 1 + | + + + | +5 | lambda a, a=20: 1 +6 | +7 | lambda a, *a: 1 + | ^ Syntax Error: Duplicate parameter "a" +8 | +9 | lambda a, *, **a: 1 + | + + + | +7 | lambda a, *a: 1 +8 | +9 | lambda a, *, **a: 1 + | ^^^ Syntax Error: Expected one or more keyword parameter after '*' separator + | + + + | +7 | lambda a, *a: 1 +8 | +9 | lambda a, *, **a: 1 + | ^ Syntax Error: Duplicate parameter "a" + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__list__comprehension.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__list__comprehension.py.snap.new new file mode 100644 index 0000000000..c045bdbe37 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__list__comprehension.py.snap.new @@ -0,0 +1,797 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/list/comprehension.py +--- +## AST + +``` +Module( + ModModule { + range: 0..376, + body: [ + Expr( + StmtExpr { + range: 33..48, + value: ListComp( + ExprListComp { + range: 33..48, + elt: Starred( + ExprStarred { + range: 34..36, + value: Name( + ExprName { + range: 35..36, + id: "x", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 37..47, + target: Name( + ExprName { + range: 41..42, + id: "x", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 46..47, + id: "y", + ctx: Load, + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 67..81, + value: ListComp( + ExprListComp { + range: 67..81, + elt: Name( + ExprName { + range: 68..69, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 70..80, + target: NumberLiteral( + ExprNumberLiteral { + range: 74..75, + value: Int( + 1, + ), + }, + ), + iter: Name( + ExprName { + range: 79..80, + id: "y", + ctx: Load, + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 82..98, + value: ListComp( + ExprListComp { + range: 82..98, + elt: Name( + ExprName { + range: 83..84, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 85..97, + target: StringLiteral( + ExprStringLiteral { + range: 89..92, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 89..92, + value: "a", + flags: StringLiteralFlags { + quote_style: Single, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + iter: Name( + ExprName { + range: 96..97, + id: "y", + ctx: Load, + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 99..118, + value: ListComp( + ExprListComp { + range: 99..118, + elt: Name( + ExprName { + range: 100..101, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 102..117, + target: Call( + ExprCall { + range: 106..112, + func: Name( + ExprName { + range: 106..110, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 110..112, + args: [], + keywords: [], + }, + }, + ), + iter: Name( + ExprName { + range: 116..117, + id: "y", + ctx: Load, + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 119..138, + value: ListComp( + ExprListComp { + range: 119..138, + elt: Name( + ExprName { + range: 120..121, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 122..137, + target: Set( + ExprSet { + range: 126..132, + elts: [ + Name( + ExprName { + range: 127..128, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 130..131, + id: "b", + ctx: Load, + }, + ), + ], + }, + ), + iter: Name( + ExprName { + range: 136..137, + id: "y", + ctx: Load, + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 155..170, + value: ListComp( + ExprListComp { + range: 155..170, + elt: Name( + ExprName { + range: 156..157, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 158..169, + target: Name( + ExprName { + range: 162..163, + id: "x", + ctx: Store, + }, + ), + iter: Starred( + ExprStarred { + range: 167..169, + value: Name( + ExprName { + range: 168..169, + id: "y", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 171..191, + value: ListComp( + ExprListComp { + range: 171..191, + elt: Name( + ExprName { + range: 172..173, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 174..190, + target: Name( + ExprName { + range: 178..179, + id: "x", + ctx: Store, + }, + ), + iter: Yield( + ExprYield { + range: 183..190, + value: Some( + Name( + ExprName { + range: 189..190, + id: "y", + ctx: Load, + }, + ), + ), + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 192..217, + value: ListComp( + ExprListComp { + range: 192..217, + elt: Name( + ExprName { + range: 193..194, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 195..216, + target: Name( + ExprName { + range: 199..200, + id: "x", + ctx: Store, + }, + ), + iter: YieldFrom( + ExprYieldFrom { + range: 204..216, + value: Name( + ExprName { + range: 215..216, + id: "y", + ctx: Load, + }, + ), + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 218..242, + value: ListComp( + ExprListComp { + range: 218..242, + elt: Name( + ExprName { + range: 219..220, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 221..241, + target: Name( + ExprName { + range: 225..226, + id: "x", + ctx: Store, + }, + ), + iter: Lambda( + ExprLambda { + range: 230..241, + parameters: Some( + Parameters { + range: 237..238, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 237..238, + parameter: Parameter { + range: 237..238, + name: Identifier { + id: "y", + range: 237..238, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: Name( + ExprName { + range: 240..241, + id: "y", + ctx: Load, + }, + ), + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 257..280, + value: ListComp( + ExprListComp { + range: 257..280, + elt: Name( + ExprName { + range: 258..259, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 260..279, + target: Name( + ExprName { + range: 264..265, + id: "x", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 269..273, + id: "data", + ctx: Load, + }, + ), + ifs: [ + Starred( + ExprStarred { + range: 277..279, + value: Name( + ExprName { + range: 278..279, + id: "y", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 281..309, + value: ListComp( + ExprListComp { + range: 281..309, + elt: Name( + ExprName { + range: 282..283, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 284..308, + target: Name( + ExprName { + range: 288..289, + id: "x", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 293..297, + id: "data", + ctx: Load, + }, + ), + ifs: [ + Yield( + ExprYield { + range: 301..308, + value: Some( + Name( + ExprName { + range: 307..308, + id: "y", + ctx: Load, + }, + ), + ), + }, + ), + ], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 310..343, + value: ListComp( + ExprListComp { + range: 310..343, + elt: Name( + ExprName { + range: 311..312, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 313..342, + target: Name( + ExprName { + range: 317..318, + id: "x", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 322..326, + id: "data", + ctx: Load, + }, + ), + ifs: [ + YieldFrom( + ExprYieldFrom { + range: 330..342, + value: Name( + ExprName { + range: 341..342, + id: "y", + ctx: Load, + }, + ), + }, + ), + ], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 344..376, + value: ListComp( + ExprListComp { + range: 344..376, + elt: Name( + ExprName { + range: 345..346, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 347..375, + target: Name( + ExprName { + range: 351..352, + id: "x", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 356..360, + id: "data", + ctx: Load, + }, + ), + ifs: [ + Lambda( + ExprLambda { + range: 364..375, + parameters: Some( + Parameters { + range: 371..372, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 371..372, + parameter: Parameter { + range: 371..372, + name: Identifier { + id: "y", + range: 371..372, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: Name( + ExprName { + range: 374..375, + id: "y", + ctx: Load, + }, + ), + }, + ), + ], + is_async: false, + }, + ], + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | # Iterable unpacking not allowed +2 | [*x for x in y] + | ^^ Syntax Error: Iterable unpacking cannot be used in a comprehension +3 | +4 | # Invalid target + | + + + | +4 | # Invalid target +5 | [x for 1 in y] + | ^ Syntax Error: Invalid assignment target +6 | [x for 'a' in y] +7 | [x for call() in y] + | + + + | +4 | # Invalid target +5 | [x for 1 in y] +6 | [x for 'a' in y] + | ^^^ Syntax Error: Invalid assignment target +7 | [x for call() in y] +8 | [x for {a, b} in y] + | + + + | +5 | [x for 1 in y] +6 | [x for 'a' in y] +7 | [x for call() in y] + | ^^^^^^ Syntax Error: Invalid assignment target +8 | [x for {a, b} in y] + | + + + | + 6 | [x for 'a' in y] + 7 | [x for call() in y] + 8 | [x for {a, b} in y] + | ^^^^^^ Syntax Error: Invalid assignment target + 9 | +10 | # Invalid iter + | + + + | +10 | # Invalid iter +11 | [x for x in *y] + | ^^ Syntax Error: Starred expression cannot be used here +12 | [x for x in yield y] +13 | [x for x in yield from y] + | + + + | +10 | # Invalid iter +11 | [x for x in *y] +12 | [x for x in yield y] + | ^^^^^^^ Syntax Error: Yield expression cannot be used here +13 | [x for x in yield from y] +14 | [x for x in lambda y: y] + | + + + | +11 | [x for x in *y] +12 | [x for x in yield y] +13 | [x for x in yield from y] + | ^^^^^^^^^^^^ Syntax Error: Yield expression cannot be used here +14 | [x for x in lambda y: y] + | + + + | +12 | [x for x in yield y] +13 | [x for x in yield from y] +14 | [x for x in lambda y: y] + | ^^^^^^^^^^^ Syntax Error: Lambda expression cannot be used here +15 | +16 | # Invalid if + | + + + | +16 | # Invalid if +17 | [x for x in data if *y] + | ^^ Syntax Error: Starred expression cannot be used here +18 | [x for x in data if yield y] +19 | [x for x in data if yield from y] + | + + + | +16 | # Invalid if +17 | [x for x in data if *y] +18 | [x for x in data if yield y] + | ^^^^^^^ Syntax Error: Yield expression cannot be used here +19 | [x for x in data if yield from y] +20 | [x for x in data if lambda y: y] + | + + + | +17 | [x for x in data if *y] +18 | [x for x in data if yield y] +19 | [x for x in data if yield from y] + | ^^^^^^^^^^^^ Syntax Error: Yield expression cannot be used here +20 | [x for x in data if lambda y: y] + | + + + | +18 | [x for x in data if yield y] +19 | [x for x in data if yield from y] +20 | [x for x in data if lambda y: y] + | ^^^^^^^^^^^ Syntax Error: Lambda expression cannot be used here + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__list__missing_closing_bracket_0.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__list__missing_closing_bracket_0.py.snap.new new file mode 100644 index 0000000000..7bdbfce6af --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__list__missing_closing_bracket_0.py.snap.new @@ -0,0 +1,44 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/list/missing_closing_bracket_0.py +--- +## AST + +``` +Module( + ModModule { + range: 0..43, + body: [ + Expr( + StmtExpr { + range: 42..43, + value: List( + ExprList { + range: 42..43, + elts: [ + Name( + ExprName { + range: 43..43, + id: "", + ctx: Invalid, + }, + ), + ], + ctx: Load, + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | # Missing closing bracket 0: No elements +2 | +3 | [ + | Syntax Error: unexpected EOF while parsing + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__list__missing_closing_bracket_1.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__list__missing_closing_bracket_1.py.snap.new new file mode 100644 index 0000000000..61cd90d686 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__list__missing_closing_bracket_1.py.snap.new @@ -0,0 +1,57 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/list/missing_closing_bracket_1.py +--- +## AST + +``` +Module( + ModModule { + range: 0..133, + body: [ + Expr( + StmtExpr { + range: 125..133, + value: List( + ExprList { + range: 125..133, + elts: [ + BinOp( + ExprBinOp { + range: 128..133, + left: Name( + ExprName { + range: 128..129, + id: "x", + ctx: Load, + }, + ), + op: Add, + right: Name( + ExprName { + range: 132..133, + id: "y", + ctx: Load, + }, + ), + }, + ), + ], + ctx: Load, + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +4 | [ +5 | +6 | x + y + | Syntax Error: unexpected EOF while parsing + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__list__missing_closing_bracket_2.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__list__missing_closing_bracket_2.py.snap.new new file mode 100644 index 0000000000..6629151ef7 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__list__missing_closing_bracket_2.py.snap.new @@ -0,0 +1,65 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/list/missing_closing_bracket_2.py +--- +## AST + +``` +Module( + ModModule { + range: 0..141, + body: [ + Expr( + StmtExpr { + range: 131..141, + value: List( + ExprList { + range: 131..141, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 132..133, + value: Int( + 1, + ), + }, + ), + BinOp( + ExprBinOp { + range: 136..141, + left: Name( + ExprName { + range: 136..137, + id: "x", + ctx: Load, + }, + ), + op: Add, + right: Name( + ExprName { + range: 140..141, + id: "y", + ctx: Load, + }, + ), + }, + ), + ], + ctx: Load, + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +4 | [1, +5 | +6 | x + y + | Syntax Error: unexpected EOF while parsing + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__list__missing_closing_bracket_3.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__list__missing_closing_bracket_3.py.snap.new new file mode 100644 index 0000000000..298e1b21fe --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__list__missing_closing_bracket_3.py.snap.new @@ -0,0 +1,84 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/list/missing_closing_bracket_3.py +--- +## AST + +``` +Module( + ModModule { + range: 0..140, + body: [ + Expr( + StmtExpr { + range: 114..119, + value: List( + ExprList { + range: 114..119, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 115..116, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 118..119, + value: Int( + 2, + ), + }, + ), + ], + ctx: Load, + }, + ), + }, + ), + FunctionDef( + StmtFunctionDef { + range: 121..140, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 125..128, + }, + type_params: None, + parameters: Parameters { + range: 128..130, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 136..140, + }, + ), + ], + }, + ), + ], + }, +) +``` +## Errors + + | +2 | # token starts a statement. +3 | +4 | [1, 2 + | ^ Syntax Error: Expected ']', found newline +5 | +6 | def foo(): +7 | pass + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__list__recover.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__list__recover.py.snap.new new file mode 100644 index 0000000000..8abb482576 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__list__recover.py.snap.new @@ -0,0 +1,315 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/list/recover.py +--- +## AST + +``` +Module( + ModModule { + range: 0..208, + body: [ + Expr( + StmtExpr { + range: 82..85, + value: List( + ExprList { + range: 82..85, + elts: [ + Name( + ExprName { + range: 83..83, + id: "", + ctx: Invalid, + }, + ), + ], + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 87..93, + value: List( + ExprList { + range: 87..93, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 88..89, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 91..92, + value: Int( + 2, + ), + }, + ), + ], + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 95..100, + value: List( + ExprList { + range: 95..100, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 96..97, + value: Int( + 1, + ), + }, + ), + ], + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 118..123, + value: List( + ExprList { + range: 118..123, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 119..120, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 121..122, + value: Int( + 2, + ), + }, + ), + ], + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 156..162, + value: List( + ExprList { + range: 156..162, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 157..158, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 160..161, + value: Int( + 2, + ), + }, + ), + ], + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 185..194, + value: List( + ExprList { + range: 185..194, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 186..187, + value: Int( + 1, + ), + }, + ), + BinOp( + ExprBinOp { + range: 189..192, + left: Name( + ExprName { + range: 189..190, + id: "x", + ctx: Load, + }, + ), + op: Add, + right: Name( + ExprName { + range: 192..192, + id: "", + ctx: Invalid, + }, + ), + }, + ), + ], + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 196..202, + value: List( + ExprList { + range: 196..202, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 197..198, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 200..201, + value: Int( + 2, + ), + }, + ), + ], + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 204..207, + value: List( + ExprList { + range: 204..207, + elts: [ + Starred( + ExprStarred { + range: 205..206, + value: Name( + ExprName { + range: 206..206, + id: "", + ctx: Invalid, + }, + ), + ctx: Load, + }, + ), + ], + ctx: Load, + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | # Test cases for list expressions where the parser recovers from a syntax error. +2 | +3 | [,] + | ^ Syntax Error: Expected an expression +4 | +5 | [1,,2] + | + + + | +3 | [,] +4 | +5 | [1,,2] + | ^ Syntax Error: Expected an expression or a ']' +6 | +7 | [1,,] + | + + + | +5 | [1,,2] +6 | +7 | [1,,] + | ^ Syntax Error: Expected an expression or a ']' +8 | +9 | # Missing comma + | + + + | + 9 | # Missing comma +10 | [1 2] + | ^ Syntax Error: Expected ',', found int +11 | +12 | # Dictionary element in a list + | + + + | +12 | # Dictionary element in a list +13 | [1: 2] + | ^ Syntax Error: Expected an expression or a ']' +14 | +15 | # Missing expression + | + + + | +15 | # Missing expression +16 | [1, x + ] + | ^ Syntax Error: Expected an expression +17 | +18 | [1; 2] + | + + + | +16 | [1, x + ] +17 | +18 | [1; 2] + | ^ Syntax Error: Expected an expression or a ']' +19 | +20 | [*] + | + + + | +18 | [1; 2] +19 | +20 | [*] + | ^ Syntax Error: Expected an expression + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__list__star_expression_precedence.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__list__star_expression_precedence.py.snap.new new file mode 100644 index 0000000000..7a912a4681 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__list__star_expression_precedence.py.snap.new @@ -0,0 +1,468 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/list/star_expression_precedence.py +--- +## AST + +``` +Module( + ModModule { + range: 0..200, + body: [ + Expr( + StmtExpr { + range: 84..93, + value: List( + ExprList { + range: 84..93, + elts: [ + Starred( + ExprStarred { + range: 86..88, + value: Name( + ExprName { + range: 87..88, + id: "x", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + Name( + ExprName { + range: 91..92, + id: "y", + ctx: Load, + }, + ), + ], + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 94..106, + value: List( + ExprList { + range: 94..106, + elts: [ + Starred( + ExprStarred { + range: 95..102, + value: Compare( + ExprCompare { + range: 96..102, + left: Name( + ExprName { + range: 96..97, + id: "x", + ctx: Load, + }, + ), + ops: [ + In, + ], + comparators: [ + Name( + ExprName { + range: 101..102, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + ctx: Load, + }, + ), + Name( + ExprName { + range: 104..105, + id: "z", + ctx: Load, + }, + ), + ], + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 107..118, + value: List( + ExprList { + range: 107..118, + elts: [ + Starred( + ExprStarred { + range: 108..114, + value: UnaryOp( + ExprUnaryOp { + range: 109..114, + op: Not, + operand: Name( + ExprName { + range: 113..114, + id: "x", + ctx: Load, + }, + ), + }, + ), + ctx: Load, + }, + ), + Name( + ExprName { + range: 116..117, + id: "z", + ctx: Load, + }, + ), + ], + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 119..132, + value: List( + ExprList { + range: 119..132, + elts: [ + Starred( + ExprStarred { + range: 120..128, + value: BoolOp( + ExprBoolOp { + range: 121..128, + op: And, + values: [ + Name( + ExprName { + range: 121..122, + id: "x", + ctx: Load, + }, + ), + Name( + ExprName { + range: 127..128, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + ctx: Load, + }, + ), + Name( + ExprName { + range: 130..131, + id: "z", + ctx: Load, + }, + ), + ], + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 133..145, + value: List( + ExprList { + range: 133..145, + elts: [ + Starred( + ExprStarred { + range: 134..141, + value: BoolOp( + ExprBoolOp { + range: 135..141, + op: Or, + values: [ + Name( + ExprName { + range: 135..136, + id: "x", + ctx: Load, + }, + ), + Name( + ExprName { + range: 140..141, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + ctx: Load, + }, + ), + Name( + ExprName { + range: 143..144, + id: "z", + ctx: Load, + }, + ), + ], + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 146..168, + value: List( + ExprList { + range: 146..168, + elts: [ + Starred( + ExprStarred { + range: 147..164, + value: If( + ExprIf { + range: 148..164, + test: BooleanLiteral( + ExprBooleanLiteral { + range: 153..157, + value: true, + }, + ), + body: Name( + ExprName { + range: 148..149, + id: "x", + ctx: Load, + }, + ), + orelse: Name( + ExprName { + range: 163..164, + id: "y", + ctx: Load, + }, + ), + }, + ), + ctx: Load, + }, + ), + Name( + ExprName { + range: 166..167, + id: "z", + ctx: Load, + }, + ), + ], + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 169..186, + value: List( + ExprList { + range: 169..186, + elts: [ + Starred( + ExprStarred { + range: 170..182, + value: Lambda( + ExprLambda { + range: 171..182, + parameters: Some( + Parameters { + range: 178..179, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 178..179, + parameter: Parameter { + range: 178..179, + name: Identifier { + id: "x", + range: 178..179, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: Name( + ExprName { + range: 181..182, + id: "x", + ctx: Load, + }, + ), + }, + ), + ctx: Load, + }, + ), + Name( + ExprName { + range: 184..185, + id: "z", + ctx: Load, + }, + ), + ], + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 187..199, + value: List( + ExprList { + range: 187..199, + elts: [ + Named( + ExprNamed { + range: 188..195, + target: Starred( + ExprStarred { + range: 188..190, + value: Name( + ExprName { + range: 189..190, + id: "x", + ctx: Store, + }, + ), + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 194..195, + value: Int( + 2, + ), + }, + ), + }, + ), + Name( + ExprName { + range: 197..198, + id: "z", + ctx: Load, + }, + ), + ], + ctx: Load, + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | # For list expression, the minimum binding power of star expression is bitwise or. +2 | +3 | [(*x), y] + | ^^ Syntax Error: Starred expression cannot be used here +4 | [*x in y, z] +5 | [*not x, z] + | + + + | +3 | [(*x), y] +4 | [*x in y, z] + | ^^^^^^ Syntax Error: Comparison expression cannot be used here +5 | [*not x, z] +6 | [*x and y, z] + | + + + | +3 | [(*x), y] +4 | [*x in y, z] +5 | [*not x, z] + | ^^^^^ Syntax Error: Boolean expression cannot be used here +6 | [*x and y, z] +7 | [*x or y, z] + | + + + | +4 | [*x in y, z] +5 | [*not x, z] +6 | [*x and y, z] + | ^^^^^^^ Syntax Error: Boolean expression cannot be used here +7 | [*x or y, z] +8 | [*x if True else y, z] + | + + + | +5 | [*not x, z] +6 | [*x and y, z] +7 | [*x or y, z] + | ^^^^^^ Syntax Error: Boolean expression cannot be used here +8 | [*x if True else y, z] +9 | [*lambda x: x, z] + | + + + | + 6 | [*x and y, z] + 7 | [*x or y, z] + 8 | [*x if True else y, z] + | ^^^^^^^^^^^^^^^^ Syntax Error: Conditional expression cannot be used here + 9 | [*lambda x: x, z] +10 | [*x := 2, z] + | + + + | + 7 | [*x or y, z] + 8 | [*x if True else y, z] + 9 | [*lambda x: x, z] + | ^^^^^^^^^^^ Syntax Error: Lambda expression cannot be used here +10 | [*x := 2, z] + | + + + | + 8 | [*x if True else y, z] + 9 | [*lambda x: x, z] +10 | [*x := 2, z] + | ^^ Syntax Error: Assignment expression target must be an identifier + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__named__invalid_target.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__named__invalid_target.py.snap.new new file mode 100644 index 0000000000..1909a153cf --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__named__invalid_target.py.snap.new @@ -0,0 +1,212 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/named/invalid_target.py +--- +## AST + +``` +Module( + ModModule { + range: 0..109, + body: [ + Expr( + StmtExpr { + range: 58..68, + value: Named( + ExprNamed { + range: 59..67, + target: Attribute( + ExprAttribute { + range: 59..62, + value: Name( + ExprName { + range: 59..60, + id: "x", + ctx: Load, + }, + ), + attr: Identifier { + id: "y", + range: 61..62, + }, + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 66..67, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 69..80, + value: Named( + ExprNamed { + range: 70..79, + target: Subscript( + ExprSubscript { + range: 70..74, + value: Name( + ExprName { + range: 70..71, + id: "x", + ctx: Load, + }, + ), + slice: Name( + ExprName { + range: 72..73, + id: "y", + ctx: Load, + }, + ), + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 78..79, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 81..90, + value: Named( + ExprNamed { + range: 82..89, + target: Starred( + ExprStarred { + range: 82..84, + value: Name( + ExprName { + range: 83..84, + id: "x", + ctx: Store, + }, + ), + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 88..89, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 91..109, + value: Named( + ExprNamed { + range: 92..108, + target: List( + ExprList { + range: 92..98, + elts: [ + Name( + ExprName { + range: 93..94, + id: "x", + ctx: Store, + }, + ), + Name( + ExprName { + range: 96..97, + id: "y", + ctx: Store, + }, + ), + ], + ctx: Store, + }, + ), + value: List( + ExprList { + range: 102..108, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 103..104, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 106..107, + value: Int( + 2, + ), + }, + ), + ], + ctx: Load, + }, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | # Assignment expression target can only be an identifier +2 | +3 | (x.y := 1) + | ^^^ Syntax Error: Assignment expression target must be an identifier +4 | (x[y] := 1) +5 | (*x := 1) + | + + + | +3 | (x.y := 1) +4 | (x[y] := 1) + | ^^^^ Syntax Error: Assignment expression target must be an identifier +5 | (*x := 1) +6 | ([x, y] := [1, 2]) + | + + + | +3 | (x.y := 1) +4 | (x[y] := 1) +5 | (*x := 1) + | ^^ Syntax Error: Assignment expression target must be an identifier +6 | ([x, y] := [1, 2]) + | + + + | +4 | (x[y] := 1) +5 | (*x := 1) +6 | ([x, y] := [1, 2]) + | ^^^^^^ Syntax Error: Assignment expression target must be an identifier + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__named__missing_expression_0.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__named__missing_expression_0.py.snap.new new file mode 100644 index 0000000000..9c609e5dbd --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__named__missing_expression_0.py.snap.new @@ -0,0 +1,44 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/named/missing_expression_0.py +--- +## AST + +``` +Module( + ModModule { + range: 0..75, + body: [ + Expr( + StmtExpr { + range: 71..72, + value: Name( + ExprName { + range: 71..72, + id: "x", + ctx: Load, + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | # There are no parentheses, so this isn't parsed as named expression. +2 | +3 | x := + | ^^ Syntax Error: Expected a statement + | + + + | +1 | # There are no parentheses, so this isn't parsed as named expression. +2 | +3 | x := + | Syntax Error: Expected a statement + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__named__missing_expression_1.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__named__missing_expression_1.py.snap.new new file mode 100644 index 0000000000..b1bd782ada --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__named__missing_expression_1.py.snap.new @@ -0,0 +1,48 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/named/missing_expression_1.py +--- +## AST + +``` +Module( + ModModule { + range: 0..33, + body: [ + Expr( + StmtExpr { + range: 28..33, + value: Named( + ExprNamed { + range: 29..33, + target: Name( + ExprName { + range: 29..30, + id: "x", + ctx: Store, + }, + ), + value: Name( + ExprName { + range: 33..33, + id: "", + ctx: Invalid, + }, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | # EOF after the `:=` token +2 | +3 | (x := + | Syntax Error: unexpected EOF while parsing + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__named__missing_expression_2.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__named__missing_expression_2.py.snap.new new file mode 100644 index 0000000000..c6be122d57 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__named__missing_expression_2.py.snap.new @@ -0,0 +1,103 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/named/missing_expression_2.py +--- +## AST + +``` +Module( + ModModule { + range: 0..87, + body: [ + Expr( + StmtExpr { + range: 61..71, + value: Named( + ExprNamed { + range: 62..71, + target: Name( + ExprName { + range: 62..63, + id: "x", + ctx: Store, + }, + ), + value: Name( + ExprName { + range: 68..71, + id: "def", + ctx: Load, + }, + ), + }, + ), + }, + ), + AnnAssign( + StmtAnnAssign { + range: 72..87, + target: Call( + ExprCall { + range: 72..77, + func: Name( + ExprName { + range: 72..75, + id: "foo", + ctx: Load, + }, + ), + arguments: Arguments { + range: 75..77, + args: [], + keywords: [], + }, + }, + ), + annotation: Name( + ExprName { + range: 83..87, + id: "pass", + ctx: Load, + }, + ), + value: None, + simple: false, + }, + ), + ], + }, +) +``` +## Errors + + | +3 | (x := +4 | +5 | def foo(): + | ^^^ Syntax Error: Expected an identifier, but found a keyword 'def' that cannot be used here +6 | pass + | + + + | +3 | (x := +4 | +5 | def foo(): + | ^^^ Syntax Error: Expected ')', found name +6 | pass + | + + + | +5 | def foo(): +6 | pass + | ^^^^ Syntax Error: Expected an identifier, but found a keyword 'pass' that cannot be used here + | + + + | +5 | def foo(): +6 | pass + | Syntax Error: unexpected EOF while parsing + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__named__missing_expression_3.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__named__missing_expression_3.py.snap.new new file mode 100644 index 0000000000..b541a33dc4 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__named__missing_expression_3.py.snap.new @@ -0,0 +1,61 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/named/missing_expression_3.py +--- +## AST + +``` +Module( + ModModule { + range: 0..112, + body: [ + Expr( + StmtExpr { + range: 100..112, + value: Named( + ExprNamed { + range: 101..112, + target: Name( + ExprName { + range: 101..102, + id: "x", + ctx: Store, + }, + ), + value: BinOp( + ExprBinOp { + range: 107..112, + left: Name( + ExprName { + range: 107..108, + id: "x", + ctx: Load, + }, + ), + op: Add, + right: Name( + ExprName { + range: 111..112, + id: "y", + ctx: Load, + }, + ), + }, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +4 | (x := +5 | +6 | x + y + | Syntax Error: unexpected EOF while parsing + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__named__missing_expression_4.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__named__missing_expression_4.py.snap.new new file mode 100644 index 0000000000..d8b96a9135 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__named__missing_expression_4.py.snap.new @@ -0,0 +1,75 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/named/missing_expression_4.py +--- +## AST + +``` +Module( + ModModule { + range: 0..78, + body: [ + Expr( + StmtExpr { + range: 64..71, + value: Named( + ExprNamed { + range: 65..69, + target: Name( + ExprName { + range: 65..66, + id: "x", + ctx: Store, + }, + ), + value: Name( + ExprName { + range: 69..69, + id: "", + ctx: Invalid, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 73..78, + value: BinOp( + ExprBinOp { + range: 73..78, + left: Name( + ExprName { + range: 73..74, + id: "x", + ctx: Load, + }, + ), + op: Add, + right: Name( + ExprName { + range: 77..78, + id: "y", + ctx: Load, + }, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | # No expression on the right side of the assignment expression +2 | +3 | (x := ) + | ^ Syntax Error: Expected an expression +4 | +5 | x + y + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__parenthesized__generator.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__parenthesized__generator.py.snap.new new file mode 100644 index 0000000000..16f7d61148 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__parenthesized__generator.py.snap.new @@ -0,0 +1,145 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/parenthesized/generator.py +--- +## AST + +``` +Module( + ModModule { + range: 0..36, + body: [ + Expr( + StmtExpr { + range: 0..15, + value: Generator( + ExprGenerator { + range: 0..15, + elt: Starred( + ExprStarred { + range: 1..3, + value: Name( + ExprName { + range: 2..3, + id: "x", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 4..14, + target: Name( + ExprName { + range: 8..9, + id: "x", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 13..14, + id: "y", + ctx: Load, + }, + ), + ifs: [], + is_async: false, + }, + ], + parenthesized: true, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 16..24, + value: Tuple( + ExprTuple { + range: 16..24, + elts: [ + Named( + ExprNamed { + range: 17..23, + target: Name( + ExprName { + range: 17..18, + id: "x", + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 22..23, + value: Int( + 1, + ), + }, + ), + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + For( + StmtFor { + range: 25..35, + is_async: false, + target: Name( + ExprName { + range: 29..30, + id: "x", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 34..35, + id: "y", + ctx: Load, + }, + ), + body: [], + orelse: [], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | (*x for x in y) + | ^^ Syntax Error: Iterable unpacking cannot be used in a comprehension +2 | (x := 1, for x in y) + | + + + | +1 | (*x for x in y) +2 | (x := 1, for x in y) + | ^^^ Syntax Error: Expected ')', found 'for' + | + + + | +1 | (*x for x in y) +2 | (x := 1, for x in y) + | ^ Syntax Error: Expected ':', found ')' + | + + + | +1 | (*x for x in y) +2 | (x := 1, for x in y) + | Syntax Error: Expected a statement + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__parenthesized__missing_closing_paren_0.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__parenthesized__missing_closing_paren_0.py.snap.new new file mode 100644 index 0000000000..47b2231d8f --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__parenthesized__missing_closing_paren_0.py.snap.new @@ -0,0 +1,36 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/parenthesized/missing_closing_paren_0.py +--- +## AST + +``` +Module( + ModModule { + range: 0..47, + body: [ + Expr( + StmtExpr { + range: 46..47, + value: Name( + ExprName { + range: 47..47, + id: "", + ctx: Invalid, + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | # Missing closing parentheses 0: No elements +2 | +3 | ( + | Syntax Error: unexpected EOF while parsing + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__parenthesized__missing_closing_paren_1.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__parenthesized__missing_closing_paren_1.py.snap.new new file mode 100644 index 0000000000..b4c3dd2ca5 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__parenthesized__missing_closing_paren_1.py.snap.new @@ -0,0 +1,49 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/parenthesized/missing_closing_paren_1.py +--- +## AST + +``` +Module( + ModModule { + range: 0..137, + body: [ + Expr( + StmtExpr { + range: 129..137, + value: BinOp( + ExprBinOp { + range: 132..137, + left: Name( + ExprName { + range: 132..133, + id: "x", + ctx: Load, + }, + ), + op: Add, + right: Name( + ExprName { + range: 136..137, + id: "y", + ctx: Load, + }, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +4 | ( +5 | +6 | x + y + | Syntax Error: unexpected EOF while parsing + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__parenthesized__missing_closing_paren_2.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__parenthesized__missing_closing_paren_2.py.snap.new new file mode 100644 index 0000000000..7a8dddf21c --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__parenthesized__missing_closing_paren_2.py.snap.new @@ -0,0 +1,66 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/parenthesized/missing_closing_paren_2.py +--- +## AST + +``` +Module( + ModModule { + range: 0..146, + body: [ + Expr( + StmtExpr { + range: 136..146, + value: Tuple( + ExprTuple { + range: 136..146, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 137..138, + value: Int( + 1, + ), + }, + ), + BinOp( + ExprBinOp { + range: 141..146, + left: Name( + ExprName { + range: 141..142, + id: "x", + ctx: Load, + }, + ), + op: Add, + right: Name( + ExprName { + range: 145..146, + id: "y", + ctx: Load, + }, + ), + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +4 | (1, +5 | +6 | x + y + | Syntax Error: unexpected EOF while parsing + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__parenthesized__missing_closing_paren_3.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__parenthesized__missing_closing_paren_3.py.snap.new new file mode 100644 index 0000000000..af97995092 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__parenthesized__missing_closing_paren_3.py.snap.new @@ -0,0 +1,85 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/parenthesized/missing_closing_paren_3.py +--- +## AST + +``` +Module( + ModModule { + range: 0..144, + body: [ + Expr( + StmtExpr { + range: 118..123, + value: Tuple( + ExprTuple { + range: 118..123, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 119..120, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 122..123, + value: Int( + 2, + ), + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + FunctionDef( + StmtFunctionDef { + range: 125..144, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 129..132, + }, + type_params: None, + parameters: Parameters { + range: 132..134, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 140..144, + }, + ), + ], + }, + ), + ], + }, +) +``` +## Errors + + | +2 | # token starts a statement. +3 | +4 | (1, 2 + | ^ Syntax Error: Expected ')', found newline +5 | +6 | def foo(): +7 | pass + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__parenthesized__parenthesized.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__parenthesized__parenthesized.py.snap.new new file mode 100644 index 0000000000..8e1f0e5643 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__parenthesized__parenthesized.py.snap.new @@ -0,0 +1,75 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/parenthesized/parenthesized.py +--- +## AST + +``` +Module( + ModModule { + range: 0..125, + body: [ + Expr( + StmtExpr { + range: 66..70, + value: Starred( + ExprStarred { + range: 67..69, + value: Name( + ExprName { + range: 68..69, + id: "x", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 119..120, + value: Name( + ExprName { + range: 119..120, + id: "x", + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 124..125, + value: NumberLiteral( + ExprNumberLiteral { + range: 124..125, + value: Int( + 1, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | # Starred expression isn't allowed in a parenthesized expression. +2 | (*x) + | ^^ Syntax Error: Starred expression cannot be used here +3 | +4 | # Unparenthesized named expression is allowed. + | + + + | +4 | # Unparenthesized named expression is allowed. +5 | x := 1 + | ^^ Syntax Error: Expected a statement + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__parenthesized__tuple.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__parenthesized__tuple.py.snap.new new file mode 100644 index 0000000000..fdad2a179d --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__parenthesized__tuple.py.snap.new @@ -0,0 +1,391 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/parenthesized/tuple.py +--- +## AST + +``` +Module( + ModModule { + range: 0..267, + body: [ + Expr( + StmtExpr { + range: 83..86, + value: Tuple( + ExprTuple { + range: 83..86, + elts: [ + Name( + ExprName { + range: 84..84, + id: "", + ctx: Invalid, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 88..94, + value: Tuple( + ExprTuple { + range: 88..94, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 89..90, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 92..93, + value: Int( + 2, + ), + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 96..101, + value: Tuple( + ExprTuple { + range: 96..101, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 97..98, + value: Int( + 1, + ), + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 119..121, + value: NumberLiteral( + ExprNumberLiteral { + range: 120..121, + value: Int( + 1, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 122..123, + value: NumberLiteral( + ExprNumberLiteral { + range: 122..123, + value: Int( + 2, + ), + }, + ), + }, + ), + AnnAssign( + StmtAnnAssign { + range: 157..162, + target: NumberLiteral( + ExprNumberLiteral { + range: 158..159, + value: Int( + 1, + ), + }, + ), + annotation: NumberLiteral( + ExprNumberLiteral { + range: 161..162, + value: Int( + 2, + ), + }, + ), + value: None, + simple: false, + }, + ), + Expr( + StmtExpr { + range: 186..195, + value: Tuple( + ExprTuple { + range: 186..195, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 187..188, + value: Int( + 1, + ), + }, + ), + BinOp( + ExprBinOp { + range: 190..193, + left: Name( + ExprName { + range: 190..191, + id: "x", + ctx: Load, + }, + ), + op: Add, + right: Name( + ExprName { + range: 193..193, + id: "", + ctx: Invalid, + }, + ), + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 197..199, + value: NumberLiteral( + ExprNumberLiteral { + range: 198..199, + value: Int( + 1, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 201..202, + value: NumberLiteral( + ExprNumberLiteral { + range: 201..202, + value: Int( + 2, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 255..267, + value: Tuple( + ExprTuple { + range: 255..267, + elts: [ + Name( + ExprName { + range: 255..256, + id: "x", + ctx: Load, + }, + ), + Name( + ExprName { + range: 258..259, + id: "y", + ctx: Load, + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 263..264, + value: Int( + 2, + ), + }, + ), + Name( + ExprName { + range: 266..267, + id: "z", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | # Test cases for tuple expressions where the parser recovers from a syntax error. +2 | +3 | (,) + | ^ Syntax Error: Expected an expression +4 | +5 | (1,,2) + | + + + | +3 | (,) +4 | +5 | (1,,2) + | ^ Syntax Error: Expected an expression or a ')' +6 | +7 | (1,,) + | + + + | +5 | (1,,2) +6 | +7 | (1,,) + | ^ Syntax Error: Expected an expression or a ')' +8 | +9 | # Missing comma + | + + + | + 9 | # Missing comma +10 | (1 2) + | ^ Syntax Error: Expected ')', found int +11 | +12 | # Dictionary element in a list + | + + + | + 9 | # Missing comma +10 | (1 2) + | ^ Syntax Error: Expected a statement +11 | +12 | # Dictionary element in a list + | + + + | + 9 | # Missing comma +10 | (1 2) + | ^ Syntax Error: Expected a statement +11 | +12 | # Dictionary element in a list +13 | (1: 2) + | + + + | +12 | # Dictionary element in a list +13 | (1: 2) + | ^ Syntax Error: Expected ')', found ':' +14 | +15 | # Missing expression + | + + + | +12 | # Dictionary element in a list +13 | (1: 2) + | ^ Syntax Error: Invalid annotated assignment target +14 | +15 | # Missing expression + | + + + | +12 | # Dictionary element in a list +13 | (1: 2) + | ^ Syntax Error: Expected a statement +14 | +15 | # Missing expression + | + + + | +12 | # Dictionary element in a list +13 | (1: 2) + | ^ Syntax Error: Expected a statement +14 | +15 | # Missing expression +16 | (1, x + ) + | + + + | +15 | # Missing expression +16 | (1, x + ) + | ^ Syntax Error: Expected an expression +17 | +18 | (1; 2) + | + + + | +16 | (1, x + ) +17 | +18 | (1; 2) + | ^ Syntax Error: Expected ')', found ';' +19 | +20 | # Unparenthesized named expression is not allowed + | + + + | +16 | (1, x + ) +17 | +18 | (1; 2) + | ^ Syntax Error: Expected a statement +19 | +20 | # Unparenthesized named expression is not allowed + | + + + | +16 | (1, x + ) +17 | +18 | (1; 2) + | ^ Syntax Error: Expected a statement +19 | +20 | # Unparenthesized named expression is not allowed +21 | x, y := 2, z + | + + + | +20 | # Unparenthesized named expression is not allowed +21 | x, y := 2, z + | ^^ Syntax Error: Expected ',', found ':=' + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__parenthesized__tuple_starred_expr.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__parenthesized__tuple_starred_expr.py.snap.new new file mode 100644 index 0000000000..b6a368efc3 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__parenthesized__tuple_starred_expr.py.snap.new @@ -0,0 +1,1381 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/parenthesized/tuple_starred_expr.py +--- +## AST + +``` +Module( + ModModule { + range: 0..536, + body: [ + Expr( + StmtExpr { + range: 161..182, + value: Tuple( + ExprTuple { + range: 161..182, + elts: [ + Starred( + ExprStarred { + range: 162..169, + value: Compare( + ExprCompare { + range: 163..169, + left: Name( + ExprName { + range: 163..164, + id: "x", + ctx: Load, + }, + ), + ops: [ + In, + ], + comparators: [ + Name( + ExprName { + range: 168..169, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + ctx: Load, + }, + ), + Name( + ExprName { + range: 171..172, + id: "z", + ctx: Load, + }, + ), + Starred( + ExprStarred { + range: 174..181, + value: Compare( + ExprCompare { + range: 175..181, + left: Name( + ExprName { + range: 175..176, + id: "x", + ctx: Load, + }, + ), + ops: [ + In, + ], + comparators: [ + Name( + ExprName { + range: 180..181, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 183..202, + value: Tuple( + ExprTuple { + range: 183..202, + elts: [ + Starred( + ExprStarred { + range: 184..190, + value: UnaryOp( + ExprUnaryOp { + range: 185..190, + op: Not, + operand: Name( + ExprName { + range: 189..190, + id: "x", + ctx: Load, + }, + ), + }, + ), + ctx: Load, + }, + ), + Name( + ExprName { + range: 192..193, + id: "z", + ctx: Load, + }, + ), + Starred( + ExprStarred { + range: 195..201, + value: UnaryOp( + ExprUnaryOp { + range: 196..201, + op: Not, + operand: Name( + ExprName { + range: 200..201, + id: "x", + ctx: Load, + }, + ), + }, + ), + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 203..226, + value: Tuple( + ExprTuple { + range: 203..226, + elts: [ + Starred( + ExprStarred { + range: 204..212, + value: BoolOp( + ExprBoolOp { + range: 205..212, + op: And, + values: [ + Name( + ExprName { + range: 205..206, + id: "x", + ctx: Load, + }, + ), + Name( + ExprName { + range: 211..212, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + ctx: Load, + }, + ), + Name( + ExprName { + range: 214..215, + id: "z", + ctx: Load, + }, + ), + Starred( + ExprStarred { + range: 217..225, + value: BoolOp( + ExprBoolOp { + range: 218..225, + op: And, + values: [ + Name( + ExprName { + range: 218..219, + id: "x", + ctx: Load, + }, + ), + Name( + ExprName { + range: 224..225, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 227..248, + value: Tuple( + ExprTuple { + range: 227..248, + elts: [ + Starred( + ExprStarred { + range: 228..235, + value: BoolOp( + ExprBoolOp { + range: 229..235, + op: Or, + values: [ + Name( + ExprName { + range: 229..230, + id: "x", + ctx: Load, + }, + ), + Name( + ExprName { + range: 234..235, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + ctx: Load, + }, + ), + Name( + ExprName { + range: 237..238, + id: "z", + ctx: Load, + }, + ), + Starred( + ExprStarred { + range: 240..247, + value: BoolOp( + ExprBoolOp { + range: 241..247, + op: Or, + values: [ + Name( + ExprName { + range: 241..242, + id: "x", + ctx: Load, + }, + ), + Name( + ExprName { + range: 246..247, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 249..290, + value: Tuple( + ExprTuple { + range: 249..290, + elts: [ + Starred( + ExprStarred { + range: 250..267, + value: If( + ExprIf { + range: 251..267, + test: BooleanLiteral( + ExprBooleanLiteral { + range: 256..260, + value: true, + }, + ), + body: Name( + ExprName { + range: 251..252, + id: "x", + ctx: Load, + }, + ), + orelse: Name( + ExprName { + range: 266..267, + id: "y", + ctx: Load, + }, + ), + }, + ), + ctx: Load, + }, + ), + Name( + ExprName { + range: 269..270, + id: "z", + ctx: Load, + }, + ), + Starred( + ExprStarred { + range: 272..289, + value: If( + ExprIf { + range: 273..289, + test: BooleanLiteral( + ExprBooleanLiteral { + range: 278..282, + value: true, + }, + ), + body: Name( + ExprName { + range: 273..274, + id: "x", + ctx: Load, + }, + ), + orelse: Name( + ExprName { + range: 288..289, + id: "y", + ctx: Load, + }, + ), + }, + ), + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 291..322, + value: Tuple( + ExprTuple { + range: 291..322, + elts: [ + Starred( + ExprStarred { + range: 292..304, + value: Lambda( + ExprLambda { + range: 293..304, + parameters: Some( + Parameters { + range: 300..301, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 300..301, + parameter: Parameter { + range: 300..301, + name: Identifier { + id: "x", + range: 300..301, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: Name( + ExprName { + range: 303..304, + id: "x", + ctx: Load, + }, + ), + }, + ), + ctx: Load, + }, + ), + Name( + ExprName { + range: 306..307, + id: "z", + ctx: Load, + }, + ), + Starred( + ExprStarred { + range: 309..321, + value: Lambda( + ExprLambda { + range: 310..321, + parameters: Some( + Parameters { + range: 317..318, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 317..318, + parameter: Parameter { + range: 317..318, + name: Identifier { + id: "x", + range: 317..318, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: Name( + ExprName { + range: 320..321, + id: "x", + ctx: Load, + }, + ), + }, + ), + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 323..344, + value: Tuple( + ExprTuple { + range: 323..344, + elts: [ + Named( + ExprNamed { + range: 324..331, + target: Starred( + ExprStarred { + range: 324..326, + value: Name( + ExprName { + range: 325..326, + id: "x", + ctx: Store, + }, + ), + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 330..331, + value: Int( + 2, + ), + }, + ), + }, + ), + Name( + ExprName { + range: 333..334, + id: "z", + ctx: Load, + }, + ), + Named( + ExprNamed { + range: 336..343, + target: Starred( + ExprStarred { + range: 336..338, + value: Name( + ExprName { + range: 337..338, + id: "x", + ctx: Store, + }, + ), + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 342..343, + value: Int( + 2, + ), + }, + ), + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 367..386, + value: Tuple( + ExprTuple { + range: 367..386, + elts: [ + Starred( + ExprStarred { + range: 367..374, + value: Compare( + ExprCompare { + range: 368..374, + left: Name( + ExprName { + range: 368..369, + id: "x", + ctx: Load, + }, + ), + ops: [ + In, + ], + comparators: [ + Name( + ExprName { + range: 373..374, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + ctx: Load, + }, + ), + Name( + ExprName { + range: 376..377, + id: "z", + ctx: Load, + }, + ), + Starred( + ExprStarred { + range: 379..386, + value: Compare( + ExprCompare { + range: 380..386, + left: Name( + ExprName { + range: 380..381, + id: "x", + ctx: Load, + }, + ), + ops: [ + In, + ], + comparators: [ + Name( + ExprName { + range: 385..386, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 387..404, + value: Tuple( + ExprTuple { + range: 387..404, + elts: [ + Starred( + ExprStarred { + range: 387..393, + value: UnaryOp( + ExprUnaryOp { + range: 388..393, + op: Not, + operand: Name( + ExprName { + range: 392..393, + id: "x", + ctx: Load, + }, + ), + }, + ), + ctx: Load, + }, + ), + Name( + ExprName { + range: 395..396, + id: "z", + ctx: Load, + }, + ), + Starred( + ExprStarred { + range: 398..404, + value: UnaryOp( + ExprUnaryOp { + range: 399..404, + op: Not, + operand: Name( + ExprName { + range: 403..404, + id: "x", + ctx: Load, + }, + ), + }, + ), + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 405..426, + value: Tuple( + ExprTuple { + range: 405..426, + elts: [ + Starred( + ExprStarred { + range: 405..413, + value: BoolOp( + ExprBoolOp { + range: 406..413, + op: And, + values: [ + Name( + ExprName { + range: 406..407, + id: "x", + ctx: Load, + }, + ), + Name( + ExprName { + range: 412..413, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + ctx: Load, + }, + ), + Name( + ExprName { + range: 415..416, + id: "z", + ctx: Load, + }, + ), + Starred( + ExprStarred { + range: 418..426, + value: BoolOp( + ExprBoolOp { + range: 419..426, + op: And, + values: [ + Name( + ExprName { + range: 419..420, + id: "x", + ctx: Load, + }, + ), + Name( + ExprName { + range: 425..426, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 427..446, + value: Tuple( + ExprTuple { + range: 427..446, + elts: [ + Starred( + ExprStarred { + range: 427..434, + value: BoolOp( + ExprBoolOp { + range: 428..434, + op: Or, + values: [ + Name( + ExprName { + range: 428..429, + id: "x", + ctx: Load, + }, + ), + Name( + ExprName { + range: 433..434, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + ctx: Load, + }, + ), + Name( + ExprName { + range: 436..437, + id: "z", + ctx: Load, + }, + ), + Starred( + ExprStarred { + range: 439..446, + value: BoolOp( + ExprBoolOp { + range: 440..446, + op: Or, + values: [ + Name( + ExprName { + range: 440..441, + id: "x", + ctx: Load, + }, + ), + Name( + ExprName { + range: 445..446, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 447..486, + value: Tuple( + ExprTuple { + range: 447..486, + elts: [ + Starred( + ExprStarred { + range: 447..464, + value: If( + ExprIf { + range: 448..464, + test: BooleanLiteral( + ExprBooleanLiteral { + range: 453..457, + value: true, + }, + ), + body: Name( + ExprName { + range: 448..449, + id: "x", + ctx: Load, + }, + ), + orelse: Name( + ExprName { + range: 463..464, + id: "y", + ctx: Load, + }, + ), + }, + ), + ctx: Load, + }, + ), + Name( + ExprName { + range: 466..467, + id: "z", + ctx: Load, + }, + ), + Starred( + ExprStarred { + range: 469..486, + value: If( + ExprIf { + range: 470..486, + test: BooleanLiteral( + ExprBooleanLiteral { + range: 475..479, + value: true, + }, + ), + body: Name( + ExprName { + range: 470..471, + id: "x", + ctx: Load, + }, + ), + orelse: Name( + ExprName { + range: 485..486, + id: "y", + ctx: Load, + }, + ), + }, + ), + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 487..516, + value: Tuple( + ExprTuple { + range: 487..516, + elts: [ + Starred( + ExprStarred { + range: 487..499, + value: Lambda( + ExprLambda { + range: 488..499, + parameters: Some( + Parameters { + range: 495..496, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 495..496, + parameter: Parameter { + range: 495..496, + name: Identifier { + id: "x", + range: 495..496, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: Name( + ExprName { + range: 498..499, + id: "x", + ctx: Load, + }, + ), + }, + ), + ctx: Load, + }, + ), + Name( + ExprName { + range: 501..502, + id: "z", + ctx: Load, + }, + ), + Starred( + ExprStarred { + range: 504..516, + value: Lambda( + ExprLambda { + range: 505..516, + parameters: Some( + Parameters { + range: 512..513, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 512..513, + parameter: Parameter { + range: 512..513, + name: Identifier { + id: "x", + range: 512..513, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: Name( + ExprName { + range: 515..516, + id: "x", + ctx: Load, + }, + ), + }, + ), + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 517..519, + value: Starred( + ExprStarred { + range: 517..519, + value: Name( + ExprName { + range: 518..519, + id: "x", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 523..536, + value: Tuple( + ExprTuple { + range: 523..536, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 523..524, + value: Int( + 2, + ), + }, + ), + Name( + ExprName { + range: 526..527, + id: "z", + ctx: Load, + }, + ), + Starred( + ExprStarred { + range: 529..531, + value: Name( + ExprName { + range: 530..531, + id: "x", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 535..536, + value: Int( + 2, + ), + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +2 | # Test the first and any other element as the there are two separate calls. +3 | +4 | (*x in y, z, *x in y) + | ^^^^^^ Syntax Error: Comparison expression cannot be used here +5 | (*not x, z, *not x) +6 | (*x and y, z, *x and y) + | + + + | +2 | # Test the first and any other element as the there are two separate calls. +3 | +4 | (*x in y, z, *x in y) + | ^^^^^^ Syntax Error: Comparison expression cannot be used here +5 | (*not x, z, *not x) +6 | (*x and y, z, *x and y) + | + + + | +4 | (*x in y, z, *x in y) +5 | (*not x, z, *not x) + | ^^^^^ Syntax Error: Boolean expression cannot be used here +6 | (*x and y, z, *x and y) +7 | (*x or y, z, *x or y) + | + + + | +4 | (*x in y, z, *x in y) +5 | (*not x, z, *not x) + | ^^^^^ Syntax Error: Boolean expression cannot be used here +6 | (*x and y, z, *x and y) +7 | (*x or y, z, *x or y) + | + + + | +4 | (*x in y, z, *x in y) +5 | (*not x, z, *not x) +6 | (*x and y, z, *x and y) + | ^^^^^^^ Syntax Error: Boolean expression cannot be used here +7 | (*x or y, z, *x or y) +8 | (*x if True else y, z, *x if True else y) + | + + + | +4 | (*x in y, z, *x in y) +5 | (*not x, z, *not x) +6 | (*x and y, z, *x and y) + | ^^^^^^^ Syntax Error: Boolean expression cannot be used here +7 | (*x or y, z, *x or y) +8 | (*x if True else y, z, *x if True else y) + | + + + | +5 | (*not x, z, *not x) +6 | (*x and y, z, *x and y) +7 | (*x or y, z, *x or y) + | ^^^^^^ Syntax Error: Boolean expression cannot be used here +8 | (*x if True else y, z, *x if True else y) +9 | (*lambda x: x, z, *lambda x: x) + | + + + | +5 | (*not x, z, *not x) +6 | (*x and y, z, *x and y) +7 | (*x or y, z, *x or y) + | ^^^^^^ Syntax Error: Boolean expression cannot be used here +8 | (*x if True else y, z, *x if True else y) +9 | (*lambda x: x, z, *lambda x: x) + | + + + | + 6 | (*x and y, z, *x and y) + 7 | (*x or y, z, *x or y) + 8 | (*x if True else y, z, *x if True else y) + | ^^^^^^^^^^^^^^^^ Syntax Error: Conditional expression cannot be used here + 9 | (*lambda x: x, z, *lambda x: x) +10 | (*x := 2, z, *x := 2) + | + + + | + 6 | (*x and y, z, *x and y) + 7 | (*x or y, z, *x or y) + 8 | (*x if True else y, z, *x if True else y) + | ^^^^^^^^^^^^^^^^ Syntax Error: Conditional expression cannot be used here + 9 | (*lambda x: x, z, *lambda x: x) +10 | (*x := 2, z, *x := 2) + | + + + | + 7 | (*x or y, z, *x or y) + 8 | (*x if True else y, z, *x if True else y) + 9 | (*lambda x: x, z, *lambda x: x) + | ^^^^^^^^^^^ Syntax Error: Lambda expression cannot be used here +10 | (*x := 2, z, *x := 2) + | + + + | + 7 | (*x or y, z, *x or y) + 8 | (*x if True else y, z, *x if True else y) + 9 | (*lambda x: x, z, *lambda x: x) + | ^^^^^^^^^^^ Syntax Error: Lambda expression cannot be used here +10 | (*x := 2, z, *x := 2) + | + + + | + 8 | (*x if True else y, z, *x if True else y) + 9 | (*lambda x: x, z, *lambda x: x) +10 | (*x := 2, z, *x := 2) + | ^^ Syntax Error: Assignment expression target must be an identifier + | + + + | + 8 | (*x if True else y, z, *x if True else y) + 9 | (*lambda x: x, z, *lambda x: x) +10 | (*x := 2, z, *x := 2) + | ^^ Syntax Error: Assignment expression target must be an identifier + | + + + | +13 | # Non-parenthesized +14 | *x in y, z, *x in y + | ^^^^^^ Syntax Error: Comparison expression cannot be used here +15 | *not x, z, *not x +16 | *x and y, z, *x and y + | + + + | +13 | # Non-parenthesized +14 | *x in y, z, *x in y + | ^^^^^^ Syntax Error: Comparison expression cannot be used here +15 | *not x, z, *not x +16 | *x and y, z, *x and y + | + + + | +13 | # Non-parenthesized +14 | *x in y, z, *x in y +15 | *not x, z, *not x + | ^^^^^ Syntax Error: Boolean expression cannot be used here +16 | *x and y, z, *x and y +17 | *x or y, z, *x or y + | + + + | +13 | # Non-parenthesized +14 | *x in y, z, *x in y +15 | *not x, z, *not x + | ^^^^^ Syntax Error: Boolean expression cannot be used here +16 | *x and y, z, *x and y +17 | *x or y, z, *x or y + | + + + | +14 | *x in y, z, *x in y +15 | *not x, z, *not x +16 | *x and y, z, *x and y + | ^^^^^^^ Syntax Error: Boolean expression cannot be used here +17 | *x or y, z, *x or y +18 | *x if True else y, z, *x if True else y + | + + + | +14 | *x in y, z, *x in y +15 | *not x, z, *not x +16 | *x and y, z, *x and y + | ^^^^^^^ Syntax Error: Boolean expression cannot be used here +17 | *x or y, z, *x or y +18 | *x if True else y, z, *x if True else y + | + + + | +15 | *not x, z, *not x +16 | *x and y, z, *x and y +17 | *x or y, z, *x or y + | ^^^^^^ Syntax Error: Boolean expression cannot be used here +18 | *x if True else y, z, *x if True else y +19 | *lambda x: x, z, *lambda x: x + | + + + | +15 | *not x, z, *not x +16 | *x and y, z, *x and y +17 | *x or y, z, *x or y + | ^^^^^^ Syntax Error: Boolean expression cannot be used here +18 | *x if True else y, z, *x if True else y +19 | *lambda x: x, z, *lambda x: x + | + + + | +16 | *x and y, z, *x and y +17 | *x or y, z, *x or y +18 | *x if True else y, z, *x if True else y + | ^^^^^^^^^^^^^^^^ Syntax Error: Conditional expression cannot be used here +19 | *lambda x: x, z, *lambda x: x +20 | *x := 2, z, *x := 2 + | + + + | +16 | *x and y, z, *x and y +17 | *x or y, z, *x or y +18 | *x if True else y, z, *x if True else y + | ^^^^^^^^^^^^^^^^ Syntax Error: Conditional expression cannot be used here +19 | *lambda x: x, z, *lambda x: x +20 | *x := 2, z, *x := 2 + | + + + | +17 | *x or y, z, *x or y +18 | *x if True else y, z, *x if True else y +19 | *lambda x: x, z, *lambda x: x + | ^^^^^^^^^^^ Syntax Error: Lambda expression cannot be used here +20 | *x := 2, z, *x := 2 + | + + + | +17 | *x or y, z, *x or y +18 | *x if True else y, z, *x if True else y +19 | *lambda x: x, z, *lambda x: x + | ^^^^^^^^^^^ Syntax Error: Lambda expression cannot be used here +20 | *x := 2, z, *x := 2 + | + + + | +18 | *x if True else y, z, *x if True else y +19 | *lambda x: x, z, *lambda x: x +20 | *x := 2, z, *x := 2 + | ^^ Syntax Error: Expected a statement + | + + + | +18 | *x if True else y, z, *x if True else y +19 | *lambda x: x, z, *lambda x: x +20 | *x := 2, z, *x := 2 + | ^^ Syntax Error: Expected ',', found ':=' + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__set__comprehension.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__set__comprehension.py.snap.new new file mode 100644 index 0000000000..d5e059d8da --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__set__comprehension.py.snap.new @@ -0,0 +1,797 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/set/comprehension.py +--- +## AST + +``` +Module( + ModModule { + range: 0..377, + body: [ + Expr( + StmtExpr { + range: 33..48, + value: SetComp( + ExprSetComp { + range: 33..48, + elt: Starred( + ExprStarred { + range: 34..36, + value: Name( + ExprName { + range: 35..36, + id: "x", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 37..47, + target: Name( + ExprName { + range: 41..42, + id: "x", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 46..47, + id: "y", + ctx: Load, + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 67..81, + value: SetComp( + ExprSetComp { + range: 67..81, + elt: Name( + ExprName { + range: 68..69, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 70..80, + target: NumberLiteral( + ExprNumberLiteral { + range: 74..75, + value: Int( + 1, + ), + }, + ), + iter: Name( + ExprName { + range: 79..80, + id: "y", + ctx: Load, + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 82..98, + value: SetComp( + ExprSetComp { + range: 82..98, + elt: Name( + ExprName { + range: 83..84, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 85..97, + target: StringLiteral( + ExprStringLiteral { + range: 89..92, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 89..92, + value: "a", + flags: StringLiteralFlags { + quote_style: Single, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + iter: Name( + ExprName { + range: 96..97, + id: "y", + ctx: Load, + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 99..118, + value: SetComp( + ExprSetComp { + range: 99..118, + elt: Name( + ExprName { + range: 100..101, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 102..117, + target: Call( + ExprCall { + range: 106..112, + func: Name( + ExprName { + range: 106..110, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 110..112, + args: [], + keywords: [], + }, + }, + ), + iter: Name( + ExprName { + range: 116..117, + id: "y", + ctx: Load, + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 119..138, + value: SetComp( + ExprSetComp { + range: 119..138, + elt: Name( + ExprName { + range: 120..121, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 122..137, + target: Set( + ExprSet { + range: 126..132, + elts: [ + Name( + ExprName { + range: 127..128, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 130..131, + id: "b", + ctx: Load, + }, + ), + ], + }, + ), + iter: Name( + ExprName { + range: 136..137, + id: "y", + ctx: Load, + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 155..170, + value: SetComp( + ExprSetComp { + range: 155..170, + elt: Name( + ExprName { + range: 156..157, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 158..169, + target: Name( + ExprName { + range: 162..163, + id: "x", + ctx: Store, + }, + ), + iter: Starred( + ExprStarred { + range: 167..169, + value: Name( + ExprName { + range: 168..169, + id: "y", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 171..191, + value: SetComp( + ExprSetComp { + range: 171..191, + elt: Name( + ExprName { + range: 172..173, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 174..190, + target: Name( + ExprName { + range: 178..179, + id: "x", + ctx: Store, + }, + ), + iter: Yield( + ExprYield { + range: 183..190, + value: Some( + Name( + ExprName { + range: 189..190, + id: "y", + ctx: Load, + }, + ), + ), + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 192..217, + value: SetComp( + ExprSetComp { + range: 192..217, + elt: Name( + ExprName { + range: 193..194, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 195..216, + target: Name( + ExprName { + range: 199..200, + id: "x", + ctx: Store, + }, + ), + iter: YieldFrom( + ExprYieldFrom { + range: 204..216, + value: Name( + ExprName { + range: 215..216, + id: "y", + ctx: Load, + }, + ), + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 218..242, + value: SetComp( + ExprSetComp { + range: 218..242, + elt: Name( + ExprName { + range: 219..220, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 221..241, + target: Name( + ExprName { + range: 225..226, + id: "x", + ctx: Store, + }, + ), + iter: Lambda( + ExprLambda { + range: 230..241, + parameters: Some( + Parameters { + range: 237..238, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 237..238, + parameter: Parameter { + range: 237..238, + name: Identifier { + id: "y", + range: 237..238, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: Name( + ExprName { + range: 240..241, + id: "y", + ctx: Load, + }, + ), + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 257..280, + value: SetComp( + ExprSetComp { + range: 257..280, + elt: Name( + ExprName { + range: 258..259, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 260..279, + target: Name( + ExprName { + range: 264..265, + id: "x", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 269..273, + id: "data", + ctx: Load, + }, + ), + ifs: [ + Starred( + ExprStarred { + range: 277..279, + value: Name( + ExprName { + range: 278..279, + id: "y", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 281..309, + value: SetComp( + ExprSetComp { + range: 281..309, + elt: Name( + ExprName { + range: 282..283, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 284..308, + target: Name( + ExprName { + range: 288..289, + id: "x", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 293..297, + id: "data", + ctx: Load, + }, + ), + ifs: [ + Yield( + ExprYield { + range: 301..308, + value: Some( + Name( + ExprName { + range: 307..308, + id: "y", + ctx: Load, + }, + ), + ), + }, + ), + ], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 310..343, + value: SetComp( + ExprSetComp { + range: 310..343, + elt: Name( + ExprName { + range: 311..312, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 313..342, + target: Name( + ExprName { + range: 317..318, + id: "x", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 322..326, + id: "data", + ctx: Load, + }, + ), + ifs: [ + YieldFrom( + ExprYieldFrom { + range: 330..342, + value: Name( + ExprName { + range: 341..342, + id: "y", + ctx: Load, + }, + ), + }, + ), + ], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 344..376, + value: SetComp( + ExprSetComp { + range: 344..376, + elt: Name( + ExprName { + range: 345..346, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 347..375, + target: Name( + ExprName { + range: 351..352, + id: "x", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 356..360, + id: "data", + ctx: Load, + }, + ), + ifs: [ + Lambda( + ExprLambda { + range: 364..375, + parameters: Some( + Parameters { + range: 371..372, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 371..372, + parameter: Parameter { + range: 371..372, + name: Identifier { + id: "y", + range: 371..372, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: Name( + ExprName { + range: 374..375, + id: "y", + ctx: Load, + }, + ), + }, + ), + ], + is_async: false, + }, + ], + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | # Iterable unpacking not allowed +2 | {*x for x in y} + | ^^ Syntax Error: Iterable unpacking cannot be used in a comprehension +3 | +4 | # Invalid target + | + + + | +4 | # Invalid target +5 | {x for 1 in y} + | ^ Syntax Error: Invalid assignment target +6 | {x for 'a' in y} +7 | {x for call() in y} + | + + + | +4 | # Invalid target +5 | {x for 1 in y} +6 | {x for 'a' in y} + | ^^^ Syntax Error: Invalid assignment target +7 | {x for call() in y} +8 | {x for {a, b} in y} + | + + + | +5 | {x for 1 in y} +6 | {x for 'a' in y} +7 | {x for call() in y} + | ^^^^^^ Syntax Error: Invalid assignment target +8 | {x for {a, b} in y} + | + + + | + 6 | {x for 'a' in y} + 7 | {x for call() in y} + 8 | {x for {a, b} in y} + | ^^^^^^ Syntax Error: Invalid assignment target + 9 | +10 | # Invalid iter + | + + + | +10 | # Invalid iter +11 | {x for x in *y} + | ^^ Syntax Error: Starred expression cannot be used here +12 | {x for x in yield y} +13 | {x for x in yield from y} + | + + + | +10 | # Invalid iter +11 | {x for x in *y} +12 | {x for x in yield y} + | ^^^^^^^ Syntax Error: Yield expression cannot be used here +13 | {x for x in yield from y} +14 | {x for x in lambda y: y} + | + + + | +11 | {x for x in *y} +12 | {x for x in yield y} +13 | {x for x in yield from y} + | ^^^^^^^^^^^^ Syntax Error: Yield expression cannot be used here +14 | {x for x in lambda y: y} + | + + + | +12 | {x for x in yield y} +13 | {x for x in yield from y} +14 | {x for x in lambda y: y} + | ^^^^^^^^^^^ Syntax Error: Lambda expression cannot be used here +15 | +16 | # Invalid if + | + + + | +16 | # Invalid if +17 | {x for x in data if *y} + | ^^ Syntax Error: Starred expression cannot be used here +18 | {x for x in data if yield y} +19 | {x for x in data if yield from y} + | + + + | +16 | # Invalid if +17 | {x for x in data if *y} +18 | {x for x in data if yield y} + | ^^^^^^^ Syntax Error: Yield expression cannot be used here +19 | {x for x in data if yield from y} +20 | {x for x in data if lambda y: y} + | + + + | +17 | {x for x in data if *y} +18 | {x for x in data if yield y} +19 | {x for x in data if yield from y} + | ^^^^^^^^^^^^ Syntax Error: Yield expression cannot be used here +20 | {x for x in data if lambda y: y} + | + + + | +18 | {x for x in data if yield y} +19 | {x for x in data if yield from y} +20 | {x for x in data if lambda y: y} + | ^^^^^^^^^^^ Syntax Error: Lambda expression cannot be used here + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__set__missing_closing_curly_brace_0.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__set__missing_closing_curly_brace_0.py.snap.new new file mode 100644 index 0000000000..1ccac5eb0c --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__set__missing_closing_curly_brace_0.py.snap.new @@ -0,0 +1,43 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/set/missing_closing_curly_brace_0.py +--- +## AST + +``` +Module( + ModModule { + range: 0..47, + body: [ + Expr( + StmtExpr { + range: 46..47, + value: Set( + ExprSet { + range: 46..47, + elts: [ + Name( + ExprName { + range: 47..47, + id: "", + ctx: Invalid, + }, + ), + ], + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | # Missing closing curly brace 0: No elements +2 | +3 | { + | Syntax Error: unexpected EOF while parsing + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__set__missing_closing_curly_brace_1.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__set__missing_closing_curly_brace_1.py.snap.new new file mode 100644 index 0000000000..3010e77f71 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__set__missing_closing_curly_brace_1.py.snap.new @@ -0,0 +1,56 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/set/missing_closing_curly_brace_1.py +--- +## AST + +``` +Module( + ModModule { + range: 0..136, + body: [ + Expr( + StmtExpr { + range: 128..136, + value: Set( + ExprSet { + range: 128..136, + elts: [ + BinOp( + ExprBinOp { + range: 131..136, + left: Name( + ExprName { + range: 131..132, + id: "x", + ctx: Load, + }, + ), + op: Add, + right: Name( + ExprName { + range: 135..136, + id: "y", + ctx: Load, + }, + ), + }, + ), + ], + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +4 | { +5 | +6 | x + y + | Syntax Error: unexpected EOF while parsing + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__set__missing_closing_curly_brace_2.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__set__missing_closing_curly_brace_2.py.snap.new new file mode 100644 index 0000000000..5bc40db8a9 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__set__missing_closing_curly_brace_2.py.snap.new @@ -0,0 +1,64 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/set/missing_closing_curly_brace_2.py +--- +## AST + +``` +Module( + ModModule { + range: 0..144, + body: [ + Expr( + StmtExpr { + range: 134..144, + value: Set( + ExprSet { + range: 134..144, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 135..136, + value: Int( + 1, + ), + }, + ), + BinOp( + ExprBinOp { + range: 139..144, + left: Name( + ExprName { + range: 139..140, + id: "x", + ctx: Load, + }, + ), + op: Add, + right: Name( + ExprName { + range: 143..144, + id: "y", + ctx: Load, + }, + ), + }, + ), + ], + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +4 | {1, +5 | +6 | x + y + | Syntax Error: unexpected EOF while parsing + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__set__missing_closing_curly_brace_3.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__set__missing_closing_curly_brace_3.py.snap.new new file mode 100644 index 0000000000..1ff2ea5b37 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__set__missing_closing_curly_brace_3.py.snap.new @@ -0,0 +1,83 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/set/missing_closing_curly_brace_3.py +--- +## AST + +``` +Module( + ModModule { + range: 0..144, + body: [ + Expr( + StmtExpr { + range: 118..123, + value: Set( + ExprSet { + range: 118..123, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 119..120, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 122..123, + value: Int( + 2, + ), + }, + ), + ], + }, + ), + }, + ), + FunctionDef( + StmtFunctionDef { + range: 125..144, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 129..132, + }, + type_params: None, + parameters: Parameters { + range: 132..134, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 140..144, + }, + ), + ], + }, + ), + ], + }, +) +``` +## Errors + + | +2 | # token starts a statement. +3 | +4 | {1, 2 + | ^ Syntax Error: Expected '}', found newline +5 | +6 | def foo(): +7 | pass + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__set__recover.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__set__recover.py.snap.new new file mode 100644 index 0000000000..e672b6efb3 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__set__recover.py.snap.new @@ -0,0 +1,303 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/set/recover.py +--- +## AST + +``` +Module( + ModModule { + range: 0..323, + body: [ + Expr( + StmtExpr { + range: 197..200, + value: Set( + ExprSet { + range: 197..200, + elts: [ + Name( + ExprName { + range: 198..198, + id: "", + ctx: Invalid, + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 202..208, + value: Set( + ExprSet { + range: 202..208, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 203..204, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 206..207, + value: Int( + 2, + ), + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 210..215, + value: Set( + ExprSet { + range: 210..215, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 211..212, + value: Int( + 1, + ), + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 233..238, + value: Set( + ExprSet { + range: 233..238, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 234..235, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 236..237, + value: Int( + 2, + ), + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 271..277, + value: Dict( + ExprDict { + range: 271..277, + items: [ + DictItem { + key: Some( + NumberLiteral( + ExprNumberLiteral { + range: 272..273, + value: Int( + 1, + ), + }, + ), + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 275..276, + value: Int( + 2, + ), + }, + ), + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 300..309, + value: Set( + ExprSet { + range: 300..309, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 301..302, + value: Int( + 1, + ), + }, + ), + BinOp( + ExprBinOp { + range: 304..307, + left: Name( + ExprName { + range: 304..305, + id: "x", + ctx: Load, + }, + ), + op: Add, + right: Name( + ExprName { + range: 307..307, + id: "", + ctx: Invalid, + }, + ), + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 311..317, + value: Set( + ExprSet { + range: 311..317, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 312..313, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 315..316, + value: Int( + 2, + ), + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 319..322, + value: List( + ExprList { + range: 319..322, + elts: [ + Starred( + ExprStarred { + range: 320..321, + value: Name( + ExprName { + range: 321..321, + id: "", + ctx: Invalid, + }, + ), + ctx: Load, + }, + ), + ], + ctx: Load, + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +3 | # These are same as for the list expressions. +4 | +5 | {,} + | ^ Syntax Error: Expected an expression +6 | +7 | {1,,2} + | + + + | +5 | {,} +6 | +7 | {1,,2} + | ^ Syntax Error: Expected an expression or a '}' +8 | +9 | {1,,} + | + + + | + 7 | {1,,2} + 8 | + 9 | {1,,} + | ^ Syntax Error: Expected an expression or a '}' +10 | +11 | # Missing comma + | + + + | +11 | # Missing comma +12 | {1 2} + | ^ Syntax Error: Expected ',', found int +13 | +14 | # Dictionary element in a list + | + + + | +17 | # Missing expression +18 | {1, x + } + | ^ Syntax Error: Expected an expression +19 | +20 | {1; 2} + | + + + | +18 | {1, x + } +19 | +20 | {1; 2} + | ^ Syntax Error: Expected an expression or a '}' +21 | +22 | [*] + | + + + | +20 | {1; 2} +21 | +22 | [*] + | ^ Syntax Error: Expected an expression + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__set__star_expression_precedence.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__set__star_expression_precedence.py.snap.new new file mode 100644 index 0000000000..03cde32507 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__set__star_expression_precedence.py.snap.new @@ -0,0 +1,460 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/set/star_expression_precedence.py +--- +## AST + +``` +Module( + ModModule { + range: 0..198, + body: [ + Expr( + StmtExpr { + range: 83..92, + value: Set( + ExprSet { + range: 83..92, + elts: [ + Starred( + ExprStarred { + range: 85..87, + value: Name( + ExprName { + range: 86..87, + id: "x", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + Name( + ExprName { + range: 90..91, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 93..105, + value: Set( + ExprSet { + range: 93..105, + elts: [ + Starred( + ExprStarred { + range: 94..101, + value: Compare( + ExprCompare { + range: 95..101, + left: Name( + ExprName { + range: 95..96, + id: "x", + ctx: Load, + }, + ), + ops: [ + In, + ], + comparators: [ + Name( + ExprName { + range: 100..101, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + ctx: Load, + }, + ), + Name( + ExprName { + range: 103..104, + id: "z", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 106..117, + value: Set( + ExprSet { + range: 106..117, + elts: [ + Starred( + ExprStarred { + range: 107..113, + value: UnaryOp( + ExprUnaryOp { + range: 108..113, + op: Not, + operand: Name( + ExprName { + range: 112..113, + id: "x", + ctx: Load, + }, + ), + }, + ), + ctx: Load, + }, + ), + Name( + ExprName { + range: 115..116, + id: "z", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 118..131, + value: Set( + ExprSet { + range: 118..131, + elts: [ + Starred( + ExprStarred { + range: 119..127, + value: BoolOp( + ExprBoolOp { + range: 120..127, + op: And, + values: [ + Name( + ExprName { + range: 120..121, + id: "x", + ctx: Load, + }, + ), + Name( + ExprName { + range: 126..127, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + ctx: Load, + }, + ), + Name( + ExprName { + range: 129..130, + id: "z", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 132..144, + value: Set( + ExprSet { + range: 132..144, + elts: [ + Starred( + ExprStarred { + range: 133..140, + value: BoolOp( + ExprBoolOp { + range: 134..140, + op: Or, + values: [ + Name( + ExprName { + range: 134..135, + id: "x", + ctx: Load, + }, + ), + Name( + ExprName { + range: 139..140, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + ctx: Load, + }, + ), + Name( + ExprName { + range: 142..143, + id: "z", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 145..167, + value: Set( + ExprSet { + range: 145..167, + elts: [ + Starred( + ExprStarred { + range: 146..163, + value: If( + ExprIf { + range: 147..163, + test: BooleanLiteral( + ExprBooleanLiteral { + range: 152..156, + value: true, + }, + ), + body: Name( + ExprName { + range: 147..148, + id: "x", + ctx: Load, + }, + ), + orelse: Name( + ExprName { + range: 162..163, + id: "y", + ctx: Load, + }, + ), + }, + ), + ctx: Load, + }, + ), + Name( + ExprName { + range: 165..166, + id: "z", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 168..185, + value: Set( + ExprSet { + range: 168..185, + elts: [ + Starred( + ExprStarred { + range: 169..181, + value: Lambda( + ExprLambda { + range: 170..181, + parameters: Some( + Parameters { + range: 177..178, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 177..178, + parameter: Parameter { + range: 177..178, + name: Identifier { + id: "x", + range: 177..178, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: Name( + ExprName { + range: 180..181, + id: "x", + ctx: Load, + }, + ), + }, + ), + ctx: Load, + }, + ), + Name( + ExprName { + range: 183..184, + id: "z", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 186..198, + value: Set( + ExprSet { + range: 186..198, + elts: [ + Named( + ExprNamed { + range: 187..194, + target: Starred( + ExprStarred { + range: 187..189, + value: Name( + ExprName { + range: 188..189, + id: "x", + ctx: Store, + }, + ), + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 193..194, + value: Int( + 2, + ), + }, + ), + }, + ), + Name( + ExprName { + range: 196..197, + id: "z", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | # For set expression, the minimum binding power of star expression is bitwise or. +2 | +3 | {(*x), y} + | ^^ Syntax Error: Starred expression cannot be used here +4 | {*x in y, z} +5 | {*not x, z} + | + + + | +3 | {(*x), y} +4 | {*x in y, z} + | ^^^^^^ Syntax Error: Comparison expression cannot be used here +5 | {*not x, z} +6 | {*x and y, z} + | + + + | +3 | {(*x), y} +4 | {*x in y, z} +5 | {*not x, z} + | ^^^^^ Syntax Error: Boolean expression cannot be used here +6 | {*x and y, z} +7 | {*x or y, z} + | + + + | +4 | {*x in y, z} +5 | {*not x, z} +6 | {*x and y, z} + | ^^^^^^^ Syntax Error: Boolean expression cannot be used here +7 | {*x or y, z} +8 | {*x if True else y, z} + | + + + | +5 | {*not x, z} +6 | {*x and y, z} +7 | {*x or y, z} + | ^^^^^^ Syntax Error: Boolean expression cannot be used here +8 | {*x if True else y, z} +9 | {*lambda x: x, z} + | + + + | + 6 | {*x and y, z} + 7 | {*x or y, z} + 8 | {*x if True else y, z} + | ^^^^^^^^^^^^^^^^ Syntax Error: Conditional expression cannot be used here + 9 | {*lambda x: x, z} +10 | {*x := 2, z} + | + + + | + 7 | {*x or y, z} + 8 | {*x if True else y, z} + 9 | {*lambda x: x, z} + | ^^^^^^^^^^^ Syntax Error: Lambda expression cannot be used here +10 | {*x := 2, z} + | + + + | + 8 | {*x if True else y, z} + 9 | {*lambda x: x, z} +10 | {*x := 2, z} + | ^^ Syntax Error: Assignment expression target must be an identifier + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__subscript__invalid_slice_element.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__subscript__invalid_slice_element.py.snap.new new file mode 100644 index 0000000000..66393e6613 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__subscript__invalid_slice_element.py.snap.new @@ -0,0 +1,304 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/subscript/invalid_slice_element.py +--- +## AST + +``` +Module( + ModModule { + range: 0..133, + body: [ + Expr( + StmtExpr { + range: 0..10, + value: Subscript( + ExprSubscript { + range: 0..10, + value: Name( + ExprName { + range: 0..1, + id: "x", + ctx: Load, + }, + ), + slice: Slice( + ExprSlice { + range: 2..9, + lower: Some( + Named( + ExprNamed { + range: 2..8, + target: Name( + ExprName { + range: 2..3, + id: "x", + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 7..8, + value: Int( + 1, + ), + }, + ), + }, + ), + ), + upper: None, + step: None, + }, + ), + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 33..39, + value: Subscript( + ExprSubscript { + range: 33..39, + value: Name( + ExprName { + range: 33..34, + id: "x", + ctx: Load, + }, + ), + slice: Slice( + ExprSlice { + range: 35..38, + lower: Some( + Starred( + ExprStarred { + range: 35..37, + value: Name( + ExprName { + range: 36..37, + id: "x", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ), + upper: None, + step: None, + }, + ), + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 40..46, + value: Subscript( + ExprSubscript { + range: 40..46, + value: Name( + ExprName { + range: 40..41, + id: "x", + ctx: Load, + }, + ), + slice: Slice( + ExprSlice { + range: 42..45, + lower: None, + upper: Some( + Starred( + ExprStarred { + range: 43..45, + value: Name( + ExprName { + range: 44..45, + id: "x", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ), + step: None, + }, + ), + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 47..54, + value: Subscript( + ExprSubscript { + range: 47..54, + value: Name( + ExprName { + range: 47..48, + id: "x", + ctx: Load, + }, + ), + slice: Slice( + ExprSlice { + range: 49..53, + lower: None, + upper: None, + step: Some( + Starred( + ExprStarred { + range: 51..53, + value: Name( + ExprName { + range: 52..53, + id: "x", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ), + }, + ), + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 70..73, + value: Subscript( + ExprSubscript { + range: 70..73, + value: Name( + ExprName { + range: 70..71, + id: "x", + ctx: Load, + }, + ), + slice: Name( + ExprName { + range: 72..73, + id: "", + ctx: Invalid, + }, + ), + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 123..133, + value: Subscript( + ExprSubscript { + range: 123..133, + value: Name( + ExprName { + range: 123..124, + id: "x", + ctx: Load, + }, + ), + slice: Named( + ExprNamed { + range: 125..132, + target: Starred( + ExprStarred { + range: 125..127, + value: Name( + ExprName { + range: 126..127, + id: "x", + ctx: Store, + }, + ), + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 131..132, + value: Int( + 1, + ), + }, + ), + }, + ), + ctx: Load, + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | x[x := 1:] + | ^^^^^^ Syntax Error: Unparenthesized named expression cannot be used here +2 | +3 | # Starred expression + | + + + | +3 | # Starred expression +4 | x[*x:] + | ^^ Syntax Error: Starred expression cannot be used here +5 | x[:*x] +6 | x[::*x] + | + + + | +3 | # Starred expression +4 | x[*x:] +5 | x[:*x] + | ^^ Syntax Error: Starred expression cannot be used here +6 | x[::*x] + | + + + | +4 | x[*x:] +5 | x[:*x] +6 | x[::*x] + | ^^ Syntax Error: Starred expression cannot be used here +7 | +8 | # Empty slice + | + + + | + 8 | # Empty slice + 9 | x[] + | ^ Syntax Error: Expected index or slice expression +10 | +11 | # Mixed starred expression and named expression + | + + + | +11 | # Mixed starred expression and named expression +12 | x[*x := 1] + | ^^ Syntax Error: Assignment expression target must be an identifier + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__subscript__unclosed_slice_0.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__subscript__unclosed_slice_0.py.snap.new new file mode 100644 index 0000000000..d9e5673f7d --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__subscript__unclosed_slice_0.py.snap.new @@ -0,0 +1,71 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/subscript/unclosed_slice_0.py +--- +## AST + +``` +Module( + ModModule { + range: 0..10, + body: [ + Expr( + StmtExpr { + range: 0..10, + value: Subscript( + ExprSubscript { + range: 0..10, + value: Name( + ExprName { + range: 0..1, + id: "x", + ctx: Load, + }, + ), + slice: Slice( + ExprSlice { + range: 2..10, + lower: None, + upper: Some( + BinOp( + ExprBinOp { + range: 5..10, + left: Name( + ExprName { + range: 5..6, + id: "x", + ctx: Load, + }, + ), + op: Add, + right: Name( + ExprName { + range: 9..10, + id: "y", + ctx: Load, + }, + ), + }, + ), + ), + step: None, + }, + ), + ctx: Load, + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | x[: +2 | +3 | x + y + | Syntax Error: unexpected EOF while parsing + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__subscript__unclosed_slice_1.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__subscript__unclosed_slice_1.py.snap.new new file mode 100644 index 0000000000..915600a7ce --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__subscript__unclosed_slice_1.py.snap.new @@ -0,0 +1,113 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/subscript/unclosed_slice_1.py +--- +## AST + +``` +Module( + ModModule { + range: 0..25, + body: [ + Expr( + StmtExpr { + range: 0..9, + value: Subscript( + ExprSubscript { + range: 0..9, + value: Name( + ExprName { + range: 0..1, + id: "x", + ctx: Load, + }, + ), + slice: Slice( + ExprSlice { + range: 2..9, + lower: None, + upper: None, + step: Some( + Name( + ExprName { + range: 6..9, + id: "def", + ctx: Load, + }, + ), + ), + }, + ), + ctx: Load, + }, + ), + }, + ), + AnnAssign( + StmtAnnAssign { + range: 10..25, + target: Call( + ExprCall { + range: 10..15, + func: Name( + ExprName { + range: 10..13, + id: "foo", + ctx: Load, + }, + ), + arguments: Arguments { + range: 13..15, + args: [], + keywords: [], + }, + }, + ), + annotation: Name( + ExprName { + range: 21..25, + id: "pass", + ctx: Load, + }, + ), + value: None, + simple: false, + }, + ), + ], + }, +) +``` +## Errors + + | +1 | x[:: +2 | +3 | def foo(): + | ^^^ Syntax Error: Expected an identifier, but found a keyword 'def' that cannot be used here +4 | pass + | + + + | +1 | x[:: +2 | +3 | def foo(): + | ^^^ Syntax Error: Expected ']', found name +4 | pass + | + + + | +3 | def foo(): +4 | pass + | ^^^^ Syntax Error: Expected an identifier, but found a keyword 'pass' that cannot be used here + | + + + | +3 | def foo(): +4 | pass + | Syntax Error: unexpected EOF while parsing + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__unary.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__unary.py.snap.new new file mode 100644 index 0000000000..a3b5c9b04d --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__unary.py.snap.new @@ -0,0 +1,53 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/unary.py +--- +## AST + +``` +Module( + ModModule { + range: 0..10, + body: [ + Expr( + StmtExpr { + range: 0..5, + value: UnaryOp( + ExprUnaryOp { + range: 0..5, + op: Not, + operand: Name( + ExprName { + range: 4..5, + id: "x", + ctx: Load, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 9..10, + value: NumberLiteral( + ExprNumberLiteral { + range: 9..10, + value: Int( + 1, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | not x := 1 + | ^^ Syntax Error: Expected a statement + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__unary__named_expression.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__unary__named_expression.py.snap.new new file mode 100644 index 0000000000..c6a90a2533 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__unary__named_expression.py.snap.new @@ -0,0 +1,92 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/unary/named_expression.py +--- +## AST + +``` +Module( + ModModule { + range: 0..18, + body: [ + Expr( + StmtExpr { + range: 0..2, + value: UnaryOp( + ExprUnaryOp { + range: 0..2, + op: USub, + operand: Name( + ExprName { + range: 1..2, + id: "x", + ctx: Load, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 6..7, + value: NumberLiteral( + ExprNumberLiteral { + range: 6..7, + value: Int( + 1, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 8..13, + value: UnaryOp( + ExprUnaryOp { + range: 8..13, + op: Not, + operand: Name( + ExprName { + range: 12..13, + id: "x", + ctx: Load, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 17..18, + value: NumberLiteral( + ExprNumberLiteral { + range: 17..18, + value: Int( + 1, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | -x := 1 + | ^^ Syntax Error: Expected a statement +2 | not x := 1 + | + + + | +1 | -x := 1 +2 | not x := 1 + | ^^ Syntax Error: Expected a statement + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__unary__no_expression_0.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__unary__no_expression_0.py.snap.new new file mode 100644 index 0000000000..1b8a58c942 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__unary__no_expression_0.py.snap.new @@ -0,0 +1,67 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/unary/no_expression_0.py +--- +## AST + +``` +Module( + ModModule { + range: 0..10, + body: [ + Expr( + StmtExpr { + range: 0..3, + value: UnaryOp( + ExprUnaryOp { + range: 0..3, + op: Not, + operand: Name( + ExprName { + range: 3..3, + id: "", + ctx: Invalid, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 5..10, + value: BinOp( + ExprBinOp { + range: 5..10, + left: Name( + ExprName { + range: 5..6, + id: "x", + ctx: Load, + }, + ), + op: Add, + right: Name( + ExprName { + range: 9..10, + id: "y", + ctx: Load, + }, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | not + | ^ Syntax Error: Expected an expression +2 | +3 | x + y + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__unary__no_expression_1.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__unary__no_expression_1.py.snap.new new file mode 100644 index 0000000000..55338d6840 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__unary__no_expression_1.py.snap.new @@ -0,0 +1,67 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/unary/no_expression_1.py +--- +## AST + +``` +Module( + ModModule { + range: 0..8, + body: [ + Expr( + StmtExpr { + range: 0..1, + value: UnaryOp( + ExprUnaryOp { + range: 0..1, + op: UAdd, + operand: Name( + ExprName { + range: 1..1, + id: "", + ctx: Invalid, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 3..8, + value: BinOp( + ExprBinOp { + range: 3..8, + left: Name( + ExprName { + range: 3..4, + id: "x", + ctx: Load, + }, + ), + op: Add, + right: Name( + ExprName { + range: 7..8, + id: "y", + ctx: Load, + }, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | + + | ^ Syntax Error: Expected an expression +2 | +3 | x + y + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__yield__named_expression.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__yield__named_expression.py.snap.new new file mode 100644 index 0000000000..44e5e6210d --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__yield__named_expression.py.snap.new @@ -0,0 +1,117 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/yield/named_expression.py +--- +## AST + +``` +Module( + ModModule { + range: 0..85, + body: [ + Expr( + StmtExpr { + range: 52..59, + value: Yield( + ExprYield { + range: 52..59, + value: Some( + Name( + ExprName { + range: 58..59, + id: "x", + ctx: Load, + }, + ), + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 63..64, + value: NumberLiteral( + ExprNumberLiteral { + range: 63..64, + value: Int( + 1, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 66..84, + value: Yield( + ExprYield { + range: 66..84, + value: Some( + Tuple( + ExprTuple { + range: 72..84, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 72..73, + value: Int( + 1, + ), + }, + ), + Name( + ExprName { + range: 75..76, + id: "x", + ctx: Load, + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 80..81, + value: Int( + 2, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 83..84, + value: Int( + 3, + ), + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | # Unparenthesized named expressions are not allowed +2 | yield x := 1 + | ^^ Syntax Error: Expected a statement +3 | +4 | yield 1, x := 2, 3 + | + + + | +2 | yield x := 1 +3 | +4 | yield 1, x := 2, 3 + | ^^ Syntax Error: Expected ',', found ':=' + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__yield__star_expression.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__yield__star_expression.py.snap.new new file mode 100644 index 0000000000..442d8a004a --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__yield__star_expression.py.snap.new @@ -0,0 +1,114 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/yield/star_expression.py +--- +## AST + +``` +Module( + ModModule { + range: 0..67, + body: [ + Expr( + StmtExpr { + range: 37..47, + value: Yield( + ExprYield { + range: 37..47, + value: Some( + Starred( + ExprStarred { + range: 44..46, + value: Name( + ExprName { + range: 45..46, + id: "x", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 49..66, + value: Yield( + ExprYield { + range: 49..66, + value: Some( + Tuple( + ExprTuple { + range: 55..66, + elts: [ + Starred( + ExprStarred { + range: 55..63, + value: BoolOp( + ExprBoolOp { + range: 56..63, + op: And, + values: [ + Name( + ExprName { + range: 56..57, + id: "x", + ctx: Load, + }, + ), + Name( + ExprName { + range: 62..63, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + ctx: Load, + }, + ), + Name( + ExprName { + range: 65..66, + id: "z", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | # Cannot use starred expression here +2 | yield (*x) + | ^^ Syntax Error: Starred expression cannot be used here +3 | +4 | yield *x and y, z + | + + + | +2 | yield (*x) +3 | +4 | yield *x and y, z + | ^^^^^^^ Syntax Error: Boolean expression cannot be used here + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__yield_from__starred_expression.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__yield_from__starred_expression.py.snap.new new file mode 100644 index 0000000000..66a2eb6ac1 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__yield_from__starred_expression.py.snap.new @@ -0,0 +1,94 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/yield_from/starred_expression.py +--- +## AST + +``` +Module( + ModModule { + range: 0..100, + body: [ + Expr( + StmtExpr { + range: 70..83, + value: YieldFrom( + ExprYieldFrom { + range: 70..83, + value: Starred( + ExprStarred { + range: 81..83, + value: Name( + ExprName { + range: 82..83, + id: "x", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 84..100, + value: YieldFrom( + ExprYieldFrom { + range: 84..100, + value: Tuple( + ExprTuple { + range: 95..100, + elts: [ + Starred( + ExprStarred { + range: 95..97, + value: Name( + ExprName { + range: 96..97, + id: "x", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + Name( + ExprName { + range: 99..100, + id: "y", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | # Yield from doesn't allow top-level starred expression unlike yield +2 | +3 | yield from *x + | ^^ Syntax Error: Starred expression cannot be used here +4 | yield from *x, y + | + + + | +3 | yield from *x +4 | yield from *x, y + | ^^ Syntax Error: Starred expression cannot be used here + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__yield_from__unparenthesized.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__yield_from__unparenthesized.py.snap.new new file mode 100644 index 0000000000..8a05fa6b2b --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@expressions__yield_from__unparenthesized.py.snap.new @@ -0,0 +1,159 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/expressions/yield_from/unparenthesized.py +--- +## AST + +``` +Module( + ModModule { + range: 0..192, + body: [ + Expr( + StmtExpr { + range: 35..47, + value: YieldFrom( + ExprYieldFrom { + range: 35..47, + value: Name( + ExprName { + range: 46..47, + id: "x", + ctx: Load, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 51..52, + value: NumberLiteral( + ExprNumberLiteral { + range: 51..52, + value: Int( + 1, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 89..104, + value: YieldFrom( + ExprYieldFrom { + range: 89..104, + value: Tuple( + ExprTuple { + range: 100..104, + elts: [ + Name( + ExprName { + range: 100..101, + id: "x", + ctx: Load, + }, + ), + Name( + ExprName { + range: 103..104, + id: "y", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 168..192, + value: YieldFrom( + ExprYieldFrom { + range: 168..192, + value: Tuple( + ExprTuple { + range: 179..192, + elts: [ + Name( + ExprName { + range: 180..181, + id: "x", + ctx: Load, + }, + ), + Starred( + ExprStarred { + range: 183..191, + value: BoolOp( + ExprBoolOp { + range: 184..191, + op: And, + values: [ + Name( + ExprName { + range: 184..185, + id: "x", + ctx: Load, + }, + ), + Name( + ExprName { + range: 190..191, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | # Unparenthesized named expression +2 | yield from x := 1 + | ^^ Syntax Error: Expected a statement +3 | +4 | # Unparenthesized tuple expression + | + + + | +4 | # Unparenthesized tuple expression +5 | yield from x, y + | ^^^^ Syntax Error: Unparenthesized tuple expression cannot be used here +6 | +7 | # This is a tuple expression parsing + | + + + | +7 | # This is a tuple expression parsing +8 | # vvvvvvvvvvvvv +9 | yield from (x, *x and y) + | ^^^^^^^ Syntax Error: Boolean expression cannot be used here + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@f_string_empty_expression.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@f_string_empty_expression.py.snap.new new file mode 100644 index 0000000000..2ccfb5cb00 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@f_string_empty_expression.py.snap.new @@ -0,0 +1,112 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/f_string_empty_expression.py +--- +## AST + +``` +Module( + ModModule { + range: 0..14, + body: [ + Expr( + StmtExpr { + range: 0..5, + value: FString( + ExprFString { + range: 0..5, + value: FStringValue { + inner: Single( + FString( + FString { + range: 0..5, + elements: [ + Expression( + FStringExpressionElement { + range: 2..4, + expression: Name( + ExprName { + range: 3..3, + id: "", + ctx: Invalid, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 6..13, + value: FString( + ExprFString { + range: 6..13, + value: FStringValue { + inner: Single( + FString( + FString { + range: 6..13, + elements: [ + Expression( + FStringExpressionElement { + range: 8..12, + expression: Name( + ExprName { + range: 9..9, + id: "", + ctx: Invalid, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | f"{}" + | ^ Syntax Error: Expected an expression +2 | f"{ }" + | + + + | +1 | f"{}" +2 | f"{ }" + | ^ Syntax Error: Expected an expression + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@f_string_invalid_conversion_flag_name_tok.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@f_string_invalid_conversion_flag_name_tok.py.snap.new new file mode 100644 index 0000000000..8e0935bcc6 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@f_string_invalid_conversion_flag_name_tok.py.snap.new @@ -0,0 +1,63 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/f_string_invalid_conversion_flag_name_tok.py +--- +## AST + +``` +Module( + ModModule { + range: 0..9, + body: [ + Expr( + StmtExpr { + range: 0..8, + value: FString( + ExprFString { + range: 0..8, + value: FStringValue { + inner: Single( + FString( + FString { + range: 0..8, + elements: [ + Expression( + FStringExpressionElement { + range: 2..7, + expression: Name( + ExprName { + range: 3..4, + id: "x", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | f"{x!z}" + | ^ Syntax Error: f-string: invalid conversion character + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@f_string_invalid_conversion_flag_other_tok.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@f_string_invalid_conversion_flag_other_tok.py.snap.new new file mode 100644 index 0000000000..25b801d24f --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@f_string_invalid_conversion_flag_other_tok.py.snap.new @@ -0,0 +1,112 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/f_string_invalid_conversion_flag_other_tok.py +--- +## AST + +``` +Module( + ModModule { + range: 0..22, + body: [ + Expr( + StmtExpr { + range: 0..10, + value: FString( + ExprFString { + range: 0..10, + value: FStringValue { + inner: Single( + FString( + FString { + range: 0..10, + elements: [ + Expression( + FStringExpressionElement { + range: 2..9, + expression: Name( + ExprName { + range: 3..4, + id: "x", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 11..21, + value: FString( + ExprFString { + range: 11..21, + value: FStringValue { + inner: Single( + FString( + FString { + range: 11..21, + elements: [ + Expression( + FStringExpressionElement { + range: 13..20, + expression: Name( + ExprName { + range: 14..15, + id: "x", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | f"{x!123}" + | ^^^ Syntax Error: f-string: invalid conversion character +2 | f"{x!'a'}" + | + + + | +1 | f"{x!123}" +2 | f"{x!'a'}" + | ^^^ Syntax Error: f-string: invalid conversion character + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@f_string_invalid_starred_expr.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@f_string_invalid_starred_expr.py.snap.new new file mode 100644 index 0000000000..0775b47790 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@f_string_invalid_starred_expr.py.snap.new @@ -0,0 +1,205 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/f_string_invalid_starred_expr.py +--- +## AST + +``` +Module( + ModModule { + range: 0..112, + body: [ + Expr( + StmtExpr { + range: 77..83, + value: FString( + ExprFString { + range: 77..83, + value: FStringValue { + inner: Single( + FString( + FString { + range: 77..83, + elements: [ + Expression( + FStringExpressionElement { + range: 79..82, + expression: Starred( + ExprStarred { + range: 80..81, + value: Name( + ExprName { + range: 81..81, + id: "", + ctx: Invalid, + }, + ), + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 84..97, + value: FString( + ExprFString { + range: 84..97, + value: FStringValue { + inner: Single( + FString( + FString { + range: 84..97, + elements: [ + Expression( + FStringExpressionElement { + range: 86..96, + expression: Starred( + ExprStarred { + range: 87..95, + value: BoolOp( + ExprBoolOp { + range: 88..95, + op: And, + values: [ + Name( + ExprName { + range: 88..89, + id: "x", + ctx: Load, + }, + ), + Name( + ExprName { + range: 94..95, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 98..111, + value: FString( + ExprFString { + range: 98..111, + value: FStringValue { + inner: Single( + FString( + FString { + range: 98..111, + elements: [ + Expression( + FStringExpressionElement { + range: 100..110, + expression: Starred( + ExprStarred { + range: 101..109, + value: Yield( + ExprYield { + range: 102..109, + value: Some( + Name( + ExprName { + range: 108..109, + id: "x", + ctx: Load, + }, + ), + ), + }, + ), + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | # Starred expression inside f-string has a minimum precedence of bitwise or. +2 | f"{*}" + | ^ Syntax Error: Expected an expression +3 | f"{*x and y}" +4 | f"{*yield x}" + | + + + | +1 | # Starred expression inside f-string has a minimum precedence of bitwise or. +2 | f"{*}" +3 | f"{*x and y}" + | ^^^^^^^ Syntax Error: Boolean expression cannot be used here +4 | f"{*yield x}" + | + + + | +2 | f"{*}" +3 | f"{*x and y}" +4 | f"{*yield x}" + | ^^^^^^^ Syntax Error: Yield expression cannot be used here + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@f_string_lambda_without_parentheses.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@f_string_lambda_without_parentheses.py.snap.new new file mode 100644 index 0000000000..2ecb44d983 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@f_string_lambda_without_parentheses.py.snap.new @@ -0,0 +1,115 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/f_string_lambda_without_parentheses.py +--- +## AST + +``` +Module( + ModModule { + range: 0..17, + body: [ + Expr( + StmtExpr { + range: 0..16, + value: FString( + ExprFString { + range: 0..16, + value: FStringValue { + inner: Single( + FString( + FString { + range: 0..16, + elements: [ + Expression( + FStringExpressionElement { + range: 2..12, + expression: Lambda( + ExprLambda { + range: 3..12, + parameters: Some( + Parameters { + range: 10..11, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 10..11, + parameter: Parameter { + range: 10..11, + name: Identifier { + id: "x", + range: 10..11, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: Name( + ExprName { + range: 12..12, + id: "", + ctx: Invalid, + }, + ), + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + Literal( + FStringLiteralElement { + range: 12..14, + value: " x", + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | f"{lambda x: x}" + | ^^ Syntax Error: Expected an expression + | + + + | +1 | f"{lambda x: x}" + | ^^^^^^^^^ Syntax Error: f-string: lambda expressions are not allowed without parentheses + | + + + | +1 | f"{lambda x: x}" + | ^^ Syntax Error: f-string: expecting '}' + | + + + | +1 | f"{lambda x: x}" + | ^ Syntax Error: Expected an f-string element or the end of the f-string + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@f_string_unclosed_lbrace.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@f_string_unclosed_lbrace.py.snap.new new file mode 100644 index 0000000000..80fcb0c07a --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@f_string_unclosed_lbrace.py.snap.new @@ -0,0 +1,351 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/f_string_unclosed_lbrace.py +--- +## AST + +``` +Module( + ModModule { + range: 0..38, + body: [ + Expr( + StmtExpr { + range: 0..4, + value: FString( + ExprFString { + range: 0..4, + value: FStringValue { + inner: Single( + FString( + FString { + range: 0..4, + elements: [ + Expression( + FStringExpressionElement { + range: 2..3, + expression: Name( + ExprName { + range: 3..3, + id: "", + ctx: Invalid, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 5..14, + value: FString( + ExprFString { + range: 5..14, + value: FStringValue { + inner: Single( + FString( + FString { + range: 5..14, + elements: [ + Expression( + FStringExpressionElement { + range: 7..14, + expression: Name( + ExprName { + range: 8..11, + id: "foo", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 15..23, + value: FString( + ExprFString { + range: 15..23, + value: FStringValue { + inner: Single( + FString( + FString { + range: 15..23, + elements: [ + Expression( + FStringExpressionElement { + range: 17..22, + expression: Name( + ExprName { + range: 18..21, + id: "foo", + ctx: Load, + }, + ), + debug_text: Some( + DebugText { + leading: "", + trailing: "=", + }, + ), + conversion: None, + format_spec: None, + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 24..37, + value: FString( + ExprFString { + range: 24..37, + value: FStringValue { + inner: Concatenated( + [ + FString( + FString { + range: 24..28, + elements: [ + Expression( + FStringExpressionElement { + range: 26..27, + expression: Name( + ExprName { + range: 27..27, + id: "", + ctx: Invalid, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + FString( + FString { + range: 29..37, + elements: [ + Expression( + FStringExpressionElement { + range: 33..34, + expression: Name( + ExprName { + range: 34..34, + id: "", + ctx: Invalid, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: true, + }, + }, + ), + ], + ), + }, + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | f"{" + | ^ Syntax Error: missing closing quote in string literal +2 | f"{foo!r" +3 | f"{foo=" + | + + + | +1 | f"{" + | Syntax Error: f-string: unterminated string +2 | f"{foo!r" +3 | f"{foo=" + | + + + | +1 | f"{" + | Syntax Error: f-string: unterminated string +2 | f"{foo!r" +3 | f"{foo=" + | + + + | +1 | f"{" +2 | f"{foo!r" + | ^^ Syntax Error: missing closing quote in string literal +3 | f"{foo=" +4 | f"{" + | + + + | +1 | f"{" +2 | f"{foo!r" + | Syntax Error: f-string: unterminated string +3 | f"{foo=" +4 | f"{" + | + + + | +1 | f"{" +2 | f"{foo!r" + | Syntax Error: f-string: unterminated string +3 | f"{foo=" +4 | f"{" + | + + + | +1 | f"{" +2 | f"{foo!r" +3 | f"{foo=" + | ^^ Syntax Error: f-string: expecting '}' +4 | f"{" +5 | f"""{""" + | + + + | +1 | f"{" +2 | f"{foo!r" + | Syntax Error: Expected FStringEnd, found Unknown +3 | f"{foo=" +4 | f"{" + | + + + | +1 | f"{" +2 | f"{foo!r" +3 | f"{foo=" + | ^ Syntax Error: missing closing quote in string literal +4 | f"{" +5 | f"""{""" + | + + + | +1 | f"{" +2 | f"{foo!r" +3 | f"{foo=" + | Syntax Error: f-string: unterminated string +4 | f"{" +5 | f"""{""" + | + + + | +1 | f"{" +2 | f"{foo!r" +3 | f"{foo=" + | Syntax Error: f-string: unterminated string +4 | f"{" +5 | f"""{""" + | + + + | +2 | f"{foo!r" +3 | f"{foo=" +4 | f"{" + | ^ Syntax Error: missing closing quote in string literal +5 | f"""{""" + | + + + | +3 | f"{foo=" +4 | f"{" +5 | f"""{""" + | ^^^^ Syntax Error: Expected FStringEnd, found FStringStart + | + + + | +3 | f"{foo=" +4 | f"{" +5 | f"""{""" + | ^^^ Syntax Error: Expected an expression + | + + + | +4 | f"{" +5 | f"""{""" + | + + + | +4 | f"{" +5 | f"""{""" + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@f_string_unclosed_lbrace_in_format_spec.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@f_string_unclosed_lbrace_in_format_spec.py.snap.new new file mode 100644 index 0000000000..aa73ebc37c --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@f_string_unclosed_lbrace_in_format_spec.py.snap.new @@ -0,0 +1,141 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/f_string_unclosed_lbrace_in_format_spec.py +--- +## AST + +``` +Module( + ModModule { + range: 0..29, + body: [ + Expr( + StmtExpr { + range: 0..12, + value: FString( + ExprFString { + range: 0..12, + value: FStringValue { + inner: Single( + FString( + FString { + range: 0..12, + elements: [ + Literal( + FStringLiteralElement { + range: 2..8, + value: "hello ", + }, + ), + Expression( + FStringExpressionElement { + range: 8..11, + expression: Name( + ExprName { + range: 9..10, + id: "x", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: Some( + FStringFormatSpec { + range: 11..11, + elements: [], + }, + ), + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 13..28, + value: FString( + ExprFString { + range: 13..28, + value: FStringValue { + inner: Single( + FString( + FString { + range: 13..28, + elements: [ + Literal( + FStringLiteralElement { + range: 15..21, + value: "hello ", + }, + ), + Expression( + FStringExpressionElement { + range: 21..27, + expression: Name( + ExprName { + range: 22..23, + id: "x", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: Some( + FStringFormatSpec { + range: 24..27, + elements: [ + Literal( + FStringLiteralElement { + range: 24..27, + value: ".3f", + }, + ), + ], + }, + ), + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | f"hello {x:" + | ^ Syntax Error: f-string: expecting '}' +2 | f"hello {x:.3f" + | + + + | +1 | f"hello {x:" +2 | f"hello {x:.3f" + | ^ Syntax Error: f-string: expecting '}' + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@for_stmt_invalid_iter_expr.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@for_stmt_invalid_iter_expr.py.snap.new new file mode 100644 index 0000000000..6e80134f74 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@for_stmt_invalid_iter_expr.py.snap.new @@ -0,0 +1,184 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/for_stmt_invalid_iter_expr.py +--- +## AST + +``` +Module( + ModModule { + range: 0..71, + body: [ + For( + StmtFor { + range: 0..22, + is_async: false, + target: Name( + ExprName { + range: 4..5, + id: "x", + ctx: Store, + }, + ), + iter: Starred( + ExprStarred { + range: 9..17, + value: BoolOp( + ExprBoolOp { + range: 10..17, + op: And, + values: [ + Name( + ExprName { + range: 10..11, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 16..17, + id: "b", + ctx: Load, + }, + ), + ], + }, + ), + ctx: Load, + }, + ), + body: [ + Expr( + StmtExpr { + range: 19..22, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 19..22, + }, + ), + }, + ), + ], + orelse: [], + }, + ), + For( + StmtFor { + range: 23..44, + is_async: false, + target: Name( + ExprName { + range: 27..28, + id: "x", + ctx: Store, + }, + ), + iter: Yield( + ExprYield { + range: 32..39, + value: Some( + Name( + ExprName { + range: 38..39, + id: "a", + ctx: Load, + }, + ), + ), + }, + ), + body: [ + Expr( + StmtExpr { + range: 41..44, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 41..44, + }, + ), + }, + ), + ], + orelse: [], + }, + ), + For( + StmtFor { + range: 45..60, + is_async: false, + target: Name( + ExprName { + range: 49..55, + id: "target", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 59..60, + id: "x", + ctx: Load, + }, + ), + body: [], + orelse: [], + }, + ), + AnnAssign( + StmtAnnAssign { + range: 64..70, + target: NumberLiteral( + ExprNumberLiteral { + range: 64..65, + value: Int( + 1, + ), + }, + ), + annotation: EllipsisLiteral( + ExprEllipsisLiteral { + range: 67..70, + }, + ), + value: None, + simple: false, + }, + ), + ], + }, +) +``` +## Errors + + | +1 | for x in *a and b: ... + | ^^^^^^^ Syntax Error: Boolean expression cannot be used here +2 | for x in yield a: ... +3 | for target in x := 1: ... + | + + + | +1 | for x in *a and b: ... +2 | for x in yield a: ... + | ^^^^^^^ Syntax Error: Yield expression cannot be used here +3 | for target in x := 1: ... + | + + + | +1 | for x in *a and b: ... +2 | for x in yield a: ... +3 | for target in x := 1: ... + | ^^ Syntax Error: Expected ':', found ':=' + | + + + | +1 | for x in *a and b: ... +2 | for x in yield a: ... +3 | for target in x := 1: ... + | ^ Syntax Error: Invalid annotated assignment target + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@for_stmt_invalid_target.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@for_stmt_invalid_target.py.snap.new new file mode 100644 index 0000000000..a05e014f07 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@for_stmt_invalid_target.py.snap.new @@ -0,0 +1,464 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/for_stmt_invalid_target.py +--- +## AST + +``` +Module( + ModModule { + range: 0..154, + body: [ + For( + StmtFor { + range: 0..15, + is_async: false, + target: NumberLiteral( + ExprNumberLiteral { + range: 4..5, + value: Int( + 1, + ), + }, + ), + iter: Name( + ExprName { + range: 9..10, + id: "x", + ctx: Load, + }, + ), + body: [ + Expr( + StmtExpr { + range: 12..15, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 12..15, + }, + ), + }, + ), + ], + orelse: [], + }, + ), + For( + StmtFor { + range: 16..33, + is_async: false, + target: StringLiteral( + ExprStringLiteral { + range: 20..23, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 20..23, + value: "a", + flags: StringLiteralFlags { + quote_style: Double, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + iter: Name( + ExprName { + range: 27..28, + id: "x", + ctx: Load, + }, + ), + body: [ + Expr( + StmtExpr { + range: 30..33, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 30..33, + }, + ), + }, + ), + ], + orelse: [], + }, + ), + For( + StmtFor { + range: 34..56, + is_async: false, + target: Starred( + ExprStarred { + range: 38..46, + value: BoolOp( + ExprBoolOp { + range: 39..46, + op: And, + values: [ + Name( + ExprName { + range: 39..40, + id: "x", + ctx: Load, + }, + ), + Name( + ExprName { + range: 45..46, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 50..51, + id: "z", + ctx: Load, + }, + ), + body: [ + Expr( + StmtExpr { + range: 53..56, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 53..56, + }, + ), + }, + ), + ], + orelse: [], + }, + ), + For( + StmtFor { + range: 57..77, + is_async: false, + target: Starred( + ExprStarred { + range: 61..67, + value: BinOp( + ExprBinOp { + range: 62..67, + left: Name( + ExprName { + range: 62..63, + id: "x", + ctx: Load, + }, + ), + op: BitOr, + right: Name( + ExprName { + range: 66..67, + id: "y", + ctx: Load, + }, + ), + }, + ), + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 71..72, + id: "z", + ctx: Load, + }, + ), + body: [ + Expr( + StmtExpr { + range: 74..77, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 74..77, + }, + ), + }, + ), + ], + orelse: [], + }, + ), + For( + StmtFor { + range: 78..99, + is_async: false, + target: Await( + ExprAwait { + range: 82..89, + value: Name( + ExprName { + range: 88..89, + id: "x", + ctx: Load, + }, + ), + }, + ), + iter: Name( + ExprName { + range: 93..94, + id: "z", + ctx: Load, + }, + ), + body: [ + Expr( + StmtExpr { + range: 96..99, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 96..99, + }, + ), + }, + ), + ], + orelse: [], + }, + ), + For( + StmtFor { + range: 100..121, + is_async: false, + target: Yield( + ExprYield { + range: 104..116, + value: Some( + Compare( + ExprCompare { + range: 110..116, + left: Name( + ExprName { + range: 110..111, + id: "x", + ctx: Load, + }, + ), + ops: [ + In, + ], + comparators: [ + Name( + ExprName { + range: 115..116, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + ), + }, + ), + iter: Name( + ExprName { + range: 116..116, + id: "", + ctx: Invalid, + }, + ), + body: [ + Expr( + StmtExpr { + range: 118..121, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 118..121, + }, + ), + }, + ), + ], + orelse: [], + }, + ), + For( + StmtFor { + range: 122..153, + is_async: false, + target: List( + ExprList { + range: 126..143, + elts: [ + Name( + ExprName { + range: 127..128, + id: "x", + ctx: Store, + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 130..131, + value: Int( + 1, + ), + }, + ), + Name( + ExprName { + range: 133..134, + id: "y", + ctx: Store, + }, + ), + Starred( + ExprStarred { + range: 136..142, + value: List( + ExprList { + range: 137..142, + elts: [ + StringLiteral( + ExprStringLiteral { + range: 138..141, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 138..141, + value: "a", + flags: StringLiteralFlags { + quote_style: Double, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + ], + ctx: Store, + }, + ), + ctx: Store, + }, + ), + ], + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 147..148, + id: "z", + ctx: Load, + }, + ), + body: [ + Expr( + StmtExpr { + range: 150..153, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 150..153, + }, + ), + }, + ), + ], + orelse: [], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | for 1 in x: ... + | ^ Syntax Error: Invalid assignment target +2 | for "a" in x: ... +3 | for *x and y in z: ... + | + + + | +1 | for 1 in x: ... +2 | for "a" in x: ... + | ^^^ Syntax Error: Invalid assignment target +3 | for *x and y in z: ... +4 | for *x | y in z: ... + | + + + | +1 | for 1 in x: ... +2 | for "a" in x: ... +3 | for *x and y in z: ... + | ^^^^^^^ Syntax Error: Invalid assignment target +4 | for *x | y in z: ... +5 | for await x in z: ... + | + + + | +2 | for "a" in x: ... +3 | for *x and y in z: ... +4 | for *x | y in z: ... + | ^^^^^ Syntax Error: Invalid assignment target +5 | for await x in z: ... +6 | for yield x in y: ... + | + + + | +3 | for *x and y in z: ... +4 | for *x | y in z: ... +5 | for await x in z: ... + | ^^^^^^^ Syntax Error: Invalid assignment target +6 | for yield x in y: ... +7 | for [x, 1, y, *["a"]] in z: ... + | + + + | +4 | for *x | y in z: ... +5 | for await x in z: ... +6 | for yield x in y: ... + | ^^^^^^^^^^^^ Syntax Error: Yield expression cannot be used here +7 | for [x, 1, y, *["a"]] in z: ... + | + + + | +4 | for *x | y in z: ... +5 | for await x in z: ... +6 | for yield x in y: ... + | ^ Syntax Error: Expected 'in', found ':' +7 | for [x, 1, y, *["a"]] in z: ... + | + + + | +5 | for await x in z: ... +6 | for yield x in y: ... +7 | for [x, 1, y, *["a"]] in z: ... + | ^ Syntax Error: Invalid assignment target + | + + + | +5 | for await x in z: ... +6 | for yield x in y: ... +7 | for [x, 1, y, *["a"]] in z: ... + | ^^^ Syntax Error: Invalid assignment target + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@for_stmt_invalid_target_binary_expr.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@for_stmt_invalid_target_binary_expr.py.snap.new new file mode 100644 index 0000000000..88c3861d2b --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@for_stmt_invalid_target_binary_expr.py.snap.new @@ -0,0 +1,342 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/for_stmt_invalid_target_binary_expr.py +--- +## AST + +``` +Module( + ModModule { + range: 0..124, + body: [ + For( + StmtFor { + range: 0..24, + is_async: false, + target: Compare( + ExprCompare { + range: 4..14, + left: Name( + ExprName { + range: 4..5, + id: "x", + ctx: Load, + }, + ), + ops: [ + NotIn, + ], + comparators: [ + Name( + ExprName { + range: 13..14, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + iter: Name( + ExprName { + range: 18..19, + id: "z", + ctx: Load, + }, + ), + body: [ + Expr( + StmtExpr { + range: 21..24, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 21..24, + }, + ), + }, + ), + ], + orelse: [], + }, + ), + For( + StmtFor { + range: 25..45, + is_async: false, + target: Compare( + ExprCompare { + range: 29..35, + left: Name( + ExprName { + range: 29..30, + id: "x", + ctx: Load, + }, + ), + ops: [ + Eq, + ], + comparators: [ + Name( + ExprName { + range: 34..35, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + iter: Name( + ExprName { + range: 39..40, + id: "z", + ctx: Load, + }, + ), + body: [ + Expr( + StmtExpr { + range: 42..45, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 42..45, + }, + ), + }, + ), + ], + orelse: [], + }, + ), + For( + StmtFor { + range: 46..66, + is_async: false, + target: BoolOp( + ExprBoolOp { + range: 50..56, + op: Or, + values: [ + Name( + ExprName { + range: 50..51, + id: "x", + ctx: Load, + }, + ), + Name( + ExprName { + range: 55..56, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + iter: Name( + ExprName { + range: 60..61, + id: "z", + ctx: Load, + }, + ), + body: [ + Expr( + StmtExpr { + range: 63..66, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 63..66, + }, + ), + }, + ), + ], + orelse: [], + }, + ), + For( + StmtFor { + range: 67..83, + is_async: false, + target: UnaryOp( + ExprUnaryOp { + range: 71..73, + op: USub, + operand: Name( + ExprName { + range: 72..73, + id: "x", + ctx: Store, + }, + ), + }, + ), + iter: Name( + ExprName { + range: 77..78, + id: "y", + ctx: Load, + }, + ), + body: [ + Expr( + StmtExpr { + range: 80..83, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 80..83, + }, + ), + }, + ), + ], + orelse: [], + }, + ), + For( + StmtFor { + range: 84..103, + is_async: false, + target: UnaryOp( + ExprUnaryOp { + range: 88..93, + op: Not, + operand: Name( + ExprName { + range: 92..93, + id: "x", + ctx: Store, + }, + ), + }, + ), + iter: Name( + ExprName { + range: 97..98, + id: "y", + ctx: Load, + }, + ), + body: [ + Expr( + StmtExpr { + range: 100..103, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 100..103, + }, + ), + }, + ), + ], + orelse: [], + }, + ), + For( + StmtFor { + range: 104..123, + is_async: false, + target: BinOp( + ExprBinOp { + range: 108..113, + left: Name( + ExprName { + range: 108..109, + id: "x", + ctx: Load, + }, + ), + op: BitOr, + right: Name( + ExprName { + range: 112..113, + id: "y", + ctx: Load, + }, + ), + }, + ), + iter: Name( + ExprName { + range: 117..118, + id: "z", + ctx: Load, + }, + ), + body: [ + Expr( + StmtExpr { + range: 120..123, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 120..123, + }, + ), + }, + ), + ], + orelse: [], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | for x not in y in z: ... + | ^^^^^^^^^^ Syntax Error: Invalid assignment target +2 | for x == y in z: ... +3 | for x or y in z: ... + | + + + | +1 | for x not in y in z: ... +2 | for x == y in z: ... + | ^^^^^^ Syntax Error: Invalid assignment target +3 | for x or y in z: ... +4 | for -x in y: ... + | + + + | +1 | for x not in y in z: ... +2 | for x == y in z: ... +3 | for x or y in z: ... + | ^^^^^^ Syntax Error: Invalid assignment target +4 | for -x in y: ... +5 | for not x in y: ... + | + + + | +2 | for x == y in z: ... +3 | for x or y in z: ... +4 | for -x in y: ... + | ^^ Syntax Error: Invalid assignment target +5 | for not x in y: ... +6 | for x | y in z: ... + | + + + | +3 | for x or y in z: ... +4 | for -x in y: ... +5 | for not x in y: ... + | ^^^^^ Syntax Error: Invalid assignment target +6 | for x | y in z: ... + | + + + | +4 | for -x in y: ... +5 | for not x in y: ... +6 | for x | y in z: ... + | ^^^^^ Syntax Error: Invalid assignment target + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@for_stmt_invalid_target_in_keyword.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@for_stmt_invalid_target_in_keyword.py.snap.new new file mode 100644 index 0000000000..e4187c32d9 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@for_stmt_invalid_target_in_keyword.py.snap.new @@ -0,0 +1,443 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/for_stmt_invalid_target_in_keyword.py +--- +## AST + +``` +Module( + ModModule { + range: 0..170, + body: [ + For( + StmtFor { + range: 0..28, + is_async: false, + target: Call( + ExprCall { + range: 4..13, + func: Name( + ExprName { + range: 4..5, + id: "d", + ctx: Load, + }, + ), + arguments: Arguments { + range: 5..13, + args: [ + Compare( + ExprCompare { + range: 6..12, + left: Name( + ExprName { + range: 6..7, + id: "x", + ctx: Load, + }, + ), + ops: [ + In, + ], + comparators: [ + Name( + ExprName { + range: 11..12, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + ], + keywords: [], + }, + }, + ), + iter: Name( + ExprName { + range: 17..23, + id: "target", + ctx: Load, + }, + ), + body: [ + Expr( + StmtExpr { + range: 25..28, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 25..28, + }, + ), + }, + ), + ], + orelse: [], + }, + ), + For( + StmtFor { + range: 29..56, + is_async: false, + target: Call( + ExprCall { + range: 33..43, + func: Compare( + ExprCompare { + range: 34..40, + left: Name( + ExprName { + range: 34..35, + id: "x", + ctx: Load, + }, + ), + ops: [ + In, + ], + comparators: [ + Name( + ExprName { + range: 39..40, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + arguments: Arguments { + range: 41..43, + args: [], + keywords: [], + }, + }, + ), + iter: Name( + ExprName { + range: 47..51, + id: "iter", + ctx: Load, + }, + ), + body: [ + Expr( + StmtExpr { + range: 53..56, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 53..56, + }, + ), + }, + ), + ], + orelse: [], + }, + ), + For( + StmtFor { + range: 57..82, + is_async: false, + target: Compare( + ExprCompare { + range: 62..68, + left: Name( + ExprName { + range: 62..63, + id: "x", + ctx: Load, + }, + ), + ops: [ + In, + ], + comparators: [ + Name( + ExprName { + range: 67..68, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + iter: Name( + ExprName { + range: 73..77, + id: "iter", + ctx: Load, + }, + ), + body: [ + Expr( + StmtExpr { + range: 79..82, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 79..82, + }, + ), + }, + ), + ], + orelse: [], + }, + ), + For( + StmtFor { + range: 83..111, + is_async: false, + target: Tuple( + ExprTuple { + range: 87..98, + elts: [ + Compare( + ExprCompare { + range: 88..94, + left: Name( + ExprName { + range: 88..89, + id: "x", + ctx: Load, + }, + ), + ops: [ + In, + ], + comparators: [ + Name( + ExprName { + range: 93..94, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + Name( + ExprName { + range: 96..97, + id: "z", + ctx: Store, + }, + ), + ], + ctx: Store, + parenthesized: true, + }, + ), + iter: Name( + ExprName { + range: 102..106, + id: "iter", + ctx: Load, + }, + ), + body: [ + Expr( + StmtExpr { + range: 108..111, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 108..111, + }, + ), + }, + ), + ], + orelse: [], + }, + ), + For( + StmtFor { + range: 112..140, + is_async: false, + target: List( + ExprList { + range: 116..127, + elts: [ + Compare( + ExprCompare { + range: 117..123, + left: Name( + ExprName { + range: 117..118, + id: "x", + ctx: Load, + }, + ), + ops: [ + In, + ], + comparators: [ + Name( + ExprName { + range: 122..123, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + Name( + ExprName { + range: 125..126, + id: "z", + ctx: Store, + }, + ), + ], + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 131..135, + id: "iter", + ctx: Load, + }, + ), + body: [ + Expr( + StmtExpr { + range: 137..140, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 137..140, + }, + ), + }, + ), + ], + orelse: [], + }, + ), + For( + StmtFor { + range: 141..169, + is_async: false, + target: Set( + ExprSet { + range: 145..156, + elts: [ + Compare( + ExprCompare { + range: 146..152, + left: Name( + ExprName { + range: 146..147, + id: "x", + ctx: Load, + }, + ), + ops: [ + In, + ], + comparators: [ + Name( + ExprName { + range: 151..152, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + Name( + ExprName { + range: 154..155, + id: "z", + ctx: Load, + }, + ), + ], + }, + ), + iter: Name( + ExprName { + range: 160..164, + id: "iter", + ctx: Load, + }, + ), + body: [ + Expr( + StmtExpr { + range: 166..169, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 166..169, + }, + ), + }, + ), + ], + orelse: [], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | for d(x in y) in target: ... + | ^^^^^^^^^ Syntax Error: Invalid assignment target +2 | for (x in y)() in iter: ... +3 | for (x in y) in iter: ... + | + + + | +1 | for d(x in y) in target: ... +2 | for (x in y)() in iter: ... + | ^^^^^^^^^^ Syntax Error: Invalid assignment target +3 | for (x in y) in iter: ... +4 | for (x in y, z) in iter: ... + | + + + | +1 | for d(x in y) in target: ... +2 | for (x in y)() in iter: ... +3 | for (x in y) in iter: ... + | ^^^^^^ Syntax Error: Invalid assignment target +4 | for (x in y, z) in iter: ... +5 | for [x in y, z] in iter: ... + | + + + | +2 | for (x in y)() in iter: ... +3 | for (x in y) in iter: ... +4 | for (x in y, z) in iter: ... + | ^^^^^^ Syntax Error: Invalid assignment target +5 | for [x in y, z] in iter: ... +6 | for {x in y, z} in iter: ... + | + + + | +3 | for (x in y) in iter: ... +4 | for (x in y, z) in iter: ... +5 | for [x in y, z] in iter: ... + | ^^^^^^ Syntax Error: Invalid assignment target +6 | for {x in y, z} in iter: ... + | + + + | +4 | for (x in y, z) in iter: ... +5 | for [x in y, z] in iter: ... +6 | for {x in y, z} in iter: ... + | ^^^^^^^^^^^ Syntax Error: Invalid assignment target + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@for_stmt_missing_in_keyword.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@for_stmt_missing_in_keyword.py.snap.new new file mode 100644 index 0000000000..012a1f8531 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@for_stmt_missing_in_keyword.py.snap.new @@ -0,0 +1,96 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/for_stmt_missing_in_keyword.py +--- +## AST + +``` +Module( + ModModule { + range: 0..24, + body: [ + For( + StmtFor { + range: 0..12, + is_async: false, + target: Name( + ExprName { + range: 4..5, + id: "a", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 6..7, + id: "b", + ctx: Load, + }, + ), + body: [ + Expr( + StmtExpr { + range: 9..12, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 9..12, + }, + ), + }, + ), + ], + orelse: [], + }, + ), + For( + StmtFor { + range: 13..23, + is_async: false, + target: Name( + ExprName { + range: 17..18, + id: "a", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 18..18, + id: "", + ctx: Invalid, + }, + ), + body: [ + Expr( + StmtExpr { + range: 20..23, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 20..23, + }, + ), + }, + ), + ], + orelse: [], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | for a b: ... + | ^ Syntax Error: Expected 'in', found name +2 | for a: ... + | + + + | +1 | for a b: ... +2 | for a: ... + | ^ Syntax Error: Expected 'in', found ':' + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@for_stmt_missing_iter.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@for_stmt_missing_iter.py.snap.new new file mode 100644 index 0000000000..12e6d79e63 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@for_stmt_missing_iter.py.snap.new @@ -0,0 +1,68 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/for_stmt_missing_iter.py +--- +## AST + +``` +Module( + ModModule { + range: 0..20, + body: [ + For( + StmtFor { + range: 0..19, + is_async: false, + target: Name( + ExprName { + range: 4..5, + id: "x", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 8..8, + id: "", + ctx: Invalid, + }, + ), + body: [ + Assign( + StmtAssign { + range: 14..19, + targets: [ + Name( + ExprName { + range: 14..15, + id: "a", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 18..19, + value: Int( + 1, + ), + }, + ), + }, + ), + ], + orelse: [], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | for x in: + | ^ Syntax Error: Expected an expression +2 | a = 1 + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@for_stmt_missing_target.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@for_stmt_missing_target.py.snap.new new file mode 100644 index 0000000000..a1a3319155 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@for_stmt_missing_target.py.snap.new @@ -0,0 +1,61 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/for_stmt_missing_target.py +--- +## AST + +``` +Module( + ModModule { + range: 0..14, + body: [ + For( + StmtFor { + range: 0..13, + is_async: false, + target: Name( + ExprName { + range: 4..6, + id: "in", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 7..8, + id: "x", + ctx: Load, + }, + ), + body: [ + Expr( + StmtExpr { + range: 10..13, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 10..13, + }, + ), + }, + ), + ], + orelse: [], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | for in x: ... + | ^^ Syntax Error: Expected an identifier, but found a keyword 'in' that cannot be used here + | + + + | +1 | for in x: ... + | ^ Syntax Error: Expected 'in', found name + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@from_import_dotted_names.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@from_import_dotted_names.py.snap.new new file mode 100644 index 0000000000..12a9d76932 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@from_import_dotted_names.py.snap.new @@ -0,0 +1,170 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/from_import_dotted_names.py +--- +## AST + +``` +Module( + ModModule { + range: 0..67, + body: [ + ImportFrom( + StmtImportFrom { + range: 0..16, + module: Some( + Identifier { + id: "x", + range: 5..6, + }, + ), + names: [ + Alias { + range: 14..15, + name: Identifier { + id: "a", + range: 14..15, + }, + asname: None, + }, + ], + level: 0, + }, + ), + ImportFrom( + StmtImportFrom { + range: 17..34, + module: Some( + Identifier { + id: "x", + range: 22..23, + }, + ), + names: [ + Alias { + range: 31..32, + name: Identifier { + id: "a", + range: 31..32, + }, + asname: None, + }, + Alias { + range: 33..34, + name: Identifier { + id: "b", + range: 33..34, + }, + asname: None, + }, + ], + level: 0, + }, + ), + ImportFrom( + StmtImportFrom { + range: 35..66, + module: Some( + Identifier { + id: "x", + range: 40..41, + }, + ), + names: [ + Alias { + range: 49..50, + name: Identifier { + id: "a", + range: 49..50, + }, + asname: None, + }, + Alias { + range: 52..53, + name: Identifier { + id: "b", + range: 52..53, + }, + asname: None, + }, + Alias { + range: 54..55, + name: Identifier { + id: "c", + range: 54..55, + }, + asname: None, + }, + Alias { + range: 57..58, + name: Identifier { + id: "d", + range: 57..58, + }, + asname: None, + }, + Alias { + range: 60..61, + name: Identifier { + id: "e", + range: 60..61, + }, + asname: None, + }, + Alias { + range: 62..63, + name: Identifier { + id: "f", + range: 62..63, + }, + asname: None, + }, + Alias { + range: 65..66, + name: Identifier { + id: "g", + range: 65..66, + }, + asname: None, + }, + ], + level: 0, + }, + ), + ], + }, +) +``` +## Errors + + | +1 | from x import a. + | ^ Syntax Error: Expected ',', found '.' +2 | from x import a.b +3 | from x import a, b.c, d, e.f, g + | + + + | +1 | from x import a. +2 | from x import a.b + | ^ Syntax Error: Expected ',', found '.' +3 | from x import a, b.c, d, e.f, g + | + + + | +1 | from x import a. +2 | from x import a.b +3 | from x import a, b.c, d, e.f, g + | ^ Syntax Error: Expected ',', found '.' + | + + + | +1 | from x import a. +2 | from x import a.b +3 | from x import a, b.c, d, e.f, g + | ^ Syntax Error: Expected ',', found '.' + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@from_import_empty_names.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@from_import_empty_names.py.snap.new new file mode 100644 index 0000000000..c4e4be1679 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@from_import_empty_names.py.snap.new @@ -0,0 +1,95 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/from_import_empty_names.py +--- +## AST + +``` +Module( + ModModule { + range: 0..48, + body: [ + ImportFrom( + StmtImportFrom { + range: 0..13, + module: Some( + Identifier { + id: "x", + range: 5..6, + }, + ), + names: [], + level: 0, + }, + ), + ImportFrom( + StmtImportFrom { + range: 14..30, + module: Some( + Identifier { + id: "x", + range: 19..20, + }, + ), + names: [], + level: 0, + }, + ), + ImportFrom( + StmtImportFrom { + range: 31..47, + module: Some( + Identifier { + id: "x", + range: 36..37, + }, + ), + names: [], + level: 0, + }, + ), + ], + }, +) +``` +## Errors + + | +1 | from x import + | ^ Syntax Error: Expected one or more symbol names after import +2 | from x import () +3 | from x import ,, + | + + + | +1 | from x import +2 | from x import () + | ^ Syntax Error: Expected one or more symbol names after import +3 | from x import ,, + | + + + | +1 | from x import +2 | from x import () +3 | from x import ,, + | ^ Syntax Error: Expected an import name + | + + + | +1 | from x import +2 | from x import () +3 | from x import ,, + | ^ Syntax Error: Expected an import name + | + + + | +1 | from x import +2 | from x import () +3 | from x import ,, + | ^ Syntax Error: Expected one or more symbol names after import + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@from_import_missing_module.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@from_import_missing_module.py.snap.new new file mode 100644 index 0000000000..05076a015a --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@from_import_missing_module.py.snap.new @@ -0,0 +1,55 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/from_import_missing_module.py +--- +## AST + +``` +Module( + ModModule { + range: 0..19, + body: [ + ImportFrom( + StmtImportFrom { + range: 0..4, + module: None, + names: [], + level: 0, + }, + ), + ImportFrom( + StmtImportFrom { + range: 5..18, + module: None, + names: [ + Alias { + range: 17..18, + name: Identifier { + id: "x", + range: 17..18, + }, + asname: None, + }, + ], + level: 0, + }, + ), + ], + }, +) +``` +## Errors + + | +1 | from + | ^ Syntax Error: Expected a module name +2 | from import x + | + + + | +1 | from +2 | from import x + | ^^^^^^ Syntax Error: Expected a module name + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@from_import_missing_rpar.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@from_import_missing_rpar.py.snap.new new file mode 100644 index 0000000000..87e944f5f7 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@from_import_missing_rpar.py.snap.new @@ -0,0 +1,148 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/from_import_missing_rpar.py +--- +## AST + +``` +Module( + ModModule { + range: 0..53, + body: [ + ImportFrom( + StmtImportFrom { + range: 0..19, + module: Some( + Identifier { + id: "x", + range: 5..6, + }, + ), + names: [ + Alias { + range: 15..16, + name: Identifier { + id: "a", + range: 15..16, + }, + asname: None, + }, + Alias { + range: 18..19, + name: Identifier { + id: "b", + range: 18..19, + }, + asname: None, + }, + ], + level: 0, + }, + ), + Expr( + StmtExpr { + range: 20..25, + value: BinOp( + ExprBinOp { + range: 20..25, + left: NumberLiteral( + ExprNumberLiteral { + range: 20..21, + value: Int( + 1, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 24..25, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + ImportFrom( + StmtImportFrom { + range: 26..46, + module: Some( + Identifier { + id: "x", + range: 31..32, + }, + ), + names: [ + Alias { + range: 41..42, + name: Identifier { + id: "a", + range: 41..42, + }, + asname: None, + }, + Alias { + range: 44..45, + name: Identifier { + id: "b", + range: 44..45, + }, + asname: None, + }, + ], + level: 0, + }, + ), + Expr( + StmtExpr { + range: 47..52, + value: BinOp( + ExprBinOp { + range: 47..52, + left: NumberLiteral( + ExprNumberLiteral { + range: 47..48, + value: Int( + 2, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 51..52, + value: Int( + 2, + ), + }, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | from x import (a, b + | ^ Syntax Error: Expected ')', found newline +2 | 1 + 1 +3 | from x import (a, b, +4 | 2 + 2 + | + + + | +1 | from x import (a, b +2 | 1 + 1 +3 | from x import (a, b, + | ^ Syntax Error: Expected ')', found newline +4 | 2 + 2 + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@from_import_star_with_other_names.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@from_import_star_with_other_names.py.snap.new new file mode 100644 index 0000000000..60cf535425 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@from_import_star_with_other_names.py.snap.new @@ -0,0 +1,191 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/from_import_star_with_other_names.py +--- +## AST + +``` +Module( + ModModule { + range: 0..87, + body: [ + ImportFrom( + StmtImportFrom { + range: 0..18, + module: Some( + Identifier { + id: "x", + range: 5..6, + }, + ), + names: [ + Alias { + range: 14..15, + name: Identifier { + id: "*", + range: 14..15, + }, + asname: None, + }, + Alias { + range: 17..18, + name: Identifier { + id: "a", + range: 17..18, + }, + asname: None, + }, + ], + level: 0, + }, + ), + ImportFrom( + StmtImportFrom { + range: 19..40, + module: Some( + Identifier { + id: "x", + range: 24..25, + }, + ), + names: [ + Alias { + range: 33..34, + name: Identifier { + id: "a", + range: 33..34, + }, + asname: None, + }, + Alias { + range: 36..37, + name: Identifier { + id: "*", + range: 36..37, + }, + asname: None, + }, + Alias { + range: 39..40, + name: Identifier { + id: "b", + range: 39..40, + }, + asname: None, + }, + ], + level: 0, + }, + ), + ImportFrom( + StmtImportFrom { + range: 41..64, + module: Some( + Identifier { + id: "x", + range: 46..47, + }, + ), + names: [ + Alias { + range: 55..56, + name: Identifier { + id: "*", + range: 55..56, + }, + asname: None, + }, + Alias { + range: 58..64, + name: Identifier { + id: "a", + range: 58..59, + }, + asname: Some( + Identifier { + id: "b", + range: 63..64, + }, + ), + }, + ], + level: 0, + }, + ), + ImportFrom( + StmtImportFrom { + range: 65..86, + module: Some( + Identifier { + id: "x", + range: 70..71, + }, + ), + names: [ + Alias { + range: 79..80, + name: Identifier { + id: "*", + range: 79..80, + }, + asname: None, + }, + Alias { + range: 82..83, + name: Identifier { + id: "*", + range: 82..83, + }, + asname: None, + }, + Alias { + range: 85..86, + name: Identifier { + id: "a", + range: 85..86, + }, + asname: None, + }, + ], + level: 0, + }, + ), + ], + }, +) +``` +## Errors + + | +1 | from x import *, a + | ^^^^ Syntax Error: Star import must be the only import +2 | from x import a, *, b +3 | from x import *, a as b + | + + + | +1 | from x import *, a +2 | from x import a, *, b + | ^^^^^^^ Syntax Error: Star import must be the only import +3 | from x import *, a as b +4 | from x import *, *, a + | + + + | +1 | from x import *, a +2 | from x import a, *, b +3 | from x import *, a as b + | ^^^^^^^^^ Syntax Error: Star import must be the only import +4 | from x import *, *, a + | + + + | +2 | from x import a, *, b +3 | from x import *, a as b +4 | from x import *, *, a + | ^^^^^^^ Syntax Error: Star import must be the only import + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@from_import_unparenthesized_trailing_comma.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@from_import_unparenthesized_trailing_comma.py.snap.new new file mode 100644 index 0000000000..1c5103a1b2 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@from_import_unparenthesized_trailing_comma.py.snap.new @@ -0,0 +1,119 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/from_import_unparenthesized_trailing_comma.py +--- +## AST + +``` +Module( + ModModule { + range: 0..59, + body: [ + ImportFrom( + StmtImportFrom { + range: 0..16, + module: Some( + Identifier { + id: "a", + range: 5..6, + }, + ), + names: [ + Alias { + range: 14..15, + name: Identifier { + id: "b", + range: 14..15, + }, + asname: None, + }, + ], + level: 0, + }, + ), + ImportFrom( + StmtImportFrom { + range: 17..38, + module: Some( + Identifier { + id: "a", + range: 22..23, + }, + ), + names: [ + Alias { + range: 31..37, + name: Identifier { + id: "b", + range: 31..32, + }, + asname: Some( + Identifier { + id: "c", + range: 36..37, + }, + ), + }, + ], + level: 0, + }, + ), + ImportFrom( + StmtImportFrom { + range: 39..58, + module: Some( + Identifier { + id: "a", + range: 44..45, + }, + ), + names: [ + Alias { + range: 53..54, + name: Identifier { + id: "b", + range: 53..54, + }, + asname: None, + }, + Alias { + range: 56..57, + name: Identifier { + id: "c", + range: 56..57, + }, + asname: None, + }, + ], + level: 0, + }, + ), + ], + }, +) +``` +## Errors + + | +1 | from a import b, + | ^ Syntax Error: Trailing comma not allowed +2 | from a import b as c, +3 | from a import b, c, + | + + + | +1 | from a import b, +2 | from a import b as c, + | ^ Syntax Error: Trailing comma not allowed +3 | from a import b, c, + | + + + | +1 | from a import b, +2 | from a import b as c, +3 | from a import b, c, + | ^ Syntax Error: Trailing comma not allowed + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@function_def_empty_body.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@function_def_empty_body.py.snap.new new file mode 100644 index 0000000000..861241eb64 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@function_def_empty_body.py.snap.new @@ -0,0 +1,106 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/function_def_empty_body.py +--- +## AST + +``` +Module( + ModModule { + range: 0..36, + body: [ + FunctionDef( + StmtFunctionDef { + range: 0..10, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 4..7, + }, + type_params: None, + parameters: Parameters { + range: 7..9, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 11..28, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 15..18, + }, + type_params: None, + parameters: Parameters { + range: 18..20, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: Some( + Name( + ExprName { + range: 24..27, + id: "int", + ctx: Load, + }, + ), + ), + body: [], + }, + ), + Assign( + StmtAssign { + range: 29..35, + targets: [ + Name( + ExprName { + range: 29..30, + id: "x", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 33..35, + value: Int( + 42, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | def foo(): +2 | def foo() -> int: + | ^^^ Syntax Error: Expected an indented block after function definition +3 | x = 42 + | + + + | +1 | def foo(): +2 | def foo() -> int: +3 | x = 42 + | ^ Syntax Error: Expected an indented block after function definition + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@function_def_invalid_return_expr.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@function_def_invalid_return_expr.py.snap.new new file mode 100644 index 0000000000..d6f7d3faff --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@function_def_invalid_return_expr.py.snap.new @@ -0,0 +1,182 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/function_def_invalid_return_expr.py +--- +## AST + +``` +Module( + ModModule { + range: 0..74, + body: [ + FunctionDef( + StmtFunctionDef { + range: 0..22, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 4..7, + }, + type_params: None, + parameters: Parameters { + range: 7..9, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: Some( + Starred( + ExprStarred { + range: 13..17, + value: Name( + ExprName { + range: 14..17, + id: "int", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ), + body: [ + Expr( + StmtExpr { + range: 19..22, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 19..22, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 23..47, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 27..30, + }, + type_params: None, + parameters: Parameters { + range: 30..32, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: Some( + Starred( + ExprStarred { + range: 37..41, + value: Name( + ExprName { + range: 38..41, + id: "int", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ), + body: [ + Expr( + StmtExpr { + range: 44..47, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 44..47, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 48..73, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 52..55, + }, + type_params: None, + parameters: Parameters { + range: 55..57, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: Some( + Yield( + ExprYield { + range: 61..68, + value: Some( + Name( + ExprName { + range: 67..68, + id: "x", + ctx: Load, + }, + ), + ), + }, + ), + ), + body: [ + Expr( + StmtExpr { + range: 70..73, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 70..73, + }, + ), + }, + ), + ], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | def foo() -> *int: ... + | ^^^^ Syntax Error: Starred expression cannot be used here +2 | def foo() -> (*int): ... +3 | def foo() -> yield x: ... + | + + + | +1 | def foo() -> *int: ... +2 | def foo() -> (*int): ... + | ^^^^ Syntax Error: Starred expression cannot be used here +3 | def foo() -> yield x: ... + | + + + | +1 | def foo() -> *int: ... +2 | def foo() -> (*int): ... +3 | def foo() -> yield x: ... + | ^^^^^^^ Syntax Error: Yield expression cannot be used here + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@function_def_missing_identifier.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@function_def_missing_identifier.py.snap.new new file mode 100644 index 0000000000..6a4b1bec4e --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@function_def_missing_identifier.py.snap.new @@ -0,0 +1,104 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/function_def_missing_identifier.py +--- +## AST + +``` +Module( + ModModule { + range: 0..31, + body: [ + FunctionDef( + StmtFunctionDef { + range: 0..11, + is_async: false, + decorator_list: [], + name: Identifier { + id: "", + range: 3..3, + }, + type_params: None, + parameters: Parameters { + range: 4..6, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 8..11, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 8..11, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 12..30, + is_async: false, + decorator_list: [], + name: Identifier { + id: "", + range: 15..15, + }, + type_params: None, + parameters: Parameters { + range: 16..18, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: Some( + Name( + ExprName { + range: 22..25, + id: "int", + ctx: Load, + }, + ), + ), + body: [ + Expr( + StmtExpr { + range: 27..30, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 27..30, + }, + ), + }, + ), + ], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | def (): ... + | ^ Syntax Error: Expected an identifier +2 | def () -> int: ... + | + + + | +1 | def (): ... +2 | def () -> int: ... + | ^ Syntax Error: Expected an identifier + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@function_def_missing_return_type.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@function_def_missing_return_type.py.snap.new new file mode 100644 index 0000000000..016cb5c42e --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@function_def_missing_return_type.py.snap.new @@ -0,0 +1,55 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/function_def_missing_return_type.py +--- +## AST + +``` +Module( + ModModule { + range: 0..19, + body: [ + FunctionDef( + StmtFunctionDef { + range: 0..18, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 4..7, + }, + type_params: None, + parameters: Parameters { + range: 7..9, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 15..18, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 15..18, + }, + ), + }, + ), + ], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | def foo() -> : ... + | ^ Syntax Error: Expected an expression + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@function_def_unclosed_parameter_list.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@function_def_unclosed_parameter_list.py.snap.new new file mode 100644 index 0000000000..c22c247465 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@function_def_unclosed_parameter_list.py.snap.new @@ -0,0 +1,234 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/function_def_unclosed_parameter_list.py +--- +## AST + +``` +Module( + ModModule { + range: 0..74, + body: [ + FunctionDef( + StmtFunctionDef { + range: 0..18, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 4..7, + }, + type_params: None, + parameters: Parameters { + range: 7..18, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 8..14, + parameter: Parameter { + range: 8..14, + name: Identifier { + id: "a", + range: 8..9, + }, + annotation: Some( + Name( + ExprName { + range: 11..14, + id: "int", + ctx: Load, + }, + ), + ), + }, + default: None, + }, + ParameterWithDefault { + range: 16..18, + parameter: Parameter { + range: 16..18, + name: Identifier { + id: "b", + range: 16..17, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 19..43, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 23..26, + }, + type_params: None, + parameters: Parameters { + range: 26..28, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Return( + StmtReturn { + range: 34..43, + value: Some( + NumberLiteral( + ExprNumberLiteral { + range: 41..43, + value: Int( + 42, + ), + }, + ), + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 44..74, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 48..51, + }, + type_params: None, + parameters: Parameters { + range: 51..74, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 52..58, + parameter: Parameter { + range: 52..58, + name: Identifier { + id: "a", + range: 52..53, + }, + annotation: Some( + Name( + ExprName { + range: 55..58, + id: "int", + ctx: Load, + }, + ), + ), + }, + default: None, + }, + ParameterWithDefault { + range: 60..66, + parameter: Parameter { + range: 60..66, + name: Identifier { + id: "b", + range: 60..61, + }, + annotation: Some( + Name( + ExprName { + range: 63..66, + id: "str", + ctx: Load, + }, + ), + ), + }, + default: None, + }, + ParameterWithDefault { + range: 67..73, + parameter: Parameter { + range: 67..68, + name: Identifier { + id: "x", + range: 67..68, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 71..73, + value: Int( + 10, + ), + }, + ), + ), + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | def foo(a: int, b: +2 | def foo(): + | ^^^ Syntax Error: Expected an expression +3 | return 42 +4 | def foo(a: int, b: str + | + + + | +1 | def foo(a: int, b: + | ^ Syntax Error: Expected ')', found newline +2 | def foo(): +3 | return 42 +4 | def foo(a: int, b: str + | + + + | +1 | def foo(a: int, b: +2 | def foo(): + | ^^^ Syntax Error: Expected an indented block after function definition +3 | return 42 +4 | def foo(a: int, b: str + | + + + | +3 | return 42 +4 | def foo(a: int, b: str +5 | x = 10 + | ^ Syntax Error: Expected ',', found name + | + + + | +4 | def foo(a: int, b: str +5 | x = 10 + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@function_def_unclosed_type_param_list.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@function_def_unclosed_type_param_list.py.snap.new new file mode 100644 index 0000000000..60875d3762 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@function_def_unclosed_type_param_list.py.snap.new @@ -0,0 +1,148 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/function_def_unclosed_type_param_list.py +--- +## AST + +``` +Module( + ModModule { + range: 0..47, + body: [ + FunctionDef( + StmtFunctionDef { + range: 0..39, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 4..7, + }, + type_params: Some( + TypeParams { + range: 7..15, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 8..10, + name: Identifier { + id: "T1", + range: 8..10, + }, + bound: None, + default: None, + }, + ), + TypeVarTuple( + TypeParamTypeVarTuple { + range: 12..15, + name: Identifier { + id: "T2", + range: 13..15, + }, + default: None, + }, + ), + ], + }, + ), + parameters: Parameters { + range: 15..21, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 16..17, + parameter: Parameter { + range: 16..17, + name: Identifier { + id: "a", + range: 16..17, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 19..20, + parameter: Parameter { + range: 19..20, + name: Identifier { + id: "b", + range: 19..20, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Return( + StmtReturn { + range: 27..39, + value: Some( + BinOp( + ExprBinOp { + range: 34..39, + left: Name( + ExprName { + range: 34..35, + id: "a", + ctx: Load, + }, + ), + op: Add, + right: Name( + ExprName { + range: 38..39, + id: "b", + ctx: Load, + }, + ), + }, + ), + ), + }, + ), + ], + }, + ), + Assign( + StmtAssign { + range: 40..46, + targets: [ + Name( + ExprName { + range: 40..41, + id: "x", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 44..46, + value: Int( + 10, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | def foo[T1, *T2(a, b): + | ^ Syntax Error: Expected ']', found '(' +2 | return a + b +3 | x = 10 + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@function_def_unparenthesized_return_types.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@function_def_unparenthesized_return_types.py.snap.new new file mode 100644 index 0000000000..03a6782e79 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@function_def_unparenthesized_return_types.py.snap.new @@ -0,0 +1,137 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/function_def_unparenthesized_return_types.py +--- +## AST + +``` +Module( + ModModule { + range: 0..50, + body: [ + FunctionDef( + StmtFunctionDef { + range: 0..22, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 4..7, + }, + type_params: None, + parameters: Parameters { + range: 7..9, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: Some( + Tuple( + ExprTuple { + range: 13..17, + elts: [ + Name( + ExprName { + range: 13..16, + id: "int", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + ), + body: [ + Expr( + StmtExpr { + range: 19..22, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 19..22, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 23..49, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 27..30, + }, + type_params: None, + parameters: Parameters { + range: 30..32, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: Some( + Tuple( + ExprTuple { + range: 36..44, + elts: [ + Name( + ExprName { + range: 36..39, + id: "int", + ctx: Load, + }, + ), + Name( + ExprName { + range: 41..44, + id: "str", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + ), + body: [ + Expr( + StmtExpr { + range: 46..49, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 46..49, + }, + ), + }, + ), + ], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | def foo() -> int,: ... + | ^^^^ Syntax Error: Multiple return types must be parenthesized +2 | def foo() -> int, str: ... + | + + + | +1 | def foo() -> int,: ... +2 | def foo() -> int, str: ... + | ^^^^^^^^ Syntax Error: Multiple return types must be parenthesized + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@global_stmt_expression.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@global_stmt_expression.py.snap.new new file mode 100644 index 0000000000..4143cc0634 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@global_stmt_expression.py.snap.new @@ -0,0 +1,52 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/global_stmt_expression.py +--- +## AST + +``` +Module( + ModModule { + range: 0..13, + body: [ + Global( + StmtGlobal { + range: 0..8, + names: [ + Identifier { + id: "x", + range: 7..8, + }, + ], + }, + ), + Expr( + StmtExpr { + range: 9..12, + value: UnaryOp( + ExprUnaryOp { + range: 9..12, + op: UAdd, + operand: NumberLiteral( + ExprNumberLiteral { + range: 11..12, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | global x + 1 + | ^ Syntax Error: Simple statements must be separated by newlines or semicolons + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@global_stmt_trailing_comma.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@global_stmt_trailing_comma.py.snap.new new file mode 100644 index 0000000000..c831a7403f --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@global_stmt_trailing_comma.py.snap.new @@ -0,0 +1,80 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/global_stmt_trailing_comma.py +--- +## AST + +``` +Module( + ModModule { + range: 0..32, + body: [ + Global( + StmtGlobal { + range: 0..8, + names: [], + }, + ), + Global( + StmtGlobal { + range: 9..18, + names: [ + Identifier { + id: "x", + range: 16..17, + }, + ], + }, + ), + Global( + StmtGlobal { + range: 19..31, + names: [ + Identifier { + id: "x", + range: 26..27, + }, + Identifier { + id: "y", + range: 29..30, + }, + ], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | global , + | ^ Syntax Error: Expected an identifier +2 | global x, +3 | global x, y, + | + + + | +1 | global , + | ^ Syntax Error: Global statement must have at least one name +2 | global x, +3 | global x, y, + | + + + | +1 | global , +2 | global x, + | ^ Syntax Error: Trailing comma not allowed +3 | global x, y, + | + + + | +1 | global , +2 | global x, +3 | global x, y, + | ^ Syntax Error: Trailing comma not allowed + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@if_stmt_elif_missing_colon.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@if_stmt_elif_missing_colon.py.snap.new new file mode 100644 index 0000000000..9b5b55d7a4 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@if_stmt_elif_missing_colon.py.snap.new @@ -0,0 +1,78 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/if_stmt_elif_missing_colon.py +--- +## AST + +``` +Module( + ModModule { + range: 0..46, + body: [ + If( + StmtIf { + range: 0..45, + test: Name( + ExprName { + range: 3..4, + id: "x", + ctx: Load, + }, + ), + body: [ + Pass( + StmtPass { + range: 10..14, + }, + ), + ], + elif_else_clauses: [ + ElifElseClause { + range: 15..30, + test: Some( + Name( + ExprName { + range: 20..21, + id: "y", + ctx: Load, + }, + ), + ), + body: [ + Pass( + StmtPass { + range: 26..30, + }, + ), + ], + }, + ElifElseClause { + range: 31..45, + test: None, + body: [ + Pass( + StmtPass { + range: 41..45, + }, + ), + ], + }, + ], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | if x: +2 | pass +3 | elif y + | ^ Syntax Error: Expected ':', found newline +4 | pass +5 | else: +6 | pass + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@if_stmt_invalid_elif_test_expr.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@if_stmt_invalid_elif_test_expr.py.snap.new new file mode 100644 index 0000000000..6a1bc76fdc --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@if_stmt_invalid_elif_test_expr.py.snap.new @@ -0,0 +1,107 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/if_stmt_invalid_elif_test_expr.py +--- +## AST + +``` +Module( + ModModule { + range: 0..56, + body: [ + If( + StmtIf { + range: 0..55, + test: Name( + ExprName { + range: 3..4, + id: "x", + ctx: Load, + }, + ), + body: [ + Pass( + StmtPass { + range: 10..14, + }, + ), + ], + elif_else_clauses: [ + ElifElseClause { + range: 15..32, + test: Some( + Starred( + ExprStarred { + range: 20..22, + value: Name( + ExprName { + range: 21..22, + id: "x", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ), + body: [ + Pass( + StmtPass { + range: 28..32, + }, + ), + ], + }, + ElifElseClause { + range: 33..55, + test: Some( + Yield( + ExprYield { + range: 38..45, + value: Some( + Name( + ExprName { + range: 44..45, + id: "x", + ctx: Load, + }, + ), + ), + }, + ), + ), + body: [ + Pass( + StmtPass { + range: 51..55, + }, + ), + ], + }, + ], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | if x: +2 | pass +3 | elif *x: + | ^^ Syntax Error: Starred expression cannot be used here +4 | pass +5 | elif yield x: + | + + + | +3 | elif *x: +4 | pass +5 | elif yield x: + | ^^^^^^^ Syntax Error: Yield expression cannot be used here +6 | pass + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@if_stmt_invalid_test_expr.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@if_stmt_invalid_test_expr.py.snap.new new file mode 100644 index 0000000000..d1ef3825a6 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@if_stmt_invalid_test_expr.py.snap.new @@ -0,0 +1,133 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/if_stmt_invalid_test_expr.py +--- +## AST + +``` +Module( + ModModule { + range: 0..48, + body: [ + If( + StmtIf { + range: 0..10, + test: Starred( + ExprStarred { + range: 3..5, + value: Name( + ExprName { + range: 4..5, + id: "x", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + body: [ + Expr( + StmtExpr { + range: 7..10, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 7..10, + }, + ), + }, + ), + ], + elif_else_clauses: [], + }, + ), + If( + StmtIf { + range: 11..26, + test: Yield( + ExprYield { + range: 14..21, + value: Some( + Name( + ExprName { + range: 20..21, + id: "x", + ctx: Load, + }, + ), + ), + }, + ), + body: [ + Expr( + StmtExpr { + range: 23..26, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 23..26, + }, + ), + }, + ), + ], + elif_else_clauses: [], + }, + ), + If( + StmtIf { + range: 27..47, + test: YieldFrom( + ExprYieldFrom { + range: 30..42, + value: Name( + ExprName { + range: 41..42, + id: "x", + ctx: Load, + }, + ), + }, + ), + body: [ + Expr( + StmtExpr { + range: 44..47, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 44..47, + }, + ), + }, + ), + ], + elif_else_clauses: [], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | if *x: ... + | ^^ Syntax Error: Starred expression cannot be used here +2 | if yield x: ... +3 | if yield from x: ... + | + + + | +1 | if *x: ... +2 | if yield x: ... + | ^^^^^^^ Syntax Error: Yield expression cannot be used here +3 | if yield from x: ... + | + + + | +1 | if *x: ... +2 | if yield x: ... +3 | if yield from x: ... + | ^^^^^^^^^^^^ Syntax Error: Yield expression cannot be used here + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@if_stmt_missing_colon.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@if_stmt_missing_colon.py.snap.new new file mode 100644 index 0000000000..76603b01c4 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@if_stmt_missing_colon.py.snap.new @@ -0,0 +1,99 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/if_stmt_missing_colon.py +--- +## AST + +``` +Module( + ModModule { + range: 0..25, + body: [ + If( + StmtIf { + range: 0..4, + test: Name( + ExprName { + range: 3..4, + id: "x", + ctx: Load, + }, + ), + body: [], + elif_else_clauses: [], + }, + ), + If( + StmtIf { + range: 5..18, + test: Name( + ExprName { + range: 8..9, + id: "x", + ctx: Load, + }, + ), + body: [ + Pass( + StmtPass { + range: 14..18, + }, + ), + ], + elif_else_clauses: [], + }, + ), + Assign( + StmtAssign { + range: 19..24, + targets: [ + Name( + ExprName { + range: 19..20, + id: "a", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 23..24, + value: Int( + 1, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | if x + | ^ Syntax Error: Expected ':', found newline +2 | if x +3 | pass +4 | a = 1 + | + + + | +1 | if x +2 | if x + | ^^ Syntax Error: Expected an indented block after `if` statement +3 | pass +4 | a = 1 + | + + + | +1 | if x +2 | if x + | ^ Syntax Error: Expected ':', found newline +3 | pass +4 | a = 1 + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@if_stmt_missing_test.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@if_stmt_missing_test.py.snap.new new file mode 100644 index 0000000000..8b61f7f020 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@if_stmt_missing_test.py.snap.new @@ -0,0 +1,47 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/if_stmt_missing_test.py +--- +## AST + +``` +Module( + ModModule { + range: 0..9, + body: [ + If( + StmtIf { + range: 0..8, + test: Name( + ExprName { + range: 2..2, + id: "", + ctx: Invalid, + }, + ), + body: [ + Expr( + StmtExpr { + range: 5..8, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 5..8, + }, + ), + }, + ), + ], + elif_else_clauses: [], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | if : ... + | ^ Syntax Error: Expected an expression + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@if_stmt_misspelled_elif.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@if_stmt_misspelled_elif.py.snap.new new file mode 100644 index 0000000000..6c4a9e0934 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@if_stmt_misspelled_elif.py.snap.new @@ -0,0 +1,128 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/if_stmt_misspelled_elif.py +--- +## AST + +``` +Module( + ModModule { + range: 0..47, + body: [ + If( + StmtIf { + range: 0..17, + test: BooleanLiteral( + ExprBooleanLiteral { + range: 3..7, + value: true, + }, + ), + body: [ + Pass( + StmtPass { + range: 13..17, + }, + ), + ], + elif_else_clauses: [], + }, + ), + AnnAssign( + StmtAnnAssign { + range: 18..22, + target: Name( + ExprName { + range: 18..21, + id: "elf", + ctx: Store, + }, + ), + annotation: Name( + ExprName { + range: 22..22, + id: "", + ctx: Invalid, + }, + ), + value: None, + simple: true, + }, + ), + Pass( + StmtPass { + range: 27..31, + }, + ), + Pass( + StmtPass { + range: 42..46, + }, + ), + ], + }, +) +``` +## Errors + + | +1 | if True: +2 | pass +3 | elf: + | ^ Syntax Error: Expected an expression +4 | pass +5 | else: +6 | pass + | + + + | +2 | pass +3 | elf: +4 | pass + | ^^^^ Syntax Error: Unexpected indentation +5 | else: +6 | pass + | + + + | +3 | elf: +4 | pass +5 | else: + | Syntax Error: Expected a statement +6 | pass + | + + + | +3 | elf: +4 | pass +5 | else: + | ^ Syntax Error: Expected a statement +6 | pass + | + + + | +3 | elf: +4 | pass +5 | else: + | ^ Syntax Error: Expected a statement +6 | pass + | + + + | +4 | pass +5 | else: +6 | pass + | ^^^^ Syntax Error: Unexpected indentation + | + + + | +5 | else: +6 | pass + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@implicitly_concatenated_unterminated_string.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@implicitly_concatenated_unterminated_string.py.snap.new new file mode 100644 index 0000000000..4c4d3542ce --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@implicitly_concatenated_unterminated_string.py.snap.new @@ -0,0 +1,179 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/implicitly_concatenated_unterminated_string.py +--- +## AST + +``` +Module( + ModModule { + range: 0..47, + body: [ + Expr( + StmtExpr { + range: 0..7, + value: StringLiteral( + ExprStringLiteral { + range: 0..7, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 0..7, + value: "hello", + flags: StringLiteralFlags { + quote_style: Single, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 15..20, + value: BinOp( + ExprBinOp { + range: 15..20, + left: NumberLiteral( + ExprNumberLiteral { + range: 15..16, + value: Int( + 1, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 19..20, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 21..40, + value: FString( + ExprFString { + range: 21..40, + value: FStringValue { + inner: Concatenated( + [ + Literal( + StringLiteral { + range: 21..28, + value: "hello", + flags: StringLiteralFlags { + quote_style: Single, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + FString( + FString { + range: 29..40, + elements: [ + Literal( + FStringLiteralElement { + range: 31..37, + value: "world ", + }, + ), + Expression( + FStringExpressionElement { + range: 37..40, + expression: Name( + ExprName { + range: 38..39, + id: "x", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + ], + flags: FStringFlags { + quote_style: Single, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ], + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 41..46, + value: BinOp( + ExprBinOp { + range: 41..46, + left: NumberLiteral( + ExprNumberLiteral { + range: 41..42, + value: Int( + 2, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 45..46, + value: Int( + 2, + ), + }, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | 'hello' 'world + | ^^^^^^ Syntax Error: missing closing quote in string literal +2 | 1 + 1 +3 | 'hello' f'world {x} + | + + + | +1 | 'hello' 'world + | ^ Syntax Error: Expected a statement +2 | 1 + 1 +3 | 'hello' f'world {x} +4 | 2 + 2 + | + + + | +1 | 'hello' 'world +2 | 1 + 1 +3 | 'hello' f'world {x} + | Syntax Error: f-string: unterminated string +4 | 2 + 2 + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@implicitly_concatenated_unterminated_string_multiline.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@implicitly_concatenated_unterminated_string_multiline.py.snap.new new file mode 100644 index 0000000000..90aa86d45e --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@implicitly_concatenated_unterminated_string_multiline.py.snap.new @@ -0,0 +1,244 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/implicitly_concatenated_unterminated_string_multiline.py +--- +## AST + +``` +Module( + ModModule { + range: 0..85, + body: [ + Expr( + StmtExpr { + range: 0..31, + value: FString( + ExprFString { + range: 6..31, + value: FStringValue { + inner: Concatenated( + [ + Literal( + StringLiteral { + range: 6..13, + value: "hello", + flags: StringLiteralFlags { + quote_style: Single, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + FString( + FString { + range: 18..31, + elements: [ + Literal( + FStringLiteralElement { + range: 20..26, + value: "world ", + }, + ), + Expression( + FStringExpressionElement { + range: 26..29, + expression: Name( + ExprName { + range: 27..28, + id: "x", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + ], + flags: FStringFlags { + quote_style: Single, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ], + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 32..37, + value: BinOp( + ExprBinOp { + range: 32..37, + left: NumberLiteral( + ExprNumberLiteral { + range: 32..33, + value: Int( + 1, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 36..37, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 38..51, + value: StringLiteral( + ExprStringLiteral { + range: 44..51, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 44..51, + value: "first", + flags: StringLiteralFlags { + quote_style: Single, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 68..76, + value: FString( + ExprFString { + range: 68..76, + value: FStringValue { + inner: Single( + FString( + FString { + range: 68..76, + elements: [ + Literal( + FStringLiteralElement { + range: 70..75, + value: "third", + }, + ), + ], + flags: FStringFlags { + quote_style: Single, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 79..84, + value: BinOp( + ExprBinOp { + range: 79..84, + left: NumberLiteral( + ExprNumberLiteral { + range: 79..80, + value: Int( + 2, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 83..84, + value: Int( + 2, + ), + }, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | ( +2 | 'hello' +3 | f'world {x} + | Syntax Error: f-string: unterminated string +4 | ) +5 | 1 + 1 + | + + + | +2 | 'hello' +3 | f'world {x} +4 | ) + | ^ Syntax Error: Expected an f-string element or the end of the f-string +5 | 1 + 1 +6 | ( + | + + + | +2 | 'hello' +3 | f'world {x} +4 | ) + | ^ Syntax Error: Expected FStringEnd, found newline +5 | 1 + 1 +6 | ( +7 | 'first' + | + + + | + 6 | ( + 7 | 'first' + 8 | 'second + | ^^^^^^^ Syntax Error: missing closing quote in string literal + 9 | f'third' +10 | ) + | + + + | + 8 | 'second + 9 | f'third' +10 | ) + | ^ Syntax Error: Expected a statement +11 | 2 + 2 + | + + + | + 8 | 'second + 9 | f'third' +10 | ) + | ^ Syntax Error: Expected a statement +11 | 2 + 2 + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@import_alias_missing_asname.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@import_alias_missing_asname.py.snap.new new file mode 100644 index 0000000000..c444fde1d0 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@import_alias_missing_asname.py.snap.new @@ -0,0 +1,37 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/import_alias_missing_asname.py +--- +## AST + +``` +Module( + ModModule { + range: 0..12, + body: [ + Import( + StmtImport { + range: 0..11, + names: [ + Alias { + range: 7..11, + name: Identifier { + id: "x", + range: 7..8, + }, + asname: None, + }, + ], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | import x as + | ^ Syntax Error: Expected symbol after `as` + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@import_stmt_parenthesized_names.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@import_stmt_parenthesized_names.py.snap.new new file mode 100644 index 0000000000..fc2b368d10 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@import_stmt_parenthesized_names.py.snap.new @@ -0,0 +1,82 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/import_stmt_parenthesized_names.py +--- +## AST + +``` +Module( + ModModule { + range: 0..25, + body: [ + Import( + StmtImport { + range: 0..6, + names: [], + }, + ), + Expr( + StmtExpr { + range: 7..10, + value: Name( + ExprName { + range: 8..9, + id: "a", + ctx: Load, + }, + ), + }, + ), + Import( + StmtImport { + range: 11..17, + names: [], + }, + ), + Expr( + StmtExpr { + range: 18..24, + value: Tuple( + ExprTuple { + range: 18..24, + elts: [ + Name( + ExprName { + range: 19..20, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 22..23, + id: "b", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | import (a) + | ^ Syntax Error: Expected one or more symbol names after import +2 | import (a, b) + | + + + | +1 | import (a) +2 | import (a, b) + | ^ Syntax Error: Expected one or more symbol names after import + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@import_stmt_star_import.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@import_stmt_star_import.py.snap.new new file mode 100644 index 0000000000..3bbb395f36 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@import_stmt_star_import.py.snap.new @@ -0,0 +1,124 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/import_stmt_star_import.py +--- +## AST + +``` +Module( + ModModule { + range: 0..24, + body: [ + Import( + StmtImport { + range: 0..6, + names: [], + }, + ), + Expr( + StmtExpr { + range: 7..8, + value: Starred( + ExprStarred { + range: 7..8, + value: Name( + ExprName { + range: 8..8, + id: "", + ctx: Invalid, + }, + ), + ctx: Load, + }, + ), + }, + ), + Import( + StmtImport { + range: 9..18, + names: [ + Alias { + range: 16..17, + name: Identifier { + id: "x", + range: 16..17, + }, + asname: None, + }, + ], + }, + ), + Expr( + StmtExpr { + range: 19..23, + value: Tuple( + ExprTuple { + range: 19..23, + elts: [ + Starred( + ExprStarred { + range: 19..20, + value: Name( + ExprName { + range: 20..20, + id: "", + ctx: Invalid, + }, + ), + ctx: Load, + }, + ), + Name( + ExprName { + range: 22..23, + id: "y", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | import * + | ^ Syntax Error: Expected one or more symbol names after import +2 | import x, *, y + | + + + | +1 | import * + | ^ Syntax Error: Expected an expression +2 | import x, *, y + | + + + | +1 | import * +2 | import x, *, y + | ^ Syntax Error: Trailing comma not allowed + | + + + | +1 | import * +2 | import x, *, y + | ^ Syntax Error: Simple statements must be separated by newlines or semicolons + | + + + | +1 | import * +2 | import x, *, y + | ^ Syntax Error: Expected an expression + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@import_stmt_trailing_comma.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@import_stmt_trailing_comma.py.snap.new new file mode 100644 index 0000000000..8ac9822392 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@import_stmt_trailing_comma.py.snap.new @@ -0,0 +1,66 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/import_stmt_trailing_comma.py +--- +## AST + +``` +Module( + ModModule { + range: 0..22, + body: [ + Import( + StmtImport { + range: 0..8, + names: [], + }, + ), + Import( + StmtImport { + range: 9..21, + names: [ + Alias { + range: 16..17, + name: Identifier { + id: "x", + range: 16..17, + }, + asname: None, + }, + Alias { + range: 19..20, + name: Identifier { + id: "y", + range: 19..20, + }, + asname: None, + }, + ], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | import , + | ^ Syntax Error: Expected an import name +2 | import x, y, + | + + + | +1 | import , + | ^ Syntax Error: Expected one or more symbol names after import +2 | import x, y, + | + + + | +1 | import , +2 | import x, y, + | ^ Syntax Error: Trailing comma not allowed + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@invalid_del_target.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@invalid_del_target.py.snap.new new file mode 100644 index 0000000000..f213dd8d4c --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@invalid_del_target.py.snap.new @@ -0,0 +1,270 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/invalid_del_target.py +--- +## AST + +``` +Module( + ModModule { + range: 0..75, + body: [ + Delete( + StmtDelete { + range: 0..9, + targets: [ + BinOp( + ExprBinOp { + range: 4..9, + left: Name( + ExprName { + range: 4..5, + id: "x", + ctx: Load, + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 8..9, + value: Int( + 1, + ), + }, + ), + }, + ), + ], + }, + ), + Delete( + StmtDelete { + range: 10..22, + targets: [ + Dict( + ExprDict { + range: 14..22, + items: [ + DictItem { + key: Some( + StringLiteral( + ExprStringLiteral { + range: 15..18, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 15..18, + value: "x", + flags: StringLiteralFlags { + quote_style: Single, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 20..21, + value: Int( + 1, + ), + }, + ), + }, + ], + }, + ), + ], + }, + ), + Delete( + StmtDelete { + range: 23..37, + targets: [ + Set( + ExprSet { + range: 27..37, + elts: [ + StringLiteral( + ExprStringLiteral { + range: 28..31, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 28..31, + value: "x", + flags: StringLiteralFlags { + quote_style: Single, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + StringLiteral( + ExprStringLiteral { + range: 33..36, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 33..36, + value: "y", + flags: StringLiteralFlags { + quote_style: Single, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + ], + }, + ), + ], + }, + ), + Delete( + StmtDelete { + range: 38..74, + targets: [ + NoneLiteral( + ExprNoneLiteral { + range: 42..46, + }, + ), + BooleanLiteral( + ExprBooleanLiteral { + range: 48..52, + value: true, + }, + ), + BooleanLiteral( + ExprBooleanLiteral { + range: 54..59, + value: false, + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 61..62, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 64..67, + value: Float( + 1.0, + ), + }, + ), + StringLiteral( + ExprStringLiteral { + range: 69..74, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 69..74, + value: "abc", + flags: StringLiteralFlags { + quote_style: Double, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + ], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | del x + 1 + | ^^^^^ Syntax Error: Invalid delete target +2 | del {'x': 1} +3 | del {'x', 'y'} + | + + + | +1 | del x + 1 +2 | del {'x': 1} + | ^^^^^^^^ Syntax Error: Invalid delete target +3 | del {'x', 'y'} +4 | del None, True, False, 1, 1.0, "abc" + | + + + | +1 | del x + 1 +2 | del {'x': 1} +3 | del {'x', 'y'} + | ^^^^^^^^^^ Syntax Error: Invalid delete target +4 | del None, True, False, 1, 1.0, "abc" + | + + + | +2 | del {'x': 1} +3 | del {'x', 'y'} +4 | del None, True, False, 1, 1.0, "abc" + | ^^^^ Syntax Error: Invalid delete target + | + + + | +2 | del {'x': 1} +3 | del {'x', 'y'} +4 | del None, True, False, 1, 1.0, "abc" + | ^^^^ Syntax Error: Invalid delete target + | + + + | +2 | del {'x': 1} +3 | del {'x', 'y'} +4 | del None, True, False, 1, 1.0, "abc" + | ^^^^^ Syntax Error: Invalid delete target + | + + + | +2 | del {'x': 1} +3 | del {'x', 'y'} +4 | del None, True, False, 1, 1.0, "abc" + | ^ Syntax Error: Invalid delete target + | + + + | +2 | del {'x': 1} +3 | del {'x', 'y'} +4 | del None, True, False, 1, 1.0, "abc" + | ^^^ Syntax Error: Invalid delete target + | + + + | +2 | del {'x': 1} +3 | del {'x', 'y'} +4 | del None, True, False, 1, 1.0, "abc" + | ^^^^^ Syntax Error: Invalid delete target + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@lambda_body_with_starred_expr.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@lambda_body_with_starred_expr.py.snap.new new file mode 100644 index 0000000000..464c1ee863 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@lambda_body_with_starred_expr.py.snap.new @@ -0,0 +1,274 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/lambda_body_with_starred_expr.py +--- +## AST + +``` +Module( + ModModule { + range: 0..62, + body: [ + Expr( + StmtExpr { + range: 0..12, + value: Lambda( + ExprLambda { + range: 0..12, + parameters: Some( + Parameters { + range: 7..8, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 7..8, + parameter: Parameter { + range: 7..8, + name: Identifier { + id: "x", + range: 7..8, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: Starred( + ExprStarred { + range: 10..12, + value: Name( + ExprName { + range: 11..12, + id: "y", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 13..26, + value: Tuple( + ExprTuple { + range: 13..26, + elts: [ + Lambda( + ExprLambda { + range: 13..25, + parameters: Some( + Parameters { + range: 20..21, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 20..21, + parameter: Parameter { + range: 20..21, + name: Identifier { + id: "x", + range: 20..21, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: Starred( + ExprStarred { + range: 23..25, + value: Name( + ExprName { + range: 24..25, + id: "y", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 27..42, + value: Tuple( + ExprTuple { + range: 27..42, + elts: [ + Lambda( + ExprLambda { + range: 27..39, + parameters: Some( + Parameters { + range: 34..35, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 34..35, + parameter: Parameter { + range: 34..35, + name: Identifier { + id: "x", + range: 34..35, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: Starred( + ExprStarred { + range: 37..39, + value: Name( + ExprName { + range: 38..39, + id: "y", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + }, + ), + Name( + ExprName { + range: 41..42, + id: "z", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 43..61, + value: Lambda( + ExprLambda { + range: 43..61, + parameters: Some( + Parameters { + range: 50..51, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 50..51, + parameter: Parameter { + range: 50..51, + name: Identifier { + id: "x", + range: 50..51, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: Starred( + ExprStarred { + range: 53..61, + value: BoolOp( + ExprBoolOp { + range: 54..61, + op: And, + values: [ + Name( + ExprName { + range: 54..55, + id: "y", + ctx: Load, + }, + ), + Name( + ExprName { + range: 60..61, + id: "z", + ctx: Load, + }, + ), + ], + }, + ), + ctx: Load, + }, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | lambda x: *y + | ^^ Syntax Error: Starred expression cannot be used here +2 | lambda x: *y, +3 | lambda x: *y, z + | + + + | +1 | lambda x: *y +2 | lambda x: *y, + | ^^ Syntax Error: Starred expression cannot be used here +3 | lambda x: *y, z +4 | lambda x: *y and z + | + + + | +1 | lambda x: *y +2 | lambda x: *y, +3 | lambda x: *y, z + | ^^ Syntax Error: Starred expression cannot be used here +4 | lambda x: *y and z + | + + + | +2 | lambda x: *y, +3 | lambda x: *y, z +4 | lambda x: *y and z + | ^^^^^^^^ Syntax Error: Starred expression cannot be used here + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@lambda_body_with_yield_expr.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@lambda_body_with_yield_expr.py.snap.new new file mode 100644 index 0000000000..b8bb377bb9 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@lambda_body_with_yield_expr.py.snap.new @@ -0,0 +1,122 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/lambda_body_with_yield_expr.py +--- +## AST + +``` +Module( + ModModule { + range: 0..41, + body: [ + Expr( + StmtExpr { + range: 0..17, + value: Lambda( + ExprLambda { + range: 0..17, + parameters: Some( + Parameters { + range: 7..8, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 7..8, + parameter: Parameter { + range: 7..8, + name: Identifier { + id: "x", + range: 7..8, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: Yield( + ExprYield { + range: 10..17, + value: Some( + Name( + ExprName { + range: 16..17, + id: "y", + ctx: Load, + }, + ), + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 18..40, + value: Lambda( + ExprLambda { + range: 18..40, + parameters: Some( + Parameters { + range: 25..26, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 25..26, + parameter: Parameter { + range: 25..26, + name: Identifier { + id: "x", + range: 25..26, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: YieldFrom( + ExprYieldFrom { + range: 28..40, + value: Name( + ExprName { + range: 39..40, + id: "y", + ctx: Load, + }, + ), + }, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | lambda x: yield y + | ^^^^^^^ Syntax Error: Yield expression cannot be used here +2 | lambda x: yield from y + | + + + | +1 | lambda x: yield y +2 | lambda x: yield from y + | ^^^^^^^^^^^^ Syntax Error: Yield expression cannot be used here + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@match_classify_as_keyword.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@match_classify_as_keyword.py.snap.new new file mode 100644 index 0000000000..54a2077dde --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@match_classify_as_keyword.py.snap.new @@ -0,0 +1,67 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/match_classify_as_keyword.py +--- +## AST + +``` +Module( + ModModule { + range: 0..33, + body: [ + Match( + StmtMatch { + range: 0..32, + subject: Yield( + ExprYield { + range: 6..15, + value: Some( + Name( + ExprName { + range: 12..15, + id: "foo", + ctx: Load, + }, + ), + ), + }, + ), + cases: [ + MatchCase { + range: 21..32, + pattern: MatchAs( + PatternMatchAs { + range: 26..27, + pattern: None, + name: None, + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 29..32, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 29..32, + }, + ), + }, + ), + ], + }, + ], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | match yield foo: + | ^^^^^^^^^ Syntax Error: Yield expression cannot be used here +2 | case _: ... + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@match_classify_as_keyword_or_identifier.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@match_classify_as_keyword_or_identifier.py.snap.new new file mode 100644 index 0000000000..740fa30bf2 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@match_classify_as_keyword_or_identifier.py.snap.new @@ -0,0 +1,66 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/match_classify_as_keyword_or_identifier.py +--- +## AST + +``` +Module( + ModModule { + range: 0..39, + body: [ + Match( + StmtMatch { + range: 0..38, + subject: Starred( + ExprStarred { + range: 6..10, + value: Name( + ExprName { + range: 7..10, + id: "foo", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 27..38, + pattern: MatchAs( + PatternMatchAs { + range: 32..33, + pattern: None, + name: None, + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 35..38, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 35..38, + }, + ), + }, + ), + ], + }, + ], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | match *foo: # Keyword + | ^^^^ Syntax Error: Starred expression cannot be used here +2 | case _: ... + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@match_stmt_expect_indented_block.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@match_stmt_expect_indented_block.py.snap.new new file mode 100644 index 0000000000..4f0fe0183f --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@match_stmt_expect_indented_block.py.snap.new @@ -0,0 +1,66 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/match_stmt_expect_indented_block.py +--- +## AST + +``` +Module( + ModModule { + range: 0..23, + body: [ + Match( + StmtMatch { + range: 0..22, + subject: Name( + ExprName { + range: 6..9, + id: "foo", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 11..22, + pattern: MatchAs( + PatternMatchAs { + range: 16..17, + pattern: None, + name: None, + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 19..22, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 19..22, + }, + ), + }, + ), + ], + }, + ], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | match foo: +2 | case _: ... + | ^^^^ Syntax Error: Expected an indented block after `match` statement + | + + + | +1 | match foo: +2 | case _: ... + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@match_stmt_expected_case_block.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@match_stmt_expected_case_block.py.snap.new new file mode 100644 index 0000000000..fe5038381a --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@match_stmt_expected_case_block.py.snap.new @@ -0,0 +1,135 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/match_stmt_expected_case_block.py +--- +## AST + +``` +Module( + ModModule { + range: 0..61, + body: [ + Match( + StmtMatch { + range: 0..13, + subject: Name( + ExprName { + range: 6..7, + id: "x", + ctx: Load, + }, + ), + cases: [], + }, + ), + Assign( + StmtAssign { + range: 13..18, + targets: [ + Name( + ExprName { + range: 13..14, + id: "x", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 17..18, + value: Int( + 1, + ), + }, + ), + }, + ), + Match( + StmtMatch { + range: 19..32, + subject: Name( + ExprName { + range: 25..26, + id: "x", + ctx: Load, + }, + ), + cases: [], + }, + ), + Match( + StmtMatch { + range: 32..60, + subject: Name( + ExprName { + range: 38..39, + id: "y", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 49..60, + pattern: MatchAs( + PatternMatchAs { + range: 54..55, + pattern: None, + name: None, + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 57..60, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 57..60, + }, + ), + }, + ), + ], + }, + ], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | match x: +2 | x = 1 + | ^ Syntax Error: Expected `case` block +3 | match x: +4 | match y: + | + + + | +1 | match x: +2 | x = 1 +3 | match x: + | Syntax Error: Expected a statement +4 | match y: +5 | case _: ... + | + + + | +2 | x = 1 +3 | match x: +4 | match y: + | ^^^^^ Syntax Error: Expected `case` block +5 | case _: ... + | + + + | +4 | match y: +5 | case _: ... + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@match_stmt_invalid_guard_expr.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@match_stmt_invalid_guard_expr.py.snap.new new file mode 100644 index 0000000000..4ae71366b8 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@match_stmt_invalid_guard_expr.py.snap.new @@ -0,0 +1,212 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/match_stmt_invalid_guard_expr.py +--- +## AST + +``` +Module( + ModModule { + range: 0..100, + body: [ + Match( + StmtMatch { + range: 0..30, + subject: Name( + ExprName { + range: 6..7, + id: "x", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 13..30, + pattern: MatchAs( + PatternMatchAs { + range: 18..19, + pattern: None, + name: Some( + Identifier { + id: "y", + range: 18..19, + }, + ), + }, + ), + guard: Some( + Starred( + ExprStarred { + range: 23..25, + value: Name( + ExprName { + range: 24..25, + id: "a", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ), + body: [ + Expr( + StmtExpr { + range: 27..30, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 27..30, + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 31..63, + subject: Name( + ExprName { + range: 37..38, + id: "x", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 44..63, + pattern: MatchAs( + PatternMatchAs { + range: 49..50, + pattern: None, + name: Some( + Identifier { + id: "y", + range: 49..50, + }, + ), + }, + ), + guard: Some( + Starred( + ExprStarred { + range: 55..57, + value: Name( + ExprName { + range: 56..57, + id: "a", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ), + body: [ + Expr( + StmtExpr { + range: 60..63, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 60..63, + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 64..99, + subject: Name( + ExprName { + range: 70..71, + id: "x", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 77..99, + pattern: MatchAs( + PatternMatchAs { + range: 82..83, + pattern: None, + name: Some( + Identifier { + id: "y", + range: 82..83, + }, + ), + }, + ), + guard: Some( + Yield( + ExprYield { + range: 87..94, + value: Some( + Name( + ExprName { + range: 93..94, + id: "x", + ctx: Load, + }, + ), + ), + }, + ), + ), + body: [ + Expr( + StmtExpr { + range: 96..99, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 96..99, + }, + ), + }, + ), + ], + }, + ], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | match x: +2 | case y if *a: ... + | ^^ Syntax Error: Starred expression cannot be used here +3 | match x: +4 | case y if (*a): ... + | + + + | +2 | case y if *a: ... +3 | match x: +4 | case y if (*a): ... + | ^^ Syntax Error: Starred expression cannot be used here +5 | match x: +6 | case y if yield x: ... + | + + + | +4 | case y if (*a): ... +5 | match x: +6 | case y if yield x: ... + | ^^^^^^^ Syntax Error: Yield expression cannot be used here + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@match_stmt_invalid_subject_expr.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@match_stmt_invalid_subject_expr.py.snap.new new file mode 100644 index 0000000000..c79c8828b0 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@match_stmt_invalid_subject_expr.py.snap.new @@ -0,0 +1,204 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/match_stmt_invalid_subject_expr.py +--- +## AST + +``` +Module( + ModModule { + range: 0..131, + body: [ + Match( + StmtMatch { + range: 0..27, + subject: Starred( + ExprStarred { + range: 7..9, + value: Name( + ExprName { + range: 8..9, + id: "x", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 16..27, + pattern: MatchAs( + PatternMatchAs { + range: 21..22, + pattern: None, + name: None, + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 24..27, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 24..27, + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 65..99, + subject: Tuple( + ExprTuple { + range: 71..82, + elts: [ + Starred( + ExprStarred { + range: 71..79, + value: BoolOp( + ExprBoolOp { + range: 72..79, + op: And, + values: [ + Name( + ExprName { + range: 72..73, + id: "x", + ctx: Load, + }, + ), + Name( + ExprName { + range: 78..79, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + ctx: Load, + }, + ), + Name( + ExprName { + range: 81..82, + id: "z", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + cases: [ + MatchCase { + range: 88..99, + pattern: MatchAs( + PatternMatchAs { + range: 93..94, + pattern: None, + name: None, + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 96..99, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 96..99, + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 100..130, + subject: Yield( + ExprYield { + range: 106..113, + value: Some( + Name( + ExprName { + range: 112..113, + id: "x", + ctx: Load, + }, + ), + ), + }, + ), + cases: [ + MatchCase { + range: 119..130, + pattern: MatchAs( + PatternMatchAs { + range: 124..125, + pattern: None, + name: None, + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 127..130, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 127..130, + }, + ), + }, + ), + ], + }, + ], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | match (*x): + | ^^ Syntax Error: Starred expression cannot be used here +2 | case _: ... +3 | # Starred expression precedence test + | + + + | +2 | case _: ... +3 | # Starred expression precedence test +4 | match *x and y, z: + | ^^^^^^^ Syntax Error: Boolean expression cannot be used here +5 | case _: ... +6 | match yield x: + | + + + | +4 | match *x and y, z: +5 | case _: ... +6 | match yield x: + | ^^^^^^^ Syntax Error: Yield expression cannot be used here +7 | case _: ... + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@match_stmt_missing_guard_expr.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@match_stmt_missing_guard_expr.py.snap.new new file mode 100644 index 0000000000..cdb363bddb --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@match_stmt_missing_guard_expr.py.snap.new @@ -0,0 +1,65 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/match_stmt_missing_guard_expr.py +--- +## AST + +``` +Module( + ModModule { + range: 0..28, + body: [ + Match( + StmtMatch { + range: 0..27, + subject: Name( + ExprName { + range: 6..7, + id: "x", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 13..27, + pattern: MatchAs( + PatternMatchAs { + range: 18..19, + pattern: None, + name: Some( + Identifier { + id: "y", + range: 18..19, + }, + ), + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 24..27, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 24..27, + }, + ), + }, + ), + ], + }, + ], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | match x: +2 | case y if: ... + | ^ Syntax Error: Expected an expression + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@match_stmt_missing_pattern.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@match_stmt_missing_pattern.py.snap.new new file mode 100644 index 0000000000..6cba7bb461 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@match_stmt_missing_pattern.py.snap.new @@ -0,0 +1,65 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/match_stmt_missing_pattern.py +--- +## AST + +``` +Module( + ModModule { + range: 0..24, + body: [ + Match( + StmtMatch { + range: 0..23, + subject: Name( + ExprName { + range: 6..7, + id: "x", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 13..23, + pattern: MatchValue( + PatternMatchValue { + range: 17..17, + value: Name( + ExprName { + range: 17..17, + id: "", + ctx: Invalid, + }, + ), + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 20..23, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 20..23, + }, + ), + }, + ), + ], + }, + ], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | match x: +2 | case : ... + | ^ Syntax Error: Expected a pattern + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@match_stmt_no_newline_before_case.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@match_stmt_no_newline_before_case.py.snap.new new file mode 100644 index 0000000000..a79dc7d3d6 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@match_stmt_no_newline_before_case.py.snap.new @@ -0,0 +1,64 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/match_stmt_no_newline_before_case.py +--- +## AST + +``` +Module( + ModModule { + range: 0..23, + body: [ + Match( + StmtMatch { + range: 0..22, + subject: Name( + ExprName { + range: 6..9, + id: "foo", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 11..22, + pattern: MatchAs( + PatternMatchAs { + range: 16..17, + pattern: None, + name: None, + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 19..22, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 19..22, + }, + ), + }, + ), + ], + }, + ], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | match foo: case _: ... + | ^^^^ Syntax Error: Expected newline, found 'case' + | + + + | +1 | match foo: case _: ... + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@match_stmt_single_starred_subject.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@match_stmt_single_starred_subject.py.snap.new new file mode 100644 index 0000000000..cfa1a32ae8 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@match_stmt_single_starred_subject.py.snap.new @@ -0,0 +1,66 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/match_stmt_single_starred_subject.py +--- +## AST + +``` +Module( + ModModule { + range: 0..28, + body: [ + Match( + StmtMatch { + range: 0..27, + subject: Starred( + ExprStarred { + range: 6..10, + value: Name( + ExprName { + range: 7..10, + id: "foo", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 16..27, + pattern: MatchAs( + PatternMatchAs { + range: 21..22, + pattern: None, + name: None, + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 24..27, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 24..27, + }, + ), + }, + ), + ], + }, + ], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | match *foo: + | ^^^^ Syntax Error: Starred expression cannot be used here +2 | case _: ... + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@multiple_clauses_on_same_line.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@multiple_clauses_on_same_line.py.snap.new new file mode 100644 index 0000000000..f9530c00e5 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@multiple_clauses_on_same_line.py.snap.new @@ -0,0 +1,389 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/multiple_clauses_on_same_line.py +--- +## AST + +``` +Module( + ModModule { + range: 0..258, + body: [ + If( + StmtIf { + range: 0..41, + test: BooleanLiteral( + ExprBooleanLiteral { + range: 3..7, + value: true, + }, + ), + body: [ + Pass( + StmtPass { + range: 9..13, + }, + ), + ], + elif_else_clauses: [ + ElifElseClause { + range: 14..30, + test: Some( + BooleanLiteral( + ExprBooleanLiteral { + range: 19..24, + value: false, + }, + ), + ), + body: [ + Pass( + StmtPass { + range: 26..30, + }, + ), + ], + }, + ElifElseClause { + range: 31..41, + test: None, + body: [ + Pass( + StmtPass { + range: 37..41, + }, + ), + ], + }, + ], + }, + ), + If( + StmtIf { + range: 42..85, + test: BooleanLiteral( + ExprBooleanLiteral { + range: 45..49, + value: true, + }, + ), + body: [ + Pass( + StmtPass { + range: 51..55, + }, + ), + ], + elif_else_clauses: [ + ElifElseClause { + range: 57..73, + test: Some( + BooleanLiteral( + ExprBooleanLiteral { + range: 62..67, + value: false, + }, + ), + ), + body: [ + Pass( + StmtPass { + range: 69..73, + }, + ), + ], + }, + ElifElseClause { + range: 75..85, + test: None, + body: [ + Pass( + StmtPass { + range: 81..85, + }, + ), + ], + }, + ], + }, + ), + For( + StmtFor { + range: 86..117, + is_async: false, + target: Name( + ExprName { + range: 90..91, + id: "x", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 95..99, + id: "iter", + ctx: Load, + }, + ), + body: [ + Break( + StmtBreak { + range: 101..106, + }, + ), + ], + orelse: [ + Pass( + StmtPass { + range: 113..117, + }, + ), + ], + }, + ), + For( + StmtFor { + range: 118..150, + is_async: false, + target: Name( + ExprName { + range: 122..123, + id: "x", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 127..131, + id: "iter", + ctx: Load, + }, + ), + body: [ + Break( + StmtBreak { + range: 133..138, + }, + ), + ], + orelse: [ + Pass( + StmtPass { + range: 146..150, + }, + ), + ], + }, + ), + Try( + StmtTry { + range: 151..202, + body: [ + Pass( + StmtPass { + range: 156..160, + }, + ), + ], + handlers: [ + ExceptHandler( + ExceptHandlerExceptHandler { + range: 161..177, + type_: Some( + Name( + ExprName { + range: 168..171, + id: "exc", + ctx: Load, + }, + ), + ), + name: None, + body: [ + Pass( + StmtPass { + range: 173..177, + }, + ), + ], + }, + ), + ], + orelse: [ + Pass( + StmtPass { + range: 184..188, + }, + ), + ], + finalbody: [ + Pass( + StmtPass { + range: 198..202, + }, + ), + ], + is_star: false, + }, + ), + Try( + StmtTry { + range: 203..257, + body: [ + Pass( + StmtPass { + range: 208..212, + }, + ), + ], + handlers: [ + ExceptHandler( + ExceptHandlerExceptHandler { + range: 214..230, + type_: Some( + Name( + ExprName { + range: 221..224, + id: "exc", + ctx: Load, + }, + ), + ), + name: None, + body: [ + Pass( + StmtPass { + range: 226..230, + }, + ), + ], + }, + ), + ], + orelse: [ + Pass( + StmtPass { + range: 238..242, + }, + ), + ], + finalbody: [ + Pass( + StmtPass { + range: 253..257, + }, + ), + ], + is_star: false, + }, + ), + ], + }, +) +``` +## Errors + + | +1 | if True: pass elif False: pass else: pass + | ^^^^ Syntax Error: Expected newline, found 'elif' +2 | if True: pass; elif False: pass; else: pass +3 | for x in iter: break else: pass + | + + + | +1 | if True: pass elif False: pass else: pass + | ^^^^ Syntax Error: Expected newline, found 'else' +2 | if True: pass; elif False: pass; else: pass +3 | for x in iter: break else: pass + | + + + | +1 | if True: pass elif False: pass else: pass +2 | if True: pass; elif False: pass; else: pass + | ^^^^ Syntax Error: Expected newline, found 'elif' +3 | for x in iter: break else: pass +4 | for x in iter: break; else: pass + | + + + | +1 | if True: pass elif False: pass else: pass +2 | if True: pass; elif False: pass; else: pass + | ^^^^ Syntax Error: Expected newline, found 'else' +3 | for x in iter: break else: pass +4 | for x in iter: break; else: pass + | + + + | +1 | if True: pass elif False: pass else: pass +2 | if True: pass; elif False: pass; else: pass +3 | for x in iter: break else: pass + | ^^^^ Syntax Error: Expected newline, found 'else' +4 | for x in iter: break; else: pass +5 | try: pass except exc: pass else: pass finally: pass + | + + + | +2 | if True: pass; elif False: pass; else: pass +3 | for x in iter: break else: pass +4 | for x in iter: break; else: pass + | ^^^^ Syntax Error: Expected newline, found 'else' +5 | try: pass except exc: pass else: pass finally: pass +6 | try: pass; except exc: pass; else: pass; finally: pass + | + + + | +3 | for x in iter: break else: pass +4 | for x in iter: break; else: pass +5 | try: pass except exc: pass else: pass finally: pass + | ^^^^^^ Syntax Error: Expected newline, found 'except' +6 | try: pass; except exc: pass; else: pass; finally: pass + | + + + | +3 | for x in iter: break else: pass +4 | for x in iter: break; else: pass +5 | try: pass except exc: pass else: pass finally: pass + | ^^^^ Syntax Error: Expected newline, found 'else' +6 | try: pass; except exc: pass; else: pass; finally: pass + | + + + | +3 | for x in iter: break else: pass +4 | for x in iter: break; else: pass +5 | try: pass except exc: pass else: pass finally: pass + | ^^^^^^^ Syntax Error: Expected newline, found 'finally' +6 | try: pass; except exc: pass; else: pass; finally: pass + | + + + | +4 | for x in iter: break; else: pass +5 | try: pass except exc: pass else: pass finally: pass +6 | try: pass; except exc: pass; else: pass; finally: pass + | ^^^^^^ Syntax Error: Expected newline, found 'except' + | + + + | +4 | for x in iter: break; else: pass +5 | try: pass except exc: pass else: pass finally: pass +6 | try: pass; except exc: pass; else: pass; finally: pass + | ^^^^ Syntax Error: Expected newline, found 'else' + | + + + | +4 | for x in iter: break; else: pass +5 | try: pass except exc: pass else: pass finally: pass +6 | try: pass; except exc: pass; else: pass; finally: pass + | ^^^^^^^ Syntax Error: Expected newline, found 'finally' + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@node_range_with_gaps.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@node_range_with_gaps.py.snap.new new file mode 100644 index 0000000000..238f6e734c --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@node_range_with_gaps.py.snap.new @@ -0,0 +1,123 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/node_range_with_gaps.py +--- +## AST + +``` +Module( + ModModule { + range: 0..41, + body: [ + FunctionDef( + StmtFunctionDef { + range: 0..7, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 4..7, + }, + type_params: None, + parameters: Parameters { + range: 7..7, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 18..32, + is_async: false, + decorator_list: [], + name: Identifier { + id: "bar", + range: 22..25, + }, + type_params: None, + parameters: Parameters { + range: 25..27, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 29..32, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 29..32, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 33..40, + is_async: false, + decorator_list: [], + name: Identifier { + id: "baz", + range: 37..40, + }, + type_params: None, + parameters: Parameters { + range: 40..40, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | def foo # comment + | ^ Syntax Error: Expected '(', found newline +2 | def bar(): ... +3 | def baz + | + + + | +1 | def foo # comment +2 | def bar(): ... + | ^^^ Syntax Error: Expected ')', found 'def' +3 | def baz + | + + + | +1 | def foo # comment +2 | def bar(): ... +3 | def baz + | ^ Syntax Error: Expected '(', found newline + | + + + | +2 | def bar(): ... +3 | def baz + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@nonlocal_stmt_expression.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@nonlocal_stmt_expression.py.snap.new new file mode 100644 index 0000000000..db9c63918c --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@nonlocal_stmt_expression.py.snap.new @@ -0,0 +1,52 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/nonlocal_stmt_expression.py +--- +## AST + +``` +Module( + ModModule { + range: 0..15, + body: [ + Nonlocal( + StmtNonlocal { + range: 0..10, + names: [ + Identifier { + id: "x", + range: 9..10, + }, + ], + }, + ), + Expr( + StmtExpr { + range: 11..14, + value: UnaryOp( + ExprUnaryOp { + range: 11..14, + op: UAdd, + operand: NumberLiteral( + ExprNumberLiteral { + range: 13..14, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | nonlocal x + 1 + | ^ Syntax Error: Simple statements must be separated by newlines or semicolons + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@nonlocal_stmt_trailing_comma.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@nonlocal_stmt_trailing_comma.py.snap.new new file mode 100644 index 0000000000..43b218c730 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@nonlocal_stmt_trailing_comma.py.snap.new @@ -0,0 +1,80 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/nonlocal_stmt_trailing_comma.py +--- +## AST + +``` +Module( + ModModule { + range: 0..38, + body: [ + Nonlocal( + StmtNonlocal { + range: 0..10, + names: [], + }, + ), + Nonlocal( + StmtNonlocal { + range: 11..22, + names: [ + Identifier { + id: "x", + range: 20..21, + }, + ], + }, + ), + Nonlocal( + StmtNonlocal { + range: 23..37, + names: [ + Identifier { + id: "x", + range: 32..33, + }, + Identifier { + id: "y", + range: 35..36, + }, + ], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | nonlocal , + | ^ Syntax Error: Expected an identifier +2 | nonlocal x, +3 | nonlocal x, y, + | + + + | +1 | nonlocal , + | ^ Syntax Error: Nonlocal statement must have at least one name +2 | nonlocal x, +3 | nonlocal x, y, + | + + + | +1 | nonlocal , +2 | nonlocal x, + | ^ Syntax Error: Trailing comma not allowed +3 | nonlocal x, y, + | + + + | +1 | nonlocal , +2 | nonlocal x, +3 | nonlocal x, y, + | ^ Syntax Error: Trailing comma not allowed + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@param_missing_annotation.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@param_missing_annotation.py.snap.new new file mode 100644 index 0000000000..5f838973ef --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@param_missing_annotation.py.snap.new @@ -0,0 +1,122 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/param_missing_annotation.py +--- +## AST + +``` +Module( + ModModule { + range: 0..35, + body: [ + FunctionDef( + StmtFunctionDef { + range: 0..16, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 4..7, + }, + type_params: None, + parameters: Parameters { + range: 7..11, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 8..10, + parameter: Parameter { + range: 8..10, + name: Identifier { + id: "x", + range: 8..9, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 13..16, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 13..16, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 17..34, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 21..24, + }, + type_params: None, + parameters: Parameters { + range: 24..29, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 25..27, + parameter: Parameter { + range: 25..27, + name: Identifier { + id: "x", + range: 25..26, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 31..34, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 31..34, + }, + ), + }, + ), + ], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | def foo(x:): ... + | ^ Syntax Error: Expected an expression +2 | def foo(x:,): ... + | + + + | +1 | def foo(x:): ... +2 | def foo(x:,): ... + | ^ Syntax Error: Expected an expression + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@param_missing_default.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@param_missing_default.py.snap.new new file mode 100644 index 0000000000..ed8bb14de6 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@param_missing_default.py.snap.new @@ -0,0 +1,130 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/param_missing_default.py +--- +## AST + +``` +Module( + ModModule { + range: 0..41, + body: [ + FunctionDef( + StmtFunctionDef { + range: 0..16, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 4..7, + }, + type_params: None, + parameters: Parameters { + range: 7..11, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 8..10, + parameter: Parameter { + range: 8..9, + name: Identifier { + id: "x", + range: 8..9, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 13..16, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 13..16, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 17..40, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 21..24, + }, + type_params: None, + parameters: Parameters { + range: 24..35, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 25..33, + parameter: Parameter { + range: 25..31, + name: Identifier { + id: "x", + range: 25..26, + }, + annotation: Some( + Name( + ExprName { + range: 28..31, + id: "int", + ctx: Load, + }, + ), + ), + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 37..40, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 37..40, + }, + ), + }, + ), + ], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | def foo(x=): ... + | ^ Syntax Error: Expected an expression +2 | def foo(x: int = ): ... + | + + + | +1 | def foo(x=): ... +2 | def foo(x: int = ): ... + | ^ Syntax Error: Expected an expression + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@param_with_invalid_annotation.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@param_with_invalid_annotation.py.snap.new new file mode 100644 index 0000000000..38d09cc97a --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@param_with_invalid_annotation.py.snap.new @@ -0,0 +1,227 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/param_with_invalid_annotation.py +--- +## AST + +``` +Module( + ModModule { + range: 0..81, + body: [ + FunctionDef( + StmtFunctionDef { + range: 0..23, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 4..7, + }, + type_params: None, + parameters: Parameters { + range: 7..18, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 8..17, + parameter: Parameter { + range: 8..17, + name: Identifier { + id: "arg", + range: 8..11, + }, + annotation: Some( + Starred( + ExprStarred { + range: 13..17, + value: Name( + ExprName { + range: 14..17, + id: "int", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ), + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 20..23, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 20..23, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 24..52, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 28..31, + }, + type_params: None, + parameters: Parameters { + range: 31..47, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 32..46, + parameter: Parameter { + range: 32..46, + name: Identifier { + id: "arg", + range: 32..35, + }, + annotation: Some( + Yield( + ExprYield { + range: 37..46, + value: Some( + Name( + ExprName { + range: 43..46, + id: "int", + ctx: Load, + }, + ), + ), + }, + ), + ), + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 49..52, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 49..52, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 53..80, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 57..60, + }, + type_params: None, + parameters: Parameters { + range: 60..75, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 61..67, + parameter: Parameter { + range: 61..67, + name: Identifier { + id: "arg", + range: 61..64, + }, + annotation: Some( + Name( + ExprName { + range: 66..67, + id: "x", + ctx: Load, + }, + ), + ), + }, + default: None, + }, + ParameterWithDefault { + range: 71..74, + parameter: Parameter { + range: 71..74, + name: Identifier { + id: "int", + range: 71..74, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 77..80, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 77..80, + }, + ), + }, + ), + ], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | def foo(arg: *int): ... + | ^^^^ Syntax Error: Starred expression cannot be used here +2 | def foo(arg: yield int): ... +3 | def foo(arg: x := int): ... + | + + + | +1 | def foo(arg: *int): ... +2 | def foo(arg: yield int): ... + | ^^^^^^^^^ Syntax Error: Yield expression cannot be used here +3 | def foo(arg: x := int): ... + | + + + | +1 | def foo(arg: *int): ... +2 | def foo(arg: yield int): ... +3 | def foo(arg: x := int): ... + | ^^ Syntax Error: Expected ',', found ':=' + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@param_with_invalid_default.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@param_with_invalid_default.py.snap.new new file mode 100644 index 0000000000..c85c9d8109 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@param_with_invalid_default.py.snap.new @@ -0,0 +1,221 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/param_with_invalid_default.py +--- +## AST + +``` +Module( + ModModule { + range: 0..68, + body: [ + FunctionDef( + StmtFunctionDef { + range: 0..20, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 4..7, + }, + type_params: None, + parameters: Parameters { + range: 7..15, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 8..14, + parameter: Parameter { + range: 8..9, + name: Identifier { + id: "x", + range: 8..9, + }, + annotation: None, + }, + default: Some( + Starred( + ExprStarred { + range: 10..14, + value: Name( + ExprName { + range: 11..14, + id: "int", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ), + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 17..20, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 17..20, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 21..43, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 25..28, + }, + type_params: None, + parameters: Parameters { + range: 28..38, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 29..37, + parameter: Parameter { + range: 29..30, + name: Identifier { + id: "x", + range: 29..30, + }, + annotation: None, + }, + default: Some( + Starred( + ExprStarred { + range: 32..36, + value: Name( + ExprName { + range: 33..36, + id: "int", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ), + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 40..43, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 40..43, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 44..67, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 48..51, + }, + type_params: None, + parameters: Parameters { + range: 51..62, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 52..61, + parameter: Parameter { + range: 52..53, + name: Identifier { + id: "x", + range: 52..53, + }, + annotation: None, + }, + default: Some( + Yield( + ExprYield { + range: 54..61, + value: Some( + Name( + ExprName { + range: 60..61, + id: "y", + ctx: Load, + }, + ), + ), + }, + ), + ), + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 64..67, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 64..67, + }, + ), + }, + ), + ], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | def foo(x=*int): ... + | ^^^^ Syntax Error: Starred expression cannot be used here +2 | def foo(x=(*int)): ... +3 | def foo(x=yield y): ... + | + + + | +1 | def foo(x=*int): ... +2 | def foo(x=(*int)): ... + | ^^^^ Syntax Error: Starred expression cannot be used here +3 | def foo(x=yield y): ... + | + + + | +1 | def foo(x=*int): ... +2 | def foo(x=(*int)): ... +3 | def foo(x=yield y): ... + | ^^^^^^^ Syntax Error: Yield expression cannot be used here + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@param_with_invalid_star_annotation.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@param_with_invalid_star_annotation.py.snap.new new file mode 100644 index 0000000000..5f68409122 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@param_with_invalid_star_annotation.py.snap.new @@ -0,0 +1,311 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/param_with_invalid_star_annotation.py +--- +## AST + +``` +Module( + ModModule { + range: 0..150, + body: [ + FunctionDef( + StmtFunctionDef { + range: 0..22, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 4..7, + }, + type_params: None, + parameters: Parameters { + range: 7..17, + posonlyargs: [], + args: [], + vararg: Some( + Parameter { + range: 8..16, + name: Identifier { + id: "args", + range: 9..13, + }, + annotation: Some( + Starred( + ExprStarred { + range: 15..16, + value: Name( + ExprName { + range: 16..16, + id: "", + ctx: Invalid, + }, + ), + ctx: Load, + }, + ), + ), + }, + ), + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 19..22, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 19..22, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 23..57, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 27..30, + }, + type_params: None, + parameters: Parameters { + range: 30..52, + posonlyargs: [], + args: [], + vararg: Some( + Parameter { + range: 31..51, + name: Identifier { + id: "args", + range: 32..36, + }, + annotation: Some( + Starred( + ExprStarred { + range: 39..50, + value: Subscript( + ExprSubscript { + range: 40..50, + value: Name( + ExprName { + range: 40..45, + id: "tuple", + ctx: Load, + }, + ), + slice: Name( + ExprName { + range: 46..49, + id: "int", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ), + }, + ), + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 54..57, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 54..57, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 58..90, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 62..65, + }, + type_params: None, + parameters: Parameters { + range: 65..85, + posonlyargs: [], + args: [], + vararg: Some( + Parameter { + range: 66..84, + name: Identifier { + id: "args", + range: 67..71, + }, + annotation: Some( + Starred( + ExprStarred { + range: 73..84, + value: BoolOp( + ExprBoolOp { + range: 74..84, + op: Or, + values: [ + Name( + ExprName { + range: 74..77, + id: "int", + ctx: Load, + }, + ), + Name( + ExprName { + range: 81..84, + id: "str", + ctx: Load, + }, + ), + ], + }, + ), + ctx: Load, + }, + ), + ), + }, + ), + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 87..90, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 87..90, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 91..120, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 95..98, + }, + type_params: None, + parameters: Parameters { + range: 98..115, + posonlyargs: [], + args: [], + vararg: Some( + Parameter { + range: 99..114, + name: Identifier { + id: "args", + range: 100..104, + }, + annotation: Some( + Starred( + ExprStarred { + range: 106..114, + value: Yield( + ExprYield { + range: 107..114, + value: Some( + Name( + ExprName { + range: 113..114, + id: "x", + ctx: Load, + }, + ), + ), + }, + ), + ctx: Load, + }, + ), + ), + }, + ), + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 117..120, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 117..120, + }, + ), + }, + ), + ], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | def foo(*args: *): ... + | ^ Syntax Error: Expected an expression +2 | def foo(*args: (*tuple[int])): ... +3 | def foo(*args: *int or str): ... + | + + + | +1 | def foo(*args: *): ... +2 | def foo(*args: (*tuple[int])): ... + | ^^^^^^^^^^^ Syntax Error: Starred expression cannot be used here +3 | def foo(*args: *int or str): ... +4 | def foo(*args: *yield x): ... + | + + + | +1 | def foo(*args: *): ... +2 | def foo(*args: (*tuple[int])): ... +3 | def foo(*args: *int or str): ... + | ^^^^^^^^^^ Syntax Error: Boolean expression cannot be used here +4 | def foo(*args: *yield x): ... +5 | # def foo(*args: **int): ... + | + + + | +2 | def foo(*args: (*tuple[int])): ... +3 | def foo(*args: *int or str): ... +4 | def foo(*args: *yield x): ... + | ^^^^^^^ Syntax Error: Yield expression cannot be used here +5 | # def foo(*args: **int): ... + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@params_duplicate_names.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@params_duplicate_names.py.snap.new new file mode 100644 index 0000000000..3edd64d419 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@params_duplicate_names.py.snap.new @@ -0,0 +1,164 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/params_duplicate_names.py +--- +## AST + +``` +Module( + ModModule { + range: 0..42, + body: [ + FunctionDef( + StmtFunctionDef { + range: 0..41, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 4..7, + }, + type_params: None, + parameters: Parameters { + range: 7..36, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 8..9, + parameter: Parameter { + range: 8..9, + name: Identifier { + id: "a", + range: 8..9, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 11..15, + parameter: Parameter { + range: 11..12, + name: Identifier { + id: "a", + range: 11..12, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 13..15, + value: Int( + 10, + ), + }, + ), + ), + }, + ], + vararg: Some( + Parameter { + range: 17..19, + name: Identifier { + id: "a", + range: 18..19, + }, + annotation: None, + }, + ), + kwonlyargs: [ + ParameterWithDefault { + range: 21..22, + parameter: Parameter { + range: 21..22, + name: Identifier { + id: "a", + range: 21..22, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 24..30, + parameter: Parameter { + range: 24..30, + name: Identifier { + id: "a", + range: 24..25, + }, + annotation: Some( + Name( + ExprName { + range: 27..30, + id: "str", + ctx: Load, + }, + ), + ), + }, + default: None, + }, + ], + kwarg: Some( + Parameter { + range: 32..35, + name: Identifier { + id: "a", + range: 34..35, + }, + annotation: None, + }, + ), + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 38..41, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 38..41, + }, + ), + }, + ), + ], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | def foo(a, a=10, *a, a, a: str, **a): ... + | ^ Syntax Error: Duplicate parameter "a" + | + + + | +1 | def foo(a, a=10, *a, a, a: str, **a): ... + | ^ Syntax Error: Duplicate parameter "a" + | + + + | +1 | def foo(a, a=10, *a, a, a: str, **a): ... + | ^ Syntax Error: Duplicate parameter "a" + | + + + | +1 | def foo(a, a=10, *a, a, a: str, **a): ... + | ^ Syntax Error: Duplicate parameter "a" + | + + + | +1 | def foo(a, a=10, *a, a, a: str, **a): ... + | ^ Syntax Error: Duplicate parameter "a" + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@params_expected_after_star_separator.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@params_expected_after_star_separator.py.snap.new new file mode 100644 index 0000000000..b8b40b5d2c --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@params_expected_after_star_separator.py.snap.new @@ -0,0 +1,260 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/params_expected_after_star_separator.py +--- +## AST + +``` +Module( + ModModule { + range: 0..98, + body: [ + FunctionDef( + StmtFunctionDef { + range: 0..15, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 4..7, + }, + type_params: None, + parameters: Parameters { + range: 7..10, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 12..15, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 12..15, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 16..32, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 20..23, + }, + type_params: None, + parameters: Parameters { + range: 23..27, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 29..32, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 29..32, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 33..51, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 37..40, + }, + type_params: None, + parameters: Parameters { + range: 40..46, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 41..42, + parameter: Parameter { + range: 41..42, + name: Identifier { + id: "a", + range: 41..42, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 48..51, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 48..51, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 52..71, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 56..59, + }, + type_params: None, + parameters: Parameters { + range: 59..66, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 60..61, + parameter: Parameter { + range: 60..61, + name: Identifier { + id: "a", + range: 60..61, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 68..71, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 68..71, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 72..97, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 76..79, + }, + type_params: None, + parameters: Parameters { + range: 79..92, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: Some( + Parameter { + range: 83..91, + name: Identifier { + id: "kwargs", + range: 85..91, + }, + annotation: None, + }, + ), + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 94..97, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 94..97, + }, + ), + }, + ), + ], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | def foo(*): ... + | ^ Syntax Error: Expected one or more keyword parameter after '*' separator +2 | def foo(*,): ... +3 | def foo(a, *): ... + | + + + | +1 | def foo(*): ... +2 | def foo(*,): ... + | ^ Syntax Error: Expected one or more keyword parameter after '*' separator +3 | def foo(a, *): ... +4 | def foo(a, *,): ... + | + + + | +1 | def foo(*): ... +2 | def foo(*,): ... +3 | def foo(a, *): ... + | ^ Syntax Error: Expected one or more keyword parameter after '*' separator +4 | def foo(a, *,): ... +5 | def foo(*, **kwargs): ... + | + + + | +2 | def foo(*,): ... +3 | def foo(a, *): ... +4 | def foo(a, *,): ... + | ^ Syntax Error: Expected one or more keyword parameter after '*' separator +5 | def foo(*, **kwargs): ... + | + + + | +3 | def foo(a, *): ... +4 | def foo(a, *,): ... +5 | def foo(*, **kwargs): ... + | ^^^^^^^^ Syntax Error: Expected one or more keyword parameter after '*' separator + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@params_kwarg_after_star_separator.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@params_kwarg_after_star_separator.py.snap.new new file mode 100644 index 0000000000..50466a6d76 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@params_kwarg_after_star_separator.py.snap.new @@ -0,0 +1,64 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/params_kwarg_after_star_separator.py +--- +## AST + +``` +Module( + ModModule { + range: 0..26, + body: [ + FunctionDef( + StmtFunctionDef { + range: 0..25, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 4..7, + }, + type_params: None, + parameters: Parameters { + range: 7..20, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: Some( + Parameter { + range: 11..19, + name: Identifier { + id: "kwargs", + range: 13..19, + }, + annotation: None, + }, + ), + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 22..25, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 22..25, + }, + ), + }, + ), + ], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | def foo(*, **kwargs): ... + | ^^^^^^^^ Syntax Error: Expected one or more keyword parameter after '*' separator + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@params_multiple_kwargs.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@params_multiple_kwargs.py.snap.new new file mode 100644 index 0000000000..ac4d515618 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@params_multiple_kwargs.py.snap.new @@ -0,0 +1,77 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/params_multiple_kwargs.py +--- +## AST + +``` +Module( + ModModule { + range: 0..38, + body: [ + FunctionDef( + StmtFunctionDef { + range: 0..37, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 4..7, + }, + type_params: None, + parameters: Parameters { + range: 7..32, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 8..9, + parameter: Parameter { + range: 8..9, + name: Identifier { + id: "a", + range: 8..9, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: Some( + Parameter { + range: 22..31, + name: Identifier { + id: "kwargs2", + range: 24..31, + }, + annotation: None, + }, + ), + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 34..37, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 34..37, + }, + ), + }, + ), + ], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | def foo(a, **kwargs1, **kwargs2): ... + | ^^ Syntax Error: Parameter cannot follow var-keyword parameter + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@params_multiple_slash_separator.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@params_multiple_slash_separator.py.snap.new new file mode 100644 index 0000000000..0265befade --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@params_multiple_slash_separator.py.snap.new @@ -0,0 +1,160 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/params_multiple_slash_separator.py +--- +## AST + +``` +Module( + ModModule { + range: 0..53, + body: [ + FunctionDef( + StmtFunctionDef { + range: 0..24, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 4..7, + }, + type_params: None, + parameters: Parameters { + range: 7..19, + posonlyargs: [ + ParameterWithDefault { + range: 8..9, + parameter: Parameter { + range: 8..9, + name: Identifier { + id: "a", + range: 8..9, + }, + annotation: None, + }, + default: None, + }, + ], + args: [ + ParameterWithDefault { + range: 17..18, + parameter: Parameter { + range: 17..18, + name: Identifier { + id: "b", + range: 17..18, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 21..24, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 21..24, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 25..52, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 29..32, + }, + type_params: None, + parameters: Parameters { + range: 32..47, + posonlyargs: [ + ParameterWithDefault { + range: 33..34, + parameter: Parameter { + range: 33..34, + name: Identifier { + id: "a", + range: 33..34, + }, + annotation: None, + }, + default: None, + }, + ], + args: [ + ParameterWithDefault { + range: 39..40, + parameter: Parameter { + range: 39..40, + name: Identifier { + id: "b", + range: 39..40, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 42..43, + parameter: Parameter { + range: 42..43, + name: Identifier { + id: "c", + range: 42..43, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 49..52, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 49..52, + }, + ), + }, + ), + ], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | def foo(a, /, /, b): ... + | ^ Syntax Error: Only one '/' separator allowed +2 | def foo(a, /, b, c, /): ... + | + + + | +1 | def foo(a, /, /, b): ... +2 | def foo(a, /, b, c, /): ... + | ^ Syntax Error: Only one '/' separator allowed + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@params_multiple_star_separator.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@params_multiple_star_separator.py.snap.new new file mode 100644 index 0000000000..e20a3c75a2 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@params_multiple_star_separator.py.snap.new @@ -0,0 +1,160 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/params_multiple_star_separator.py +--- +## AST + +``` +Module( + ModModule { + range: 0..53, + body: [ + FunctionDef( + StmtFunctionDef { + range: 0..24, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 4..7, + }, + type_params: None, + parameters: Parameters { + range: 7..19, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 8..9, + parameter: Parameter { + range: 8..9, + name: Identifier { + id: "a", + range: 8..9, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [ + ParameterWithDefault { + range: 17..18, + parameter: Parameter { + range: 17..18, + name: Identifier { + id: "b", + range: 17..18, + }, + annotation: None, + }, + default: None, + }, + ], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 21..24, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 21..24, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 25..52, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 29..32, + }, + type_params: None, + parameters: Parameters { + range: 32..47, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 33..34, + parameter: Parameter { + range: 33..34, + name: Identifier { + id: "a", + range: 33..34, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [ + ParameterWithDefault { + range: 39..40, + parameter: Parameter { + range: 39..40, + name: Identifier { + id: "b", + range: 39..40, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 42..43, + parameter: Parameter { + range: 42..43, + name: Identifier { + id: "c", + range: 42..43, + }, + annotation: None, + }, + default: None, + }, + ], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 49..52, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 49..52, + }, + ), + }, + ), + ], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | def foo(a, *, *, b): ... + | ^ Syntax Error: Only one '*' separator allowed +2 | def foo(a, *, b, c, *): ... + | + + + | +1 | def foo(a, *, *, b): ... +2 | def foo(a, *, b, c, *): ... + | ^ Syntax Error: Only one '*' separator allowed + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@params_multiple_varargs.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@params_multiple_varargs.py.snap.new new file mode 100644 index 0000000000..844707d834 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@params_multiple_varargs.py.snap.new @@ -0,0 +1,257 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/params_multiple_varargs.py +--- +## AST + +``` +Module( + ModModule { + range: 0..136, + body: [ + FunctionDef( + StmtFunctionDef { + range: 0..28, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 4..7, + }, + type_params: None, + parameters: Parameters { + range: 7..23, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 8..9, + parameter: Parameter { + range: 8..9, + name: Identifier { + id: "a", + range: 8..9, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: Some( + Parameter { + range: 14..19, + name: Identifier { + id: "args", + range: 15..19, + }, + annotation: None, + }, + ), + kwonlyargs: [ + ParameterWithDefault { + range: 21..22, + parameter: Parameter { + range: 21..22, + name: Identifier { + id: "b", + range: 21..22, + }, + annotation: None, + }, + default: None, + }, + ], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 25..28, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 25..28, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 63..97, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 67..70, + }, + type_params: None, + parameters: Parameters { + range: 70..92, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 71..72, + parameter: Parameter { + range: 71..72, + name: Identifier { + id: "a", + range: 71..72, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: Some( + Parameter { + range: 74..80, + name: Identifier { + id: "args1", + range: 75..80, + }, + annotation: None, + }, + ), + kwonlyargs: [ + ParameterWithDefault { + range: 90..91, + parameter: Parameter { + range: 90..91, + name: Identifier { + id: "b", + range: 90..91, + }, + annotation: None, + }, + default: None, + }, + ], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 94..97, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 94..97, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 98..135, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 102..105, + }, + type_params: None, + parameters: Parameters { + range: 105..130, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 106..107, + parameter: Parameter { + range: 106..107, + name: Identifier { + id: "a", + range: 106..107, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: Some( + Parameter { + range: 109..115, + name: Identifier { + id: "args1", + range: 110..115, + }, + annotation: None, + }, + ), + kwonlyargs: [ + ParameterWithDefault { + range: 117..118, + parameter: Parameter { + range: 117..118, + name: Identifier { + id: "b", + range: 117..118, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 120..121, + parameter: Parameter { + range: 120..121, + name: Identifier { + id: "c", + range: 120..121, + }, + annotation: None, + }, + default: None, + }, + ], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 132..135, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 132..135, + }, + ), + }, + ), + ], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | def foo(a, *, *args, b): ... + | ^^^^^ Syntax Error: Only one '*' parameter allowed +2 | # def foo(a, *, b, c, *args): ... +3 | def foo(a, *args1, *args2, b): ... + | + + + | +1 | def foo(a, *, *args, b): ... +2 | # def foo(a, *, b, c, *args): ... +3 | def foo(a, *args1, *args2, b): ... + | ^^^^^^ Syntax Error: Only one '*' parameter allowed +4 | def foo(a, *args1, b, c, *args2): ... + | + + + | +2 | # def foo(a, *, b, c, *args): ... +3 | def foo(a, *args1, *args2, b): ... +4 | def foo(a, *args1, b, c, *args2): ... + | ^^^^^^ Syntax Error: Only one '*' parameter allowed + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@params_no_arg_before_slash.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@params_no_arg_before_slash.py.snap.new new file mode 100644 index 0000000000..dabc4cf2f2 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@params_no_arg_before_slash.py.snap.new @@ -0,0 +1,109 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/params_no_arg_before_slash.py +--- +## AST + +``` +Module( + ModModule { + range: 0..35, + body: [ + FunctionDef( + StmtFunctionDef { + range: 0..15, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 4..7, + }, + type_params: None, + parameters: Parameters { + range: 7..10, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 12..15, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 12..15, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 16..34, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 20..23, + }, + type_params: None, + parameters: Parameters { + range: 23..29, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 27..28, + parameter: Parameter { + range: 27..28, + name: Identifier { + id: "a", + range: 27..28, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 31..34, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 31..34, + }, + ), + }, + ), + ], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | def foo(/): ... + | ^ Syntax Error: Position-only parameter separator not allowed as first parameter +2 | def foo(/, a): ... + | + + + | +1 | def foo(/): ... +2 | def foo(/, a): ... + | ^ Syntax Error: Position-only parameter separator not allowed as first parameter + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@params_non_default_after_default.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@params_non_default_after_default.py.snap.new new file mode 100644 index 0000000000..80bea22dc4 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@params_non_default_after_default.py.snap.new @@ -0,0 +1,115 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/params_non_default_after_default.py +--- +## AST + +``` +Module( + ModModule { + range: 0..30, + body: [ + FunctionDef( + StmtFunctionDef { + range: 0..29, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 4..7, + }, + type_params: None, + parameters: Parameters { + range: 7..24, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 8..12, + parameter: Parameter { + range: 8..9, + name: Identifier { + id: "a", + range: 8..9, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 10..12, + value: Int( + 10, + ), + }, + ), + ), + }, + ParameterWithDefault { + range: 14..15, + parameter: Parameter { + range: 14..15, + name: Identifier { + id: "b", + range: 14..15, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 17..23, + parameter: Parameter { + range: 17..23, + name: Identifier { + id: "c", + range: 17..18, + }, + annotation: Some( + Name( + ExprName { + range: 20..23, + id: "int", + ctx: Load, + }, + ), + ), + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 26..29, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 26..29, + }, + ), + }, + ), + ], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | def foo(a=10, b, c: int): ... + | ^ Syntax Error: Parameter without a default cannot follow a parameter with a default + | + + + | +1 | def foo(a=10, b, c: int): ... + | ^^^^^^ Syntax Error: Parameter without a default cannot follow a parameter with a default + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@params_star_after_slash.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@params_star_after_slash.py.snap.new new file mode 100644 index 0000000000..802beea39f --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@params_star_after_slash.py.snap.new @@ -0,0 +1,302 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/params_star_after_slash.py +--- +## AST + +``` +Module( + ModModule { + range: 0..105, + body: [ + FunctionDef( + StmtFunctionDef { + range: 0..19, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 4..7, + }, + type_params: None, + parameters: Parameters { + range: 7..14, + posonlyargs: [], + args: [], + vararg: Some( + Parameter { + range: 8..10, + name: Identifier { + id: "a", + range: 9..10, + }, + annotation: None, + }, + ), + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 16..19, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 16..19, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 20..48, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 24..27, + }, + type_params: None, + parameters: Parameters { + range: 27..43, + posonlyargs: [ + ParameterWithDefault { + range: 28..29, + parameter: Parameter { + range: 28..29, + name: Identifier { + id: "a", + range: 28..29, + }, + annotation: None, + }, + default: None, + }, + ], + args: [], + vararg: Some( + Parameter { + range: 31..36, + name: Identifier { + id: "args", + range: 32..36, + }, + annotation: None, + }, + ), + kwonlyargs: [ + ParameterWithDefault { + range: 38..39, + parameter: Parameter { + range: 38..39, + name: Identifier { + id: "b", + range: 38..39, + }, + annotation: None, + }, + default: None, + }, + ], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 45..48, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 45..48, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 49..73, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 53..56, + }, + type_params: None, + parameters: Parameters { + range: 56..68, + posonlyargs: [ + ParameterWithDefault { + range: 57..58, + parameter: Parameter { + range: 57..58, + name: Identifier { + id: "a", + range: 57..58, + }, + annotation: None, + }, + default: None, + }, + ], + args: [], + vararg: None, + kwonlyargs: [ + ParameterWithDefault { + range: 66..67, + parameter: Parameter { + range: 66..67, + name: Identifier { + id: "b", + range: 66..67, + }, + annotation: None, + }, + default: None, + }, + ], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 70..73, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 70..73, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 74..104, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 78..81, + }, + type_params: None, + parameters: Parameters { + range: 81..99, + posonlyargs: [ + ParameterWithDefault { + range: 82..83, + parameter: Parameter { + range: 82..83, + name: Identifier { + id: "a", + range: 82..83, + }, + annotation: None, + }, + default: None, + }, + ], + args: [], + vararg: None, + kwonlyargs: [ + ParameterWithDefault { + range: 88..89, + parameter: Parameter { + range: 88..89, + name: Identifier { + id: "b", + range: 88..89, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 91..92, + parameter: Parameter { + range: 91..92, + name: Identifier { + id: "c", + range: 91..92, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 97..98, + parameter: Parameter { + range: 97..98, + name: Identifier { + id: "d", + range: 97..98, + }, + annotation: None, + }, + default: None, + }, + ], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 101..104, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 101..104, + }, + ), + }, + ), + ], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | def foo(*a, /): ... + | ^ Syntax Error: '/' parameter must appear before '*' parameter +2 | def foo(a, *args, b, /): ... +3 | def foo(a, *, /, b): ... + | + + + | +1 | def foo(*a, /): ... +2 | def foo(a, *args, b, /): ... + | ^ Syntax Error: '/' parameter must appear before '*' parameter +3 | def foo(a, *, /, b): ... +4 | def foo(a, *, b, c, /, d): ... + | + + + | +1 | def foo(*a, /): ... +2 | def foo(a, *args, b, /): ... +3 | def foo(a, *, /, b): ... + | ^ Syntax Error: '/' parameter must appear before '*' parameter +4 | def foo(a, *, b, c, /, d): ... + | + + + | +2 | def foo(a, *args, b, /): ... +3 | def foo(a, *, /, b): ... +4 | def foo(a, *, b, c, /, d): ... + | ^ Syntax Error: '/' parameter must appear before '*' parameter + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@params_star_separator_after_star_param.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@params_star_separator_after_star_param.py.snap.new new file mode 100644 index 0000000000..849c0067b2 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@params_star_separator_after_star_param.py.snap.new @@ -0,0 +1,178 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/params_star_separator_after_star_param.py +--- +## AST + +``` +Module( + ModModule { + range: 0..61, + body: [ + FunctionDef( + StmtFunctionDef { + range: 0..28, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 4..7, + }, + type_params: None, + parameters: Parameters { + range: 7..23, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 8..9, + parameter: Parameter { + range: 8..9, + name: Identifier { + id: "a", + range: 8..9, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: Some( + Parameter { + range: 11..16, + name: Identifier { + id: "args", + range: 12..16, + }, + annotation: None, + }, + ), + kwonlyargs: [ + ParameterWithDefault { + range: 21..22, + parameter: Parameter { + range: 21..22, + name: Identifier { + id: "b", + range: 21..22, + }, + annotation: None, + }, + default: None, + }, + ], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 25..28, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 25..28, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 29..60, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 33..36, + }, + type_params: None, + parameters: Parameters { + range: 36..55, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 37..38, + parameter: Parameter { + range: 37..38, + name: Identifier { + id: "a", + range: 37..38, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: Some( + Parameter { + range: 40..45, + name: Identifier { + id: "args", + range: 41..45, + }, + annotation: None, + }, + ), + kwonlyargs: [ + ParameterWithDefault { + range: 47..48, + parameter: Parameter { + range: 47..48, + name: Identifier { + id: "b", + range: 47..48, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 50..51, + parameter: Parameter { + range: 50..51, + name: Identifier { + id: "c", + range: 50..51, + }, + annotation: None, + }, + default: None, + }, + ], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 57..60, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 57..60, + }, + ), + }, + ), + ], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | def foo(a, *args, *, b): ... + | ^ Syntax Error: Keyword-only parameter separator not allowed after '*' parameter +2 | def foo(a, *args, b, c, *): ... + | + + + | +1 | def foo(a, *args, *, b): ... +2 | def foo(a, *args, b, c, *): ... + | ^ Syntax Error: Keyword-only parameter separator not allowed after '*' parameter + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@params_var_keyword_with_default.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@params_var_keyword_with_default.py.snap.new new file mode 100644 index 0000000000..605176a24e --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@params_var_keyword_with_default.py.snap.new @@ -0,0 +1,167 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/params_var_keyword_with_default.py +--- +## AST + +``` +Module( + ModModule { + range: 0..43, + body: [ + FunctionDef( + StmtFunctionDef { + range: 0..36, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 4..7, + }, + type_params: None, + parameters: Parameters { + range: 7..20, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 8..9, + parameter: Parameter { + range: 8..9, + name: Identifier { + id: "a", + range: 8..9, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: Some( + Parameter { + range: 11..19, + name: Identifier { + id: "kwargs", + range: 13..19, + }, + annotation: None, + }, + ), + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 20..36, + value: Dict( + ExprDict { + range: 20..36, + items: [ + DictItem { + key: Some( + StringLiteral( + ExprStringLiteral { + range: 21..24, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 21..24, + value: "b", + flags: StringLiteralFlags { + quote_style: Single, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 26..27, + value: Int( + 1, + ), + }, + ), + }, + DictItem { + key: Some( + StringLiteral( + ExprStringLiteral { + range: 29..32, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 29..32, + value: "c", + flags: StringLiteralFlags { + quote_style: Single, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 34..35, + value: Int( + 2, + ), + }, + ), + }, + ], + }, + ), + }, + ), + ], + }, + ), + Expr( + StmtExpr { + range: 39..42, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 39..42, + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | def foo(a, **kwargs={'b': 1, 'c': 2}): ... + | ^ Syntax Error: Parameter with '*' or '**' cannot have default value + | + + + | +1 | def foo(a, **kwargs={'b': 1, 'c': 2}): ... + | ^ Syntax Error: Expected ')', found '{' + | + + + | +1 | def foo(a, **kwargs={'b': 1, 'c': 2}): ... + | ^ Syntax Error: Expected newline, found ')' + | + + + | +1 | def foo(a, **kwargs={'b': 1, 'c': 2}): ... + | ^ Syntax Error: Expected a statement + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@params_var_positional_with_default.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@params_var_positional_with_default.py.snap.new new file mode 100644 index 0000000000..a7b3b8db02 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@params_var_positional_with_default.py.snap.new @@ -0,0 +1,125 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/params_var_positional_with_default.py +--- +## AST + +``` +Module( + ModModule { + range: 0..30, + body: [ + FunctionDef( + StmtFunctionDef { + range: 0..23, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 4..7, + }, + type_params: None, + parameters: Parameters { + range: 7..17, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 8..9, + parameter: Parameter { + range: 8..9, + name: Identifier { + id: "a", + range: 8..9, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: Some( + Parameter { + range: 11..16, + name: Identifier { + id: "args", + range: 12..16, + }, + annotation: None, + }, + ), + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 17..23, + value: Tuple( + ExprTuple { + range: 17..23, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 18..19, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 21..22, + value: Int( + 2, + ), + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + ], + }, + ), + Expr( + StmtExpr { + range: 26..29, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 26..29, + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | def foo(a, *args=(1, 2)): ... + | ^ Syntax Error: Parameter with '*' or '**' cannot have default value + | + + + | +1 | def foo(a, *args=(1, 2)): ... + | ^ Syntax Error: Expected ')', found '(' + | + + + | +1 | def foo(a, *args=(1, 2)): ... + | ^ Syntax Error: Expected newline, found ')' + | + + + | +1 | def foo(a, *args=(1, 2)): ... + | ^ Syntax Error: Expected a statement + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@raise_stmt_invalid_cause.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@raise_stmt_invalid_cause.py.snap.new new file mode 100644 index 0000000000..21d6d06ef8 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@raise_stmt_invalid_cause.py.snap.new @@ -0,0 +1,135 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/raise_stmt_invalid_cause.py +--- +## AST + +``` +Module( + ModModule { + range: 0..57, + body: [ + Raise( + StmtRaise { + range: 0..15, + exc: Some( + Name( + ExprName { + range: 6..7, + id: "x", + ctx: Load, + }, + ), + ), + cause: Some( + Starred( + ExprStarred { + range: 13..15, + value: Name( + ExprName { + range: 14..15, + id: "y", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ), + }, + ), + Raise( + StmtRaise { + range: 16..36, + exc: Some( + Name( + ExprName { + range: 22..23, + id: "x", + ctx: Load, + }, + ), + ), + cause: Some( + Yield( + ExprYield { + range: 29..36, + value: Some( + Name( + ExprName { + range: 35..36, + id: "y", + ctx: Load, + }, + ), + ), + }, + ), + ), + }, + ), + Raise( + StmtRaise { + range: 37..51, + exc: Some( + Name( + ExprName { + range: 43..44, + id: "x", + ctx: Load, + }, + ), + ), + cause: Some( + Name( + ExprName { + range: 50..51, + id: "y", + ctx: Load, + }, + ), + ), + }, + ), + Expr( + StmtExpr { + range: 55..56, + value: NumberLiteral( + ExprNumberLiteral { + range: 55..56, + value: Int( + 1, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | raise x from *y + | ^^ Syntax Error: Starred expression cannot be used here +2 | raise x from yield y +3 | raise x from y := 1 + | + + + | +1 | raise x from *y +2 | raise x from yield y + | ^^^^^^^ Syntax Error: Yield expression cannot be used here +3 | raise x from y := 1 + | + + + | +1 | raise x from *y +2 | raise x from yield y +3 | raise x from y := 1 + | ^^ Syntax Error: Expected a statement + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@raise_stmt_invalid_exc.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@raise_stmt_invalid_exc.py.snap.new new file mode 100644 index 0000000000..91a8125dda --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@raise_stmt_invalid_exc.py.snap.new @@ -0,0 +1,111 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/raise_stmt_invalid_exc.py +--- +## AST + +``` +Module( + ModModule { + range: 0..36, + body: [ + Raise( + StmtRaise { + range: 0..8, + exc: Some( + Starred( + ExprStarred { + range: 6..8, + value: Name( + ExprName { + range: 7..8, + id: "x", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ), + cause: None, + }, + ), + Raise( + StmtRaise { + range: 9..22, + exc: Some( + Yield( + ExprYield { + range: 15..22, + value: Some( + Name( + ExprName { + range: 21..22, + id: "x", + ctx: Load, + }, + ), + ), + }, + ), + ), + cause: None, + }, + ), + Raise( + StmtRaise { + range: 23..30, + exc: Some( + Name( + ExprName { + range: 29..30, + id: "x", + ctx: Load, + }, + ), + ), + cause: None, + }, + ), + Expr( + StmtExpr { + range: 34..35, + value: NumberLiteral( + ExprNumberLiteral { + range: 34..35, + value: Int( + 1, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | raise *x + | ^^ Syntax Error: Starred expression cannot be used here +2 | raise yield x +3 | raise x := 1 + | + + + | +1 | raise *x +2 | raise yield x + | ^^^^^^^ Syntax Error: Yield expression cannot be used here +3 | raise x := 1 + | + + + | +1 | raise *x +2 | raise yield x +3 | raise x := 1 + | ^^ Syntax Error: Expected a statement + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@raise_stmt_unparenthesized_tuple_cause.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@raise_stmt_unparenthesized_tuple_cause.py.snap.new new file mode 100644 index 0000000000..9ada6dca47 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@raise_stmt_unparenthesized_tuple_cause.py.snap.new @@ -0,0 +1,101 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/raise_stmt_unparenthesized_tuple_cause.py +--- +## AST + +``` +Module( + ModModule { + range: 0..34, + body: [ + Raise( + StmtRaise { + range: 0..15, + exc: Some( + Name( + ExprName { + range: 6..7, + id: "x", + ctx: Load, + }, + ), + ), + cause: Some( + Tuple( + ExprTuple { + range: 13..15, + elts: [ + Name( + ExprName { + range: 13..14, + id: "y", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + ), + }, + ), + Raise( + StmtRaise { + range: 16..33, + exc: Some( + Name( + ExprName { + range: 22..23, + id: "x", + ctx: Load, + }, + ), + ), + cause: Some( + Tuple( + ExprTuple { + range: 29..33, + elts: [ + Name( + ExprName { + range: 29..30, + id: "y", + ctx: Load, + }, + ), + Name( + ExprName { + range: 32..33, + id: "z", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | raise x from y, + | ^^ Syntax Error: Unparenthesized tuple expression cannot be used here +2 | raise x from y, z + | + + + | +1 | raise x from y, +2 | raise x from y, z + | ^^^^ Syntax Error: Unparenthesized tuple expression cannot be used here + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@raise_stmt_unparenthesized_tuple_exc.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@raise_stmt_unparenthesized_tuple_exc.py.snap.new new file mode 100644 index 0000000000..00f744c54b --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@raise_stmt_unparenthesized_tuple_exc.py.snap.new @@ -0,0 +1,134 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/raise_stmt_unparenthesized_tuple_exc.py +--- +## AST + +``` +Module( + ModModule { + range: 0..38, + body: [ + Raise( + StmtRaise { + range: 0..8, + exc: Some( + Tuple( + ExprTuple { + range: 6..8, + elts: [ + Name( + ExprName { + range: 6..7, + id: "x", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + ), + cause: None, + }, + ), + Raise( + StmtRaise { + range: 9..19, + exc: Some( + Tuple( + ExprTuple { + range: 15..19, + elts: [ + Name( + ExprName { + range: 15..16, + id: "x", + ctx: Load, + }, + ), + Name( + ExprName { + range: 18..19, + id: "y", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + ), + cause: None, + }, + ), + Raise( + StmtRaise { + range: 20..37, + exc: Some( + Tuple( + ExprTuple { + range: 26..30, + elts: [ + Name( + ExprName { + range: 26..27, + id: "x", + ctx: Load, + }, + ), + Name( + ExprName { + range: 29..30, + id: "y", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + ), + cause: Some( + Name( + ExprName { + range: 36..37, + id: "z", + ctx: Load, + }, + ), + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | raise x, + | ^^ Syntax Error: Unparenthesized tuple expression cannot be used here +2 | raise x, y +3 | raise x, y from z + | + + + | +1 | raise x, +2 | raise x, y + | ^^^^ Syntax Error: Unparenthesized tuple expression cannot be used here +3 | raise x, y from z + | + + + | +1 | raise x, +2 | raise x, y +3 | raise x, y from z + | ^^^^ Syntax Error: Unparenthesized tuple expression cannot be used here + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@re_lex_logical_token.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@re_lex_logical_token.py.snap.new new file mode 100644 index 0000000000..ee6eaa9613 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@re_lex_logical_token.py.snap.new @@ -0,0 +1,839 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/re_lex_logical_token.py +--- +## AST + +``` +Module( + ModModule { + range: 0..979, + body: [ + If( + StmtIf { + range: 48..59, + test: Call( + ExprCall { + range: 51..59, + func: Name( + ExprName { + range: 51..55, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 55..59, + args: [ + Name( + ExprName { + range: 56..59, + id: "foo", + ctx: Load, + }, + ), + ], + keywords: [], + }, + }, + ), + body: [], + elif_else_clauses: [], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 60..79, + is_async: false, + decorator_list: [], + name: Identifier { + id: "bar", + range: 64..67, + }, + type_params: None, + parameters: Parameters { + range: 67..69, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 75..79, + }, + ), + ], + }, + ), + If( + StmtIf { + range: 113..152, + test: Call( + ExprCall { + range: 116..124, + func: Name( + ExprName { + range: 116..120, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 120..124, + args: [ + Name( + ExprName { + range: 121..124, + id: "foo", + ctx: Load, + }, + ), + ], + keywords: [], + }, + }, + ), + body: [ + FunctionDef( + StmtFunctionDef { + range: 129..152, + is_async: false, + decorator_list: [], + name: Identifier { + id: "bar", + range: 133..136, + }, + type_params: None, + parameters: Parameters { + range: 136..138, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 148..152, + }, + ), + ], + }, + ), + ], + elif_else_clauses: [], + }, + ), + If( + StmtIf { + range: 228..269, + test: Call( + ExprCall { + range: 231..239, + func: Name( + ExprName { + range: 231..235, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 235..239, + args: [ + Name( + ExprName { + range: 236..239, + id: "foo", + ctx: Load, + }, + ), + ], + keywords: [], + }, + }, + ), + body: [ + FunctionDef( + StmtFunctionDef { + range: 246..269, + is_async: false, + decorator_list: [], + name: Identifier { + id: "bar", + range: 250..253, + }, + type_params: None, + parameters: Parameters { + range: 253..255, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 265..269, + }, + ), + ], + }, + ), + ], + elif_else_clauses: [], + }, + ), + If( + StmtIf { + range: 344..392, + test: Call( + ExprCall { + range: 347..355, + func: Name( + ExprName { + range: 347..351, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 351..355, + args: [ + Name( + ExprName { + range: 352..355, + id: "foo", + ctx: Load, + }, + ), + ], + keywords: [], + }, + }, + ), + body: [ + FunctionDef( + StmtFunctionDef { + range: 369..392, + is_async: false, + decorator_list: [], + name: Identifier { + id: "bar", + range: 373..376, + }, + type_params: None, + parameters: Parameters { + range: 376..378, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 388..392, + }, + ), + ], + }, + ), + ], + elif_else_clauses: [], + }, + ), + If( + StmtIf { + range: 453..499, + test: Call( + ExprCall { + range: 456..472, + func: Name( + ExprName { + range: 456..460, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 460..472, + args: [ + Name( + ExprName { + range: 461..464, + id: "foo", + ctx: Load, + }, + ), + List( + ExprList { + range: 466..471, + elts: [ + Name( + ExprName { + range: 467..468, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 470..471, + id: "b", + ctx: Load, + }, + ), + ], + ctx: Load, + }, + ), + ], + keywords: [], + }, + }, + ), + body: [ + FunctionDef( + StmtFunctionDef { + range: 476..499, + is_async: false, + decorator_list: [], + name: Identifier { + id: "bar", + range: 480..483, + }, + type_params: None, + parameters: Parameters { + range: 483..485, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 495..499, + }, + ), + ], + }, + ), + ], + elif_else_clauses: [], + }, + ), + If( + StmtIf { + range: 564..611, + test: Call( + ExprCall { + range: 567..583, + func: Name( + ExprName { + range: 567..571, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 571..583, + args: [ + Name( + ExprName { + range: 572..575, + id: "foo", + ctx: Load, + }, + ), + List( + ExprList { + range: 577..582, + elts: [ + Name( + ExprName { + range: 578..579, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 581..582, + id: "b", + ctx: Load, + }, + ), + ], + ctx: Load, + }, + ), + ], + keywords: [], + }, + }, + ), + body: [ + FunctionDef( + StmtFunctionDef { + range: 588..611, + is_async: false, + decorator_list: [], + name: Identifier { + id: "bar", + range: 592..595, + }, + type_params: None, + parameters: Parameters { + range: 595..597, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 607..611, + }, + ), + ], + }, + ), + ], + elif_else_clauses: [], + }, + ), + If( + StmtIf { + range: 772..824, + test: Call( + ExprCall { + range: 775..796, + func: Name( + ExprName { + range: 775..779, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 779..796, + args: [ + Name( + ExprName { + range: 780..783, + id: "foo", + ctx: Load, + }, + ), + List( + ExprList { + range: 785..794, + elts: [ + Name( + ExprName { + range: 786..787, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 793..794, + id: "b", + ctx: Load, + }, + ), + ], + ctx: Load, + }, + ), + ], + keywords: [], + }, + }, + ), + body: [ + FunctionDef( + StmtFunctionDef { + range: 801..824, + is_async: false, + decorator_list: [], + name: Identifier { + id: "bar", + range: 805..808, + }, + type_params: None, + parameters: Parameters { + range: 808..810, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 820..824, + }, + ), + ], + }, + ), + ], + elif_else_clauses: [], + }, + ), + If( + StmtIf { + range: 887..933, + test: Call( + ExprCall { + range: 890..905, + func: Name( + ExprName { + range: 890..894, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 894..905, + args: [ + FString( + ExprFString { + range: 895..905, + value: FStringValue { + inner: Single( + FString( + FString { + range: 895..905, + elements: [ + Literal( + FStringLiteralElement { + range: 897..903, + value: "hello ", + }, + ), + Expression( + FStringExpressionElement { + range: 903..905, + expression: Name( + ExprName { + range: 904..905, + id: "x", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + ], + keywords: [], + }, + }, + ), + body: [ + FunctionDef( + StmtFunctionDef { + range: 910..933, + is_async: false, + decorator_list: [], + name: Identifier { + id: "bar", + range: 914..917, + }, + type_params: None, + parameters: Parameters { + range: 917..919, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 929..933, + }, + ), + ], + }, + ), + ], + elif_else_clauses: [], + }, + ), + If( + StmtIf { + range: 936..956, + test: Call( + ExprCall { + range: 939..956, + func: Name( + ExprName { + range: 939..943, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 943..956, + args: [ + FString( + ExprFString { + range: 944..951, + value: FStringValue { + inner: Single( + FString( + FString { + range: 944..951, + elements: [], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + ], + keywords: [], + }, + }, + ), + body: [], + elif_else_clauses: [], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 956..979, + is_async: false, + decorator_list: [], + name: Identifier { + id: "bar", + range: 960..963, + }, + type_params: None, + parameters: Parameters { + range: 963..965, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 975..979, + }, + ), + ], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | # No indentation before the function definition +2 | if call(foo + | ^ Syntax Error: Expected ')', found newline +3 | def bar(): +4 | pass + | + + + | +1 | # No indentation before the function definition +2 | if call(foo +3 | def bar(): + | ^^^ Syntax Error: Expected an indented block after `if` statement +4 | pass + | + + + | + 7 | # Indented function definition + 8 | if call(foo + | ^ Syntax Error: Expected ')', found newline + 9 | def bar(): +10 | pass + | + + + | +13 | # There are multiple non-logical newlines (blank lines) in the `if` body +14 | if call(foo + | ^ Syntax Error: Expected ')', found newline +15 | +16 | +17 | def bar(): + | + + + | +21 | # There are trailing whitespaces in the blank line inside the `if` body +22 | if call(foo + | ^ Syntax Error: Expected ')', found newline +23 | +24 | def bar(): +25 | pass + | + + + | +28 | # The lexer is nested with multiple levels of parentheses +29 | if call(foo, [a, b + | ^ Syntax Error: Expected ']', found NonLogicalNewline +30 | def bar(): +31 | pass + | + + + | +34 | # The outer parenthesis is closed but the inner bracket isn't +35 | if call(foo, [a, b) + | ^ Syntax Error: Expected ']', found ')' +36 | def bar(): +37 | pass + | + + + | +34 | # The outer parenthesis is closed but the inner bracket isn't +35 | if call(foo, [a, b) + | ^ Syntax Error: Expected ':', found newline +36 | def bar(): +37 | pass + | + + + | +41 | # test is to make sure it emits a `NonLogicalNewline` token after `b`. +42 | if call(foo, [a, +43 | b + | ^ Syntax Error: Expected ']', found NonLogicalNewline +44 | ) +45 | def bar(): +46 | pass + | + + + | +42 | if call(foo, [a, +43 | b +44 | ) + | ^ Syntax Error: Expected ':', found newline +45 | def bar(): +46 | pass + | + + + | +49 | # F-strings uses normal list parsing, so test those as well +50 | if call(f"hello {x + | Syntax Error: f-string: unterminated string +51 | def bar(): +52 | pass + | + + + | +49 | # F-strings uses normal list parsing, so test those as well +50 | if call(f"hello {x +51 | def bar(): + | ^^^ Syntax Error: f-string: expecting '}' +52 | pass + | + + + | +49 | # F-strings uses normal list parsing, so test those as well +50 | if call(f"hello {x + | Syntax Error: Expected FStringEnd, found Unknown +51 | def bar(): +52 | pass + | + + + | +55 | if call(f"hello + | ^^^^^ Syntax Error: f-string: unterminated string +56 | def bar(): +57 | pass + | + + + | +55 | if call(f"hello + | ^ Syntax Error: Expected FStringEnd, found newline +56 | def bar(): +57 | pass + | + + + | +55 | if call(f"hello +56 | def bar(): + | ^^^^ Syntax Error: Expected ',', found indent +57 | pass + | + + + | +55 | if call(f"hello +56 | def bar(): + | ^^^ Syntax Error: Expected ')', found 'def' +57 | pass + | + + + | +55 | if call(f"hello +56 | def bar(): +57 | pass + | Syntax Error: Expected a statement + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@re_lex_logical_token_mac_eol.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@re_lex_logical_token_mac_eol.py.snap.new new file mode 100644 index 0000000000..bdf6962669 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@re_lex_logical_token_mac_eol.py.snap.new @@ -0,0 +1,105 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/re_lex_logical_token_mac_eol.py +--- +## AST + +``` +Module( + ModModule { + range: 0..46, + body: [ + If( + StmtIf { + range: 0..46, + test: Call( + ExprCall { + range: 3..19, + func: Name( + ExprName { + range: 3..7, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 7..19, + args: [ + Name( + ExprName { + range: 8..11, + id: "foo", + ctx: Load, + }, + ), + List( + ExprList { + range: 13..18, + elts: [ + Name( + ExprName { + range: 14..15, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 17..18, + id: "b", + ctx: Load, + }, + ), + ], + ctx: Load, + }, + ), + ], + keywords: [], + }, + }, + ), + body: [ + FunctionDef( + StmtFunctionDef { + range: 23..46, + is_async: false, + decorator_list: [], + name: Identifier { + id: "bar", + range: 27..30, + }, + type_params: None, + parameters: Parameters { + range: 30..32, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 42..46, + }, + ), + ], + }, + ), + ], + elif_else_clauses: [], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | if call(foo, [a, b def bar(): pass + | Syntax Error: Expected ']', found NonLogicalNewline + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@re_lex_logical_token_windows_eol.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@re_lex_logical_token_windows_eol.py.snap.new new file mode 100644 index 0000000000..d234119959 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@re_lex_logical_token_windows_eol.py.snap.new @@ -0,0 +1,108 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/re_lex_logical_token_windows_eol.py +--- +## AST + +``` +Module( + ModModule { + range: 0..50, + body: [ + If( + StmtIf { + range: 0..48, + test: Call( + ExprCall { + range: 3..20, + func: Name( + ExprName { + range: 3..7, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 7..20, + args: [ + Name( + ExprName { + range: 8..11, + id: "foo", + ctx: Load, + }, + ), + List( + ExprList { + range: 13..18, + elts: [ + Name( + ExprName { + range: 14..15, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 17..18, + id: "b", + ctx: Load, + }, + ), + ], + ctx: Load, + }, + ), + ], + keywords: [], + }, + }, + ), + body: [ + FunctionDef( + StmtFunctionDef { + range: 24..48, + is_async: false, + decorator_list: [], + name: Identifier { + id: "bar", + range: 28..31, + }, + type_params: None, + parameters: Parameters { + range: 31..33, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 44..48, + }, + ), + ], + }, + ), + ], + elif_else_clauses: [], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | if call(foo, [a, b + | ___________________^ +2 | | def bar(): + | |_^ Syntax Error: Expected ']', found NonLogicalNewline +3 | pass + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@re_lexing__fstring_format_spec_1.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@re_lexing__fstring_format_spec_1.py.snap.new new file mode 100644 index 0000000000..a0cd2c5175 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@re_lexing__fstring_format_spec_1.py.snap.new @@ -0,0 +1,424 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/re_lexing/fstring_format_spec_1.py +--- +## AST + +``` +Module( + ModModule { + range: 0..298, + body: [ + Expr( + StmtExpr { + range: 162..192, + value: FString( + ExprFString { + range: 162..192, + value: FStringValue { + inner: Single( + FString( + FString { + range: 162..192, + elements: [ + Literal( + FStringLiteralElement { + range: 164..171, + value: "middle ", + }, + ), + Expression( + FStringExpressionElement { + range: 171..191, + expression: StringLiteral( + ExprStringLiteral { + range: 172..180, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 172..180, + value: "string", + flags: StringLiteralFlags { + quote_style: Single, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + debug_text: None, + conversion: None, + format_spec: Some( + FStringFormatSpec { + range: 181..191, + elements: [ + Literal( + FStringLiteralElement { + range: 181..191, + value: " ", + }, + ), + ], + }, + ), + }, + ), + ], + flags: FStringFlags { + quote_style: Single, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 192..198, + value: Name( + ExprName { + range: 192..198, + id: "format", + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 199..203, + value: Name( + ExprName { + range: 199..203, + id: "spec", + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 207..228, + value: FString( + ExprFString { + range: 207..228, + value: FStringValue { + inner: Single( + FString( + FString { + range: 207..228, + elements: [ + Literal( + FStringLiteralElement { + range: 209..216, + value: "middle ", + }, + ), + Expression( + FStringExpressionElement { + range: 216..228, + expression: StringLiteral( + ExprStringLiteral { + range: 217..225, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 217..225, + value: "string", + flags: StringLiteralFlags { + quote_style: Single, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + debug_text: None, + conversion: None, + format_spec: Some( + FStringFormatSpec { + range: 226..228, + elements: [ + Literal( + FStringLiteralElement { + range: 226..228, + value: "\\", + }, + ), + ], + }, + ), + }, + ), + ], + flags: FStringFlags { + quote_style: Single, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 237..250, + value: StringLiteral( + ExprStringLiteral { + range: 237..250, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 237..250, + value: "format spec", + flags: StringLiteralFlags { + quote_style: Single, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 253..285, + value: FString( + ExprFString { + range: 253..285, + value: FStringValue { + inner: Single( + FString( + FString { + range: 253..285, + elements: [ + Literal( + FStringLiteralElement { + range: 255..262, + value: "middle ", + }, + ), + Expression( + FStringExpressionElement { + range: 262..284, + expression: StringLiteral( + ExprStringLiteral { + range: 263..271, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 263..271, + value: "string", + flags: StringLiteralFlags { + quote_style: Single, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + debug_text: None, + conversion: None, + format_spec: Some( + FStringFormatSpec { + range: 272..284, + elements: [ + Literal( + FStringLiteralElement { + range: 272..284, + value: "\\ ", + }, + ), + ], + }, + ), + }, + ), + ], + flags: FStringFlags { + quote_style: Single, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 285..291, + value: Name( + ExprName { + range: 285..291, + id: "format", + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 292..296, + value: Name( + ExprName { + range: 292..296, + id: "spec", + ctx: Load, + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +5 | f'middle {'string':\ +6 | 'format spec'} + | ^ Syntax Error: f-string: expecting '}' +7 | +8 | f'middle {'string':\\ + | + + + | +5 | f'middle {'string':\ +6 | 'format spec'} + | ^^^^^^ Syntax Error: Simple statements must be separated by newlines or semicolons +7 | +8 | f'middle {'string':\\ + | + + + | +5 | f'middle {'string':\ +6 | 'format spec'} + | ^^^^ Syntax Error: Simple statements must be separated by newlines or semicolons +7 | +8 | f'middle {'string':\\ + | + + + | +5 | f'middle {'string':\ +6 | 'format spec'} + | ^^ Syntax Error: missing closing quote in string literal +7 | +8 | f'middle {'string':\\ + | + + + | +5 | f'middle {'string':\ +6 | 'format spec'} + | ^ Syntax Error: Expected a statement +7 | +8 | f'middle {'string':\\ +9 | 'format spec'} + | + + + | +6 | 'format spec'} +7 | +8 | f'middle {'string':\\ + | Syntax Error: f-string: unterminated string +9 | 'format spec'} + | + + + | + 8 | f'middle {'string':\\ + 9 | 'format spec'} + | ^^^^^^^^ Syntax Error: Unexpected indentation +10 | +11 | f'middle {'string':\\\ + | + + + | + 8 | f'middle {'string':\\ + 9 | 'format spec'} + | ^ Syntax Error: Expected a statement +10 | +11 | f'middle {'string':\\\ + | + + + | + 8 | f'middle {'string':\\ + 9 | 'format spec'} + | ^ Syntax Error: Expected a statement +10 | +11 | f'middle {'string':\\\ +12 | 'format spec'} + | + + + | + 9 | 'format spec'} +10 | +11 | f'middle {'string':\\\ + | Syntax Error: Expected a statement +12 | 'format spec'} + | + + + | +11 | f'middle {'string':\\\ +12 | 'format spec'} + | ^ Syntax Error: f-string: expecting '}' + | + + + | +11 | f'middle {'string':\\\ +12 | 'format spec'} + | ^^^^^^ Syntax Error: Simple statements must be separated by newlines or semicolons + | + + + | +11 | f'middle {'string':\\\ +12 | 'format spec'} + | ^^^^ Syntax Error: Simple statements must be separated by newlines or semicolons + | + + + | +11 | f'middle {'string':\\\ +12 | 'format spec'} + | ^^ Syntax Error: Got unexpected string + | + + + | +11 | f'middle {'string':\\\ +12 | 'format spec'} + | Syntax Error: Expected a statement + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@re_lexing__line_continuation_1.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@re_lexing__line_continuation_1.py.snap.new new file mode 100644 index 0000000000..d2df2ee573 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@re_lexing__line_continuation_1.py.snap.new @@ -0,0 +1,106 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/re_lexing/line_continuation_1.py +--- +## AST + +``` +Module( + ModModule { + range: 0..36, + body: [ + Expr( + StmtExpr { + range: 0..13, + value: Call( + ExprCall { + range: 0..13, + func: Name( + ExprName { + range: 0..4, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 4..13, + args: [ + Name( + ExprName { + range: 5..6, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 8..9, + id: "b", + ctx: Load, + }, + ), + ], + keywords: [], + }, + }, + ), + }, + ), + FunctionDef( + StmtFunctionDef { + range: 16..35, + is_async: false, + decorator_list: [], + name: Identifier { + id: "bar", + range: 20..23, + }, + type_params: None, + parameters: Parameters { + range: 23..25, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 31..35, + }, + ), + ], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | call(a, b, \\\ + | ^ Syntax Error: Expected a newline after line continuation character +2 | +3 | def bar(): + | + + + | +1 | call(a, b, \\\ + | ^ Syntax Error: Expected a newline after line continuation character +2 | +3 | def bar(): + | + + + | +1 | call(a, b, \\\ +2 | + | ^ Syntax Error: Expected ')', found newline +3 | def bar(): +4 | pass + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@re_lexing__line_continuation_windows_eol.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@re_lexing__line_continuation_windows_eol.py.snap.new new file mode 100644 index 0000000000..447a718882 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@re_lexing__line_continuation_windows_eol.py.snap.new @@ -0,0 +1,91 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/re_lexing/line_continuation_windows_eol.py +--- +## AST + +``` +Module( + ModModule { + range: 0..46, + body: [ + Expr( + StmtExpr { + range: 0..10, + value: Call( + ExprCall { + range: 0..10, + func: Name( + ExprName { + range: 0..4, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 4..10, + args: [ + Name( + ExprName { + range: 5..6, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 8..9, + id: "b", + ctx: Load, + }, + ), + ], + keywords: [], + }, + }, + ), + }, + ), + FunctionDef( + StmtFunctionDef { + range: 26..46, + is_async: false, + decorator_list: [], + name: Identifier { + id: "bar", + range: 30..33, + }, + type_params: None, + parameters: Parameters { + range: 33..35, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 42..46, + }, + ), + ], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | call(a, b, # comment \ + | _______________________^ +2 | | + | |_^ Syntax Error: Expected ')', found newline +3 | def bar(): +4 | pass + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@re_lexing__triple_quoted_fstring_1.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@re_lexing__triple_quoted_fstring_1.py.snap.new new file mode 100644 index 0000000000..58e85bf80a --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@re_lexing__triple_quoted_fstring_1.py.snap.new @@ -0,0 +1,97 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/re_lexing/triple_quoted_fstring_1.py +--- +## AST + +``` +Module( + ModModule { + range: 0..198, + body: [ + Expr( + StmtExpr { + range: 166..178, + value: FString( + ExprFString { + range: 166..178, + value: FStringValue { + inner: Single( + FString( + FString { + range: 166..178, + elements: [ + Literal( + FStringLiteralElement { + range: 170..176, + value: "hello ", + }, + ), + Expression( + FStringExpressionElement { + range: 176..178, + expression: Name( + ExprName { + range: 177..178, + id: "x", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: true, + }, + }, + ), + ), + }, + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +3 | # https://github.com/astral-sh/ruff/issues/11929 +4 | +5 | f"""hello {x # comment + | ___________________________^ +6 | | y = 1 + | |_____^ Syntax Error: f-string: unterminated triple-quoted string + | + + + | +5 | f"""hello {x # comment +6 | y = 1 + | ^ Syntax Error: f-string: expecting '}' + | + + + | +3 | # https://github.com/astral-sh/ruff/issues/11929 +4 | +5 | f"""hello {x # comment + | ___________________________^ +6 | | y = 1 + | |_____^ Syntax Error: Expected FStringEnd, found Unknown + | + + + | +5 | f"""hello {x # comment +6 | y = 1 + | Syntax Error: Expected a statement + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@re_lexing__triple_quoted_fstring_2.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@re_lexing__triple_quoted_fstring_2.py.snap.new new file mode 100644 index 0000000000..048d12450a --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@re_lexing__triple_quoted_fstring_2.py.snap.new @@ -0,0 +1,76 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/re_lexing/triple_quoted_fstring_2.py +--- +## AST + +``` +Module( + ModModule { + range: 0..183, + body: [ + Expr( + StmtExpr { + range: 167..183, + value: FString( + ExprFString { + range: 167..183, + value: FStringValue { + inner: Single( + FString( + FString { + range: 167..183, + elements: [ + Expression( + FStringExpressionElement { + range: 171..180, + expression: Name( + ExprName { + range: 172..175, + id: "foo", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: Some( + FStringFormatSpec { + range: 176..180, + elements: [ + Literal( + FStringLiteralElement { + range: 176..180, + value: ".3f\n", + }, + ), + ], + }, + ), + }, + ), + ], + flags: FStringFlags { + quote_style: Single, + prefix: Regular, + triple_quoted: true, + }, + }, + ), + ), + }, + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +5 | f'''{foo:.3f +6 | ''' + | ^^^ Syntax Error: f-string: expecting '}' + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@re_lexing__triple_quoted_fstring_3.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@re_lexing__triple_quoted_fstring_3.py.snap.new new file mode 100644 index 0000000000..1bb140afee --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@re_lexing__triple_quoted_fstring_3.py.snap.new @@ -0,0 +1,111 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/re_lexing/triple_quoted_fstring_3.py +--- +## AST + +``` +Module( + ModModule { + range: 0..262, + body: [ + If( + StmtIf { + range: 231..262, + test: Call( + ExprCall { + range: 234..253, + func: Name( + ExprName { + range: 234..238, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 238..253, + args: [ + FString( + ExprFString { + range: 239..253, + value: FStringValue { + inner: Single( + FString( + FString { + range: 239..253, + elements: [ + Expression( + FStringExpressionElement { + range: 243..250, + expression: Name( + ExprName { + range: 244..245, + id: "x", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: Some( + FStringFormatSpec { + range: 246..250, + elements: [ + Literal( + FStringLiteralElement { + range: 246..250, + value: ".3f\n", + }, + ), + ], + }, + ), + }, + ), + ], + flags: FStringFlags { + quote_style: Single, + prefix: Regular, + triple_quoted: true, + }, + }, + ), + ), + }, + }, + ), + ], + keywords: [], + }, + }, + ), + body: [ + Pass( + StmtPass { + range: 258..262, + }, + ), + ], + elif_else_clauses: [], + }, + ), + ], + }, +) +``` +## Errors + + | +5 | if call(f'''{x:.3f +6 | ''' + | ^^^ Syntax Error: f-string: expecting '}' +7 | pass + | + + + | +5 | if call(f'''{x:.3f +6 | ''' + | ^ Syntax Error: Expected ')', found newline +7 | pass + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@return_stmt_invalid_expr.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@return_stmt_invalid_expr.py.snap.new new file mode 100644 index 0000000000..47494e5884 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@return_stmt_invalid_expr.py.snap.new @@ -0,0 +1,183 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/return_stmt_invalid_expr.py +--- +## AST + +``` +Module( + ModModule { + range: 0..74, + body: [ + Return( + StmtReturn { + range: 0..8, + value: Some( + Starred( + ExprStarred { + range: 7..8, + value: Name( + ExprName { + range: 8..8, + id: "", + ctx: Invalid, + }, + ), + ctx: Load, + }, + ), + ), + }, + ), + Return( + StmtReturn { + range: 9..23, + value: Some( + Yield( + ExprYield { + range: 16..23, + value: Some( + Name( + ExprName { + range: 22..23, + id: "x", + ctx: Load, + }, + ), + ), + }, + ), + ), + }, + ), + Return( + StmtReturn { + range: 24..43, + value: Some( + YieldFrom( + ExprYieldFrom { + range: 31..43, + value: Name( + ExprName { + range: 42..43, + id: "x", + ctx: Load, + }, + ), + }, + ), + ), + }, + ), + Return( + StmtReturn { + range: 44..52, + value: Some( + Name( + ExprName { + range: 51..52, + id: "x", + ctx: Load, + }, + ), + ), + }, + ), + Expr( + StmtExpr { + range: 56..57, + value: NumberLiteral( + ExprNumberLiteral { + range: 56..57, + value: Int( + 1, + ), + }, + ), + }, + ), + Return( + StmtReturn { + range: 58..73, + value: Some( + Starred( + ExprStarred { + range: 65..73, + value: BoolOp( + ExprBoolOp { + range: 66..73, + op: And, + values: [ + Name( + ExprName { + range: 66..67, + id: "x", + ctx: Load, + }, + ), + Name( + ExprName { + range: 72..73, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + ctx: Load, + }, + ), + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | return * + | ^ Syntax Error: Expected an expression +2 | return yield x +3 | return yield from x +4 | return x := 1 + | + + + | +1 | return * +2 | return yield x + | ^^^^^^^ Syntax Error: Yield expression cannot be used here +3 | return yield from x +4 | return x := 1 + | + + + | +1 | return * +2 | return yield x +3 | return yield from x + | ^^^^^^^^^^^^ Syntax Error: Yield expression cannot be used here +4 | return x := 1 +5 | return *x and y + | + + + | +2 | return yield x +3 | return yield from x +4 | return x := 1 + | ^^ Syntax Error: Expected a statement +5 | return *x and y + | + + + | +3 | return yield from x +4 | return x := 1 +5 | return *x and y + | ^^^^^^^ Syntax Error: Boolean expression cannot be used here + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@simple_and_compound_stmt_on_same_line.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@simple_and_compound_stmt_on_same_line.py.snap.new new file mode 100644 index 0000000000..588bc942c4 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@simple_and_compound_stmt_on_same_line.py.snap.new @@ -0,0 +1,66 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/simple_and_compound_stmt_on_same_line.py +--- +## AST + +``` +Module( + ModModule { + range: 0..17, + body: [ + Expr( + StmtExpr { + range: 0..1, + value: Name( + ExprName { + range: 0..1, + id: "a", + ctx: Load, + }, + ), + }, + ), + If( + StmtIf { + range: 3..16, + test: Name( + ExprName { + range: 6..7, + id: "b", + ctx: Load, + }, + ), + body: [ + Pass( + StmtPass { + range: 9..13, + }, + ), + Expr( + StmtExpr { + range: 15..16, + value: Name( + ExprName { + range: 15..16, + id: "b", + ctx: Load, + }, + ), + }, + ), + ], + elif_else_clauses: [], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | a; if b: pass; b + | ^^ Syntax Error: Compound statements are not allowed on the same line as simple statements + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@simple_stmts_on_same_line.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@simple_stmts_on_same_line.py.snap.new new file mode 100644 index 0000000000..1cacba6c79 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@simple_stmts_on_same_line.py.snap.new @@ -0,0 +1,147 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/simple_stmts_on_same_line.py +--- +## AST + +``` +Module( + ModModule { + range: 0..53, + body: [ + Expr( + StmtExpr { + range: 0..1, + value: Name( + ExprName { + range: 0..1, + id: "a", + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 2..3, + value: Name( + ExprName { + range: 2..3, + id: "b", + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 4..9, + value: BinOp( + ExprBinOp { + range: 4..9, + left: Name( + ExprName { + range: 4..5, + id: "a", + ctx: Load, + }, + ), + op: Add, + right: Name( + ExprName { + range: 8..9, + id: "b", + ctx: Load, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 10..15, + value: BinOp( + ExprBinOp { + range: 10..15, + left: Name( + ExprName { + range: 10..11, + id: "c", + ctx: Load, + }, + ), + op: Add, + right: Name( + ExprName { + range: 14..15, + id: "d", + ctx: Load, + }, + ), + }, + ), + }, + ), + Break( + StmtBreak { + range: 16..21, + }, + ), + Continue( + StmtContinue { + range: 23..31, + }, + ), + Pass( + StmtPass { + range: 32..36, + }, + ), + Continue( + StmtContinue { + range: 38..46, + }, + ), + Break( + StmtBreak { + range: 47..52, + }, + ), + ], + }, +) +``` +## Errors + + | +1 | a b + | ^ Syntax Error: Simple statements must be separated by newlines or semicolons +2 | a + b c + d +3 | break; continue pass; continue break + | + + + | +1 | a b +2 | a + b c + d + | ^ Syntax Error: Simple statements must be separated by newlines or semicolons +3 | break; continue pass; continue break + | + + + | +1 | a b +2 | a + b c + d +3 | break; continue pass; continue break + | ^^^^ Syntax Error: Simple statements must be separated by newlines or semicolons + | + + + | +1 | a b +2 | a + b c + d +3 | break; continue pass; continue break + | ^^^^^ Syntax Error: Simple statements must be separated by newlines or semicolons + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__function_type_parameters.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__function_type_parameters.py.snap.new new file mode 100644 index 0000000000..a7820ca504 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__function_type_parameters.py.snap.new @@ -0,0 +1,404 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/statements/function_type_parameters.py +--- +## AST + +``` +Module( + ModModule { + range: 0..988, + body: [ + FunctionDef( + StmtFunctionDef { + range: 796..824, + is_async: false, + decorator_list: [], + name: Identifier { + id: "keyword", + range: 800..807, + }, + type_params: Some( + TypeParams { + range: 807..817, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 808..809, + name: Identifier { + id: "A", + range: 808..809, + }, + bound: None, + default: None, + }, + ), + TypeVar( + TypeParamTypeVar { + range: 811..816, + name: Identifier { + id: "await", + range: 811..816, + }, + bound: None, + default: None, + }, + ), + ], + }, + ), + parameters: Parameters { + range: 817..819, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 821..824, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 821..824, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 826..862, + is_async: false, + decorator_list: [], + name: Identifier { + id: "not_a_type_param", + range: 830..846, + }, + type_params: Some( + TypeParams { + range: 846..855, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 847..848, + name: Identifier { + id: "A", + range: 847..848, + }, + bound: None, + default: None, + }, + ), + TypeVar( + TypeParamTypeVar { + range: 853..854, + name: Identifier { + id: "B", + range: 853..854, + }, + bound: None, + default: None, + }, + ), + ], + }, + ), + parameters: Parameters { + range: 855..857, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 859..862, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 859..862, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 864..896, + is_async: false, + decorator_list: [], + name: Identifier { + id: "multiple_commas", + range: 868..883, + }, + type_params: Some( + TypeParams { + range: 883..889, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 884..885, + name: Identifier { + id: "A", + range: 884..885, + }, + bound: None, + default: None, + }, + ), + TypeVar( + TypeParamTypeVar { + range: 887..888, + name: Identifier { + id: "B", + range: 887..888, + }, + bound: None, + default: None, + }, + ), + ], + }, + ), + parameters: Parameters { + range: 889..891, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 893..896, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 893..896, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 898..938, + is_async: false, + decorator_list: [], + name: Identifier { + id: "multiple_trailing_commas", + range: 902..926, + }, + type_params: Some( + TypeParams { + range: 926..931, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 927..928, + name: Identifier { + id: "A", + range: 927..928, + }, + bound: None, + default: None, + }, + ), + ], + }, + ), + parameters: Parameters { + range: 931..933, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 935..938, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 935..938, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 940..979, + is_async: false, + decorator_list: [], + name: Identifier { + id: "multiple_commas_and_recovery", + range: 944..972, + }, + type_params: Some( + TypeParams { + range: 972..976, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 973..974, + name: Identifier { + id: "A", + range: 973..974, + }, + bound: None, + default: None, + }, + ), + ], + }, + ), + parameters: Parameters { + range: 976..976, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 976..979, + value: NumberLiteral( + ExprNumberLiteral { + range: 976..979, + value: Int( + 100, + ), + }, + ), + }, + ), + ], + }, + ), + AnnAssign( + StmtAnnAssign { + range: 980..987, + target: Tuple( + ExprTuple { + range: 980..982, + elts: [], + ctx: Store, + parenthesized: true, + }, + ), + annotation: EllipsisLiteral( + ExprEllipsisLiteral { + range: 984..987, + }, + ), + value: None, + simple: false, + }, + ), + ], + }, +) +``` +## Errors + + | + 9 | # on following lines. +10 | +11 | def keyword[A, await](): ... + | ^^^^^ Syntax Error: Expected an identifier, but found a keyword 'await' that cannot be used here +12 | +13 | def not_a_type_param[A, |, B](): ... + | + + + | +11 | def keyword[A, await](): ... +12 | +13 | def not_a_type_param[A, |, B](): ... + | ^ Syntax Error: Expected ',', found '|' +14 | +15 | def multiple_commas[A,,B](): ... + | + + + | +11 | def keyword[A, await](): ... +12 | +13 | def not_a_type_param[A, |, B](): ... + | ^ Syntax Error: Expected a type parameter or the end of the type parameter list +14 | +15 | def multiple_commas[A,,B](): ... + | + + + | +13 | def not_a_type_param[A, |, B](): ... +14 | +15 | def multiple_commas[A,,B](): ... + | ^ Syntax Error: Expected a type parameter or the end of the type parameter list +16 | +17 | def multiple_trailing_commas[A,,](): ... + | + + + | +15 | def multiple_commas[A,,B](): ... +16 | +17 | def multiple_trailing_commas[A,,](): ... + | ^ Syntax Error: Expected a type parameter or the end of the type parameter list +18 | +19 | def multiple_commas_and_recovery[A,,100](): ... + | + + + | +17 | def multiple_trailing_commas[A,,](): ... +18 | +19 | def multiple_commas_and_recovery[A,,100](): ... + | ^ Syntax Error: Expected a type parameter or the end of the type parameter list + | + + + | +17 | def multiple_trailing_commas[A,,](): ... +18 | +19 | def multiple_commas_and_recovery[A,,100](): ... + | ^^^ Syntax Error: Expected ']', found int + | + + + | +17 | def multiple_trailing_commas[A,,](): ... +18 | +19 | def multiple_commas_and_recovery[A,,100](): ... + | ^ Syntax Error: Expected newline, found ']' + | + + + | +17 | def multiple_trailing_commas[A,,](): ... +18 | +19 | def multiple_commas_and_recovery[A,,100](): ... + | ^^ Syntax Error: Only single target (not tuple) can be annotated + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__if_extra_indent.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__if_extra_indent.py.snap.new new file mode 100644 index 0000000000..d5bb5d9a85 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__if_extra_indent.py.snap.new @@ -0,0 +1,105 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/statements/if_extra_indent.py +--- +## AST + +``` +Module( + ModModule { + range: 0..153, + body: [ + If( + StmtIf { + range: 103..134, + test: BooleanLiteral( + ExprBooleanLiteral { + range: 106..110, + value: true, + }, + ), + body: [ + Pass( + StmtPass { + range: 116..120, + }, + ), + Expr( + StmtExpr { + range: 129..134, + value: BinOp( + ExprBinOp { + range: 129..134, + left: Name( + ExprName { + range: 129..130, + id: "a", + ctx: Load, + }, + ), + op: Add, + right: Name( + ExprName { + range: 133..134, + id: "b", + ctx: Load, + }, + ), + }, + ), + }, + ), + ], + elif_else_clauses: [], + }, + ), + Pass( + StmtPass { + range: 140..144, + }, + ), + Assign( + StmtAssign { + range: 146..152, + targets: [ + Name( + ExprName { + range: 146..147, + id: "a", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 150..152, + value: Int( + 10, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +2 | if True: +3 | pass +4 | a + b + | ^^^^^^^^ Syntax Error: Unexpected indentation +5 | +6 | pass + | + + + | +6 | pass +7 | +8 | a = 10 + | Syntax Error: Expected a statement + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__invalid_assignment_targets.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__invalid_assignment_targets.py.snap.new new file mode 100644 index 0000000000..aaa43e26ef --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__invalid_assignment_targets.py.snap.new @@ -0,0 +1,1693 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/statements/invalid_assignment_targets.py +--- +## AST + +``` +Module( + ModModule { + range: 0..788, + body: [ + Assign( + StmtAssign { + range: 201..206, + targets: [ + NumberLiteral( + ExprNumberLiteral { + range: 201..202, + value: Int( + 5, + ), + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 205..206, + value: Int( + 3, + ), + }, + ), + }, + ), + AugAssign( + StmtAugAssign { + range: 208..214, + target: NumberLiteral( + ExprNumberLiteral { + range: 208..209, + value: Int( + 5, + ), + }, + ), + op: Add, + value: NumberLiteral( + ExprNumberLiteral { + range: 213..214, + value: Int( + 3, + ), + }, + ), + }, + ), + AnnAssign( + StmtAnnAssign { + range: 216..228, + target: NumberLiteral( + ExprNumberLiteral { + range: 217..218, + value: Int( + 5, + ), + }, + ), + annotation: Name( + ExprName { + range: 221..224, + id: "int", + ctx: Load, + }, + ), + value: Some( + NumberLiteral( + ExprNumberLiteral { + range: 227..228, + value: Int( + 3, + ), + }, + ), + ), + simple: false, + }, + ), + Assign( + StmtAssign { + range: 303..314, + targets: [ + BoolOp( + ExprBoolOp { + range: 303..309, + op: Or, + values: [ + Name( + ExprName { + range: 303..304, + id: "x", + ctx: Load, + }, + ), + Name( + ExprName { + range: 308..309, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 312..314, + value: Int( + 42, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 315..328, + targets: [ + Named( + ExprNamed { + range: 316..322, + target: Name( + ExprName { + range: 316..317, + id: "x", + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 321..322, + value: Int( + 5, + ), + }, + ), + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 326..328, + value: Int( + 42, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 329..339, + targets: [ + BinOp( + ExprBinOp { + range: 329..334, + left: Name( + ExprName { + range: 329..330, + id: "x", + ctx: Load, + }, + ), + op: Add, + right: Name( + ExprName { + range: 333..334, + id: "y", + ctx: Load, + }, + ), + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 337..339, + value: Int( + 42, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 340..347, + targets: [ + UnaryOp( + ExprUnaryOp { + range: 340..342, + op: USub, + operand: Name( + ExprName { + range: 341..342, + id: "x", + ctx: Store, + }, + ), + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 345..347, + value: Int( + 42, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 348..366, + targets: [ + Lambda( + ExprLambda { + range: 349..360, + parameters: Some( + Parameters { + range: 356..357, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 356..357, + parameter: Parameter { + range: 356..357, + name: Identifier { + id: "_", + range: 356..357, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: NumberLiteral( + ExprNumberLiteral { + range: 359..360, + value: Int( + 1, + ), + }, + ), + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 364..366, + value: Int( + 42, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 367..385, + targets: [ + If( + ExprIf { + range: 367..380, + test: Name( + ExprName { + range: 372..373, + id: "b", + ctx: Load, + }, + ), + body: Name( + ExprName { + range: 367..368, + id: "a", + ctx: Load, + }, + ), + orelse: Name( + ExprName { + range: 379..380, + id: "c", + ctx: Load, + }, + ), + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 383..385, + value: Int( + 42, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 386..399, + targets: [ + Dict( + ExprDict { + range: 386..394, + items: [ + DictItem { + key: Some( + StringLiteral( + ExprStringLiteral { + range: 387..390, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 387..390, + value: "a", + flags: StringLiteralFlags { + quote_style: Double, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 392..393, + value: Int( + 5, + ), + }, + ), + }, + ], + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 397..399, + value: Int( + 42, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 400..408, + targets: [ + Set( + ExprSet { + range: 400..403, + elts: [ + Name( + ExprName { + range: 401..402, + id: "a", + ctx: Load, + }, + ), + ], + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 406..408, + value: Int( + 42, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 409..429, + targets: [ + ListComp( + ExprListComp { + range: 409..424, + elt: Name( + ExprName { + range: 410..411, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 412..423, + target: Name( + ExprName { + range: 416..417, + id: "x", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 421..423, + id: "xs", + ctx: Load, + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 427..429, + value: Int( + 42, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 430..450, + targets: [ + SetComp( + ExprSetComp { + range: 430..445, + elt: Name( + ExprName { + range: 431..432, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 433..444, + target: Name( + ExprName { + range: 437..438, + id: "x", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 442..444, + id: "xs", + ctx: Load, + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 448..450, + value: Int( + 42, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 451..478, + targets: [ + DictComp( + ExprDictComp { + range: 451..473, + key: Name( + ExprName { + range: 452..453, + id: "x", + ctx: Load, + }, + ), + value: BinOp( + ExprBinOp { + range: 455..460, + left: Name( + ExprName { + range: 455..456, + id: "x", + ctx: Load, + }, + ), + op: Mult, + right: NumberLiteral( + ExprNumberLiteral { + range: 459..460, + value: Int( + 2, + ), + }, + ), + }, + ), + generators: [ + Comprehension { + range: 461..472, + target: Name( + ExprName { + range: 465..466, + id: "x", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 470..472, + id: "xs", + ctx: Load, + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 476..478, + value: Int( + 42, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 479..499, + targets: [ + Generator( + ExprGenerator { + range: 479..494, + elt: Name( + ExprName { + range: 480..481, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 482..493, + target: Name( + ExprName { + range: 486..487, + id: "x", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 491..493, + id: "xs", + ctx: Load, + }, + ), + ifs: [], + is_async: false, + }, + ], + parenthesized: true, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 497..499, + value: Int( + 42, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 500..512, + targets: [ + Await( + ExprAwait { + range: 500..507, + value: Name( + ExprName { + range: 506..507, + id: "x", + ctx: Load, + }, + ), + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 510..512, + value: Int( + 42, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 513..527, + targets: [ + Yield( + ExprYield { + range: 514..521, + value: Some( + Name( + ExprName { + range: 520..521, + id: "x", + ctx: Load, + }, + ), + ), + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 525..527, + value: Int( + 42, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 528..548, + targets: [ + YieldFrom( + ExprYieldFrom { + range: 529..542, + value: Name( + ExprName { + range: 540..542, + id: "xs", + ctx: Load, + }, + ), + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 546..548, + value: Int( + 42, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 549..563, + targets: [ + Compare( + ExprCompare { + range: 549..558, + left: Name( + ExprName { + range: 549..550, + id: "a", + ctx: Load, + }, + ), + ops: [ + Lt, + Lt, + ], + comparators: [ + Name( + ExprName { + range: 553..554, + id: "b", + ctx: Load, + }, + ), + Name( + ExprName { + range: 557..558, + id: "c", + ctx: Load, + }, + ), + ], + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 561..563, + value: Int( + 42, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 564..574, + targets: [ + Call( + ExprCall { + range: 564..569, + func: Name( + ExprName { + range: 564..567, + id: "foo", + ctx: Load, + }, + ), + arguments: Arguments { + range: 567..569, + args: [], + keywords: [], + }, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 572..574, + value: Int( + 42, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 576..590, + targets: [ + FString( + ExprFString { + range: 576..585, + value: FStringValue { + inner: Single( + FString( + FString { + range: 576..585, + elements: [ + Expression( + FStringExpressionElement { + range: 578..584, + expression: Name( + ExprName { + range: 579..583, + id: "quux", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 588..590, + value: Int( + 42, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 591..614, + targets: [ + FString( + ExprFString { + range: 591..609, + value: FStringValue { + inner: Single( + FString( + FString { + range: 591..609, + elements: [ + Expression( + FStringExpressionElement { + range: 593..598, + expression: Name( + ExprName { + range: 594..597, + id: "foo", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + Literal( + FStringLiteralElement { + range: 598..603, + value: " and ", + }, + ), + Expression( + FStringExpressionElement { + range: 603..608, + expression: Name( + ExprName { + range: 604..607, + id: "bar", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 612..614, + value: Int( + 42, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 616..626, + targets: [ + StringLiteral( + ExprStringLiteral { + range: 616..621, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 616..621, + value: "foo", + flags: StringLiteralFlags { + quote_style: Double, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 624..626, + value: Int( + 42, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 627..638, + targets: [ + BytesLiteral( + ExprBytesLiteral { + range: 627..633, + value: BytesLiteralValue { + inner: Single( + BytesLiteral { + range: 627..633, + value: [ + 102, + 111, + 111, + ], + flags: BytesLiteralFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 636..638, + value: Int( + 42, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 639..647, + targets: [ + NumberLiteral( + ExprNumberLiteral { + range: 639..642, + value: Int( + 123, + ), + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 645..647, + value: Int( + 42, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 648..657, + targets: [ + BooleanLiteral( + ExprBooleanLiteral { + range: 648..652, + value: true, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 655..657, + value: Int( + 42, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 658..667, + targets: [ + NoneLiteral( + ExprNoneLiteral { + range: 658..662, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 665..667, + value: Int( + 42, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 668..676, + targets: [ + EllipsisLiteral( + ExprEllipsisLiteral { + range: 668..671, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 674..676, + value: Int( + 42, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 677..688, + targets: [ + Starred( + ExprStarred { + range: 677..683, + value: Call( + ExprCall { + range: 678..683, + func: Name( + ExprName { + range: 678..681, + id: "foo", + ctx: Load, + }, + ), + arguments: Arguments { + range: 681..683, + args: [], + keywords: [], + }, + }, + ), + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 686..688, + value: Int( + 42, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 689..717, + targets: [ + List( + ExprList { + range: 689..702, + elts: [ + Name( + ExprName { + range: 690..691, + id: "x", + ctx: Store, + }, + ), + Call( + ExprCall { + range: 693..698, + func: Name( + ExprName { + range: 693..696, + id: "foo", + ctx: Load, + }, + ), + arguments: Arguments { + range: 696..698, + args: [], + keywords: [], + }, + }, + ), + Name( + ExprName { + range: 700..701, + id: "y", + ctx: Store, + }, + ), + ], + ctx: Store, + }, + ), + ], + value: List( + ExprList { + range: 705..717, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 706..708, + value: Int( + 42, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 710..712, + value: Int( + 42, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 714..716, + value: Int( + 42, + ), + }, + ), + ], + ctx: Load, + }, + ), + }, + ), + Assign( + StmtAssign { + range: 718..758, + targets: [ + List( + ExprList { + range: 718..737, + elts: [ + List( + ExprList { + range: 719..725, + elts: [ + Name( + ExprName { + range: 720..721, + id: "a", + ctx: Store, + }, + ), + Name( + ExprName { + range: 723..724, + id: "b", + ctx: Store, + }, + ), + ], + ctx: Store, + }, + ), + List( + ExprList { + range: 727..733, + elts: [ + List( + ExprList { + range: 728..732, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 729..731, + value: Int( + 42, + ), + }, + ), + ], + ctx: Store, + }, + ), + ], + ctx: Store, + }, + ), + Name( + ExprName { + range: 735..736, + id: "d", + ctx: Store, + }, + ), + ], + ctx: Store, + }, + ), + ], + value: List( + ExprList { + range: 740..758, + elts: [ + List( + ExprList { + range: 741..747, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 742..743, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 745..746, + value: Int( + 2, + ), + }, + ), + ], + ctx: Load, + }, + ), + List( + ExprList { + range: 749..754, + elts: [ + List( + ExprList { + range: 750..753, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 751..752, + value: Int( + 3, + ), + }, + ), + ], + ctx: Load, + }, + ), + ], + ctx: Load, + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 756..757, + value: Int( + 4, + ), + }, + ), + ], + ctx: Load, + }, + ), + }, + ), + Assign( + StmtAssign { + range: 759..787, + targets: [ + Tuple( + ExprTuple { + range: 759..772, + elts: [ + Name( + ExprName { + range: 760..761, + id: "x", + ctx: Store, + }, + ), + Call( + ExprCall { + range: 763..768, + func: Name( + ExprName { + range: 763..766, + id: "foo", + ctx: Load, + }, + ), + arguments: Arguments { + range: 766..768, + args: [], + keywords: [], + }, + }, + ), + Name( + ExprName { + range: 770..771, + id: "y", + ctx: Store, + }, + ), + ], + ctx: Store, + parenthesized: true, + }, + ), + ], + value: Tuple( + ExprTuple { + range: 775..787, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 776..778, + value: Int( + 42, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 780..782, + value: Int( + 42, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 784..786, + value: Int( + 42, + ), + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +3 | # rejected by the parser. e.g., `5 = 3`, `5 += 3`, `(5): int = 3`. +4 | +5 | 5 = 3 + | ^ Syntax Error: Invalid assignment target +6 | +7 | 5 += 3 + | + + + | +5 | 5 = 3 +6 | +7 | 5 += 3 + | ^ Syntax Error: Invalid augmented assignment target +8 | +9 | (5): int = 3 + | + + + | + 7 | 5 += 3 + 8 | + 9 | (5): int = 3 + | ^ Syntax Error: Invalid annotated assignment target +10 | +11 | # Now we exhaustively test all possible cases where assignment can fail. + | + + + | +11 | # Now we exhaustively test all possible cases where assignment can fail. +12 | x or y = 42 + | ^^^^^^ Syntax Error: Invalid assignment target +13 | (x := 5) = 42 +14 | x + y = 42 + | + + + | +11 | # Now we exhaustively test all possible cases where assignment can fail. +12 | x or y = 42 +13 | (x := 5) = 42 + | ^^^^^^ Syntax Error: Invalid assignment target +14 | x + y = 42 +15 | -x = 42 + | + + + | +12 | x or y = 42 +13 | (x := 5) = 42 +14 | x + y = 42 + | ^^^^^ Syntax Error: Invalid assignment target +15 | -x = 42 +16 | (lambda _: 1) = 42 + | + + + | +13 | (x := 5) = 42 +14 | x + y = 42 +15 | -x = 42 + | ^^ Syntax Error: Invalid assignment target +16 | (lambda _: 1) = 42 +17 | a if b else c = 42 + | + + + | +14 | x + y = 42 +15 | -x = 42 +16 | (lambda _: 1) = 42 + | ^^^^^^^^^^^ Syntax Error: Invalid assignment target +17 | a if b else c = 42 +18 | {"a": 5} = 42 + | + + + | +15 | -x = 42 +16 | (lambda _: 1) = 42 +17 | a if b else c = 42 + | ^^^^^^^^^^^^^ Syntax Error: Invalid assignment target +18 | {"a": 5} = 42 +19 | {a} = 42 + | + + + | +16 | (lambda _: 1) = 42 +17 | a if b else c = 42 +18 | {"a": 5} = 42 + | ^^^^^^^^ Syntax Error: Invalid assignment target +19 | {a} = 42 +20 | [x for x in xs] = 42 + | + + + | +17 | a if b else c = 42 +18 | {"a": 5} = 42 +19 | {a} = 42 + | ^^^ Syntax Error: Invalid assignment target +20 | [x for x in xs] = 42 +21 | {x for x in xs} = 42 + | + + + | +18 | {"a": 5} = 42 +19 | {a} = 42 +20 | [x for x in xs] = 42 + | ^^^^^^^^^^^^^^^ Syntax Error: Invalid assignment target +21 | {x for x in xs} = 42 +22 | {x: x * 2 for x in xs} = 42 + | + + + | +19 | {a} = 42 +20 | [x for x in xs] = 42 +21 | {x for x in xs} = 42 + | ^^^^^^^^^^^^^^^ Syntax Error: Invalid assignment target +22 | {x: x * 2 for x in xs} = 42 +23 | (x for x in xs) = 42 + | + + + | +20 | [x for x in xs] = 42 +21 | {x for x in xs} = 42 +22 | {x: x * 2 for x in xs} = 42 + | ^^^^^^^^^^^^^^^^^^^^^^ Syntax Error: Invalid assignment target +23 | (x for x in xs) = 42 +24 | await x = 42 + | + + + | +21 | {x for x in xs} = 42 +22 | {x: x * 2 for x in xs} = 42 +23 | (x for x in xs) = 42 + | ^^^^^^^^^^^^^^^ Syntax Error: Invalid assignment target +24 | await x = 42 +25 | (yield x) = 42 + | + + + | +22 | {x: x * 2 for x in xs} = 42 +23 | (x for x in xs) = 42 +24 | await x = 42 + | ^^^^^^^ Syntax Error: Invalid assignment target +25 | (yield x) = 42 +26 | (yield from xs) = 42 + | + + + | +23 | (x for x in xs) = 42 +24 | await x = 42 +25 | (yield x) = 42 + | ^^^^^^^ Syntax Error: Invalid assignment target +26 | (yield from xs) = 42 +27 | a < b < c = 42 + | + + + | +24 | await x = 42 +25 | (yield x) = 42 +26 | (yield from xs) = 42 + | ^^^^^^^^^^^^^ Syntax Error: Invalid assignment target +27 | a < b < c = 42 +28 | foo() = 42 + | + + + | +25 | (yield x) = 42 +26 | (yield from xs) = 42 +27 | a < b < c = 42 + | ^^^^^^^^^ Syntax Error: Invalid assignment target +28 | foo() = 42 + | + + + | +26 | (yield from xs) = 42 +27 | a < b < c = 42 +28 | foo() = 42 + | ^^^^^ Syntax Error: Invalid assignment target +29 | +30 | f"{quux}" = 42 + | + + + | +28 | foo() = 42 +29 | +30 | f"{quux}" = 42 + | ^^^^^^^^^ Syntax Error: Invalid assignment target +31 | f"{foo} and {bar}" = 42 + | + + + | +30 | f"{quux}" = 42 +31 | f"{foo} and {bar}" = 42 + | ^^^^^^^^^^^^^^^^^^ Syntax Error: Invalid assignment target +32 | +33 | "foo" = 42 + | + + + | +31 | f"{foo} and {bar}" = 42 +32 | +33 | "foo" = 42 + | ^^^^^ Syntax Error: Invalid assignment target +34 | b"foo" = 42 +35 | 123 = 42 + | + + + | +33 | "foo" = 42 +34 | b"foo" = 42 + | ^^^^^^ Syntax Error: Invalid assignment target +35 | 123 = 42 +36 | True = 42 + | + + + | +33 | "foo" = 42 +34 | b"foo" = 42 +35 | 123 = 42 + | ^^^ Syntax Error: Invalid assignment target +36 | True = 42 +37 | None = 42 + | + + + | +34 | b"foo" = 42 +35 | 123 = 42 +36 | True = 42 + | ^^^^ Syntax Error: Invalid assignment target +37 | None = 42 +38 | ... = 42 + | + + + | +35 | 123 = 42 +36 | True = 42 +37 | None = 42 + | ^^^^ Syntax Error: Invalid assignment target +38 | ... = 42 +39 | *foo() = 42 + | + + + | +36 | True = 42 +37 | None = 42 +38 | ... = 42 + | ^^^ Syntax Error: Invalid assignment target +39 | *foo() = 42 +40 | [x, foo(), y] = [42, 42, 42] + | + + + | +37 | None = 42 +38 | ... = 42 +39 | *foo() = 42 + | ^^^^^ Syntax Error: Invalid assignment target +40 | [x, foo(), y] = [42, 42, 42] +41 | [[a, b], [[42]], d] = [[1, 2], [[3]], 4] + | + + + | +38 | ... = 42 +39 | *foo() = 42 +40 | [x, foo(), y] = [42, 42, 42] + | ^^^^^ Syntax Error: Invalid assignment target +41 | [[a, b], [[42]], d] = [[1, 2], [[3]], 4] +42 | (x, foo(), y) = (42, 42, 42) + | + + + | +39 | *foo() = 42 +40 | [x, foo(), y] = [42, 42, 42] +41 | [[a, b], [[42]], d] = [[1, 2], [[3]], 4] + | ^^ Syntax Error: Invalid assignment target +42 | (x, foo(), y) = (42, 42, 42) + | + + + | +40 | [x, foo(), y] = [42, 42, 42] +41 | [[a, b], [[42]], d] = [[1, 2], [[3]], 4] +42 | (x, foo(), y) = (42, 42, 42) + | ^^^^^ Syntax Error: Invalid assignment target + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__invalid_augmented_assignment_target.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__invalid_augmented_assignment_target.py.snap.new new file mode 100644 index 0000000000..34323c9f7a --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__invalid_augmented_assignment_target.py.snap.new @@ -0,0 +1,1558 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/statements/invalid_augmented_assignment_target.py +--- +## AST + +``` +Module( + ModModule { + range: 0..611, + body: [ + AugAssign( + StmtAugAssign { + range: 97..109, + target: BoolOp( + ExprBoolOp { + range: 97..103, + op: Or, + values: [ + Name( + ExprName { + range: 97..98, + id: "x", + ctx: Load, + }, + ), + Name( + ExprName { + range: 102..103, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + op: Add, + value: NumberLiteral( + ExprNumberLiteral { + range: 107..109, + value: Int( + 42, + ), + }, + ), + }, + ), + AugAssign( + StmtAugAssign { + range: 110..124, + target: Named( + ExprNamed { + range: 111..117, + target: Name( + ExprName { + range: 111..112, + id: "x", + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 116..117, + value: Int( + 5, + ), + }, + ), + }, + ), + op: Add, + value: NumberLiteral( + ExprNumberLiteral { + range: 122..124, + value: Int( + 42, + ), + }, + ), + }, + ), + AugAssign( + StmtAugAssign { + range: 125..136, + target: BinOp( + ExprBinOp { + range: 125..130, + left: Name( + ExprName { + range: 125..126, + id: "x", + ctx: Load, + }, + ), + op: Add, + right: Name( + ExprName { + range: 129..130, + id: "y", + ctx: Load, + }, + ), + }, + ), + op: Add, + value: NumberLiteral( + ExprNumberLiteral { + range: 134..136, + value: Int( + 42, + ), + }, + ), + }, + ), + AugAssign( + StmtAugAssign { + range: 137..145, + target: UnaryOp( + ExprUnaryOp { + range: 137..139, + op: USub, + operand: Name( + ExprName { + range: 138..139, + id: "x", + ctx: Store, + }, + ), + }, + ), + op: Add, + value: NumberLiteral( + ExprNumberLiteral { + range: 143..145, + value: Int( + 42, + ), + }, + ), + }, + ), + AugAssign( + StmtAugAssign { + range: 146..165, + target: Lambda( + ExprLambda { + range: 147..158, + parameters: Some( + Parameters { + range: 154..155, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 154..155, + parameter: Parameter { + range: 154..155, + name: Identifier { + id: "_", + range: 154..155, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: NumberLiteral( + ExprNumberLiteral { + range: 157..158, + value: Int( + 1, + ), + }, + ), + }, + ), + op: Add, + value: NumberLiteral( + ExprNumberLiteral { + range: 163..165, + value: Int( + 42, + ), + }, + ), + }, + ), + AugAssign( + StmtAugAssign { + range: 166..185, + target: If( + ExprIf { + range: 166..179, + test: Name( + ExprName { + range: 171..172, + id: "b", + ctx: Load, + }, + ), + body: Name( + ExprName { + range: 166..167, + id: "a", + ctx: Load, + }, + ), + orelse: Name( + ExprName { + range: 178..179, + id: "c", + ctx: Load, + }, + ), + }, + ), + op: Add, + value: NumberLiteral( + ExprNumberLiteral { + range: 183..185, + value: Int( + 42, + ), + }, + ), + }, + ), + AugAssign( + StmtAugAssign { + range: 186..200, + target: Dict( + ExprDict { + range: 186..194, + items: [ + DictItem { + key: Some( + StringLiteral( + ExprStringLiteral { + range: 187..190, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 187..190, + value: "a", + flags: StringLiteralFlags { + quote_style: Double, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 192..193, + value: Int( + 5, + ), + }, + ), + }, + ], + }, + ), + op: Add, + value: NumberLiteral( + ExprNumberLiteral { + range: 198..200, + value: Int( + 42, + ), + }, + ), + }, + ), + AugAssign( + StmtAugAssign { + range: 201..210, + target: Set( + ExprSet { + range: 201..204, + elts: [ + Name( + ExprName { + range: 202..203, + id: "a", + ctx: Load, + }, + ), + ], + }, + ), + op: Add, + value: NumberLiteral( + ExprNumberLiteral { + range: 208..210, + value: Int( + 42, + ), + }, + ), + }, + ), + AugAssign( + StmtAugAssign { + range: 211..232, + target: ListComp( + ExprListComp { + range: 211..226, + elt: Name( + ExprName { + range: 212..213, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 214..225, + target: Name( + ExprName { + range: 218..219, + id: "x", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 223..225, + id: "xs", + ctx: Load, + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + op: Add, + value: NumberLiteral( + ExprNumberLiteral { + range: 230..232, + value: Int( + 42, + ), + }, + ), + }, + ), + AugAssign( + StmtAugAssign { + range: 233..254, + target: SetComp( + ExprSetComp { + range: 233..248, + elt: Name( + ExprName { + range: 234..235, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 236..247, + target: Name( + ExprName { + range: 240..241, + id: "x", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 245..247, + id: "xs", + ctx: Load, + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + op: Add, + value: NumberLiteral( + ExprNumberLiteral { + range: 252..254, + value: Int( + 42, + ), + }, + ), + }, + ), + AugAssign( + StmtAugAssign { + range: 255..283, + target: DictComp( + ExprDictComp { + range: 255..277, + key: Name( + ExprName { + range: 256..257, + id: "x", + ctx: Load, + }, + ), + value: BinOp( + ExprBinOp { + range: 259..264, + left: Name( + ExprName { + range: 259..260, + id: "x", + ctx: Load, + }, + ), + op: Mult, + right: NumberLiteral( + ExprNumberLiteral { + range: 263..264, + value: Int( + 2, + ), + }, + ), + }, + ), + generators: [ + Comprehension { + range: 265..276, + target: Name( + ExprName { + range: 269..270, + id: "x", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 274..276, + id: "xs", + ctx: Load, + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + op: Add, + value: NumberLiteral( + ExprNumberLiteral { + range: 281..283, + value: Int( + 42, + ), + }, + ), + }, + ), + AugAssign( + StmtAugAssign { + range: 284..305, + target: Generator( + ExprGenerator { + range: 284..299, + elt: Name( + ExprName { + range: 285..286, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 287..298, + target: Name( + ExprName { + range: 291..292, + id: "x", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 296..298, + id: "xs", + ctx: Load, + }, + ), + ifs: [], + is_async: false, + }, + ], + parenthesized: true, + }, + ), + op: Add, + value: NumberLiteral( + ExprNumberLiteral { + range: 303..305, + value: Int( + 42, + ), + }, + ), + }, + ), + AugAssign( + StmtAugAssign { + range: 306..319, + target: Await( + ExprAwait { + range: 306..313, + value: Name( + ExprName { + range: 312..313, + id: "x", + ctx: Load, + }, + ), + }, + ), + op: Add, + value: NumberLiteral( + ExprNumberLiteral { + range: 317..319, + value: Int( + 42, + ), + }, + ), + }, + ), + AugAssign( + StmtAugAssign { + range: 320..335, + target: Yield( + ExprYield { + range: 321..328, + value: Some( + Name( + ExprName { + range: 327..328, + id: "x", + ctx: Load, + }, + ), + ), + }, + ), + op: Add, + value: NumberLiteral( + ExprNumberLiteral { + range: 333..335, + value: Int( + 42, + ), + }, + ), + }, + ), + AugAssign( + StmtAugAssign { + range: 336..357, + target: YieldFrom( + ExprYieldFrom { + range: 337..350, + value: Name( + ExprName { + range: 348..350, + id: "xs", + ctx: Load, + }, + ), + }, + ), + op: Add, + value: NumberLiteral( + ExprNumberLiteral { + range: 355..357, + value: Int( + 42, + ), + }, + ), + }, + ), + AugAssign( + StmtAugAssign { + range: 358..373, + target: Compare( + ExprCompare { + range: 358..367, + left: Name( + ExprName { + range: 358..359, + id: "a", + ctx: Load, + }, + ), + ops: [ + Lt, + Lt, + ], + comparators: [ + Name( + ExprName { + range: 362..363, + id: "b", + ctx: Load, + }, + ), + Name( + ExprName { + range: 366..367, + id: "c", + ctx: Load, + }, + ), + ], + }, + ), + op: Add, + value: NumberLiteral( + ExprNumberLiteral { + range: 371..373, + value: Int( + 42, + ), + }, + ), + }, + ), + AugAssign( + StmtAugAssign { + range: 374..385, + target: Call( + ExprCall { + range: 374..379, + func: Name( + ExprName { + range: 374..377, + id: "foo", + ctx: Load, + }, + ), + arguments: Arguments { + range: 377..379, + args: [], + keywords: [], + }, + }, + ), + op: Add, + value: NumberLiteral( + ExprNumberLiteral { + range: 383..385, + value: Int( + 42, + ), + }, + ), + }, + ), + AugAssign( + StmtAugAssign { + range: 387..402, + target: FString( + ExprFString { + range: 387..396, + value: FStringValue { + inner: Single( + FString( + FString { + range: 387..396, + elements: [ + Expression( + FStringExpressionElement { + range: 389..395, + expression: Name( + ExprName { + range: 390..394, + id: "quux", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + op: Add, + value: NumberLiteral( + ExprNumberLiteral { + range: 400..402, + value: Int( + 42, + ), + }, + ), + }, + ), + AugAssign( + StmtAugAssign { + range: 403..427, + target: FString( + ExprFString { + range: 403..421, + value: FStringValue { + inner: Single( + FString( + FString { + range: 403..421, + elements: [ + Expression( + FStringExpressionElement { + range: 405..410, + expression: Name( + ExprName { + range: 406..409, + id: "foo", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + Literal( + FStringLiteralElement { + range: 410..415, + value: " and ", + }, + ), + Expression( + FStringExpressionElement { + range: 415..420, + expression: Name( + ExprName { + range: 416..419, + id: "bar", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + op: Add, + value: NumberLiteral( + ExprNumberLiteral { + range: 425..427, + value: Int( + 42, + ), + }, + ), + }, + ), + AugAssign( + StmtAugAssign { + range: 429..440, + target: StringLiteral( + ExprStringLiteral { + range: 429..434, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 429..434, + value: "foo", + flags: StringLiteralFlags { + quote_style: Double, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + op: Add, + value: NumberLiteral( + ExprNumberLiteral { + range: 438..440, + value: Int( + 42, + ), + }, + ), + }, + ), + AugAssign( + StmtAugAssign { + range: 441..453, + target: BytesLiteral( + ExprBytesLiteral { + range: 441..447, + value: BytesLiteralValue { + inner: Single( + BytesLiteral { + range: 441..447, + value: [ + 102, + 111, + 111, + ], + flags: BytesLiteralFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + op: Add, + value: NumberLiteral( + ExprNumberLiteral { + range: 451..453, + value: Int( + 42, + ), + }, + ), + }, + ), + AugAssign( + StmtAugAssign { + range: 454..463, + target: NumberLiteral( + ExprNumberLiteral { + range: 454..457, + value: Int( + 123, + ), + }, + ), + op: Add, + value: NumberLiteral( + ExprNumberLiteral { + range: 461..463, + value: Int( + 42, + ), + }, + ), + }, + ), + AugAssign( + StmtAugAssign { + range: 464..474, + target: BooleanLiteral( + ExprBooleanLiteral { + range: 464..468, + value: true, + }, + ), + op: Add, + value: NumberLiteral( + ExprNumberLiteral { + range: 472..474, + value: Int( + 42, + ), + }, + ), + }, + ), + AugAssign( + StmtAugAssign { + range: 475..485, + target: NoneLiteral( + ExprNoneLiteral { + range: 475..479, + }, + ), + op: Add, + value: NumberLiteral( + ExprNumberLiteral { + range: 483..485, + value: Int( + 42, + ), + }, + ), + }, + ), + AugAssign( + StmtAugAssign { + range: 486..495, + target: EllipsisLiteral( + ExprEllipsisLiteral { + range: 486..489, + }, + ), + op: Add, + value: NumberLiteral( + ExprNumberLiteral { + range: 493..495, + value: Int( + 42, + ), + }, + ), + }, + ), + AugAssign( + StmtAugAssign { + range: 496..508, + target: Starred( + ExprStarred { + range: 496..502, + value: Call( + ExprCall { + range: 497..502, + func: Name( + ExprName { + range: 497..500, + id: "foo", + ctx: Load, + }, + ), + arguments: Arguments { + range: 500..502, + args: [], + keywords: [], + }, + }, + ), + ctx: Store, + }, + ), + op: Add, + value: NumberLiteral( + ExprNumberLiteral { + range: 506..508, + value: Int( + 42, + ), + }, + ), + }, + ), + AugAssign( + StmtAugAssign { + range: 509..538, + target: List( + ExprList { + range: 509..522, + elts: [ + Name( + ExprName { + range: 510..511, + id: "x", + ctx: Store, + }, + ), + Call( + ExprCall { + range: 513..518, + func: Name( + ExprName { + range: 513..516, + id: "foo", + ctx: Load, + }, + ), + arguments: Arguments { + range: 516..518, + args: [], + keywords: [], + }, + }, + ), + Name( + ExprName { + range: 520..521, + id: "y", + ctx: Store, + }, + ), + ], + ctx: Store, + }, + ), + op: Add, + value: List( + ExprList { + range: 526..538, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 527..529, + value: Int( + 42, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 531..533, + value: Int( + 42, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 535..537, + value: Int( + 42, + ), + }, + ), + ], + ctx: Load, + }, + ), + }, + ), + AugAssign( + StmtAugAssign { + range: 539..580, + target: List( + ExprList { + range: 539..558, + elts: [ + List( + ExprList { + range: 540..546, + elts: [ + Name( + ExprName { + range: 541..542, + id: "a", + ctx: Store, + }, + ), + Name( + ExprName { + range: 544..545, + id: "b", + ctx: Store, + }, + ), + ], + ctx: Store, + }, + ), + List( + ExprList { + range: 548..554, + elts: [ + List( + ExprList { + range: 549..553, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 550..552, + value: Int( + 42, + ), + }, + ), + ], + ctx: Store, + }, + ), + ], + ctx: Store, + }, + ), + Name( + ExprName { + range: 556..557, + id: "d", + ctx: Store, + }, + ), + ], + ctx: Store, + }, + ), + op: Add, + value: List( + ExprList { + range: 562..580, + elts: [ + List( + ExprList { + range: 563..569, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 564..565, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 567..568, + value: Int( + 2, + ), + }, + ), + ], + ctx: Load, + }, + ), + List( + ExprList { + range: 571..576, + elts: [ + List( + ExprList { + range: 572..575, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 573..574, + value: Int( + 3, + ), + }, + ), + ], + ctx: Load, + }, + ), + ], + ctx: Load, + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 578..579, + value: Int( + 4, + ), + }, + ), + ], + ctx: Load, + }, + ), + }, + ), + AugAssign( + StmtAugAssign { + range: 581..610, + target: Tuple( + ExprTuple { + range: 581..594, + elts: [ + Name( + ExprName { + range: 582..583, + id: "x", + ctx: Store, + }, + ), + Call( + ExprCall { + range: 585..590, + func: Name( + ExprName { + range: 585..588, + id: "foo", + ctx: Load, + }, + ), + arguments: Arguments { + range: 588..590, + args: [], + keywords: [], + }, + }, + ), + Name( + ExprName { + range: 592..593, + id: "y", + ctx: Store, + }, + ), + ], + ctx: Store, + parenthesized: true, + }, + ), + op: Add, + value: Tuple( + ExprTuple { + range: 598..610, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 599..601, + value: Int( + 42, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 603..605, + value: Int( + 42, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 607..609, + value: Int( + 42, + ), + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +2 | # assignment targets. +3 | +4 | x or y += 42 + | ^^^^^^ Syntax Error: Invalid augmented assignment target +5 | (x := 5) += 42 +6 | x + y += 42 + | + + + | +4 | x or y += 42 +5 | (x := 5) += 42 + | ^^^^^^ Syntax Error: Invalid augmented assignment target +6 | x + y += 42 +7 | -x += 42 + | + + + | +4 | x or y += 42 +5 | (x := 5) += 42 +6 | x + y += 42 + | ^^^^^ Syntax Error: Invalid augmented assignment target +7 | -x += 42 +8 | (lambda _: 1) += 42 + | + + + | +5 | (x := 5) += 42 +6 | x + y += 42 +7 | -x += 42 + | ^^ Syntax Error: Invalid augmented assignment target +8 | (lambda _: 1) += 42 +9 | a if b else c += 42 + | + + + | + 6 | x + y += 42 + 7 | -x += 42 + 8 | (lambda _: 1) += 42 + | ^^^^^^^^^^^ Syntax Error: Invalid augmented assignment target + 9 | a if b else c += 42 +10 | {"a": 5} += 42 + | + + + | + 7 | -x += 42 + 8 | (lambda _: 1) += 42 + 9 | a if b else c += 42 + | ^^^^^^^^^^^^^ Syntax Error: Invalid augmented assignment target +10 | {"a": 5} += 42 +11 | {a} += 42 + | + + + | + 8 | (lambda _: 1) += 42 + 9 | a if b else c += 42 +10 | {"a": 5} += 42 + | ^^^^^^^^ Syntax Error: Invalid augmented assignment target +11 | {a} += 42 +12 | [x for x in xs] += 42 + | + + + | + 9 | a if b else c += 42 +10 | {"a": 5} += 42 +11 | {a} += 42 + | ^^^ Syntax Error: Invalid augmented assignment target +12 | [x for x in xs] += 42 +13 | {x for x in xs} += 42 + | + + + | +10 | {"a": 5} += 42 +11 | {a} += 42 +12 | [x for x in xs] += 42 + | ^^^^^^^^^^^^^^^ Syntax Error: Invalid augmented assignment target +13 | {x for x in xs} += 42 +14 | {x: x * 2 for x in xs} += 42 + | + + + | +11 | {a} += 42 +12 | [x for x in xs] += 42 +13 | {x for x in xs} += 42 + | ^^^^^^^^^^^^^^^ Syntax Error: Invalid augmented assignment target +14 | {x: x * 2 for x in xs} += 42 +15 | (x for x in xs) += 42 + | + + + | +12 | [x for x in xs] += 42 +13 | {x for x in xs} += 42 +14 | {x: x * 2 for x in xs} += 42 + | ^^^^^^^^^^^^^^^^^^^^^^ Syntax Error: Invalid augmented assignment target +15 | (x for x in xs) += 42 +16 | await x += 42 + | + + + | +13 | {x for x in xs} += 42 +14 | {x: x * 2 for x in xs} += 42 +15 | (x for x in xs) += 42 + | ^^^^^^^^^^^^^^^ Syntax Error: Invalid augmented assignment target +16 | await x += 42 +17 | (yield x) += 42 + | + + + | +14 | {x: x * 2 for x in xs} += 42 +15 | (x for x in xs) += 42 +16 | await x += 42 + | ^^^^^^^ Syntax Error: Invalid augmented assignment target +17 | (yield x) += 42 +18 | (yield from xs) += 42 + | + + + | +15 | (x for x in xs) += 42 +16 | await x += 42 +17 | (yield x) += 42 + | ^^^^^^^ Syntax Error: Invalid augmented assignment target +18 | (yield from xs) += 42 +19 | a < b < c += 42 + | + + + | +16 | await x += 42 +17 | (yield x) += 42 +18 | (yield from xs) += 42 + | ^^^^^^^^^^^^^ Syntax Error: Invalid augmented assignment target +19 | a < b < c += 42 +20 | foo() += 42 + | + + + | +17 | (yield x) += 42 +18 | (yield from xs) += 42 +19 | a < b < c += 42 + | ^^^^^^^^^ Syntax Error: Invalid augmented assignment target +20 | foo() += 42 + | + + + | +18 | (yield from xs) += 42 +19 | a < b < c += 42 +20 | foo() += 42 + | ^^^^^ Syntax Error: Invalid augmented assignment target +21 | +22 | f"{quux}" += 42 + | + + + | +20 | foo() += 42 +21 | +22 | f"{quux}" += 42 + | ^^^^^^^^^ Syntax Error: Invalid augmented assignment target +23 | f"{foo} and {bar}" += 42 + | + + + | +22 | f"{quux}" += 42 +23 | f"{foo} and {bar}" += 42 + | ^^^^^^^^^^^^^^^^^^ Syntax Error: Invalid augmented assignment target +24 | +25 | "foo" += 42 + | + + + | +23 | f"{foo} and {bar}" += 42 +24 | +25 | "foo" += 42 + | ^^^^^ Syntax Error: Invalid augmented assignment target +26 | b"foo" += 42 +27 | 123 += 42 + | + + + | +25 | "foo" += 42 +26 | b"foo" += 42 + | ^^^^^^ Syntax Error: Invalid augmented assignment target +27 | 123 += 42 +28 | True += 42 + | + + + | +25 | "foo" += 42 +26 | b"foo" += 42 +27 | 123 += 42 + | ^^^ Syntax Error: Invalid augmented assignment target +28 | True += 42 +29 | None += 42 + | + + + | +26 | b"foo" += 42 +27 | 123 += 42 +28 | True += 42 + | ^^^^ Syntax Error: Invalid augmented assignment target +29 | None += 42 +30 | ... += 42 + | + + + | +27 | 123 += 42 +28 | True += 42 +29 | None += 42 + | ^^^^ Syntax Error: Invalid augmented assignment target +30 | ... += 42 +31 | *foo() += 42 + | + + + | +28 | True += 42 +29 | None += 42 +30 | ... += 42 + | ^^^ Syntax Error: Invalid augmented assignment target +31 | *foo() += 42 +32 | [x, foo(), y] += [42, 42, 42] + | + + + | +29 | None += 42 +30 | ... += 42 +31 | *foo() += 42 + | ^^^^^^ Syntax Error: Invalid augmented assignment target +32 | [x, foo(), y] += [42, 42, 42] +33 | [[a, b], [[42]], d] += [[1, 2], [[3]], 4] + | + + + | +30 | ... += 42 +31 | *foo() += 42 +32 | [x, foo(), y] += [42, 42, 42] + | ^^^^^^^^^^^^^ Syntax Error: Invalid augmented assignment target +33 | [[a, b], [[42]], d] += [[1, 2], [[3]], 4] +34 | (x, foo(), y) += (42, 42, 42) + | + + + | +31 | *foo() += 42 +32 | [x, foo(), y] += [42, 42, 42] +33 | [[a, b], [[42]], d] += [[1, 2], [[3]], 4] + | ^^^^^^^^^^^^^^^^^^^ Syntax Error: Invalid augmented assignment target +34 | (x, foo(), y) += (42, 42, 42) + | + + + | +32 | [x, foo(), y] += [42, 42, 42] +33 | [[a, b], [[42]], d] += [[1, 2], [[3]], 4] +34 | (x, foo(), y) += (42, 42, 42) + | ^^^^^^^^^^^^^ Syntax Error: Invalid augmented assignment target + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__match__as_pattern_0.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__match__as_pattern_0.py.snap.new new file mode 100644 index 0000000000..e1e08a374e --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__match__as_pattern_0.py.snap.new @@ -0,0 +1,93 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/statements/match/as_pattern_0.py +--- +## AST + +``` +Module( + ModModule { + range: 0..198, + body: [ + Match( + StmtMatch { + range: 0..197, + subject: Name( + ExprName { + range: 6..13, + id: "subject", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 127..197, + pattern: MatchClass( + PatternMatchClass { + range: 132..146, + cls: Name( + ExprName { + range: 133..139, + id: "", + ctx: Invalid, + }, + ), + arguments: PatternArguments { + range: 140..146, + patterns: [ + MatchAs( + PatternMatchAs { + range: 141..142, + pattern: None, + name: Some( + Identifier { + id: "a", + range: 141..142, + }, + ), + }, + ), + MatchAs( + PatternMatchAs { + range: 144..145, + pattern: None, + name: Some( + Identifier { + id: "b", + range: 144..145, + }, + ), + }, + ), + ], + keywords: [], + }, + }, + ), + guard: None, + body: [ + Pass( + StmtPass { + range: 193..197, + }, + ), + ], + }, + ], + }, + ), + ], + }, +) +``` +## Errors + + | +3 | # class pattern +4 | # v +5 | case (x as y)(a, b): + | ^^^^^^ Syntax Error: Invalid value for a class pattern +6 | # ^^^^^^ +7 | # as-pattern + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__match__as_pattern_1.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__match__as_pattern_1.py.snap.new new file mode 100644 index 0000000000..2bb3e1c827 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__match__as_pattern_1.py.snap.new @@ -0,0 +1,78 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/statements/match/as_pattern_1.py +--- +## AST + +``` +Module( + ModModule { + range: 0..210, + body: [ + Match( + StmtMatch { + range: 0..209, + subject: Name( + ExprName { + range: 6..13, + id: "subject", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 140..209, + pattern: MatchValue( + PatternMatchValue { + range: 145..158, + value: BinOp( + ExprBinOp { + range: 145..158, + left: Name( + ExprName { + range: 146..152, + id: "", + ctx: Invalid, + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 156..158, + value: Complex { + real: 0.0, + imag: 1.0, + }, + }, + ), + }, + ), + }, + ), + guard: None, + body: [ + Pass( + StmtPass { + range: 205..209, + }, + ), + ], + }, + ], + }, + ), + ], + }, +) +``` +## Errors + + | +3 | # complex literal pattern +4 | # v +5 | case (x as y) + 1j: + | ^^^^^^ Syntax Error: Expected a real number in complex literal pattern +6 | # ^^^^^^ +7 | # as-pattern + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__match__as_pattern_2.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__match__as_pattern_2.py.snap.new new file mode 100644 index 0000000000..f0d953fc59 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__match__as_pattern_2.py.snap.new @@ -0,0 +1,127 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/statements/match/as_pattern_2.py +--- +## AST + +``` +Module( + ModModule { + range: 0..190, + body: [ + Match( + StmtMatch { + range: 0..176, + subject: Name( + ExprName { + range: 6..13, + id: "subject", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 159..176, + pattern: MatchAs( + PatternMatchAs { + range: 164..170, + pattern: Some( + MatchAs( + PatternMatchAs { + range: 164..165, + pattern: None, + name: Some( + Identifier { + id: "x", + range: 164..165, + }, + ), + }, + ), + ), + name: Some( + Identifier { + id: "y", + range: 169..170, + }, + ), + }, + ), + guard: None, + body: [ + AnnAssign( + StmtAnnAssign { + range: 171..176, + target: UnaryOp( + ExprUnaryOp { + range: 171..175, + op: UAdd, + operand: NumberLiteral( + ExprNumberLiteral { + range: 173..175, + value: Complex { + real: 0.0, + imag: 1.0, + }, + }, + ), + }, + ), + annotation: Name( + ExprName { + range: 176..176, + id: "", + ctx: Invalid, + }, + ), + value: None, + simple: false, + }, + ), + ], + }, + ], + }, + ), + Pass( + StmtPass { + range: 185..189, + }, + ), + ], + }, +) +``` +## Errors + + | +2 | # This `as` pattern is unparenthesied so the parser never takes the path +3 | # where it might be confused as a complex literal pattern. +4 | case x as y + 1j: + | ^ Syntax Error: Expected ':', found '+' +5 | pass + | + + + | +2 | # This `as` pattern is unparenthesied so the parser never takes the path +3 | # where it might be confused as a complex literal pattern. +4 | case x as y + 1j: + | ^ Syntax Error: Expected an expression +5 | pass + | + + + | +3 | # where it might be confused as a complex literal pattern. +4 | case x as y + 1j: +5 | pass + | ^^^^^^^^ Syntax Error: Expected dedent, found indent + | + + + | +4 | case x as y + 1j: +5 | pass + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__match__as_pattern_3.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__match__as_pattern_3.py.snap.new new file mode 100644 index 0000000000..b4cd1d738a --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__match__as_pattern_3.py.snap.new @@ -0,0 +1,155 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/statements/match/as_pattern_3.py +--- +## AST + +``` +Module( + ModModule { + range: 0..136, + body: [ + Match( + StmtMatch { + range: 0..120, + subject: Name( + ExprName { + range: 6..13, + id: "subject", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 103..120, + pattern: MatchClass( + PatternMatchClass { + range: 108..117, + cls: Dict( + ExprDict { + range: 108..109, + items: [], + }, + ), + arguments: PatternArguments { + range: 109..117, + patterns: [ + MatchAs( + PatternMatchAs { + range: 110..116, + pattern: Some( + MatchAs( + PatternMatchAs { + range: 110..111, + pattern: None, + name: Some( + Identifier { + id: "x", + range: 110..111, + }, + ), + }, + ), + ), + name: Some( + Identifier { + id: "y", + range: 115..116, + }, + ), + }, + ), + ], + keywords: [], + }, + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 119..120, + value: NumberLiteral( + ExprNumberLiteral { + range: 119..120, + value: Int( + 1, + ), + }, + ), + }, + ), + ], + }, + ], + }, + ), + Pass( + StmtPass { + range: 131..135, + }, + ), + ], + }, +) +``` +## Errors + + | +2 | # Not in the mapping start token set, so the list parsing bails +3 | # v +4 | case {(x as y): 1}: + | ^ Syntax Error: Expected '}', found '(' +5 | pass + | + + + | +2 | # Not in the mapping start token set, so the list parsing bails +3 | # v +4 | case {(x as y): 1}: + | ^ Syntax Error: Invalid value for a class pattern +5 | pass + | + + + | +2 | # Not in the mapping start token set, so the list parsing bails +3 | # v +4 | case {(x as y): 1}: + | ^ Syntax Error: Expected newline, found '}' +5 | pass + | + + + | +2 | # Not in the mapping start token set, so the list parsing bails +3 | # v +4 | case {(x as y): 1}: + | ^ Syntax Error: Expected a statement +5 | pass + | + + + | +2 | # Not in the mapping start token set, so the list parsing bails +3 | # v +4 | case {(x as y): 1}: + | ^ Syntax Error: Expected a statement +5 | pass + | + + + | +3 | # v +4 | case {(x as y): 1}: +5 | pass + | ^^^^^^^^ Syntax Error: Unexpected indentation + | + + + | +4 | case {(x as y): 1}: +5 | pass + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__match__as_pattern_4.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__match__as_pattern_4.py.snap.new new file mode 100644 index 0000000000..b753840784 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__match__as_pattern_4.py.snap.new @@ -0,0 +1,117 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/statements/match/as_pattern_4.py +--- +## AST + +``` +Module( + ModModule { + range: 0..187, + body: [ + Match( + StmtMatch { + range: 0..186, + subject: Name( + ExprName { + range: 6..13, + id: "subject", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 156..186, + pattern: MatchMapping( + PatternMatchMapping { + range: 161..172, + keys: [ + Name( + ExprName { + range: 162..163, + id: "x", + ctx: Store, + }, + ), + Name( + ExprName { + range: 167..168, + id: "y", + ctx: Store, + }, + ), + ], + patterns: [ + MatchAs( + PatternMatchAs { + range: 164..166, + pattern: None, + name: Some( + Identifier { + id: "as", + range: 164..166, + }, + ), + }, + ), + MatchValue( + PatternMatchValue { + range: 170..171, + value: NumberLiteral( + ExprNumberLiteral { + range: 170..171, + value: Int( + 1, + ), + }, + ), + }, + ), + ], + rest: None, + }, + ), + guard: None, + body: [ + Pass( + StmtPass { + range: 182..186, + }, + ), + ], + }, + ], + }, + ), + ], + }, +) +``` +## Errors + + | +2 | # This `as` pattern is unparenthesized so the parser never takes the path +3 | # where it might be confused as a mapping key pattern. +4 | case {x as y: 1}: + | ^ Syntax Error: Invalid mapping pattern key +5 | pass + | + + + | +2 | # This `as` pattern is unparenthesized so the parser never takes the path +3 | # where it might be confused as a mapping key pattern. +4 | case {x as y: 1}: + | ^^ Syntax Error: Expected ':', found 'as' +5 | pass + | + + + | +2 | # This `as` pattern is unparenthesized so the parser never takes the path +3 | # where it might be confused as a mapping key pattern. +4 | case {x as y: 1}: + | ^ Syntax Error: Expected ',', found name +5 | pass + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__match__invalid_class_pattern.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__match__invalid_class_pattern.py.snap.new new file mode 100644 index 0000000000..099ecf153a --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__match__invalid_class_pattern.py.snap.new @@ -0,0 +1,384 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/statements/match/invalid_class_pattern.py +--- +## AST + +``` +Module( + ModModule { + range: 0..383, + body: [ + Match( + StmtMatch { + range: 44..285, + subject: Name( + ExprName { + range: 50..57, + id: "subject", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 63..97, + pattern: MatchClass( + PatternMatchClass { + range: 68..83, + cls: Name( + ExprName { + range: 68..71, + id: "Foo", + ctx: Load, + }, + ), + arguments: PatternArguments { + range: 71..83, + patterns: [], + keywords: [ + PatternKeyword { + range: 72..82, + attr: Identifier { + id: "", + range: 80..80, + }, + pattern: MatchValue( + PatternMatchValue { + range: 81..82, + value: NumberLiteral( + ExprNumberLiteral { + range: 81..82, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ], + }, + }, + ), + guard: None, + body: [ + Pass( + StmtPass { + range: 93..97, + }, + ), + ], + }, + MatchCase { + range: 102..135, + pattern: MatchClass( + PatternMatchClass { + range: 107..121, + cls: Name( + ExprName { + range: 107..110, + id: "Foo", + ctx: Load, + }, + ), + arguments: PatternArguments { + range: 110..121, + patterns: [], + keywords: [ + PatternKeyword { + range: 111..120, + attr: Identifier { + id: "", + range: 118..118, + }, + pattern: MatchValue( + PatternMatchValue { + range: 119..120, + value: NumberLiteral( + ExprNumberLiteral { + range: 119..120, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ], + }, + }, + ), + guard: None, + body: [ + Pass( + StmtPass { + range: 131..135, + }, + ), + ], + }, + MatchCase { + range: 140..174, + pattern: MatchClass( + PatternMatchClass { + range: 145..160, + cls: Name( + ExprName { + range: 145..148, + id: "Foo", + ctx: Load, + }, + ), + arguments: PatternArguments { + range: 148..160, + patterns: [], + keywords: [ + PatternKeyword { + range: 149..159, + attr: Identifier { + id: "", + range: 157..157, + }, + pattern: MatchValue( + PatternMatchValue { + range: 158..159, + value: NumberLiteral( + ExprNumberLiteral { + range: 158..159, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ], + }, + }, + ), + guard: None, + body: [ + Pass( + StmtPass { + range: 170..174, + }, + ), + ], + }, + MatchCase { + range: 179..217, + pattern: MatchClass( + PatternMatchClass { + range: 184..203, + cls: Name( + ExprName { + range: 184..187, + id: "Foo", + ctx: Load, + }, + ), + arguments: PatternArguments { + range: 187..203, + patterns: [], + keywords: [ + PatternKeyword { + range: 188..202, + attr: Identifier { + id: "", + range: 200..200, + }, + pattern: MatchValue( + PatternMatchValue { + range: 201..202, + value: NumberLiteral( + ExprNumberLiteral { + range: 201..202, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ], + }, + }, + ), + guard: None, + body: [ + Pass( + StmtPass { + range: 213..217, + }, + ), + ], + }, + MatchCase { + range: 222..249, + pattern: MatchClass( + PatternMatchClass { + range: 227..235, + cls: Name( + ExprName { + range: 227..230, + id: "Foo", + ctx: Load, + }, + ), + arguments: PatternArguments { + range: 230..235, + patterns: [], + keywords: [ + PatternKeyword { + range: 231..234, + attr: Identifier { + id: "", + range: 233..233, + }, + pattern: MatchValue( + PatternMatchValue { + range: 233..234, + value: NumberLiteral( + ExprNumberLiteral { + range: 233..234, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ], + }, + }, + ), + guard: None, + body: [ + Pass( + StmtPass { + range: 245..249, + }, + ), + ], + }, + MatchCase { + range: 254..285, + pattern: MatchClass( + PatternMatchClass { + range: 259..271, + cls: Name( + ExprName { + range: 259..262, + id: "Foo", + ctx: Load, + }, + ), + arguments: PatternArguments { + range: 262..271, + patterns: [], + keywords: [ + PatternKeyword { + range: 263..270, + attr: Identifier { + id: "", + range: 269..269, + }, + pattern: MatchValue( + PatternMatchValue { + range: 269..270, + value: NumberLiteral( + ExprNumberLiteral { + range: 269..270, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ], + }, + }, + ), + guard: None, + body: [ + Pass( + StmtPass { + range: 281..285, + }, + ), + ], + }, + ], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | # Invalid keyword pattern in class argument +2 | match subject: +3 | case Foo(x as y = 1): + | ^^^^^^ Syntax Error: Expected an identifier for the keyword pattern +4 | pass +5 | case Foo(x | y = 1): + | + + + | +3 | case Foo(x as y = 1): +4 | pass +5 | case Foo(x | y = 1): + | ^^^^^ Syntax Error: Expected an identifier for the keyword pattern +6 | pass +7 | case Foo([x, y] = 1): + | + + + | +5 | case Foo(x | y = 1): +6 | pass +7 | case Foo([x, y] = 1): + | ^^^^^^ Syntax Error: Expected an identifier for the keyword pattern +8 | pass +9 | case Foo({False: 0} = 1): + | + + + | + 7 | case Foo([x, y] = 1): + 8 | pass + 9 | case Foo({False: 0} = 1): + | ^^^^^^^^^^ Syntax Error: Expected an identifier for the keyword pattern +10 | pass +11 | case Foo(1=1): + | + + + | + 9 | case Foo({False: 0} = 1): +10 | pass +11 | case Foo(1=1): + | ^ Syntax Error: Expected an identifier for the keyword pattern +12 | pass +13 | case Foo(Bar()=1): + | + + + | +11 | case Foo(1=1): +12 | pass +13 | case Foo(Bar()=1): + | ^^^^^ Syntax Error: Expected an identifier for the keyword pattern +14 | pass +15 | # Positional pattern cannot follow keyword pattern + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__match__invalid_lhs_or_rhs_pattern.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__match__invalid_lhs_or_rhs_pattern.py.snap.new new file mode 100644 index 0000000000..faafb031f3 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__match__invalid_lhs_or_rhs_pattern.py.snap.new @@ -0,0 +1,1101 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/statements/match/invalid_lhs_or_rhs_pattern.py +--- +## AST + +``` +Module( + ModModule { + range: 0..695, + body: [ + Match( + StmtMatch { + range: 0..332, + subject: Name( + ExprName { + range: 6..25, + id: "invalid_lhs_pattern", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 31..60, + pattern: MatchValue( + PatternMatchValue { + range: 36..46, + value: BinOp( + ExprBinOp { + range: 36..46, + left: Call( + ExprCall { + range: 36..41, + func: Name( + ExprName { + range: 36..39, + id: "Foo", + ctx: Load, + }, + ), + arguments: Arguments { + range: 39..41, + args: [], + keywords: [], + }, + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 44..46, + value: Complex { + real: 0.0, + imag: 1.0, + }, + }, + ), + }, + ), + }, + ), + guard: None, + body: [ + Pass( + StmtPass { + range: 56..60, + }, + ), + ], + }, + MatchCase { + range: 65..90, + pattern: MatchValue( + PatternMatchValue { + range: 70..76, + value: BinOp( + ExprBinOp { + range: 70..76, + left: Name( + ExprName { + range: 70..71, + id: "x", + ctx: Store, + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 74..76, + value: Complex { + real: 0.0, + imag: 2.0, + }, + }, + ), + }, + ), + }, + ), + guard: None, + body: [ + Pass( + StmtPass { + range: 86..90, + }, + ), + ], + }, + MatchCase { + range: 95..120, + pattern: MatchValue( + PatternMatchValue { + range: 100..106, + value: BinOp( + ExprBinOp { + range: 100..106, + left: Name( + ExprName { + range: 100..101, + id: "_", + ctx: Store, + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 104..106, + value: Complex { + real: 0.0, + imag: 3.0, + }, + }, + ), + }, + ), + }, + ), + guard: None, + body: [ + Pass( + StmtPass { + range: 116..120, + }, + ), + ], + }, + MatchCase { + range: 125..156, + pattern: MatchValue( + PatternMatchValue { + range: 130..142, + value: BinOp( + ExprBinOp { + range: 130..142, + left: BinOp( + ExprBinOp { + range: 131..136, + left: NumberLiteral( + ExprNumberLiteral { + range: 131..132, + value: Int( + 1, + ), + }, + ), + op: BitOr, + right: NumberLiteral( + ExprNumberLiteral { + range: 135..136, + value: Int( + 2, + ), + }, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 140..142, + value: Complex { + real: 0.0, + imag: 4.0, + }, + }, + ), + }, + ), + }, + ), + guard: None, + body: [ + Pass( + StmtPass { + range: 152..156, + }, + ), + ], + }, + MatchCase { + range: 161..191, + pattern: MatchValue( + PatternMatchValue { + range: 166..177, + value: BinOp( + ExprBinOp { + range: 166..177, + left: List( + ExprList { + range: 166..172, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 167..168, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 170..171, + value: Int( + 2, + ), + }, + ), + ], + ctx: Store, + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 175..177, + value: Complex { + real: 0.0, + imag: 5.0, + }, + }, + ), + }, + ), + }, + ), + guard: None, + body: [ + Pass( + StmtPass { + range: 187..191, + }, + ), + ], + }, + MatchCase { + range: 196..229, + pattern: MatchValue( + PatternMatchValue { + range: 201..215, + value: BinOp( + ExprBinOp { + range: 201..215, + left: Dict( + ExprDict { + range: 201..210, + items: [ + DictItem { + key: Some( + BooleanLiteral( + ExprBooleanLiteral { + range: 202..206, + value: true, + }, + ), + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 208..209, + value: Int( + 1, + ), + }, + ), + }, + ], + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 213..215, + value: Complex { + real: 0.0, + imag: 6.0, + }, + }, + ), + }, + ), + }, + ), + guard: None, + body: [ + Pass( + StmtPass { + range: 225..229, + }, + ), + ], + }, + MatchCase { + range: 234..260, + pattern: MatchValue( + PatternMatchValue { + range: 239..246, + value: BinOp( + ExprBinOp { + range: 239..246, + left: NumberLiteral( + ExprNumberLiteral { + range: 239..241, + value: Complex { + real: 0.0, + imag: 1.0, + }, + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 244..246, + value: Complex { + real: 0.0, + imag: 2.0, + }, + }, + ), + }, + ), + }, + ), + guard: None, + body: [ + Pass( + StmtPass { + range: 256..260, + }, + ), + ], + }, + MatchCase { + range: 265..292, + pattern: MatchValue( + PatternMatchValue { + range: 270..278, + value: BinOp( + ExprBinOp { + range: 270..278, + left: UnaryOp( + ExprUnaryOp { + range: 270..273, + op: USub, + operand: NumberLiteral( + ExprNumberLiteral { + range: 271..273, + value: Complex { + real: 0.0, + imag: 1.0, + }, + }, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 276..278, + value: Complex { + real: 0.0, + imag: 2.0, + }, + }, + ), + }, + ), + }, + ), + guard: None, + body: [ + Pass( + StmtPass { + range: 288..292, + }, + ), + ], + }, + MatchCase { + range: 297..332, + pattern: MatchValue( + PatternMatchValue { + range: 302..318, + value: BinOp( + ExprBinOp { + range: 302..318, + left: Call( + ExprCall { + range: 302..313, + func: Name( + ExprName { + range: 302..305, + id: "Foo", + ctx: Load, + }, + ), + arguments: Arguments { + range: 305..313, + args: [ + Name( + ExprName { + range: 306..312, + id: "", + ctx: Invalid, + }, + ), + ], + keywords: [], + }, + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 316..318, + value: Complex { + real: 0.0, + imag: 1.0, + }, + }, + ), + }, + ), + }, + ), + guard: None, + body: [ + Pass( + StmtPass { + range: 328..332, + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 334..625, + subject: Name( + ExprName { + range: 340..359, + id: "invalid_rhs_pattern", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 365..393, + pattern: MatchValue( + PatternMatchValue { + range: 370..379, + value: BinOp( + ExprBinOp { + range: 370..379, + left: NumberLiteral( + ExprNumberLiteral { + range: 370..371, + value: Int( + 1, + ), + }, + ), + op: Add, + right: Call( + ExprCall { + range: 374..379, + func: Name( + ExprName { + range: 374..377, + id: "Foo", + ctx: Load, + }, + ), + arguments: Arguments { + range: 377..379, + args: [], + keywords: [], + }, + }, + ), + }, + ), + }, + ), + guard: None, + body: [ + Pass( + StmtPass { + range: 389..393, + }, + ), + ], + }, + MatchCase { + range: 398..422, + pattern: MatchValue( + PatternMatchValue { + range: 403..408, + value: BinOp( + ExprBinOp { + range: 403..408, + left: NumberLiteral( + ExprNumberLiteral { + range: 403..404, + value: Int( + 2, + ), + }, + ), + op: Add, + right: Name( + ExprName { + range: 407..408, + id: "x", + ctx: Store, + }, + ), + }, + ), + }, + ), + guard: None, + body: [ + Pass( + StmtPass { + range: 418..422, + }, + ), + ], + }, + MatchCase { + range: 427..451, + pattern: MatchValue( + PatternMatchValue { + range: 432..437, + value: BinOp( + ExprBinOp { + range: 432..437, + left: NumberLiteral( + ExprNumberLiteral { + range: 432..433, + value: Int( + 3, + ), + }, + ), + op: Add, + right: Name( + ExprName { + range: 436..437, + id: "_", + ctx: Store, + }, + ), + }, + ), + }, + ), + guard: None, + body: [ + Pass( + StmtPass { + range: 447..451, + }, + ), + ], + }, + MatchCase { + range: 456..486, + pattern: MatchValue( + PatternMatchValue { + range: 461..472, + value: BinOp( + ExprBinOp { + range: 461..472, + left: NumberLiteral( + ExprNumberLiteral { + range: 461..462, + value: Int( + 4, + ), + }, + ), + op: Add, + right: BinOp( + ExprBinOp { + range: 466..471, + left: NumberLiteral( + ExprNumberLiteral { + range: 466..467, + value: Int( + 1, + ), + }, + ), + op: BitOr, + right: NumberLiteral( + ExprNumberLiteral { + range: 470..471, + value: Int( + 2, + ), + }, + ), + }, + ), + }, + ), + }, + ), + guard: None, + body: [ + Pass( + StmtPass { + range: 482..486, + }, + ), + ], + }, + MatchCase { + range: 491..520, + pattern: MatchValue( + PatternMatchValue { + range: 496..506, + value: BinOp( + ExprBinOp { + range: 496..506, + left: NumberLiteral( + ExprNumberLiteral { + range: 496..497, + value: Int( + 5, + ), + }, + ), + op: Add, + right: List( + ExprList { + range: 500..506, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 501..502, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 504..505, + value: Int( + 2, + ), + }, + ), + ], + ctx: Store, + }, + ), + }, + ), + }, + ), + guard: None, + body: [ + Pass( + StmtPass { + range: 516..520, + }, + ), + ], + }, + MatchCase { + range: 525..557, + pattern: MatchValue( + PatternMatchValue { + range: 530..543, + value: BinOp( + ExprBinOp { + range: 530..543, + left: NumberLiteral( + ExprNumberLiteral { + range: 530..531, + value: Int( + 6, + ), + }, + ), + op: Add, + right: Dict( + ExprDict { + range: 534..543, + items: [ + DictItem { + key: Some( + BooleanLiteral( + ExprBooleanLiteral { + range: 535..539, + value: true, + }, + ), + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 541..542, + value: Int( + 1, + ), + }, + ), + }, + ], + }, + ), + }, + ), + }, + ), + guard: None, + body: [ + Pass( + StmtPass { + range: 553..557, + }, + ), + ], + }, + MatchCase { + range: 562..586, + pattern: MatchValue( + PatternMatchValue { + range: 567..572, + value: BinOp( + ExprBinOp { + range: 567..572, + left: NumberLiteral( + ExprNumberLiteral { + range: 567..568, + value: Int( + 1, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 571..572, + value: Int( + 2, + ), + }, + ), + }, + ), + }, + ), + guard: None, + body: [ + Pass( + StmtPass { + range: 582..586, + }, + ), + ], + }, + MatchCase { + range: 591..625, + pattern: MatchValue( + PatternMatchValue { + range: 596..611, + value: BinOp( + ExprBinOp { + range: 596..611, + left: NumberLiteral( + ExprNumberLiteral { + range: 596..597, + value: Int( + 1, + ), + }, + ), + op: Add, + right: Call( + ExprCall { + range: 600..611, + func: Name( + ExprName { + range: 600..603, + id: "Foo", + ctx: Load, + }, + ), + arguments: Arguments { + range: 603..611, + args: [ + Name( + ExprName { + range: 604..610, + id: "", + ctx: Invalid, + }, + ), + ], + keywords: [], + }, + }, + ), + }, + ), + }, + ), + guard: None, + body: [ + Pass( + StmtPass { + range: 621..625, + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 627..694, + subject: Name( + ExprName { + range: 633..656, + id: "invalid_lhs_rhs_pattern", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 662..694, + pattern: MatchValue( + PatternMatchValue { + range: 667..680, + value: BinOp( + ExprBinOp { + range: 667..680, + left: Call( + ExprCall { + range: 667..672, + func: Name( + ExprName { + range: 667..670, + id: "Foo", + ctx: Load, + }, + ), + arguments: Arguments { + range: 670..672, + args: [], + keywords: [], + }, + }, + ), + op: Add, + right: Call( + ExprCall { + range: 675..680, + func: Name( + ExprName { + range: 675..678, + id: "Bar", + ctx: Load, + }, + ), + arguments: Arguments { + range: 678..680, + args: [], + keywords: [], + }, + }, + ), + }, + ), + }, + ), + guard: None, + body: [ + Pass( + StmtPass { + range: 690..694, + }, + ), + ], + }, + ], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | match invalid_lhs_pattern: +2 | case Foo() + 1j: + | ^^^^^ Syntax Error: Expected a real number in complex literal pattern +3 | pass +4 | case x + 2j: + | + + + | +2 | case Foo() + 1j: +3 | pass +4 | case x + 2j: + | ^ Syntax Error: Expected a real number in complex literal pattern +5 | pass +6 | case _ + 3j: + | + + + | +4 | case x + 2j: +5 | pass +6 | case _ + 3j: + | ^ Syntax Error: Expected a real number in complex literal pattern +7 | pass +8 | case (1 | 2) + 4j: + | + + + | + 6 | case _ + 3j: + 7 | pass + 8 | case (1 | 2) + 4j: + | ^^^^^ Syntax Error: Expected a real number in complex literal pattern + 9 | pass +10 | case [1, 2] + 5j: + | + + + | + 8 | case (1 | 2) + 4j: + 9 | pass +10 | case [1, 2] + 5j: + | ^^^^^^ Syntax Error: Expected a real number in complex literal pattern +11 | pass +12 | case {True: 1} + 6j: + | + + + | +10 | case [1, 2] + 5j: +11 | pass +12 | case {True: 1} + 6j: + | ^^^^^^^^^ Syntax Error: Expected a real number in complex literal pattern +13 | pass +14 | case 1j + 2j: + | + + + | +12 | case {True: 1} + 6j: +13 | pass +14 | case 1j + 2j: + | ^^ Syntax Error: Expected a real number in complex literal pattern +15 | pass +16 | case -1j + 2j: + | + + + | +14 | case 1j + 2j: +15 | pass +16 | case -1j + 2j: + | ^^^ Syntax Error: Expected a real number in complex literal pattern +17 | pass +18 | case Foo(a as b) + 1j: + | + + + | +16 | case -1j + 2j: +17 | pass +18 | case Foo(a as b) + 1j: + | ^^^^^^^^^^^ Syntax Error: Expected a real number in complex literal pattern +19 | pass + | + + + | +21 | match invalid_rhs_pattern: +22 | case 1 + Foo(): + | ^^^^^ Syntax Error: Expected an imaginary number in complex literal pattern +23 | pass +24 | case 2 + x: + | + + + | +22 | case 1 + Foo(): +23 | pass +24 | case 2 + x: + | ^ Syntax Error: Expected an imaginary number in complex literal pattern +25 | pass +26 | case 3 + _: + | + + + | +24 | case 2 + x: +25 | pass +26 | case 3 + _: + | ^ Syntax Error: Expected an imaginary number in complex literal pattern +27 | pass +28 | case 4 + (1 | 2): + | + + + | +26 | case 3 + _: +27 | pass +28 | case 4 + (1 | 2): + | ^^^^^ Syntax Error: Expected an imaginary number in complex literal pattern +29 | pass +30 | case 5 + [1, 2]: + | + + + | +28 | case 4 + (1 | 2): +29 | pass +30 | case 5 + [1, 2]: + | ^^^^^^ Syntax Error: Expected an imaginary number in complex literal pattern +31 | pass +32 | case 6 + {True: 1}: + | + + + | +30 | case 5 + [1, 2]: +31 | pass +32 | case 6 + {True: 1}: + | ^^^^^^^^^ Syntax Error: Expected an imaginary number in complex literal pattern +33 | pass +34 | case 1 + 2: + | + + + | +32 | case 6 + {True: 1}: +33 | pass +34 | case 1 + 2: + | ^ Syntax Error: Expected an imaginary number in complex literal pattern +35 | pass +36 | case 1 + Foo(a as b): + | + + + | +34 | case 1 + 2: +35 | pass +36 | case 1 + Foo(a as b): + | ^^^^^^^^^^^ Syntax Error: Expected an imaginary number in complex literal pattern +37 | pass + | + + + | +39 | match invalid_lhs_rhs_pattern: +40 | case Foo() + Bar(): + | ^^^^^ Syntax Error: Expected a real number in complex literal pattern +41 | pass + | + + + | +39 | match invalid_lhs_rhs_pattern: +40 | case Foo() + Bar(): + | ^^^^^ Syntax Error: Expected an imaginary number in complex literal pattern +41 | pass + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__match__invalid_mapping_pattern.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__match__invalid_mapping_pattern.py.snap.new new file mode 100644 index 0000000000..02bb0c96c1 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__match__invalid_mapping_pattern.py.snap.new @@ -0,0 +1,581 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/statements/match/invalid_mapping_pattern.py +--- +## AST + +``` +Module( + ModModule { + range: 0..509, + body: [ + Match( + StmtMatch { + range: 61..209, + subject: Name( + ExprName { + range: 67..74, + id: "subject", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 80..105, + pattern: MatchMapping( + PatternMatchMapping { + range: 85..91, + keys: [ + Starred( + ExprStarred { + range: 86..90, + value: Name( + ExprName { + range: 87..90, + id: "key", + ctx: Store, + }, + ), + ctx: Store, + }, + ), + ], + patterns: [ + MatchValue( + PatternMatchValue { + range: 90..90, + value: Name( + ExprName { + range: 90..90, + id: "", + ctx: Invalid, + }, + ), + }, + ), + ], + rest: None, + }, + ), + guard: None, + body: [ + Pass( + StmtPass { + range: 101..105, + }, + ), + ], + }, + MatchCase { + range: 110..138, + pattern: MatchMapping( + PatternMatchMapping { + range: 115..124, + keys: [ + Starred( + ExprStarred { + range: 116..120, + value: Name( + ExprName { + range: 117..120, + id: "key", + ctx: Store, + }, + ), + ctx: Store, + }, + ), + ], + patterns: [ + MatchValue( + PatternMatchValue { + range: 122..123, + value: NumberLiteral( + ExprNumberLiteral { + range: 122..123, + value: Int( + 1, + ), + }, + ), + }, + ), + ], + rest: None, + }, + ), + guard: None, + body: [ + Pass( + StmtPass { + range: 134..138, + }, + ), + ], + }, + MatchCase { + range: 143..170, + pattern: MatchMapping( + PatternMatchMapping { + range: 148..156, + keys: [ + Starred( + ExprStarred { + range: 149..153, + value: Name( + ExprName { + range: 150..153, + id: "key", + ctx: Store, + }, + ), + ctx: Store, + }, + ), + ], + patterns: [ + MatchValue( + PatternMatchValue { + range: 154..155, + value: NumberLiteral( + ExprNumberLiteral { + range: 154..155, + value: Int( + 1, + ), + }, + ), + }, + ), + ], + rest: None, + }, + ), + guard: None, + body: [ + Pass( + StmtPass { + range: 166..170, + }, + ), + ], + }, + MatchCase { + range: 175..209, + pattern: MatchMapping( + PatternMatchMapping { + range: 180..195, + keys: [ + Starred( + ExprStarred { + range: 181..185, + value: Name( + ExprName { + range: 182..185, + id: "key", + ctx: Store, + }, + ), + ctx: Store, + }, + ), + NoneLiteral( + ExprNoneLiteral { + range: 187..191, + }, + ), + ], + patterns: [ + MatchValue( + PatternMatchValue { + range: 185..185, + value: Name( + ExprName { + range: 185..185, + id: "", + ctx: Invalid, + }, + ), + }, + ), + MatchValue( + PatternMatchValue { + range: 193..194, + value: NumberLiteral( + ExprNumberLiteral { + range: 193..194, + value: Int( + 1, + ), + }, + ), + }, + ), + ], + rest: None, + }, + ), + guard: None, + body: [ + Pass( + StmtPass { + range: 205..209, + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 305..462, + subject: Name( + ExprName { + range: 311..318, + id: "subject", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 324..360, + pattern: MatchMapping( + PatternMatchMapping { + range: 329..346, + keys: [ + NoneLiteral( + ExprNoneLiteral { + range: 338..342, + }, + ), + ], + patterns: [ + MatchValue( + PatternMatchValue { + range: 344..345, + value: NumberLiteral( + ExprNumberLiteral { + range: 344..345, + value: Int( + 1, + ), + }, + ), + }, + ), + ], + rest: Some( + Identifier { + id: "rest", + range: 332..336, + }, + ), + }, + ), + guard: None, + body: [ + Pass( + StmtPass { + range: 356..360, + }, + ), + ], + }, + MatchCase { + range: 365..411, + pattern: MatchMapping( + PatternMatchMapping { + range: 370..397, + keys: [ + NoneLiteral( + ExprNoneLiteral { + range: 389..393, + }, + ), + ], + patterns: [ + MatchValue( + PatternMatchValue { + range: 395..396, + value: NumberLiteral( + ExprNumberLiteral { + range: 395..396, + value: Int( + 1, + ), + }, + ), + }, + ), + ], + rest: Some( + Identifier { + id: "rest2", + range: 382..387, + }, + ), + }, + ), + guard: None, + body: [ + Pass( + StmtPass { + range: 407..411, + }, + ), + ], + }, + MatchCase { + range: 416..462, + pattern: MatchMapping( + PatternMatchMapping { + range: 421..448, + keys: [ + NoneLiteral( + ExprNoneLiteral { + range: 431..435, + }, + ), + ], + patterns: [ + MatchValue( + PatternMatchValue { + range: 437..438, + value: NumberLiteral( + ExprNumberLiteral { + range: 437..438, + value: Int( + 1, + ), + }, + ), + }, + ), + ], + rest: Some( + Identifier { + id: "rest2", + range: 442..447, + }, + ), + }, + ), + guard: None, + body: [ + Pass( + StmtPass { + range: 458..462, + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 464..509, + subject: Name( + ExprName { + range: 470..477, + id: "subject", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 483..509, + pattern: MatchMapping( + PatternMatchMapping { + range: 488..504, + keys: [ + Call( + ExprCall { + range: 489..500, + func: Name( + ExprName { + range: 489..492, + id: "Foo", + ctx: Load, + }, + ), + arguments: Arguments { + range: 492..500, + args: [ + Name( + ExprName { + range: 493..499, + id: "", + ctx: Invalid, + }, + ), + ], + keywords: [], + }, + }, + ), + ], + patterns: [ + MatchValue( + PatternMatchValue { + range: 502..503, + value: NumberLiteral( + ExprNumberLiteral { + range: 502..503, + value: Int( + 1, + ), + }, + ), + }, + ), + ], + rest: None, + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 506..509, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 506..509, + }, + ), + }, + ), + ], + }, + ], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | # Starred expression is not allowed as a mapping pattern key +2 | match subject: +3 | case {*key}: + | ^^^^ Syntax Error: Star pattern cannot be used here +4 | pass +5 | case {*key: 1}: + | + + + | +1 | # Starred expression is not allowed as a mapping pattern key +2 | match subject: +3 | case {*key}: + | ^ Syntax Error: Expected ':', found '}' +4 | pass +5 | case {*key: 1}: + | + + + | +3 | case {*key}: +4 | pass +5 | case {*key: 1}: + | ^^^^ Syntax Error: Star pattern cannot be used here +6 | pass +7 | case {*key 1}: + | + + + | +5 | case {*key: 1}: +6 | pass +7 | case {*key 1}: + | ^^^^ Syntax Error: Star pattern cannot be used here +8 | pass +9 | case {*key, None: 1}: + | + + + | +5 | case {*key: 1}: +6 | pass +7 | case {*key 1}: + | ^ Syntax Error: Expected ':', found int +8 | pass +9 | case {*key, None: 1}: + | + + + | + 7 | case {*key 1}: + 8 | pass + 9 | case {*key, None: 1}: + | ^^^^ Syntax Error: Star pattern cannot be used here +10 | pass + | + + + | + 7 | case {*key 1}: + 8 | pass + 9 | case {*key, None: 1}: + | ^ Syntax Error: Expected ':', found ',' +10 | pass + | + + + | +13 | # Multiple double star patterns are not allowed +14 | match subject: +15 | case {**rest, None: 1}: + | ^^^^^^^ Syntax Error: Pattern cannot follow a double star pattern +16 | pass +17 | case {**rest1, **rest2, None: 1}: + | + + + | +15 | case {**rest, None: 1}: +16 | pass +17 | case {**rest1, **rest2, None: 1}: + | ^^^^^^^ Syntax Error: Only one double star pattern is allowed +18 | pass +19 | case {**rest1, None: 1, **rest2}: + | + + + | +15 | case {**rest, None: 1}: +16 | pass +17 | case {**rest1, **rest2, None: 1}: + | ^^^^^^^ Syntax Error: Pattern cannot follow a double star pattern +18 | pass +19 | case {**rest1, None: 1, **rest2}: + | + + + | +17 | case {**rest1, **rest2, None: 1}: +18 | pass +19 | case {**rest1, None: 1, **rest2}: + | ^^^^^^^ Syntax Error: Pattern cannot follow a double star pattern +20 | pass + | + + + | +17 | case {**rest1, **rest2, None: 1}: +18 | pass +19 | case {**rest1, None: 1, **rest2}: + | ^^^^^^^ Syntax Error: Only one double star pattern is allowed +20 | pass + | + + + | +22 | match subject: +23 | case {Foo(a as b): 1}: ... + | ^^^^^^^^^^^ Syntax Error: Invalid mapping pattern key + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__match__star_pattern_usage.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__match__star_pattern_usage.py.snap.new new file mode 100644 index 0000000000..ec0973e443 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__match__star_pattern_usage.py.snap.new @@ -0,0 +1,547 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/statements/match/star_pattern_usage.py +--- +## AST + +``` +Module( + ModModule { + range: 0..408, + body: [ + Match( + StmtMatch { + range: 57..407, + subject: Name( + ExprName { + range: 63..70, + id: "subject", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 76..97, + pattern: MatchStar( + PatternMatchStar { + range: 81..83, + name: None, + }, + ), + guard: None, + body: [ + Pass( + StmtPass { + range: 93..97, + }, + ), + ], + }, + MatchCase { + range: 102..128, + pattern: MatchAs( + PatternMatchAs { + range: 107..114, + pattern: Some( + MatchStar( + PatternMatchStar { + range: 107..109, + name: None, + }, + ), + ), + name: Some( + Identifier { + id: "x", + range: 113..114, + }, + ), + }, + ), + guard: None, + body: [ + Pass( + StmtPass { + range: 124..128, + }, + ), + ], + }, + MatchCase { + range: 133..156, + pattern: MatchStar( + PatternMatchStar { + range: 138..142, + name: Some( + Identifier { + id: "foo", + range: 139..142, + }, + ), + }, + ), + guard: None, + body: [ + Pass( + StmtPass { + range: 152..156, + }, + ), + ], + }, + MatchCase { + range: 161..188, + pattern: MatchOr( + PatternMatchOr { + range: 166..174, + patterns: [ + MatchStar( + PatternMatchStar { + range: 166..170, + name: Some( + Identifier { + id: "foo", + range: 167..170, + }, + ), + }, + ), + MatchValue( + PatternMatchValue { + range: 173..174, + value: NumberLiteral( + ExprNumberLiteral { + range: 173..174, + value: Int( + 1, + ), + }, + ), + }, + ), + ], + }, + ), + guard: None, + body: [ + Pass( + StmtPass { + range: 184..188, + }, + ), + ], + }, + MatchCase { + range: 193..220, + pattern: MatchOr( + PatternMatchOr { + range: 198..206, + patterns: [ + MatchValue( + PatternMatchValue { + range: 198..199, + value: NumberLiteral( + ExprNumberLiteral { + range: 198..199, + value: Int( + 1, + ), + }, + ), + }, + ), + MatchStar( + PatternMatchStar { + range: 202..206, + name: Some( + Identifier { + id: "foo", + range: 203..206, + }, + ), + }, + ), + ], + }, + ), + guard: None, + body: [ + Pass( + StmtPass { + range: 216..220, + }, + ), + ], + }, + MatchCase { + range: 225..251, + pattern: MatchClass( + PatternMatchClass { + range: 230..237, + cls: Name( + ExprName { + range: 230..233, + id: "Foo", + ctx: Load, + }, + ), + arguments: PatternArguments { + range: 233..237, + patterns: [ + MatchStar( + PatternMatchStar { + range: 234..236, + name: None, + }, + ), + ], + keywords: [], + }, + }, + ), + guard: None, + body: [ + Pass( + StmtPass { + range: 247..251, + }, + ), + ], + }, + MatchCase { + range: 256..284, + pattern: MatchClass( + PatternMatchClass { + range: 261..270, + cls: Name( + ExprName { + range: 261..264, + id: "Foo", + ctx: Load, + }, + ), + arguments: PatternArguments { + range: 264..270, + patterns: [], + keywords: [ + PatternKeyword { + range: 265..269, + attr: Identifier { + id: "x", + range: 265..266, + }, + pattern: MatchStar( + PatternMatchStar { + range: 267..269, + name: None, + }, + ), + }, + ], + }, + }, + ), + guard: None, + body: [ + Pass( + StmtPass { + range: 280..284, + }, + ), + ], + }, + MatchCase { + range: 289..312, + pattern: MatchMapping( + PatternMatchMapping { + range: 294..298, + keys: [ + Starred( + ExprStarred { + range: 295..297, + value: Name( + ExprName { + range: 296..297, + id: "_", + ctx: Store, + }, + ), + ctx: Store, + }, + ), + ], + patterns: [ + MatchValue( + PatternMatchValue { + range: 297..297, + value: Name( + ExprName { + range: 297..297, + id: "", + ctx: Invalid, + }, + ), + }, + ), + ], + rest: None, + }, + ), + guard: None, + body: [ + Pass( + StmtPass { + range: 308..312, + }, + ), + ], + }, + MatchCase { + range: 317..343, + pattern: MatchMapping( + PatternMatchMapping { + range: 322..329, + keys: [ + Starred( + ExprStarred { + range: 323..325, + value: Name( + ExprName { + range: 324..325, + id: "_", + ctx: Store, + }, + ), + ctx: Store, + }, + ), + ], + patterns: [ + MatchValue( + PatternMatchValue { + range: 327..328, + value: NumberLiteral( + ExprNumberLiteral { + range: 327..328, + value: Int( + 1, + ), + }, + ), + }, + ), + ], + rest: None, + }, + ), + guard: None, + body: [ + Pass( + StmtPass { + range: 339..343, + }, + ), + ], + }, + MatchCase { + range: 348..377, + pattern: MatchMapping( + PatternMatchMapping { + range: 353..363, + keys: [ + NoneLiteral( + ExprNoneLiteral { + range: 354..358, + }, + ), + ], + patterns: [ + MatchStar( + PatternMatchStar { + range: 360..362, + name: None, + }, + ), + ], + rest: None, + }, + ), + guard: None, + body: [ + Pass( + StmtPass { + range: 373..377, + }, + ), + ], + }, + MatchCase { + range: 382..407, + pattern: MatchValue( + PatternMatchValue { + range: 387..393, + value: BinOp( + ExprBinOp { + range: 387..393, + left: NumberLiteral( + ExprNumberLiteral { + range: 387..388, + value: Int( + 1, + ), + }, + ), + op: Add, + right: Starred( + ExprStarred { + range: 391..393, + value: Name( + ExprName { + range: 392..393, + id: "_", + ctx: Store, + }, + ), + ctx: Store, + }, + ), + }, + ), + }, + ), + guard: None, + body: [ + Pass( + StmtPass { + range: 403..407, + }, + ), + ], + }, + ], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | # Star pattern is only allowed inside a sequence pattern +2 | match subject: +3 | case *_: + | ^^ Syntax Error: Star pattern cannot be used here +4 | pass +5 | case *_ as x: + | + + + | +3 | case *_: +4 | pass +5 | case *_ as x: + | ^^ Syntax Error: Star pattern cannot be used here +6 | pass +7 | case *foo: + | + + + | +5 | case *_ as x: +6 | pass +7 | case *foo: + | ^^^^ Syntax Error: Star pattern cannot be used here +8 | pass +9 | case *foo | 1: + | + + + | + 7 | case *foo: + 8 | pass + 9 | case *foo | 1: + | ^^^^ Syntax Error: Star pattern cannot be used here +10 | pass +11 | case 1 | *foo: + | + + + | + 9 | case *foo | 1: +10 | pass +11 | case 1 | *foo: + | ^^^^ Syntax Error: Star pattern cannot be used here +12 | pass +13 | case Foo(*_): + | + + + | +11 | case 1 | *foo: +12 | pass +13 | case Foo(*_): + | ^^ Syntax Error: Star pattern cannot be used here +14 | pass +15 | case Foo(x=*_): + | + + + | +13 | case Foo(*_): +14 | pass +15 | case Foo(x=*_): + | ^^ Syntax Error: Star pattern cannot be used here +16 | pass +17 | case {*_}: + | + + + | +15 | case Foo(x=*_): +16 | pass +17 | case {*_}: + | ^^ Syntax Error: Star pattern cannot be used here +18 | pass +19 | case {*_: 1}: + | + + + | +15 | case Foo(x=*_): +16 | pass +17 | case {*_}: + | ^ Syntax Error: Expected ':', found '}' +18 | pass +19 | case {*_: 1}: + | + + + | +17 | case {*_}: +18 | pass +19 | case {*_: 1}: + | ^^ Syntax Error: Star pattern cannot be used here +20 | pass +21 | case {None: *_}: + | + + + | +19 | case {*_: 1}: +20 | pass +21 | case {None: *_}: + | ^^ Syntax Error: Star pattern cannot be used here +22 | pass +23 | case 1 + *_: + | + + + | +21 | case {None: *_}: +22 | pass +23 | case 1 + *_: + | ^^ Syntax Error: Star pattern cannot be used here +24 | pass + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__match__unary_add_usage.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__match__unary_add_usage.py.snap.new new file mode 100644 index 0000000000..f3bf2bc4e3 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__match__unary_add_usage.py.snap.new @@ -0,0 +1,399 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/statements/match/unary_add_usage.py +--- +## AST + +``` +Module( + ModModule { + range: 0..269, + body: [ + Match( + StmtMatch { + range: 74..268, + subject: Name( + ExprName { + range: 80..87, + id: "subject", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 93..114, + pattern: MatchValue( + PatternMatchValue { + range: 98..100, + value: UnaryOp( + ExprUnaryOp { + range: 98..100, + op: UAdd, + operand: NumberLiteral( + ExprNumberLiteral { + range: 99..100, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + guard: None, + body: [ + Pass( + StmtPass { + range: 110..114, + }, + ), + ], + }, + MatchCase { + range: 119..149, + pattern: MatchOr( + PatternMatchOr { + range: 124..135, + patterns: [ + MatchValue( + PatternMatchValue { + range: 124..125, + value: NumberLiteral( + ExprNumberLiteral { + range: 124..125, + value: Int( + 1, + ), + }, + ), + }, + ), + MatchValue( + PatternMatchValue { + range: 128..130, + value: UnaryOp( + ExprUnaryOp { + range: 128..130, + op: UAdd, + operand: NumberLiteral( + ExprNumberLiteral { + range: 129..130, + value: Int( + 2, + ), + }, + ), + }, + ), + }, + ), + MatchValue( + PatternMatchValue { + range: 133..135, + value: UnaryOp( + ExprUnaryOp { + range: 133..135, + op: USub, + operand: NumberLiteral( + ExprNumberLiteral { + range: 134..135, + value: Int( + 3, + ), + }, + ), + }, + ), + }, + ), + ], + }, + ), + guard: None, + body: [ + Pass( + StmtPass { + range: 145..149, + }, + ), + ], + }, + MatchCase { + range: 154..184, + pattern: MatchSequence( + PatternMatchSequence { + range: 159..170, + patterns: [ + MatchValue( + PatternMatchValue { + range: 160..161, + value: NumberLiteral( + ExprNumberLiteral { + range: 160..161, + value: Int( + 1, + ), + }, + ), + }, + ), + MatchValue( + PatternMatchValue { + range: 163..165, + value: UnaryOp( + ExprUnaryOp { + range: 163..165, + op: UAdd, + operand: NumberLiteral( + ExprNumberLiteral { + range: 164..165, + value: Int( + 2, + ), + }, + ), + }, + ), + }, + ), + MatchValue( + PatternMatchValue { + range: 167..169, + value: UnaryOp( + ExprUnaryOp { + range: 167..169, + op: USub, + operand: NumberLiteral( + ExprNumberLiteral { + range: 168..169, + value: Int( + 3, + ), + }, + ), + }, + ), + }, + ), + ], + }, + ), + guard: None, + body: [ + Pass( + StmtPass { + range: 180..184, + }, + ), + ], + }, + MatchCase { + range: 189..223, + pattern: MatchClass( + PatternMatchClass { + range: 194..209, + cls: Name( + ExprName { + range: 194..197, + id: "Foo", + ctx: Load, + }, + ), + arguments: PatternArguments { + range: 197..209, + patterns: [], + keywords: [ + PatternKeyword { + range: 198..202, + attr: Identifier { + id: "x", + range: 198..199, + }, + pattern: MatchValue( + PatternMatchValue { + range: 200..202, + value: UnaryOp( + ExprUnaryOp { + range: 200..202, + op: UAdd, + operand: NumberLiteral( + ExprNumberLiteral { + range: 201..202, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + }, + PatternKeyword { + range: 204..208, + attr: Identifier { + id: "y", + range: 204..205, + }, + pattern: MatchValue( + PatternMatchValue { + range: 206..208, + value: UnaryOp( + ExprUnaryOp { + range: 206..208, + op: USub, + operand: NumberLiteral( + ExprNumberLiteral { + range: 207..208, + value: Int( + 2, + ), + }, + ), + }, + ), + }, + ), + }, + ], + }, + }, + ), + guard: None, + body: [ + Pass( + StmtPass { + range: 219..223, + }, + ), + ], + }, + MatchCase { + range: 228..268, + pattern: MatchMapping( + PatternMatchMapping { + range: 233..254, + keys: [ + BooleanLiteral( + ExprBooleanLiteral { + range: 234..238, + value: true, + }, + ), + BooleanLiteral( + ExprBooleanLiteral { + range: 244..249, + value: false, + }, + ), + ], + patterns: [ + MatchValue( + PatternMatchValue { + range: 240..242, + value: UnaryOp( + ExprUnaryOp { + range: 240..242, + op: UAdd, + operand: NumberLiteral( + ExprNumberLiteral { + range: 241..242, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + MatchValue( + PatternMatchValue { + range: 251..253, + value: UnaryOp( + ExprUnaryOp { + range: 251..253, + op: USub, + operand: NumberLiteral( + ExprNumberLiteral { + range: 252..253, + value: Int( + 2, + ), + }, + ), + }, + ), + }, + ), + ], + rest: None, + }, + ), + guard: None, + body: [ + Pass( + StmtPass { + range: 264..268, + }, + ), + ], + }, + ], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | # Unary addition isn't allowed but we parse it for better error recovery. +2 | match subject: +3 | case +1: + | ^^ Syntax Error: Unary '+' is not allowed as a literal pattern +4 | pass +5 | case 1 | +2 | -3: + | + + + | +3 | case +1: +4 | pass +5 | case 1 | +2 | -3: + | ^^ Syntax Error: Unary '+' is not allowed as a literal pattern +6 | pass +7 | case [1, +2, -3]: + | + + + | +5 | case 1 | +2 | -3: +6 | pass +7 | case [1, +2, -3]: + | ^^ Syntax Error: Unary '+' is not allowed as a literal pattern +8 | pass +9 | case Foo(x=+1, y=-2): + | + + + | + 7 | case [1, +2, -3]: + 8 | pass + 9 | case Foo(x=+1, y=-2): + | ^^ Syntax Error: Unary '+' is not allowed as a literal pattern +10 | pass +11 | case {True: +1, False: -2}: + | + + + | + 9 | case Foo(x=+1, y=-2): +10 | pass +11 | case {True: +1, False: -2}: + | ^^ Syntax Error: Unary '+' is not allowed as a literal pattern +12 | pass + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__with__ambiguous_lpar_with_items.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__with__ambiguous_lpar_with_items.py.snap.new new file mode 100644 index 0000000000..e4eb831ef2 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__with__ambiguous_lpar_with_items.py.snap.new @@ -0,0 +1,1675 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/statements/with/ambiguous_lpar_with_items.py +--- +## AST + +``` +Module( + ModModule { + range: 0..950, + body: [ + With( + StmtWith { + range: 163..188, + is_async: false, + items: [ + WithItem { + range: 168..182, + context_expr: Tuple( + ExprTuple { + range: 168..182, + elts: [ + Name( + ExprName { + range: 169..174, + id: "item1", + ctx: Load, + }, + ), + Name( + ExprName { + range: 176..181, + id: "item2", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 185..188, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 185..188, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 189..219, + is_async: false, + items: [ + WithItem { + range: 194..208, + context_expr: Tuple( + ExprTuple { + range: 194..208, + elts: [ + Name( + ExprName { + range: 195..200, + id: "item1", + ctx: Load, + }, + ), + Name( + ExprName { + range: 202..207, + id: "item2", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + optional_vars: None, + }, + WithItem { + range: 213..214, + context_expr: Name( + ExprName { + range: 213..214, + id: "f", + ctx: Load, + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 216..219, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 216..219, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 220..252, + is_async: false, + items: [ + WithItem { + range: 225..239, + context_expr: Tuple( + ExprTuple { + range: 225..239, + elts: [ + Name( + ExprName { + range: 226..231, + id: "item1", + ctx: Load, + }, + ), + Name( + ExprName { + range: 233..238, + id: "item2", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + optional_vars: None, + }, + WithItem { + range: 241..246, + context_expr: Name( + ExprName { + range: 241..246, + id: "item3", + ctx: Load, + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 249..252, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 249..252, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 253..270, + is_async: false, + items: [ + WithItem { + range: 258..265, + context_expr: Starred( + ExprStarred { + range: 259..264, + value: Name( + ExprName { + range: 260..264, + id: "item", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 267..270, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 267..270, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 271..293, + is_async: false, + items: [ + WithItem { + range: 276..288, + context_expr: Starred( + ExprStarred { + range: 277..282, + value: Name( + ExprName { + range: 278..282, + id: "item", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + optional_vars: Some( + Name( + ExprName { + range: 287..288, + id: "f", + ctx: Store, + }, + ), + ), + }, + ], + body: [ + Expr( + StmtExpr { + range: 290..293, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 290..293, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 294..321, + is_async: false, + items: [ + WithItem { + range: 300..315, + context_expr: Named( + ExprNamed { + range: 300..310, + target: Name( + ExprName { + range: 300..304, + id: "item", + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 308..310, + value: Int( + 10, + ), + }, + ), + }, + ), + optional_vars: Some( + Name( + ExprName { + range: 314..315, + id: "f", + ctx: Store, + }, + ), + ), + }, + ], + body: [ + Expr( + StmtExpr { + range: 318..321, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 318..321, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 322..357, + is_async: false, + items: [ + WithItem { + range: 328..333, + context_expr: Name( + ExprName { + range: 328..333, + id: "item1", + ctx: Load, + }, + ), + optional_vars: None, + }, + WithItem { + range: 335..351, + context_expr: Named( + ExprNamed { + range: 335..346, + target: Name( + ExprName { + range: 335..340, + id: "item2", + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 344..346, + value: Int( + 10, + ), + }, + ), + }, + ), + optional_vars: Some( + Name( + ExprName { + range: 350..351, + id: "f", + ctx: Store, + }, + ), + ), + }, + ], + body: [ + Expr( + StmtExpr { + range: 354..357, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 354..357, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 358..396, + is_async: false, + items: [ + WithItem { + range: 363..384, + context_expr: Generator( + ExprGenerator { + range: 363..384, + elt: Name( + ExprName { + range: 364..365, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 366..384, + target: Name( + ExprName { + range: 370..371, + id: "x", + ctx: Store, + }, + ), + iter: Call( + ExprCall { + range: 375..384, + func: Name( + ExprName { + range: 375..380, + id: "range", + ctx: Load, + }, + ), + arguments: Arguments { + range: 380..384, + args: [ + NumberLiteral( + ExprNumberLiteral { + range: 381..383, + value: Int( + 10, + ), + }, + ), + ], + keywords: [], + }, + }, + ), + ifs: [], + is_async: false, + }, + ], + parenthesized: true, + }, + ), + optional_vars: None, + }, + WithItem { + range: 386..390, + context_expr: Name( + ExprName { + range: 386..390, + id: "item", + ctx: Load, + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 393..396, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 393..396, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 397..410, + is_async: false, + items: [ + WithItem { + range: 402..410, + context_expr: Tuple( + ExprTuple { + range: 402..410, + elts: [ + Name( + ExprName { + range: 403..407, + id: "item", + ctx: Load, + }, + ), + Name( + ExprName { + range: 409..410, + id: "x", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + optional_vars: None, + }, + ], + body: [], + }, + ), + For( + StmtFor { + range: 411..429, + is_async: false, + target: Name( + ExprName { + range: 415..416, + id: "x", + ctx: Store, + }, + ), + iter: Call( + ExprCall { + range: 420..429, + func: Name( + ExprName { + range: 420..425, + id: "range", + ctx: Load, + }, + ), + arguments: Arguments { + range: 425..429, + args: [ + NumberLiteral( + ExprNumberLiteral { + range: 426..428, + value: Int( + 10, + ), + }, + ), + ], + keywords: [], + }, + }, + ), + body: [], + orelse: [], + }, + ), + Expr( + StmtExpr { + range: 432..435, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 432..435, + }, + ), + }, + ), + With( + StmtWith { + range: 496..515, + is_async: false, + items: [ + WithItem { + range: 502..509, + context_expr: Starred( + ExprStarred { + range: 503..508, + value: Name( + ExprName { + range: 504..508, + id: "item", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 512..515, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 512..515, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 517..551, + is_async: false, + items: [ + WithItem { + range: 522..539, + context_expr: Generator( + ExprGenerator { + range: 522..539, + elt: Starred( + ExprStarred { + range: 523..525, + value: Name( + ExprName { + range: 524..525, + id: "x", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 526..539, + target: Name( + ExprName { + range: 530..531, + id: "x", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 535..539, + id: "iter", + ctx: Load, + }, + ), + ifs: [], + is_async: false, + }, + ], + parenthesized: true, + }, + ), + optional_vars: None, + }, + WithItem { + range: 541..545, + context_expr: Name( + ExprName { + range: 541..545, + id: "item", + ctx: Load, + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 548..551, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 548..551, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 552..567, + is_async: false, + items: [ + WithItem { + range: 557..567, + context_expr: Tuple( + ExprTuple { + range: 557..567, + elts: [ + Name( + ExprName { + range: 558..563, + id: "item1", + ctx: Load, + }, + ), + Starred( + ExprStarred { + range: 565..567, + value: Name( + ExprName { + range: 566..567, + id: "x", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + optional_vars: None, + }, + ], + body: [], + }, + ), + For( + StmtFor { + range: 568..588, + is_async: false, + target: Name( + ExprName { + range: 572..573, + id: "x", + ctx: Store, + }, + ), + iter: Tuple( + ExprTuple { + range: 577..588, + elts: [ + Name( + ExprName { + range: 577..581, + id: "iter", + ctx: Load, + }, + ), + Name( + ExprName { + range: 583..588, + id: "item2", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + body: [], + orelse: [], + }, + ), + Expr( + StmtExpr { + range: 591..594, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 591..594, + }, + ), + }, + ), + With( + StmtWith { + range: 595..617, + is_async: false, + items: [ + WithItem { + range: 601..607, + context_expr: Name( + ExprName { + range: 601..602, + id: "x", + ctx: Load, + }, + ), + optional_vars: Some( + Name( + ExprName { + range: 606..607, + id: "f", + ctx: Store, + }, + ), + ), + }, + WithItem { + range: 609..611, + context_expr: Starred( + ExprStarred { + range: 609..611, + value: Name( + ExprName { + range: 610..611, + id: "y", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 614..617, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 614..617, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 618..640, + is_async: false, + items: [ + WithItem { + range: 624..626, + context_expr: Starred( + ExprStarred { + range: 624..626, + value: Name( + ExprName { + range: 625..626, + id: "x", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + optional_vars: None, + }, + WithItem { + range: 628..634, + context_expr: Name( + ExprName { + range: 628..629, + id: "y", + ctx: Load, + }, + ), + optional_vars: Some( + Name( + ExprName { + range: 633..634, + id: "f", + ctx: Store, + }, + ), + ), + }, + ], + body: [ + Expr( + StmtExpr { + range: 637..640, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 637..640, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 641..663, + is_async: false, + items: [ + WithItem { + range: 646..658, + context_expr: Tuple( + ExprTuple { + range: 646..658, + elts: [ + Name( + ExprName { + range: 647..648, + id: "x", + ctx: Load, + }, + ), + Yield( + ExprYield { + range: 650..657, + value: Some( + Name( + ExprName { + range: 656..657, + id: "y", + ctx: Load, + }, + ), + ), + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 660..663, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 660..663, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 664..689, + is_async: false, + items: [ + WithItem { + range: 669..684, + context_expr: Tuple( + ExprTuple { + range: 669..684, + elts: [ + Name( + ExprName { + range: 670..671, + id: "x", + ctx: Load, + }, + ), + Yield( + ExprYield { + range: 673..683, + value: Some( + Tuple( + ExprTuple { + range: 679..683, + elts: [ + Name( + ExprName { + range: 679..680, + id: "y", + ctx: Load, + }, + ), + Name( + ExprName { + range: 682..683, + id: "z", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + ), + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 686..689, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 686..689, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 690..717, + is_async: false, + items: [ + WithItem { + range: 695..712, + context_expr: Tuple( + ExprTuple { + range: 695..712, + elts: [ + Name( + ExprName { + range: 696..697, + id: "x", + ctx: Load, + }, + ), + YieldFrom( + ExprYieldFrom { + range: 699..711, + value: Name( + ExprName { + range: 710..711, + id: "y", + ctx: Load, + }, + ), + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 714..717, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 714..717, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 718..734, + is_async: false, + items: [ + WithItem { + range: 724..730, + context_expr: Name( + ExprName { + range: 724..725, + id: "x", + ctx: Load, + }, + ), + optional_vars: Some( + Name( + ExprName { + range: 729..730, + id: "f", + ctx: Store, + }, + ), + ), + }, + WithItem { + range: 732..733, + context_expr: Name( + ExprName { + range: 732..733, + id: "y", + ctx: Load, + }, + ), + optional_vars: None, + }, + ], + body: [], + }, + ), + AnnAssign( + StmtAnnAssign { + range: 738..744, + target: Name( + ExprName { + range: 738..739, + id: "f", + ctx: Store, + }, + ), + annotation: EllipsisLiteral( + ExprEllipsisLiteral { + range: 741..744, + }, + ), + value: None, + simple: true, + }, + ), + With( + StmtWith { + range: 745..777, + is_async: false, + items: [ + WithItem { + range: 750..771, + context_expr: Generator( + ExprGenerator { + range: 750..766, + elt: Name( + ExprName { + range: 751..752, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 753..766, + target: Name( + ExprName { + range: 757..758, + id: "x", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 762..766, + id: "iter", + ctx: Load, + }, + ), + ifs: [], + is_async: false, + }, + ], + parenthesized: true, + }, + ), + optional_vars: Some( + Name( + ExprName { + range: 770..771, + id: "y", + ctx: Store, + }, + ), + ), + }, + ], + body: [ + Expr( + StmtExpr { + range: 774..777, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 774..777, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 837..854, + is_async: false, + items: [ + WithItem { + range: 843..853, + context_expr: Name( + ExprName { + range: 844..848, + id: "item", + ctx: Load, + }, + ), + optional_vars: Some( + Name( + ExprName { + range: 852..853, + id: "f", + ctx: Store, + }, + ), + ), + }, + ], + body: [], + }, + ), + Expr( + StmtExpr { + range: 857..860, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 857..860, + }, + ), + }, + ), + With( + StmtWith { + range: 862..878, + is_async: false, + items: [ + WithItem { + range: 868..877, + context_expr: Name( + ExprName { + range: 868..872, + id: "item", + ctx: Load, + }, + ), + optional_vars: Some( + Name( + ExprName { + range: 876..877, + id: "f", + ctx: Store, + }, + ), + ), + }, + ], + body: [], + }, + ), + AnnAssign( + StmtAnnAssign { + range: 880..886, + target: Name( + ExprName { + range: 880..881, + id: "x", + ctx: Store, + }, + ), + annotation: EllipsisLiteral( + ExprEllipsisLiteral { + range: 883..886, + }, + ), + value: None, + simple: true, + }, + ), + With( + StmtWith { + range: 887..904, + is_async: false, + items: [ + WithItem { + range: 893..903, + context_expr: Name( + ExprName { + range: 893..897, + id: "item", + ctx: Load, + }, + ), + optional_vars: Some( + Name( + ExprName { + range: 901..903, + id: "f1", + ctx: Store, + }, + ), + ), + }, + ], + body: [], + }, + ), + AnnAssign( + StmtAnnAssign { + range: 908..915, + target: Name( + ExprName { + range: 908..910, + id: "f2", + ctx: Store, + }, + ), + annotation: EllipsisLiteral( + ExprEllipsisLiteral { + range: 912..915, + }, + ), + value: None, + simple: true, + }, + ), + With( + StmtWith { + range: 916..950, + is_async: false, + items: [ + WithItem { + range: 922..932, + context_expr: Name( + ExprName { + range: 922..927, + id: "item1", + ctx: Load, + }, + ), + optional_vars: Some( + Name( + ExprName { + range: 931..932, + id: "f", + ctx: Store, + }, + ), + ), + }, + WithItem { + range: 934..944, + context_expr: Named( + ExprNamed { + range: 934..944, + target: Name( + ExprName { + range: 934..939, + id: "item2", + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 943..944, + value: Int( + 0, + ), + }, + ), + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 947..950, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 947..950, + }, + ), + }, + ), + ], + }, + ), + ], + }, +) +``` +## Errors + + | +2 | # These cases should raise the correct syntax error and recover properly. +3 | +4 | with (item1, item2),: ... + | ^ Syntax Error: Trailing comma not allowed +5 | with (item1, item2), as f: ... +6 | with (item1, item2), item3,: ... + | + + + | +4 | with (item1, item2),: ... +5 | with (item1, item2), as f: ... + | ^^ Syntax Error: Expected ',', found 'as' +6 | with (item1, item2), item3,: ... +7 | with (*item): ... + | + + + | +4 | with (item1, item2),: ... +5 | with (item1, item2), as f: ... +6 | with (item1, item2), item3,: ... + | ^ Syntax Error: Trailing comma not allowed +7 | with (*item): ... +8 | with (*item) as f: ... + | + + + | +5 | with (item1, item2), as f: ... +6 | with (item1, item2), item3,: ... +7 | with (*item): ... + | ^^^^^ Syntax Error: Starred expression cannot be used here +8 | with (*item) as f: ... +9 | with (item := 10 as f): ... + | + + + | + 6 | with (item1, item2), item3,: ... + 7 | with (*item): ... + 8 | with (*item) as f: ... + | ^^^^^ Syntax Error: Starred expression cannot be used here + 9 | with (item := 10 as f): ... +10 | with (item1, item2 := 10 as f): ... + | + + + | + 7 | with (*item): ... + 8 | with (*item) as f: ... + 9 | with (item := 10 as f): ... + | ^^^^^^^^^^ Syntax Error: Unparenthesized named expression cannot be used here +10 | with (item1, item2 := 10 as f): ... +11 | with (x for x in range(10), item): ... + | + + + | + 8 | with (*item) as f: ... + 9 | with (item := 10 as f): ... +10 | with (item1, item2 := 10 as f): ... + | ^^^^^^^^^^^ Syntax Error: Unparenthesized named expression cannot be used here +11 | with (x for x in range(10), item): ... +12 | with (item, x for x in range(10)): ... + | + + + | + 9 | with (item := 10 as f): ... +10 | with (item1, item2 := 10 as f): ... +11 | with (x for x in range(10), item): ... + | ^ Syntax Error: Expected ')', found ',' +12 | with (item, x for x in range(10)): ... + | + + + | + 9 | with (item := 10 as f): ... +10 | with (item1, item2 := 10 as f): ... +11 | with (x for x in range(10), item): ... + | ^ Syntax Error: Expected ',', found ')' +12 | with (item, x for x in range(10)): ... + | + + + | +10 | with (item1, item2 := 10 as f): ... +11 | with (x for x in range(10), item): ... +12 | with (item, x for x in range(10)): ... + | ^^^ Syntax Error: Expected ')', found 'for' +13 | +14 | # Make sure the parser doesn't report the same error twice + | + + + | +10 | with (item1, item2 := 10 as f): ... +11 | with (x for x in range(10), item): ... +12 | with (item, x for x in range(10)): ... + | ^ Syntax Error: Expected ':', found ')' +13 | +14 | # Make sure the parser doesn't report the same error twice + | + + + | +10 | with (item1, item2 := 10 as f): ... +11 | with (x for x in range(10), item): ... +12 | with (item, x for x in range(10)): ... + | ^ Syntax Error: Expected a statement +13 | +14 | # Make sure the parser doesn't report the same error twice + | + + + | +14 | # Make sure the parser doesn't report the same error twice +15 | with ((*item)): ... + | ^^^^^ Syntax Error: Starred expression cannot be used here +16 | +17 | with (*x for x in iter, item): ... + | + + + | +15 | with ((*item)): ... +16 | +17 | with (*x for x in iter, item): ... + | ^^ Syntax Error: Iterable unpacking cannot be used in a comprehension +18 | with (item1, *x for x in iter, item2): ... +19 | with (x as f, *y): ... + | + + + | +15 | with ((*item)): ... +16 | +17 | with (*x for x in iter, item): ... + | ^ Syntax Error: Expected ')', found ',' +18 | with (item1, *x for x in iter, item2): ... +19 | with (x as f, *y): ... + | + + + | +15 | with ((*item)): ... +16 | +17 | with (*x for x in iter, item): ... + | ^ Syntax Error: Expected ',', found ')' +18 | with (item1, *x for x in iter, item2): ... +19 | with (x as f, *y): ... + | + + + | +17 | with (*x for x in iter, item): ... +18 | with (item1, *x for x in iter, item2): ... + | ^^^ Syntax Error: Expected ')', found 'for' +19 | with (x as f, *y): ... +20 | with (*x, y as f): ... + | + + + | +17 | with (*x for x in iter, item): ... +18 | with (item1, *x for x in iter, item2): ... + | ^ Syntax Error: Expected ':', found ')' +19 | with (x as f, *y): ... +20 | with (*x, y as f): ... + | + + + | +17 | with (*x for x in iter, item): ... +18 | with (item1, *x for x in iter, item2): ... + | ^ Syntax Error: Expected a statement +19 | with (x as f, *y): ... +20 | with (*x, y as f): ... + | + + + | +17 | with (*x for x in iter, item): ... +18 | with (item1, *x for x in iter, item2): ... +19 | with (x as f, *y): ... + | ^^ Syntax Error: Starred expression cannot be used here +20 | with (*x, y as f): ... +21 | with (x, yield y): ... + | + + + | +18 | with (item1, *x for x in iter, item2): ... +19 | with (x as f, *y): ... +20 | with (*x, y as f): ... + | ^^ Syntax Error: Starred expression cannot be used here +21 | with (x, yield y): ... +22 | with (x, yield y, z): ... + | + + + | +19 | with (x as f, *y): ... +20 | with (*x, y as f): ... +21 | with (x, yield y): ... + | ^^^^^^^ Syntax Error: Yield expression cannot be used here +22 | with (x, yield y, z): ... +23 | with (x, yield from y): ... + | + + + | +20 | with (*x, y as f): ... +21 | with (x, yield y): ... +22 | with (x, yield y, z): ... + | ^^^^^^^^^^ Syntax Error: Yield expression cannot be used here +23 | with (x, yield from y): ... +24 | with (x as f, y) as f: ... + | + + + | +21 | with (x, yield y): ... +22 | with (x, yield y, z): ... +23 | with (x, yield from y): ... + | ^^^^^^^^^^^^ Syntax Error: Yield expression cannot be used here +24 | with (x as f, y) as f: ... +25 | with (x for x in iter as y): ... + | + + + | +22 | with (x, yield y, z): ... +23 | with (x, yield from y): ... +24 | with (x as f, y) as f: ... + | ^^ Syntax Error: Expected ':', found 'as' +25 | with (x for x in iter as y): ... + | + + + | +23 | with (x, yield from y): ... +24 | with (x as f, y) as f: ... +25 | with (x for x in iter as y): ... + | ^^ Syntax Error: Expected ')', found 'as' +26 | +27 | # The inner `(...)` is parsed as parenthesized expression + | + + + | +23 | with (x, yield from y): ... +24 | with (x as f, y) as f: ... +25 | with (x for x in iter as y): ... + | ^ Syntax Error: Expected ',', found ')' +26 | +27 | # The inner `(...)` is parsed as parenthesized expression + | + + + | +27 | # The inner `(...)` is parsed as parenthesized expression +28 | with ((item as f)): ... + | ^^ Syntax Error: Expected ')', found 'as' +29 | +30 | with (item as f), x: ... + | + + + | +27 | # The inner `(...)` is parsed as parenthesized expression +28 | with ((item as f)): ... + | ^ Syntax Error: Expected ':', found ')' +29 | +30 | with (item as f), x: ... + | + + + | +27 | # The inner `(...)` is parsed as parenthesized expression +28 | with ((item as f)): ... + | ^ Syntax Error: Expected a statement +29 | +30 | with (item as f), x: ... + | + + + | +28 | with ((item as f)): ... +29 | +30 | with (item as f), x: ... + | ^ Syntax Error: Expected ':', found ',' +31 | with (item as f1) as f2: ... +32 | with (item1 as f, item2 := 0): ... + | + + + | +30 | with (item as f), x: ... +31 | with (item as f1) as f2: ... + | ^^ Syntax Error: Expected ':', found 'as' +32 | with (item1 as f, item2 := 0): ... + | + + + | +30 | with (item as f), x: ... +31 | with (item as f1) as f2: ... +32 | with (item1 as f, item2 := 0): ... + | ^^^^^^^^^^ Syntax Error: Unparenthesized named expression cannot be used here + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__with__empty_with_items.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__with__empty_with_items.py.snap.new new file mode 100644 index 0000000000..d67e218d29 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__with__empty_with_items.py.snap.new @@ -0,0 +1,70 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/statements/with/empty_with_items.py +--- +## AST + +``` +Module( + ModModule { + range: 0..105, + body: [ + With( + StmtWith { + range: 88..98, + is_async: false, + items: [], + body: [ + Expr( + StmtExpr { + range: 95..98, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 95..98, + }, + ), + }, + ), + ], + }, + ), + Expr( + StmtExpr { + range: 100..105, + value: BinOp( + ExprBinOp { + range: 100..105, + left: Name( + ExprName { + range: 100..101, + id: "x", + ctx: Load, + }, + ), + op: Add, + right: Name( + ExprName { + range: 104..105, + id: "y", + ctx: Load, + }, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +2 | # The parser should recover from this syntax error. +3 | +4 | with : ... + | ^ Syntax Error: Expected the start of an expression after `with` keyword +5 | +6 | x + y + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__with__unclosed_ambiguous_lpar.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__with__unclosed_ambiguous_lpar.py.snap.new new file mode 100644 index 0000000000..5cafa4ff69 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__with__unclosed_ambiguous_lpar.py.snap.new @@ -0,0 +1,78 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/statements/with/unclosed_ambiguous_lpar.py +--- +## AST + +``` +Module( + ModModule { + range: 0..14, + body: [ + With( + StmtWith { + range: 0..14, + is_async: false, + items: [ + WithItem { + range: 5..6, + context_expr: Name( + ExprName { + range: 6..6, + id: "", + ctx: Invalid, + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 9..14, + value: BinOp( + ExprBinOp { + range: 9..14, + left: Name( + ExprName { + range: 9..10, + id: "x", + ctx: Load, + }, + ), + op: Add, + right: Name( + ExprName { + range: 13..14, + id: "y", + ctx: Load, + }, + ), + }, + ), + }, + ), + ], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | with (: + | ^ Syntax Error: Expected an expression +2 | +3 | x + y + | + + + | +1 | with (: +2 | +3 | x + y + | Syntax Error: unexpected EOF while parsing + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__with__unclosed_ambiguous_lpar_eof.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__with__unclosed_ambiguous_lpar_eof.py.snap.new new file mode 100644 index 0000000000..924c11a959 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__with__unclosed_ambiguous_lpar_eof.py.snap.new @@ -0,0 +1,42 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/statements/with/unclosed_ambiguous_lpar_eof.py +--- +## AST + +``` +Module( + ModModule { + range: 0..6, + body: [ + With( + StmtWith { + range: 0..6, + is_async: false, + items: [ + WithItem { + range: 5..6, + context_expr: Name( + ExprName { + range: 6..6, + id: "", + ctx: Invalid, + }, + ), + optional_vars: None, + }, + ], + body: [], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | with ( + | Syntax Error: unexpected EOF while parsing + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__with__unparenthesized_with_items.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__with__unparenthesized_with_items.py.snap.new new file mode 100644 index 0000000000..6d13719da7 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@statements__with__unparenthesized_with_items.py.snap.new @@ -0,0 +1,362 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/invalid/statements/with/unparenthesized_with_items.py +--- +## AST + +``` +Module( + ModModule { + range: 0..249, + body: [ + With( + StmtWith { + range: 86..102, + is_async: false, + items: [ + WithItem { + range: 91..95, + context_expr: Name( + ExprName { + range: 91..95, + id: "item", + ctx: Load, + }, + ), + optional_vars: None, + }, + ], + body: [ + Pass( + StmtPass { + range: 98..102, + }, + ), + ], + }, + ), + With( + StmtWith { + range: 103..124, + is_async: false, + items: [ + WithItem { + range: 108..117, + context_expr: Name( + ExprName { + range: 108..112, + id: "item", + ctx: Load, + }, + ), + optional_vars: Some( + Name( + ExprName { + range: 116..117, + id: "x", + ctx: Store, + }, + ), + ), + }, + ], + body: [ + Pass( + StmtPass { + range: 120..124, + }, + ), + ], + }, + ), + With( + StmtWith { + range: 125..141, + is_async: false, + items: [ + WithItem { + range: 130..135, + context_expr: Starred( + ExprStarred { + range: 130..135, + value: Name( + ExprName { + range: 131..135, + id: "item", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + optional_vars: None, + }, + ], + body: [ + Pass( + StmtPass { + range: 137..141, + }, + ), + ], + }, + ), + With( + StmtWith { + range: 142..163, + is_async: false, + items: [ + WithItem { + range: 147..157, + context_expr: Starred( + ExprStarred { + range: 147..152, + value: Name( + ExprName { + range: 148..152, + id: "item", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + optional_vars: Some( + Name( + ExprName { + range: 156..157, + id: "x", + ctx: Store, + }, + ), + ), + }, + ], + body: [ + Pass( + StmtPass { + range: 159..163, + }, + ), + ], + }, + ), + With( + StmtWith { + range: 164..193, + is_async: false, + items: [ + WithItem { + range: 169..175, + context_expr: Starred( + ExprStarred { + range: 169..175, + value: Name( + ExprName { + range: 170..175, + id: "item1", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + optional_vars: None, + }, + WithItem { + range: 177..187, + context_expr: Name( + ExprName { + range: 177..182, + id: "item2", + ctx: Load, + }, + ), + optional_vars: Some( + Name( + ExprName { + range: 186..187, + id: "f", + ctx: Store, + }, + ), + ), + }, + ], + body: [ + Pass( + StmtPass { + range: 189..193, + }, + ), + ], + }, + ), + With( + StmtWith { + range: 194..223, + is_async: false, + items: [ + WithItem { + range: 199..209, + context_expr: Name( + ExprName { + range: 199..204, + id: "item1", + ctx: Load, + }, + ), + optional_vars: Some( + Name( + ExprName { + range: 208..209, + id: "f", + ctx: Store, + }, + ), + ), + }, + WithItem { + range: 211..217, + context_expr: Starred( + ExprStarred { + range: 211..217, + value: Name( + ExprName { + range: 212..217, + id: "item2", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + optional_vars: None, + }, + ], + body: [ + Pass( + StmtPass { + range: 219..223, + }, + ), + ], + }, + ), + With( + StmtWith { + range: 224..249, + is_async: false, + items: [ + WithItem { + range: 229..233, + context_expr: Name( + ExprName { + range: 229..233, + id: "item", + ctx: Load, + }, + ), + optional_vars: None, + }, + WithItem { + range: 237..243, + context_expr: NumberLiteral( + ExprNumberLiteral { + range: 237..238, + value: Int( + 0, + ), + }, + ), + optional_vars: Some( + Name( + ExprName { + range: 242..243, + id: "f", + ctx: Store, + }, + ), + ), + }, + ], + body: [ + Pass( + StmtPass { + range: 245..249, + }, + ), + ], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | # For parenthesized with items test cases, refer to `./ambiguous_lpar_with_items.py` +2 | +3 | with item,: pass + | ^ Syntax Error: Trailing comma not allowed +4 | with item as x,: pass +5 | with *item: pass + | + + + | +3 | with item,: pass +4 | with item as x,: pass + | ^ Syntax Error: Trailing comma not allowed +5 | with *item: pass +6 | with *item as x: pass + | + + + | +3 | with item,: pass +4 | with item as x,: pass +5 | with *item: pass + | ^^^^^ Syntax Error: Starred expression cannot be used here +6 | with *item as x: pass +7 | with *item1, item2 as f: pass + | + + + | +4 | with item as x,: pass +5 | with *item: pass +6 | with *item as x: pass + | ^^^^^ Syntax Error: Starred expression cannot be used here +7 | with *item1, item2 as f: pass +8 | with item1 as f, *item2: pass + | + + + | +5 | with *item: pass +6 | with *item as x: pass +7 | with *item1, item2 as f: pass + | ^^^^^^ Syntax Error: Starred expression cannot be used here +8 | with item1 as f, *item2: pass +9 | with item := 0 as f: pass + | + + + | +6 | with *item as x: pass +7 | with *item1, item2 as f: pass +8 | with item1 as f, *item2: pass + | ^^^^^^ Syntax Error: Starred expression cannot be used here +9 | with item := 0 as f: pass + | + + + | +7 | with *item1, item2 as f: pass +8 | with item1 as f, *item2: pass +9 | with item := 0 as f: pass + | ^^ Syntax Error: Expected ',', found ':=' + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@try_stmt_misspelled_except.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@try_stmt_misspelled_except.py.snap.new new file mode 100644 index 0000000000..635b7c558f --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@try_stmt_misspelled_except.py.snap.new @@ -0,0 +1,273 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/try_stmt_misspelled_except.py +--- +## AST + +``` +Module( + ModModule { + range: 0..165, + body: [ + Try( + StmtTry { + range: 0..13, + body: [ + Pass( + StmtPass { + range: 9..13, + }, + ), + ], + handlers: [], + orelse: [], + finalbody: [], + is_star: false, + }, + ), + AnnAssign( + StmtAnnAssign { + range: 14..20, + target: Name( + ExprName { + range: 14..19, + id: "exept", + ctx: Store, + }, + ), + annotation: Name( + ExprName { + range: 20..20, + id: "", + ctx: Invalid, + }, + ), + value: None, + simple: true, + }, + ), + Pass( + StmtPass { + range: 54..58, + }, + ), + Pass( + StmtPass { + range: 72..76, + }, + ), + Assign( + StmtAssign { + range: 77..82, + targets: [ + Name( + ExprName { + range: 77..78, + id: "a", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 81..82, + value: Int( + 1, + ), + }, + ), + }, + ), + Try( + StmtTry { + range: 83..113, + body: [ + Pass( + StmtPass { + range: 92..96, + }, + ), + ], + handlers: [ + ExceptHandler( + ExceptHandlerExceptHandler { + range: 97..113, + type_: None, + name: None, + body: [ + Pass( + StmtPass { + range: 109..113, + }, + ), + ], + }, + ), + ], + orelse: [], + finalbody: [], + is_star: false, + }, + ), + AnnAssign( + StmtAnnAssign { + range: 114..120, + target: Name( + ExprName { + range: 114..119, + id: "exept", + ctx: Store, + }, + ), + annotation: Name( + ExprName { + range: 120..120, + id: "", + ctx: Invalid, + }, + ), + value: None, + simple: true, + }, + ), + Pass( + StmtPass { + range: 154..158, + }, + ), + Assign( + StmtAssign { + range: 159..164, + targets: [ + Name( + ExprName { + range: 159..160, + id: "b", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 163..164, + value: Int( + 1, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | try: +2 | pass +3 | exept: # spellchecker:disable-line + | ^^^^^ Syntax Error: Expected `except` or `finally` after `try` block +4 | pass +5 | finally: + | + + + | +1 | try: +2 | pass +3 | exept: # spellchecker:disable-line + | ^ Syntax Error: Expected an expression +4 | pass +5 | finally: +6 | pass + | + + + | +2 | pass +3 | exept: # spellchecker:disable-line +4 | pass + | ^^^^ Syntax Error: Unexpected indentation +5 | finally: +6 | pass + | + + + | +3 | exept: # spellchecker:disable-line +4 | pass +5 | finally: + | Syntax Error: Expected a statement +6 | pass +7 | a = 1 + | + + + | +3 | exept: # spellchecker:disable-line +4 | pass +5 | finally: + | ^ Syntax Error: Expected a statement +6 | pass +7 | a = 1 + | + + + | +3 | exept: # spellchecker:disable-line +4 | pass +5 | finally: + | ^ Syntax Error: Expected a statement +6 | pass +7 | a = 1 +8 | try: + | + + + | +4 | pass +5 | finally: +6 | pass + | ^^^^ Syntax Error: Unexpected indentation +7 | a = 1 +8 | try: + | + + + | +5 | finally: +6 | pass +7 | a = 1 + | Syntax Error: Expected a statement +8 | try: +9 | pass + | + + + | +10 | except: +11 | pass +12 | exept: # spellchecker:disable-line + | ^ Syntax Error: Expected an expression +13 | pass +14 | b = 1 + | + + + | +11 | pass +12 | exept: # spellchecker:disable-line +13 | pass + | ^^^^ Syntax Error: Unexpected indentation +14 | b = 1 + | + + + | +12 | exept: # spellchecker:disable-line +13 | pass +14 | b = 1 + | Syntax Error: Expected a statement + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@type_alias_incomplete_stmt.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@type_alias_incomplete_stmt.py.snap.new new file mode 100644 index 0000000000..b27b0f4cfb --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@type_alias_incomplete_stmt.py.snap.new @@ -0,0 +1,88 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/type_alias_incomplete_stmt.py +--- +## AST + +``` +Module( + ModModule { + range: 0..21, + body: [ + Expr( + StmtExpr { + range: 0..4, + value: Name( + ExprName { + range: 0..4, + id: "type", + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 5..9, + value: Name( + ExprName { + range: 5..9, + id: "type", + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 10..11, + value: Name( + ExprName { + range: 10..11, + id: "x", + ctx: Load, + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 12..20, + name: Name( + ExprName { + range: 17..18, + id: "x", + ctx: Store, + }, + ), + type_params: None, + value: Name( + ExprName { + range: 20..20, + id: "", + ctx: Invalid, + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | type +2 | type x + | ^ Syntax Error: Simple statements must be separated by newlines or semicolons +3 | type x = + | + + + | +1 | type +2 | type x +3 | type x = + | ^ Syntax Error: Expected an expression + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@type_alias_invalid_value_expr.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@type_alias_invalid_value_expr.py.snap.new new file mode 100644 index 0000000000..b656fa9f91 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@type_alias_invalid_value_expr.py.snap.new @@ -0,0 +1,161 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/type_alias_invalid_value_expr.py +--- +## AST + +``` +Module( + ModModule { + range: 0..67, + body: [ + TypeAlias( + StmtTypeAlias { + range: 0..11, + name: Name( + ExprName { + range: 5..6, + id: "x", + ctx: Store, + }, + ), + type_params: None, + value: Starred( + ExprStarred { + range: 9..11, + value: Name( + ExprName { + range: 10..11, + id: "y", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 12..28, + name: Name( + ExprName { + range: 17..18, + id: "x", + ctx: Store, + }, + ), + type_params: None, + value: Yield( + ExprYield { + range: 21..28, + value: Some( + Name( + ExprName { + range: 27..28, + id: "y", + ctx: Load, + }, + ), + ), + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 29..50, + name: Name( + ExprName { + range: 34..35, + id: "x", + ctx: Store, + }, + ), + type_params: None, + value: YieldFrom( + ExprYieldFrom { + range: 38..50, + value: Name( + ExprName { + range: 49..50, + id: "y", + ctx: Load, + }, + ), + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 51..61, + name: Name( + ExprName { + range: 56..57, + id: "x", + ctx: Store, + }, + ), + type_params: None, + value: Name( + ExprName { + range: 60..61, + id: "x", + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 65..66, + value: NumberLiteral( + ExprNumberLiteral { + range: 65..66, + value: Int( + 1, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | type x = *y + | ^^ Syntax Error: Starred expression cannot be used here +2 | type x = yield y +3 | type x = yield from y + | + + + | +1 | type x = *y +2 | type x = yield y + | ^^^^^^^ Syntax Error: Yield expression cannot be used here +3 | type x = yield from y +4 | type x = x := 1 + | + + + | +1 | type x = *y +2 | type x = yield y +3 | type x = yield from y + | ^^^^^^^^^^^^ Syntax Error: Yield expression cannot be used here +4 | type x = x := 1 + | + + + | +2 | type x = yield y +3 | type x = yield from y +4 | type x = x := 1 + | ^^ Syntax Error: Expected a statement + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@type_param_invalid_bound_expr.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@type_param_invalid_bound_expr.py.snap.new new file mode 100644 index 0000000000..de47eb1a7d --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@type_param_invalid_bound_expr.py.snap.new @@ -0,0 +1,259 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/type_param_invalid_bound_expr.py +--- +## AST + +``` +Module( + ModModule { + range: 0..103, + body: [ + TypeAlias( + StmtTypeAlias { + range: 0..21, + name: Name( + ExprName { + range: 5..6, + id: "X", + ctx: Store, + }, + ), + type_params: Some( + TypeParams { + range: 6..15, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 7..14, + name: Identifier { + id: "T", + range: 7..8, + }, + bound: Some( + Starred( + ExprStarred { + range: 10..14, + value: Name( + ExprName { + range: 11..14, + id: "int", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ), + default: None, + }, + ), + ], + }, + ), + value: Name( + ExprName { + range: 18..21, + id: "int", + ctx: Load, + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 22..46, + name: Name( + ExprName { + range: 27..28, + id: "X", + ctx: Store, + }, + ), + type_params: Some( + TypeParams { + range: 28..40, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 29..39, + name: Identifier { + id: "T", + range: 29..30, + }, + bound: Some( + Yield( + ExprYield { + range: 32..39, + value: Some( + Name( + ExprName { + range: 38..39, + id: "x", + ctx: Load, + }, + ), + ), + }, + ), + ), + default: None, + }, + ), + ], + }, + ), + value: Name( + ExprName { + range: 43..46, + id: "int", + ctx: Load, + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 47..76, + name: Name( + ExprName { + range: 52..53, + id: "X", + ctx: Store, + }, + ), + type_params: Some( + TypeParams { + range: 53..70, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 54..69, + name: Identifier { + id: "T", + range: 54..55, + }, + bound: Some( + YieldFrom( + ExprYieldFrom { + range: 57..69, + value: Name( + ExprName { + range: 68..69, + id: "x", + ctx: Load, + }, + ), + }, + ), + ), + default: None, + }, + ), + ], + }, + ), + value: Name( + ExprName { + range: 73..76, + id: "int", + ctx: Load, + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 77..102, + name: Name( + ExprName { + range: 82..83, + id: "X", + ctx: Store, + }, + ), + type_params: Some( + TypeParams { + range: 83..96, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 84..88, + name: Identifier { + id: "T", + range: 84..85, + }, + bound: Some( + Name( + ExprName { + range: 87..88, + id: "x", + ctx: Load, + }, + ), + ), + default: None, + }, + ), + TypeVar( + TypeParamTypeVar { + range: 92..95, + name: Identifier { + id: "int", + range: 92..95, + }, + bound: None, + default: None, + }, + ), + ], + }, + ), + value: Name( + ExprName { + range: 99..102, + id: "int", + ctx: Load, + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | type X[T: *int] = int + | ^^^^ Syntax Error: Starred expression cannot be used here +2 | type X[T: yield x] = int +3 | type X[T: yield from x] = int + | + + + | +1 | type X[T: *int] = int +2 | type X[T: yield x] = int + | ^^^^^^^ Syntax Error: Yield expression cannot be used here +3 | type X[T: yield from x] = int +4 | type X[T: x := int] = int + | + + + | +1 | type X[T: *int] = int +2 | type X[T: yield x] = int +3 | type X[T: yield from x] = int + | ^^^^^^^^^^^^ Syntax Error: Yield expression cannot be used here +4 | type X[T: x := int] = int + | + + + | +2 | type X[T: yield x] = int +3 | type X[T: yield from x] = int +4 | type X[T: x := int] = int + | ^^ Syntax Error: Expected ',', found ':=' + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@type_param_missing_bound.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@type_param_missing_bound.py.snap.new new file mode 100644 index 0000000000..9439450b96 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@type_param_missing_bound.py.snap.new @@ -0,0 +1,115 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/type_param_missing_bound.py +--- +## AST + +``` +Module( + ModModule { + range: 0..41, + body: [ + TypeAlias( + StmtTypeAlias { + range: 0..17, + name: Name( + ExprName { + range: 5..6, + id: "X", + ctx: Store, + }, + ), + type_params: Some( + TypeParams { + range: 6..11, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 7..9, + name: Identifier { + id: "T", + range: 7..8, + }, + bound: None, + default: None, + }, + ), + ], + }, + ), + value: Name( + ExprName { + range: 14..17, + id: "int", + ctx: Load, + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 18..40, + name: Name( + ExprName { + range: 23..24, + id: "X", + ctx: Store, + }, + ), + type_params: Some( + TypeParams { + range: 24..34, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 25..28, + name: Identifier { + id: "T1", + range: 25..27, + }, + bound: None, + default: None, + }, + ), + TypeVar( + TypeParamTypeVar { + range: 31..33, + name: Identifier { + id: "T2", + range: 31..33, + }, + bound: None, + default: None, + }, + ), + ], + }, + ), + value: Name( + ExprName { + range: 37..40, + id: "int", + ctx: Load, + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | type X[T: ] = int + | ^ Syntax Error: Expected an expression +2 | type X[T1: , T2] = int + | + + + | +1 | type X[T: ] = int +2 | type X[T1: , T2] = int + | ^ Syntax Error: Expected an expression + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@type_param_param_spec_bound.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@type_param_param_spec_bound.py.snap.new new file mode 100644 index 0000000000..7ca453bd91 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@type_param_param_spec_bound.py.snap.new @@ -0,0 +1,94 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/type_param_param_spec_bound.py +--- +## AST + +``` +Module( + ModModule { + range: 0..23, + body: [ + TypeAlias( + StmtTypeAlias { + range: 0..10, + name: Name( + ExprName { + range: 5..6, + id: "X", + ctx: Store, + }, + ), + type_params: Some( + TypeParams { + range: 6..10, + type_params: [ + ParamSpec( + TypeParamParamSpec { + range: 7..10, + name: Identifier { + id: "T", + range: 9..10, + }, + default: None, + }, + ), + ], + }, + ), + value: Name( + ExprName { + range: 10..10, + id: "", + ctx: Invalid, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 12..15, + value: Name( + ExprName { + range: 12..15, + id: "int", + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 19..22, + value: Name( + ExprName { + range: 19..22, + id: "int", + ctx: Load, + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | type X[**T: int] = int + | ^ Syntax Error: Expected ']', found ':' + | + + + | +1 | type X[**T: int] = int + | ^ Syntax Error: Expected a statement + | + + + | +1 | type X[**T: int] = int + | ^ Syntax Error: Expected a statement + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@type_param_param_spec_invalid_default_expr.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@type_param_param_spec_invalid_default_expr.py.snap.new new file mode 100644 index 0000000000..887ad11218 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@type_param_param_spec_invalid_default_expr.py.snap.new @@ -0,0 +1,315 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/type_param_param_spec_invalid_default_expr.py +--- +## AST + +``` +Module( + ModModule { + range: 0..140, + body: [ + TypeAlias( + StmtTypeAlias { + range: 0..24, + name: Name( + ExprName { + range: 5..6, + id: "X", + ctx: Store, + }, + ), + type_params: Some( + TypeParams { + range: 6..18, + type_params: [ + ParamSpec( + TypeParamParamSpec { + range: 7..17, + name: Identifier { + id: "P", + range: 9..10, + }, + default: Some( + Starred( + ExprStarred { + range: 13..17, + value: Name( + ExprName { + range: 14..17, + id: "int", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ), + }, + ), + ], + }, + ), + value: Name( + ExprName { + range: 21..24, + id: "int", + ctx: Load, + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 25..52, + name: Name( + ExprName { + range: 30..31, + id: "X", + ctx: Store, + }, + ), + type_params: Some( + TypeParams { + range: 31..46, + type_params: [ + ParamSpec( + TypeParamParamSpec { + range: 32..45, + name: Identifier { + id: "P", + range: 34..35, + }, + default: Some( + Yield( + ExprYield { + range: 38..45, + value: Some( + Name( + ExprName { + range: 44..45, + id: "x", + ctx: Load, + }, + ), + ), + }, + ), + ), + }, + ), + ], + }, + ), + value: Name( + ExprName { + range: 49..52, + id: "int", + ctx: Load, + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 53..85, + name: Name( + ExprName { + range: 58..59, + id: "X", + ctx: Store, + }, + ), + type_params: Some( + TypeParams { + range: 59..79, + type_params: [ + ParamSpec( + TypeParamParamSpec { + range: 60..78, + name: Identifier { + id: "P", + range: 62..63, + }, + default: Some( + YieldFrom( + ExprYieldFrom { + range: 66..78, + value: Name( + ExprName { + range: 77..78, + id: "x", + ctx: Load, + }, + ), + }, + ), + ), + }, + ), + ], + }, + ), + value: Name( + ExprName { + range: 82..85, + id: "int", + ctx: Load, + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 86..114, + name: Name( + ExprName { + range: 91..92, + id: "X", + ctx: Store, + }, + ), + type_params: Some( + TypeParams { + range: 92..108, + type_params: [ + ParamSpec( + TypeParamParamSpec { + range: 93..100, + name: Identifier { + id: "P", + range: 95..96, + }, + default: Some( + Name( + ExprName { + range: 99..100, + id: "x", + ctx: Load, + }, + ), + ), + }, + ), + TypeVar( + TypeParamTypeVar { + range: 104..107, + name: Identifier { + id: "int", + range: 104..107, + }, + bound: None, + default: None, + }, + ), + ], + }, + ), + value: Name( + ExprName { + range: 111..114, + id: "int", + ctx: Load, + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 115..139, + name: Name( + ExprName { + range: 120..121, + id: "X", + ctx: Store, + }, + ), + type_params: Some( + TypeParams { + range: 121..133, + type_params: [ + ParamSpec( + TypeParamParamSpec { + range: 122..132, + name: Identifier { + id: "P", + range: 124..125, + }, + default: Some( + Starred( + ExprStarred { + range: 128..132, + value: Name( + ExprName { + range: 129..132, + id: "int", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ), + }, + ), + ], + }, + ), + value: Name( + ExprName { + range: 136..139, + id: "int", + ctx: Load, + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | type X[**P = *int] = int + | ^^^^ Syntax Error: Starred expression cannot be used here +2 | type X[**P = yield x] = int +3 | type X[**P = yield from x] = int + | + + + | +1 | type X[**P = *int] = int +2 | type X[**P = yield x] = int + | ^^^^^^^ Syntax Error: Yield expression cannot be used here +3 | type X[**P = yield from x] = int +4 | type X[**P = x := int] = int + | + + + | +1 | type X[**P = *int] = int +2 | type X[**P = yield x] = int +3 | type X[**P = yield from x] = int + | ^^^^^^^^^^^^ Syntax Error: Yield expression cannot be used here +4 | type X[**P = x := int] = int +5 | type X[**P = *int] = int + | + + + | +2 | type X[**P = yield x] = int +3 | type X[**P = yield from x] = int +4 | type X[**P = x := int] = int + | ^^ Syntax Error: Expected ',', found ':=' +5 | type X[**P = *int] = int + | + + + | +3 | type X[**P = yield from x] = int +4 | type X[**P = x := int] = int +5 | type X[**P = *int] = int + | ^^^^ Syntax Error: Starred expression cannot be used here + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@type_param_param_spec_missing_default.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@type_param_param_spec_missing_default.py.snap.new new file mode 100644 index 0000000000..09083d9454 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@type_param_param_spec_missing_default.py.snap.new @@ -0,0 +1,113 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/type_param_param_spec_missing_default.py +--- +## AST + +``` +Module( + ModModule { + range: 0..44, + body: [ + TypeAlias( + StmtTypeAlias { + range: 0..19, + name: Name( + ExprName { + range: 5..6, + id: "X", + ctx: Store, + }, + ), + type_params: Some( + TypeParams { + range: 6..13, + type_params: [ + ParamSpec( + TypeParamParamSpec { + range: 7..12, + name: Identifier { + id: "P", + range: 9..10, + }, + default: None, + }, + ), + ], + }, + ), + value: Name( + ExprName { + range: 16..19, + id: "int", + ctx: Load, + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 20..43, + name: Name( + ExprName { + range: 25..26, + id: "X", + ctx: Store, + }, + ), + type_params: Some( + TypeParams { + range: 26..37, + type_params: [ + ParamSpec( + TypeParamParamSpec { + range: 27..32, + name: Identifier { + id: "P", + range: 29..30, + }, + default: None, + }, + ), + TypeVar( + TypeParamTypeVar { + range: 34..36, + name: Identifier { + id: "T2", + range: 34..36, + }, + bound: None, + default: None, + }, + ), + ], + }, + ), + value: Name( + ExprName { + range: 40..43, + id: "int", + ctx: Load, + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | type X[**P =] = int + | ^ Syntax Error: Expected an expression +2 | type X[**P =, T2] = int + | + + + | +1 | type X[**P =] = int +2 | type X[**P =, T2] = int + | ^ Syntax Error: Expected an expression + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@type_param_type_var_invalid_default_expr.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@type_param_type_var_invalid_default_expr.py.snap.new new file mode 100644 index 0000000000..4d1908e67a --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@type_param_type_var_invalid_default_expr.py.snap.new @@ -0,0 +1,380 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/type_param_type_var_invalid_default_expr.py +--- +## AST + +``` +Module( + ModModule { + range: 0..163, + body: [ + TypeAlias( + StmtTypeAlias { + range: 0..22, + name: Name( + ExprName { + range: 5..6, + id: "X", + ctx: Store, + }, + ), + type_params: Some( + TypeParams { + range: 6..16, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 7..15, + name: Identifier { + id: "T", + range: 7..8, + }, + bound: None, + default: Some( + Starred( + ExprStarred { + range: 11..15, + value: Name( + ExprName { + range: 12..15, + id: "int", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ), + }, + ), + ], + }, + ), + value: Name( + ExprName { + range: 19..22, + id: "int", + ctx: Load, + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 23..48, + name: Name( + ExprName { + range: 28..29, + id: "X", + ctx: Store, + }, + ), + type_params: Some( + TypeParams { + range: 29..42, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 30..41, + name: Identifier { + id: "T", + range: 30..31, + }, + bound: None, + default: Some( + Yield( + ExprYield { + range: 34..41, + value: Some( + Name( + ExprName { + range: 40..41, + id: "x", + ctx: Load, + }, + ), + ), + }, + ), + ), + }, + ), + ], + }, + ), + value: Name( + ExprName { + range: 45..48, + id: "int", + ctx: Load, + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 49..76, + name: Name( + ExprName { + range: 54..55, + id: "X", + ctx: Store, + }, + ), + type_params: Some( + TypeParams { + range: 55..70, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 56..69, + name: Identifier { + id: "T", + range: 56..57, + }, + bound: None, + default: Some( + Yield( + ExprYield { + range: 61..68, + value: Some( + Name( + ExprName { + range: 67..68, + id: "x", + ctx: Load, + }, + ), + ), + }, + ), + ), + }, + ), + ], + }, + ), + value: Name( + ExprName { + range: 73..76, + id: "int", + ctx: Load, + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 77..107, + name: Name( + ExprName { + range: 82..83, + id: "X", + ctx: Store, + }, + ), + type_params: Some( + TypeParams { + range: 83..101, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 84..100, + name: Identifier { + id: "T", + range: 84..85, + }, + bound: None, + default: Some( + YieldFrom( + ExprYieldFrom { + range: 88..100, + value: Name( + ExprName { + range: 99..100, + id: "x", + ctx: Load, + }, + ), + }, + ), + ), + }, + ), + ], + }, + ), + value: Name( + ExprName { + range: 104..107, + id: "int", + ctx: Load, + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 108..134, + name: Name( + ExprName { + range: 113..114, + id: "X", + ctx: Store, + }, + ), + type_params: Some( + TypeParams { + range: 114..128, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 115..120, + name: Identifier { + id: "T", + range: 115..116, + }, + bound: None, + default: Some( + Name( + ExprName { + range: 119..120, + id: "x", + ctx: Load, + }, + ), + ), + }, + ), + TypeVar( + TypeParamTypeVar { + range: 124..127, + name: Identifier { + id: "int", + range: 124..127, + }, + bound: None, + default: None, + }, + ), + ], + }, + ), + value: Name( + ExprName { + range: 131..134, + id: "int", + ctx: Load, + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 135..162, + name: Name( + ExprName { + range: 140..141, + id: "X", + ctx: Store, + }, + ), + type_params: Some( + TypeParams { + range: 141..156, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 142..155, + name: Identifier { + id: "T", + range: 142..143, + }, + bound: Some( + Name( + ExprName { + range: 145..148, + id: "int", + ctx: Load, + }, + ), + ), + default: Some( + Starred( + ExprStarred { + range: 151..155, + value: Name( + ExprName { + range: 152..155, + id: "int", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ), + }, + ), + ], + }, + ), + value: Name( + ExprName { + range: 159..162, + id: "int", + ctx: Load, + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | type X[T = *int] = int + | ^^^^ Syntax Error: Starred expression cannot be used here +2 | type X[T = yield x] = int +3 | type X[T = (yield x)] = int + | + + + | +1 | type X[T = *int] = int +2 | type X[T = yield x] = int + | ^^^^^^^ Syntax Error: Yield expression cannot be used here +3 | type X[T = (yield x)] = int +4 | type X[T = yield from x] = int + | + + + | +2 | type X[T = yield x] = int +3 | type X[T = (yield x)] = int +4 | type X[T = yield from x] = int + | ^^^^^^^^^^^^ Syntax Error: Yield expression cannot be used here +5 | type X[T = x := int] = int +6 | type X[T: int = *int] = int + | + + + | +3 | type X[T = (yield x)] = int +4 | type X[T = yield from x] = int +5 | type X[T = x := int] = int + | ^^ Syntax Error: Expected ',', found ':=' +6 | type X[T: int = *int] = int + | + + + | +4 | type X[T = yield from x] = int +5 | type X[T = x := int] = int +6 | type X[T: int = *int] = int + | ^^^^ Syntax Error: Starred expression cannot be used here + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@type_param_type_var_missing_default.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@type_param_type_var_missing_default.py.snap.new new file mode 100644 index 0000000000..9eebfb0e0a --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@type_param_type_var_missing_default.py.snap.new @@ -0,0 +1,170 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/type_param_type_var_missing_default.py +--- +## AST + +``` +Module( + ModModule { + range: 0..64, + body: [ + TypeAlias( + StmtTypeAlias { + range: 0..17, + name: Name( + ExprName { + range: 5..6, + id: "X", + ctx: Store, + }, + ), + type_params: Some( + TypeParams { + range: 6..11, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 7..10, + name: Identifier { + id: "T", + range: 7..8, + }, + bound: None, + default: None, + }, + ), + ], + }, + ), + value: Name( + ExprName { + range: 14..17, + id: "int", + ctx: Load, + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 18..40, + name: Name( + ExprName { + range: 23..24, + id: "X", + ctx: Store, + }, + ), + type_params: Some( + TypeParams { + range: 24..34, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 25..33, + name: Identifier { + id: "T", + range: 25..26, + }, + bound: Some( + Name( + ExprName { + range: 28..31, + id: "int", + ctx: Load, + }, + ), + ), + default: None, + }, + ), + ], + }, + ), + value: Name( + ExprName { + range: 37..40, + id: "int", + ctx: Load, + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 41..63, + name: Name( + ExprName { + range: 46..47, + id: "X", + ctx: Store, + }, + ), + type_params: Some( + TypeParams { + range: 47..57, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 48..52, + name: Identifier { + id: "T1", + range: 48..50, + }, + bound: None, + default: None, + }, + ), + TypeVar( + TypeParamTypeVar { + range: 54..56, + name: Identifier { + id: "T2", + range: 54..56, + }, + bound: None, + default: None, + }, + ), + ], + }, + ), + value: Name( + ExprName { + range: 60..63, + id: "int", + ctx: Load, + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | type X[T =] = int + | ^ Syntax Error: Expected an expression +2 | type X[T: int =] = int +3 | type X[T1 =, T2] = int + | + + + | +1 | type X[T =] = int +2 | type X[T: int =] = int + | ^ Syntax Error: Expected an expression +3 | type X[T1 =, T2] = int + | + + + | +1 | type X[T =] = int +2 | type X[T: int =] = int +3 | type X[T1 =, T2] = int + | ^ Syntax Error: Expected an expression + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@type_param_type_var_tuple_bound.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@type_param_type_var_tuple_bound.py.snap.new new file mode 100644 index 0000000000..123a0fa134 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@type_param_type_var_tuple_bound.py.snap.new @@ -0,0 +1,94 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/type_param_type_var_tuple_bound.py +--- +## AST + +``` +Module( + ModModule { + range: 0..22, + body: [ + TypeAlias( + StmtTypeAlias { + range: 0..9, + name: Name( + ExprName { + range: 5..6, + id: "X", + ctx: Store, + }, + ), + type_params: Some( + TypeParams { + range: 6..9, + type_params: [ + TypeVarTuple( + TypeParamTypeVarTuple { + range: 7..9, + name: Identifier { + id: "T", + range: 8..9, + }, + default: None, + }, + ), + ], + }, + ), + value: Name( + ExprName { + range: 9..9, + id: "", + ctx: Invalid, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 11..14, + value: Name( + ExprName { + range: 11..14, + id: "int", + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 18..21, + value: Name( + ExprName { + range: 18..21, + id: "int", + ctx: Load, + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | type X[*T: int] = int + | ^ Syntax Error: Expected ']', found ':' + | + + + | +1 | type X[*T: int] = int + | ^ Syntax Error: Expected a statement + | + + + | +1 | type X[*T: int] = int + | ^ Syntax Error: Expected a statement + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@type_param_type_var_tuple_invalid_default_expr.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@type_param_type_var_tuple_invalid_default_expr.py.snap.new new file mode 100644 index 0000000000..38d65d9f42 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@type_param_type_var_tuple_invalid_default_expr.py.snap.new @@ -0,0 +1,322 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/type_param_type_var_tuple_invalid_default_expr.py +--- +## AST + +``` +Module( + ModModule { + range: 0..147, + body: [ + TypeAlias( + StmtTypeAlias { + range: 0..24, + name: Name( + ExprName { + range: 5..6, + id: "X", + ctx: Store, + }, + ), + type_params: Some( + TypeParams { + range: 6..18, + type_params: [ + TypeVarTuple( + TypeParamTypeVarTuple { + range: 7..17, + name: Identifier { + id: "Ts", + range: 8..10, + }, + default: Some( + Starred( + ExprStarred { + range: 13..17, + value: Name( + ExprName { + range: 14..17, + id: "int", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ), + }, + ), + ], + }, + ), + value: Name( + ExprName { + range: 21..24, + id: "int", + ctx: Load, + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 25..56, + name: Name( + ExprName { + range: 30..31, + id: "X", + ctx: Store, + }, + ), + type_params: Some( + TypeParams { + range: 31..50, + type_params: [ + TypeVarTuple( + TypeParamTypeVarTuple { + range: 32..49, + name: Identifier { + id: "Ts", + range: 33..35, + }, + default: Some( + Starred( + ExprStarred { + range: 38..49, + value: BoolOp( + ExprBoolOp { + range: 39..49, + op: Or, + values: [ + Name( + ExprName { + range: 39..42, + id: "int", + ctx: Load, + }, + ), + Name( + ExprName { + range: 46..49, + id: "str", + ctx: Load, + }, + ), + ], + }, + ), + ctx: Load, + }, + ), + ), + }, + ), + ], + }, + ), + value: Name( + ExprName { + range: 53..56, + id: "int", + ctx: Load, + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 57..84, + name: Name( + ExprName { + range: 62..63, + id: "X", + ctx: Store, + }, + ), + type_params: Some( + TypeParams { + range: 63..78, + type_params: [ + TypeVarTuple( + TypeParamTypeVarTuple { + range: 64..77, + name: Identifier { + id: "Ts", + range: 65..67, + }, + default: Some( + Yield( + ExprYield { + range: 70..77, + value: Some( + Name( + ExprName { + range: 76..77, + id: "x", + ctx: Load, + }, + ), + ), + }, + ), + ), + }, + ), + ], + }, + ), + value: Name( + ExprName { + range: 81..84, + id: "int", + ctx: Load, + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 85..117, + name: Name( + ExprName { + range: 90..91, + id: "X", + ctx: Store, + }, + ), + type_params: Some( + TypeParams { + range: 91..111, + type_params: [ + TypeVarTuple( + TypeParamTypeVarTuple { + range: 92..110, + name: Identifier { + id: "Ts", + range: 93..95, + }, + default: Some( + YieldFrom( + ExprYieldFrom { + range: 98..110, + value: Name( + ExprName { + range: 109..110, + id: "x", + ctx: Load, + }, + ), + }, + ), + ), + }, + ), + ], + }, + ), + value: Name( + ExprName { + range: 114..117, + id: "int", + ctx: Load, + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 118..146, + name: Name( + ExprName { + range: 123..124, + id: "X", + ctx: Store, + }, + ), + type_params: Some( + TypeParams { + range: 124..140, + type_params: [ + TypeVarTuple( + TypeParamTypeVarTuple { + range: 125..132, + name: Identifier { + id: "Ts", + range: 126..128, + }, + default: Some( + Name( + ExprName { + range: 131..132, + id: "x", + ctx: Load, + }, + ), + ), + }, + ), + TypeVar( + TypeParamTypeVar { + range: 136..139, + name: Identifier { + id: "int", + range: 136..139, + }, + bound: None, + default: None, + }, + ), + ], + }, + ), + value: Name( + ExprName { + range: 143..146, + id: "int", + ctx: Load, + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | type X[*Ts = *int] = int +2 | type X[*Ts = *int or str] = int + | ^^^^^^^^^^ Syntax Error: Boolean expression cannot be used here +3 | type X[*Ts = yield x] = int +4 | type X[*Ts = yield from x] = int + | + + + | +1 | type X[*Ts = *int] = int +2 | type X[*Ts = *int or str] = int +3 | type X[*Ts = yield x] = int + | ^^^^^^^ Syntax Error: Yield expression cannot be used here +4 | type X[*Ts = yield from x] = int +5 | type X[*Ts = x := int] = int + | + + + | +2 | type X[*Ts = *int or str] = int +3 | type X[*Ts = yield x] = int +4 | type X[*Ts = yield from x] = int + | ^^^^^^^^^^^^ Syntax Error: Yield expression cannot be used here +5 | type X[*Ts = x := int] = int + | + + + | +3 | type X[*Ts = yield x] = int +4 | type X[*Ts = yield from x] = int +5 | type X[*Ts = x := int] = int + | ^^ Syntax Error: Expected ',', found ':=' + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@type_param_type_var_tuple_missing_default.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@type_param_type_var_tuple_missing_default.py.snap.new new file mode 100644 index 0000000000..a5c151e6dd --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@type_param_type_var_tuple_missing_default.py.snap.new @@ -0,0 +1,113 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/type_param_type_var_tuple_missing_default.py +--- +## AST + +``` +Module( + ModModule { + range: 0..44, + body: [ + TypeAlias( + StmtTypeAlias { + range: 0..19, + name: Name( + ExprName { + range: 5..6, + id: "X", + ctx: Store, + }, + ), + type_params: Some( + TypeParams { + range: 6..13, + type_params: [ + TypeVarTuple( + TypeParamTypeVarTuple { + range: 7..12, + name: Identifier { + id: "Ts", + range: 8..10, + }, + default: None, + }, + ), + ], + }, + ), + value: Name( + ExprName { + range: 16..19, + id: "int", + ctx: Load, + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 20..43, + name: Name( + ExprName { + range: 25..26, + id: "X", + ctx: Store, + }, + ), + type_params: Some( + TypeParams { + range: 26..37, + type_params: [ + TypeVarTuple( + TypeParamTypeVarTuple { + range: 27..32, + name: Identifier { + id: "Ts", + range: 28..30, + }, + default: None, + }, + ), + TypeVar( + TypeParamTypeVar { + range: 34..36, + name: Identifier { + id: "T2", + range: 34..36, + }, + bound: None, + default: None, + }, + ), + ], + }, + ), + value: Name( + ExprName { + range: 40..43, + id: "int", + ctx: Load, + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | type X[*Ts =] = int + | ^ Syntax Error: Expected an expression +2 | type X[*Ts =, T2] = int + | + + + | +1 | type X[*Ts =] = int +2 | type X[*Ts =, T2] = int + | ^ Syntax Error: Expected an expression + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@type_params_empty.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@type_params_empty.py.snap.new new file mode 100644 index 0000000000..420665cc1e --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@type_params_empty.py.snap.new @@ -0,0 +1,103 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/type_params_empty.py +--- +## AST + +``` +Module( + ModModule { + range: 0..52, + body: [ + FunctionDef( + StmtFunctionDef { + range: 0..21, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 4..7, + }, + type_params: Some( + TypeParams { + range: 7..9, + type_params: [], + }, + ), + parameters: Parameters { + range: 9..11, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 17..21, + }, + ), + ], + }, + ), + TypeAlias( + StmtTypeAlias { + range: 22..51, + name: Name( + ExprName { + range: 27..36, + id: "ListOrSet", + ctx: Store, + }, + ), + type_params: Some( + TypeParams { + range: 36..38, + type_params: [], + }, + ), + value: BinOp( + ExprBinOp { + range: 41..51, + left: Name( + ExprName { + range: 41..45, + id: "list", + ctx: Load, + }, + ), + op: BitOr, + right: Name( + ExprName { + range: 48..51, + id: "set", + ctx: Load, + }, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | def foo[](): + | ^ Syntax Error: Type parameter list cannot be empty +2 | pass +3 | type ListOrSet[] = list | set + | + + + | +1 | def foo[](): +2 | pass +3 | type ListOrSet[] = list | set + | ^ Syntax Error: Type parameter list cannot be empty + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@unterminated_fstring_newline_recovery.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@unterminated_fstring_newline_recovery.py.snap.new new file mode 100644 index 0000000000..104a9827ce --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@unterminated_fstring_newline_recovery.py.snap.new @@ -0,0 +1,361 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/unterminated_fstring_newline_recovery.py +--- +## AST + +``` +Module( + ModModule { + range: 0..67, + body: [ + Expr( + StmtExpr { + range: 0..7, + value: FString( + ExprFString { + range: 0..7, + value: FStringValue { + inner: Single( + FString( + FString { + range: 0..7, + elements: [], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 8..13, + value: BinOp( + ExprBinOp { + range: 8..13, + left: NumberLiteral( + ExprNumberLiteral { + range: 8..9, + value: Int( + 1, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 12..13, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 14..24, + value: FString( + ExprFString { + range: 14..24, + value: FStringValue { + inner: Single( + FString( + FString { + range: 14..24, + elements: [ + Literal( + FStringLiteralElement { + range: 16..22, + value: "hello ", + }, + ), + Expression( + FStringExpressionElement { + range: 22..24, + expression: Name( + ExprName { + range: 23..24, + id: "x", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 25..30, + value: BinOp( + ExprBinOp { + range: 25..30, + left: NumberLiteral( + ExprNumberLiteral { + range: 25..26, + value: Int( + 2, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 29..30, + value: Int( + 2, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 31..42, + value: FString( + ExprFString { + range: 31..42, + value: FStringValue { + inner: Single( + FString( + FString { + range: 31..42, + elements: [ + Literal( + FStringLiteralElement { + range: 33..39, + value: "hello ", + }, + ), + Expression( + FStringExpressionElement { + range: 39..42, + expression: Name( + ExprName { + range: 40..41, + id: "x", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: Some( + FStringFormatSpec { + range: 42..42, + elements: [], + }, + ), + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 43..48, + value: BinOp( + ExprBinOp { + range: 43..48, + left: NumberLiteral( + ExprNumberLiteral { + range: 43..44, + value: Int( + 3, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 47..48, + value: Int( + 3, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 49..60, + value: FString( + ExprFString { + range: 49..60, + value: FStringValue { + inner: Single( + FString( + FString { + range: 49..60, + elements: [ + Literal( + FStringLiteralElement { + range: 51..57, + value: "hello ", + }, + ), + Expression( + FStringExpressionElement { + range: 57..60, + expression: Name( + ExprName { + range: 58..59, + id: "x", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 61..66, + value: BinOp( + ExprBinOp { + range: 61..66, + left: NumberLiteral( + ExprNumberLiteral { + range: 61..62, + value: Int( + 4, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 65..66, + value: Int( + 4, + ), + }, + ), + }, + ), + }, + ), + ], + }, +) +``` +## Errors + + | +1 | f"hello + | ^^^^^ Syntax Error: f-string: unterminated string +2 | 1 + 1 +3 | f"hello {x + | + + + | +1 | f"hello + | ^ Syntax Error: Expected FStringEnd, found newline +2 | 1 + 1 +3 | f"hello {x +4 | 2 + 2 + | + + + | +1 | f"hello +2 | 1 + 1 +3 | f"hello {x + | Syntax Error: f-string: unterminated string +4 | 2 + 2 +5 | f"hello {x: + | + + + | +2 | 1 + 1 +3 | f"hello {x +4 | 2 + 2 + | ^ Syntax Error: f-string: expecting '}' +5 | f"hello {x: +6 | 3 + 3 + | + + + | +1 | f"hello +2 | 1 + 1 +3 | f"hello {x + | Syntax Error: Expected FStringEnd, found Unknown +4 | 2 + 2 +5 | f"hello {x: + | + + + | +3 | f"hello {x +4 | 2 + 2 +5 | f"hello {x: + | Syntax Error: f-string: unterminated string +6 | 3 + 3 +7 | f"hello {x} + | + + + | +5 | f"hello {x: +6 | 3 + 3 +7 | f"hello {x} + | Syntax Error: f-string: unterminated string +8 | 4 + 4 + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@while_stmt_invalid_test_expr.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@while_stmt_invalid_test_expr.py.snap.new new file mode 100644 index 0000000000..cfeb864cdf --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@while_stmt_invalid_test_expr.py.snap.new @@ -0,0 +1,192 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/while_stmt_invalid_test_expr.py +--- +## AST + +``` +Module( + ModModule { + range: 0..70, + body: [ + While( + StmtWhile { + range: 0..13, + test: Starred( + ExprStarred { + range: 6..8, + value: Name( + ExprName { + range: 7..8, + id: "x", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + body: [ + Expr( + StmtExpr { + range: 10..13, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 10..13, + }, + ), + }, + ), + ], + orelse: [], + }, + ), + While( + StmtWhile { + range: 14..32, + test: Yield( + ExprYield { + range: 20..27, + value: Some( + Name( + ExprName { + range: 26..27, + id: "x", + ctx: Load, + }, + ), + ), + }, + ), + body: [ + Expr( + StmtExpr { + range: 29..32, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 29..32, + }, + ), + }, + ), + ], + orelse: [], + }, + ), + While( + StmtWhile { + range: 33..40, + test: Name( + ExprName { + range: 39..40, + id: "a", + ctx: Load, + }, + ), + body: [], + orelse: [], + }, + ), + AnnAssign( + StmtAnnAssign { + range: 42..48, + target: Name( + ExprName { + range: 42..43, + id: "b", + ctx: Store, + }, + ), + annotation: EllipsisLiteral( + ExprEllipsisLiteral { + range: 45..48, + }, + ), + value: None, + simple: true, + }, + ), + While( + StmtWhile { + range: 49..61, + test: Named( + ExprNamed { + range: 55..61, + target: Name( + ExprName { + range: 55..56, + id: "a", + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 60..61, + value: Int( + 1, + ), + }, + ), + }, + ), + body: [], + orelse: [], + }, + ), + AnnAssign( + StmtAnnAssign { + range: 63..69, + target: Name( + ExprName { + range: 63..64, + id: "b", + ctx: Store, + }, + ), + annotation: EllipsisLiteral( + ExprEllipsisLiteral { + range: 66..69, + }, + ), + value: None, + simple: true, + }, + ), + ], + }, +) +``` +## Errors + + | +1 | while *x: ... + | ^^ Syntax Error: Starred expression cannot be used here +2 | while yield x: ... +3 | while a, b: ... + | + + + | +1 | while *x: ... +2 | while yield x: ... + | ^^^^^^^ Syntax Error: Yield expression cannot be used here +3 | while a, b: ... +4 | while a := 1, b: ... + | + + + | +1 | while *x: ... +2 | while yield x: ... +3 | while a, b: ... + | ^ Syntax Error: Expected ':', found ',' +4 | while a := 1, b: ... + | + + + | +2 | while yield x: ... +3 | while a, b: ... +4 | while a := 1, b: ... + | ^ Syntax Error: Expected ':', found ',' + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@while_stmt_missing_colon.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@while_stmt_missing_colon.py.snap.new new file mode 100644 index 0000000000..6c83c59bca --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@while_stmt_missing_colon.py.snap.new @@ -0,0 +1,63 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/while_stmt_missing_colon.py +--- +## AST + +``` +Module( + ModModule { + range: 0..40, + body: [ + While( + StmtWhile { + range: 0..39, + test: Compare( + ExprCompare { + range: 12..18, + left: Name( + ExprName { + range: 12..13, + id: "a", + ctx: Load, + }, + ), + ops: [ + Lt, + ], + comparators: [ + NumberLiteral( + ExprNumberLiteral { + range: 16..18, + value: Int( + 30, + ), + }, + ), + ], + }, + ), + body: [ + Pass( + StmtPass { + range: 35..39, + }, + ), + ], + orelse: [], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | while ( +2 | a < 30 # comment +3 | ) + | ^ Syntax Error: Expected ':', found newline +4 | pass + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@while_stmt_missing_test.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@while_stmt_missing_test.py.snap.new new file mode 100644 index 0000000000..46a5434907 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@while_stmt_missing_test.py.snap.new @@ -0,0 +1,94 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/while_stmt_missing_test.py +--- +## AST + +``` +Module( + ModModule { + range: 0..30, + body: [ + While( + StmtWhile { + range: 0..11, + test: Name( + ExprName { + range: 5..5, + id: "", + ctx: Invalid, + }, + ), + body: [ + Expr( + StmtExpr { + range: 8..11, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 8..11, + }, + ), + }, + ), + ], + orelse: [], + }, + ), + While( + StmtWhile { + range: 12..29, + test: Name( + ExprName { + range: 17..17, + id: "", + ctx: Invalid, + }, + ), + body: [ + Assign( + StmtAssign { + range: 24..29, + targets: [ + Name( + ExprName { + range: 24..25, + id: "a", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 28..29, + value: Int( + 1, + ), + }, + ), + }, + ), + ], + orelse: [], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | while : ... + | ^ Syntax Error: Expected an expression +2 | while : +3 | a = 1 + | + + + | +1 | while : ... +2 | while : + | ^ Syntax Error: Expected an expression +3 | a = 1 + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@with_items_parenthesized_missing_colon.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@with_items_parenthesized_missing_colon.py.snap.new new file mode 100644 index 0000000000..d0387be628 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@with_items_parenthesized_missing_colon.py.snap.new @@ -0,0 +1,61 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/with_items_parenthesized_missing_colon.py +--- +## AST + +``` +Module( + ModModule { + range: 0..57, + body: [ + With( + StmtWith { + range: 28..56, + is_async: false, + items: [ + WithItem { + range: 34..39, + context_expr: Name( + ExprName { + range: 34..39, + id: "item1", + ctx: Load, + }, + ), + optional_vars: None, + }, + WithItem { + range: 41..46, + context_expr: Name( + ExprName { + range: 41..46, + id: "item2", + ctx: Load, + }, + ), + optional_vars: None, + }, + ], + body: [ + Pass( + StmtPass { + range: 52..56, + }, + ), + ], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | # `)` followed by a newline +2 | with (item1, item2) + | ^ Syntax Error: Expected ':', found newline +3 | pass + | diff --git a/crates/ruff_python_parser/tests/snapshots/invalid_syntax@with_items_parenthesized_missing_comma.py.snap.new b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@with_items_parenthesized_missing_comma.py.snap.new new file mode 100644 index 0000000000..c888be366f --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/invalid_syntax@with_items_parenthesized_missing_comma.py.snap.new @@ -0,0 +1,335 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 122 +input_file: crates/ruff_python_parser/resources/inline/err/with_items_parenthesized_missing_comma.py +--- +## AST + +``` +Module( + ModModule { + range: 0..160, + body: [ + With( + StmtWith { + range: 0..23, + is_async: false, + items: [ + WithItem { + range: 6..11, + context_expr: Name( + ExprName { + range: 6..11, + id: "item1", + ctx: Load, + }, + ), + optional_vars: None, + }, + WithItem { + range: 12..17, + context_expr: Name( + ExprName { + range: 12..17, + id: "item2", + ctx: Load, + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 20..23, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 20..23, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 24..53, + is_async: false, + items: [ + WithItem { + range: 30..41, + context_expr: Name( + ExprName { + range: 30..35, + id: "item1", + ctx: Load, + }, + ), + optional_vars: Some( + Name( + ExprName { + range: 39..41, + id: "f1", + ctx: Store, + }, + ), + ), + }, + WithItem { + range: 42..47, + context_expr: Name( + ExprName { + range: 42..47, + id: "item2", + ctx: Load, + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 50..53, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 50..53, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 54..91, + is_async: false, + items: [ + WithItem { + range: 60..65, + context_expr: Name( + ExprName { + range: 60..65, + id: "item1", + ctx: Load, + }, + ), + optional_vars: None, + }, + WithItem { + range: 67..72, + context_expr: Name( + ExprName { + range: 67..72, + id: "item2", + ctx: Load, + }, + ), + optional_vars: None, + }, + WithItem { + range: 73..78, + context_expr: Name( + ExprName { + range: 73..78, + id: "item3", + ctx: Load, + }, + ), + optional_vars: None, + }, + WithItem { + range: 80..85, + context_expr: Name( + ExprName { + range: 80..85, + id: "item4", + ctx: Load, + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 88..91, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 88..91, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 92..135, + is_async: false, + items: [ + WithItem { + range: 98..103, + context_expr: Name( + ExprName { + range: 98..103, + id: "item1", + ctx: Load, + }, + ), + optional_vars: None, + }, + WithItem { + range: 105..116, + context_expr: Name( + ExprName { + range: 105..110, + id: "item2", + ctx: Load, + }, + ), + optional_vars: Some( + Name( + ExprName { + range: 114..116, + id: "f1", + ctx: Store, + }, + ), + ), + }, + WithItem { + range: 117..122, + context_expr: Name( + ExprName { + range: 117..122, + id: "item3", + ctx: Load, + }, + ), + optional_vars: None, + }, + WithItem { + range: 124..129, + context_expr: Name( + ExprName { + range: 124..129, + id: "item4", + ctx: Load, + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 132..135, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 132..135, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 136..159, + is_async: false, + items: [ + WithItem { + range: 141..154, + context_expr: Tuple( + ExprTuple { + range: 141..154, + elts: [ + Name( + ExprName { + range: 142..147, + id: "item1", + ctx: Load, + }, + ), + Name( + ExprName { + range: 149..154, + id: "item2", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 156..159, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 156..159, + }, + ), + }, + ), + ], + }, + ), + ], + }, +) +``` +## Errors + + | +1 | with (item1 item2): ... + | ^^^^^ Syntax Error: Expected ',', found name +2 | with (item1 as f1 item2): ... +3 | with (item1, item2 item3, item4): ... + | + + + | +1 | with (item1 item2): ... +2 | with (item1 as f1 item2): ... + | ^^^^^ Syntax Error: Expected ',', found name +3 | with (item1, item2 item3, item4): ... +4 | with (item1, item2 as f1 item3, item4): ... + | + + + | +1 | with (item1 item2): ... +2 | with (item1 as f1 item2): ... +3 | with (item1, item2 item3, item4): ... + | ^^^^^ Syntax Error: Expected ',', found name +4 | with (item1, item2 as f1 item3, item4): ... +5 | with (item1, item2: ... + | + + + | +2 | with (item1 as f1 item2): ... +3 | with (item1, item2 item3, item4): ... +4 | with (item1, item2 as f1 item3, item4): ... + | ^^^^^ Syntax Error: Expected ',', found name +5 | with (item1, item2: ... + | + + + | +3 | with (item1, item2 item3, item4): ... +4 | with (item1, item2 as f1 item3, item4): ... +5 | with (item1, item2: ... + | ^ Syntax Error: Expected ')', found ':' + | diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@ambiguous_lpar_with_items_binary_expr.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@ambiguous_lpar_with_items_binary_expr.py.snap.new new file mode 100644 index 0000000000..461882ce41 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@ambiguous_lpar_with_items_binary_expr.py.snap.new @@ -0,0 +1,373 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/inline/ok/ambiguous_lpar_with_items_binary_expr.py +--- +## AST + +``` +Module( + ModModule { + range: 0..337, + body: [ + With( + StmtWith { + range: 124..143, + is_async: false, + items: [ + WithItem { + range: 129..138, + context_expr: BoolOp( + ExprBoolOp { + range: 129..138, + op: And, + values: [ + Name( + ExprName { + range: 130..131, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 137..138, + id: "b", + ctx: Load, + }, + ), + ], + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 140..143, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 140..143, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 144..166, + is_async: false, + items: [ + WithItem { + range: 149..161, + context_expr: Compare( + ExprCompare { + range: 149..161, + left: Name( + ExprName { + range: 150..151, + id: "a", + ctx: Load, + }, + ), + ops: [ + IsNot, + ], + comparators: [ + Name( + ExprName { + range: 160..161, + id: "b", + ctx: Load, + }, + ), + ], + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 163..166, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 163..166, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 196..220, + is_async: false, + items: [ + WithItem { + range: 201..215, + context_expr: BoolOp( + ExprBoolOp { + range: 201..215, + op: Or, + values: [ + Name( + ExprName { + range: 202..203, + id: "a", + ctx: Load, + }, + ), + BoolOp( + ExprBoolOp { + range: 208..215, + op: And, + values: [ + Name( + ExprName { + range: 208..209, + id: "b", + ctx: Load, + }, + ), + Name( + ExprName { + range: 214..215, + id: "c", + ctx: Load, + }, + ), + ], + }, + ), + ], + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 217..220, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 217..220, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 221..245, + is_async: false, + items: [ + WithItem { + range: 226..240, + context_expr: BoolOp( + ExprBoolOp { + range: 226..240, + op: Or, + values: [ + BoolOp( + ExprBoolOp { + range: 226..235, + op: And, + values: [ + Name( + ExprName { + range: 227..228, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 234..235, + id: "b", + ctx: Load, + }, + ), + ], + }, + ), + Name( + ExprName { + range: 239..240, + id: "c", + ctx: Load, + }, + ), + ], + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 242..245, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 242..245, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 246..272, + is_async: false, + items: [ + WithItem { + range: 251..267, + context_expr: BinOp( + ExprBinOp { + range: 251..267, + left: BinOp( + ExprBinOp { + range: 251..263, + left: BinOp( + ExprBinOp { + range: 252..257, + left: Name( + ExprName { + range: 252..253, + id: "a", + ctx: Load, + }, + ), + op: BitOr, + right: Name( + ExprName { + range: 256..257, + id: "b", + ctx: Load, + }, + ), + }, + ), + op: LShift, + right: Name( + ExprName { + range: 262..263, + id: "c", + ctx: Load, + }, + ), + }, + ), + op: BitOr, + right: Name( + ExprName { + range: 266..267, + id: "d", + ctx: Load, + }, + ), + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 269..272, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 269..272, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 312..336, + is_async: false, + items: [ + WithItem { + range: 317..331, + context_expr: BinOp( + ExprBinOp { + range: 317..331, + left: Subscript( + ExprSubscript { + range: 317..323, + value: Name( + ExprName { + range: 318..319, + id: "a", + ctx: Load, + }, + ), + slice: NumberLiteral( + ExprNumberLiteral { + range: 321..322, + value: Int( + 0, + ), + }, + ), + ctx: Load, + }, + ), + op: Add, + right: BinOp( + ExprBinOp { + range: 326..331, + left: Name( + ExprName { + range: 326..327, + id: "b", + ctx: Load, + }, + ), + op: Mult, + right: Name( + ExprName { + range: 330..331, + id: "c", + ctx: Load, + }, + ), + }, + ), + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 333..336, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 333..336, + }, + ), + }, + ), + ], + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@ambiguous_lpar_with_items_if_expr.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@ambiguous_lpar_with_items_if_expr.py.snap.new new file mode 100644 index 0000000000..4cee4c7fe8 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@ambiguous_lpar_with_items_if_expr.py.snap.new @@ -0,0 +1,280 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/inline/ok/ambiguous_lpar_with_items_if_expr.py +--- +## AST + +``` +Module( + ModModule { + range: 0..153, + body: [ + With( + StmtWith { + range: 0..28, + is_async: false, + items: [ + WithItem { + range: 5..23, + context_expr: If( + ExprIf { + range: 5..23, + test: BooleanLiteral( + ExprBooleanLiteral { + range: 12..16, + value: true, + }, + ), + body: Name( + ExprName { + range: 6..7, + id: "x", + ctx: Load, + }, + ), + orelse: Name( + ExprName { + range: 22..23, + id: "y", + ctx: Load, + }, + ), + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 25..28, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 25..28, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 29..71, + is_async: false, + items: [ + WithItem { + range: 34..66, + context_expr: If( + ExprIf { + range: 34..66, + test: BooleanLiteral( + ExprBooleanLiteral { + range: 55..59, + value: true, + }, + ), + body: Generator( + ExprGenerator { + range: 34..51, + elt: Name( + ExprName { + range: 35..36, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 37..50, + target: Name( + ExprName { + range: 41..42, + id: "x", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 46..50, + id: "iter", + ctx: Load, + }, + ), + ifs: [], + is_async: false, + }, + ], + parenthesized: true, + }, + ), + orelse: Name( + ExprName { + range: 65..66, + id: "y", + ctx: Load, + }, + ), + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 68..71, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 68..71, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 72..120, + is_async: false, + items: [ + WithItem { + range: 77..115, + context_expr: If( + ExprIf { + range: 77..115, + test: BooleanLiteral( + ExprBooleanLiteral { + range: 104..108, + value: true, + }, + ), + body: Generator( + ExprGenerator { + range: 77..100, + elt: Name( + ExprName { + range: 78..79, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 80..99, + target: Name( + ExprName { + range: 90..91, + id: "x", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 95..99, + id: "iter", + ctx: Load, + }, + ), + ifs: [], + is_async: true, + }, + ], + parenthesized: true, + }, + ), + orelse: Name( + ExprName { + range: 114..115, + id: "y", + ctx: Load, + }, + ), + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 117..120, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 117..120, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 121..152, + is_async: false, + items: [ + WithItem { + range: 126..147, + context_expr: If( + ExprIf { + range: 126..147, + test: BooleanLiteral( + ExprBooleanLiteral { + range: 136..140, + value: true, + }, + ), + body: Subscript( + ExprSubscript { + range: 126..132, + value: Name( + ExprName { + range: 127..128, + id: "x", + ctx: Load, + }, + ), + slice: NumberLiteral( + ExprNumberLiteral { + range: 130..131, + value: Int( + 0, + ), + }, + ), + ctx: Load, + }, + ), + orelse: Name( + ExprName { + range: 146..147, + id: "y", + ctx: Load, + }, + ), + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 149..152, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 149..152, + }, + ), + }, + ), + ], + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@ann_assign_stmt_simple_target.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@ann_assign_stmt_simple_target.py.snap.new new file mode 100644 index 0000000000..2f2d5ca177 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@ann_assign_stmt_simple_target.py.snap.new @@ -0,0 +1,124 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/inline/ok/ann_assign_stmt_simple_target.py +--- +## AST + +``` +Module( + ModModule { + range: 0..45, + body: [ + AnnAssign( + StmtAnnAssign { + range: 0..6, + target: Name( + ExprName { + range: 0..1, + id: "a", + ctx: Store, + }, + ), + annotation: Name( + ExprName { + range: 3..6, + id: "int", + ctx: Load, + }, + ), + value: None, + simple: true, + }, + ), + AnnAssign( + StmtAnnAssign { + range: 17..25, + target: Name( + ExprName { + range: 18..19, + id: "a", + ctx: Store, + }, + ), + annotation: Name( + ExprName { + range: 22..25, + id: "int", + ctx: Load, + }, + ), + value: None, + simple: false, + }, + ), + AnnAssign( + StmtAnnAssign { + range: 26..34, + target: Attribute( + ExprAttribute { + range: 26..29, + value: Name( + ExprName { + range: 26..27, + id: "a", + ctx: Load, + }, + ), + attr: Identifier { + id: "b", + range: 28..29, + }, + ctx: Store, + }, + ), + annotation: Name( + ExprName { + range: 31..34, + id: "int", + ctx: Load, + }, + ), + value: None, + simple: false, + }, + ), + AnnAssign( + StmtAnnAssign { + range: 35..44, + target: Subscript( + ExprSubscript { + range: 35..39, + value: Name( + ExprName { + range: 35..36, + id: "a", + ctx: Load, + }, + ), + slice: NumberLiteral( + ExprNumberLiteral { + range: 37..38, + value: Int( + 0, + ), + }, + ), + ctx: Store, + }, + ), + annotation: Name( + ExprName { + range: 41..44, + id: "int", + ctx: Load, + }, + ), + value: None, + simple: false, + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@assign_targets_terminator.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@assign_targets_terminator.py.snap.new new file mode 100644 index 0000000000..60ed452949 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@assign_targets_terminator.py.snap.new @@ -0,0 +1,144 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/inline/ok/assign_targets_terminator.py +--- +## AST + +``` +Module( + ModModule { + range: 0..39, + body: [ + Assign( + StmtAssign { + range: 0..13, + targets: [ + Name( + ExprName { + range: 0..1, + id: "x", + ctx: Store, + }, + ), + Name( + ExprName { + range: 4..5, + id: "y", + ctx: Store, + }, + ), + Name( + ExprName { + range: 8..9, + id: "z", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 12..13, + value: Int( + 1, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 15..19, + value: Tuple( + ExprTuple { + range: 15..19, + elts: [ + Name( + ExprName { + range: 15..16, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 18..19, + id: "b", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + }, + ), + Assign( + StmtAssign { + range: 20..33, + targets: [ + Name( + ExprName { + range: 20..21, + id: "x", + ctx: Store, + }, + ), + Name( + ExprName { + range: 24..25, + id: "y", + ctx: Store, + }, + ), + Name( + ExprName { + range: 28..29, + id: "z", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 32..33, + value: Int( + 1, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 34..38, + value: Tuple( + ExprTuple { + range: 34..38, + elts: [ + Name( + ExprName { + range: 34..35, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 37..38, + id: "b", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@async_for_statement.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@async_for_statement.py.snap.new new file mode 100644 index 0000000000..e7733905b6 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@async_for_statement.py.snap.new @@ -0,0 +1,49 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/inline/ok/async_for_statement.py +--- +## AST + +``` +Module( + ModModule { + range: 0..30, + body: [ + For( + StmtFor { + range: 0..29, + is_async: true, + target: Name( + ExprName { + range: 10..16, + id: "target", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 20..24, + id: "iter", + ctx: Load, + }, + ), + body: [ + Expr( + StmtExpr { + range: 26..29, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 26..29, + }, + ), + }, + ), + ], + orelse: [], + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@async_function_definition.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@async_function_definition.py.snap.new new file mode 100644 index 0000000000..fca5112f4d --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@async_function_definition.py.snap.new @@ -0,0 +1,49 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/inline/ok/async_function_definition.py +--- +## AST + +``` +Module( + ModModule { + range: 0..21, + body: [ + FunctionDef( + StmtFunctionDef { + range: 0..20, + is_async: true, + decorator_list: [], + name: Identifier { + id: "foo", + range: 10..13, + }, + type_params: None, + parameters: Parameters { + range: 13..15, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 17..20, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 17..20, + }, + ), + }, + ), + ], + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@async_with_statement.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@async_with_statement.py.snap.new new file mode 100644 index 0000000000..b354c1069b --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@async_with_statement.py.snap.new @@ -0,0 +1,47 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/inline/ok/async_with_statement.py +--- +## AST + +``` +Module( + ModModule { + range: 0..21, + body: [ + With( + StmtWith { + range: 0..20, + is_async: true, + items: [ + WithItem { + range: 11..15, + context_expr: Name( + ExprName { + range: 11..15, + id: "item", + ctx: Load, + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 17..20, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 17..20, + }, + ), + }, + ), + ], + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@class_def_arguments.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@class_def_arguments.py.snap.new new file mode 100644 index 0000000000..caddabb85d --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@class_def_arguments.py.snap.new @@ -0,0 +1,70 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/inline/ok/class_def_arguments.py +--- +## AST + +``` +Module( + ModModule { + range: 0..32, + body: [ + ClassDef( + StmtClassDef { + range: 0..14, + decorator_list: [], + name: Identifier { + id: "Foo", + range: 6..9, + }, + type_params: None, + arguments: None, + body: [ + Expr( + StmtExpr { + range: 11..14, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 11..14, + }, + ), + }, + ), + ], + }, + ), + ClassDef( + StmtClassDef { + range: 15..31, + decorator_list: [], + name: Identifier { + id: "Foo", + range: 21..24, + }, + type_params: None, + arguments: Some( + Arguments { + range: 24..26, + args: [], + keywords: [], + }, + ), + body: [ + Expr( + StmtExpr { + range: 28..31, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 28..31, + }, + ), + }, + ), + ], + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@decorator_async_function.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@decorator_async_function.py.snap.new new file mode 100644 index 0000000000..e70f92620e --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@decorator_async_function.py.snap.new @@ -0,0 +1,60 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/inline/ok/decorator_async_function.py +--- +## AST + +``` +Module( + ModModule { + range: 0..32, + body: [ + FunctionDef( + StmtFunctionDef { + range: 0..31, + is_async: true, + decorator_list: [ + Decorator { + range: 0..10, + expression: Name( + ExprName { + range: 1..10, + id: "decorator", + ctx: Load, + }, + ), + }, + ], + name: Identifier { + id: "foo", + range: 21..24, + }, + type_params: None, + parameters: Parameters { + range: 24..26, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 28..31, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 28..31, + }, + ), + }, + ), + ], + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@del_targets_terminator.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@del_targets_terminator.py.snap.new new file mode 100644 index 0000000000..c90f62c460 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@del_targets_terminator.py.snap.new @@ -0,0 +1,114 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/inline/ok/del_targets_terminator.py +--- +## AST + +``` +Module( + ModModule { + range: 0..29, + body: [ + Delete( + StmtDelete { + range: 0..8, + targets: [ + Name( + ExprName { + range: 4..5, + id: "a", + ctx: Del, + }, + ), + Name( + ExprName { + range: 7..8, + id: "b", + ctx: Del, + }, + ), + ], + }, + ), + Expr( + StmtExpr { + range: 10..14, + value: Tuple( + ExprTuple { + range: 10..14, + elts: [ + Name( + ExprName { + range: 10..11, + id: "c", + ctx: Load, + }, + ), + Name( + ExprName { + range: 13..14, + id: "d", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + }, + ), + Delete( + StmtDelete { + range: 15..23, + targets: [ + Name( + ExprName { + range: 19..20, + id: "a", + ctx: Del, + }, + ), + Name( + ExprName { + range: 22..23, + id: "b", + ctx: Del, + }, + ), + ], + }, + ), + Expr( + StmtExpr { + range: 24..28, + value: Tuple( + ExprTuple { + range: 24..28, + elts: [ + Name( + ExprName { + range: 24..25, + id: "c", + ctx: Load, + }, + ), + Name( + ExprName { + range: 27..28, + id: "d", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@dotted_name_normalized_spaces.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@dotted_name_normalized_spaces.py.snap.new new file mode 100644 index 0000000000..f1c51871da --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@dotted_name_normalized_spaces.py.snap.new @@ -0,0 +1,46 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/inline/ok/dotted_name_normalized_spaces.py +--- +## AST + +``` +Module( + ModModule { + range: 0..32, + body: [ + Import( + StmtImport { + range: 0..12, + names: [ + Alias { + range: 7..12, + name: Identifier { + id: "a.b.c", + range: 7..12, + }, + asname: None, + }, + ], + }, + ), + Import( + StmtImport { + range: 13..31, + names: [ + Alias { + range: 20..31, + name: Identifier { + id: "a.b.c", + range: 20..31, + }, + asname: None, + }, + ], + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@except_stmt_as_name_soft_keyword.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@except_stmt_as_name_soft_keyword.py.snap.new new file mode 100644 index 0000000000..bec5ee7eba --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@except_stmt_as_name_soft_keyword.py.snap.new @@ -0,0 +1,134 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/inline/ok/except_stmt_as_name_soft_keyword.py +--- +## AST + +``` +Module( + ModModule { + range: 0..100, + body: [ + Try( + StmtTry { + range: 0..99, + body: [ + Expr( + StmtExpr { + range: 5..8, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 5..8, + }, + ), + }, + ), + ], + handlers: [ + ExceptHandler( + ExceptHandlerExceptHandler { + range: 9..39, + type_: Some( + Name( + ExprName { + range: 16..25, + id: "Exception", + ctx: Load, + }, + ), + ), + name: Some( + Identifier { + id: "match", + range: 29..34, + }, + ), + body: [ + Expr( + StmtExpr { + range: 36..39, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 36..39, + }, + ), + }, + ), + ], + }, + ), + ExceptHandler( + ExceptHandlerExceptHandler { + range: 40..69, + type_: Some( + Name( + ExprName { + range: 47..56, + id: "Exception", + ctx: Load, + }, + ), + ), + name: Some( + Identifier { + id: "case", + range: 60..64, + }, + ), + body: [ + Expr( + StmtExpr { + range: 66..69, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 66..69, + }, + ), + }, + ), + ], + }, + ), + ExceptHandler( + ExceptHandlerExceptHandler { + range: 70..99, + type_: Some( + Name( + ExprName { + range: 77..86, + id: "Exception", + ctx: Load, + }, + ), + ), + name: Some( + Identifier { + id: "type", + range: 90..94, + }, + ), + body: [ + Expr( + StmtExpr { + range: 96..99, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 96..99, + }, + ), + }, + ), + ], + }, + ), + ], + orelse: [], + finalbody: [], + is_star: false, + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__arguments.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__arguments.py.snap.new new file mode 100644 index 0000000000..0171f63594 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__arguments.py.snap.new @@ -0,0 +1,1726 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/valid/expressions/arguments.py +--- +## AST + +``` +Module( + ModModule { + range: 0..805, + body: [ + Expr( + StmtExpr { + range: 102..108, + value: Call( + ExprCall { + range: 102..108, + func: Name( + ExprName { + range: 102..106, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 106..108, + args: [], + keywords: [], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 109..119, + value: Call( + ExprCall { + range: 109..119, + func: Name( + ExprName { + range: 109..113, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 113..119, + args: [ + Name( + ExprName { + range: 114..115, + id: "x", + ctx: Load, + }, + ), + Name( + ExprName { + range: 117..118, + id: "y", + ctx: Load, + }, + ), + ], + keywords: [], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 120..131, + value: Call( + ExprCall { + range: 120..131, + func: Name( + ExprName { + range: 120..124, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 124..131, + args: [ + Name( + ExprName { + range: 125..126, + id: "x", + ctx: Load, + }, + ), + Name( + ExprName { + range: 128..129, + id: "y", + ctx: Load, + }, + ), + ], + keywords: [], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 150..164, + value: Call( + ExprCall { + range: 150..164, + func: Name( + ExprName { + range: 150..154, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 154..164, + args: [], + keywords: [ + Keyword { + range: 155..158, + arg: Some( + Identifier { + id: "x", + range: 155..156, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 157..158, + value: Int( + 1, + ), + }, + ), + }, + Keyword { + range: 160..163, + arg: Some( + Identifier { + id: "y", + range: 160..161, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 162..163, + value: Int( + 2, + ), + }, + ), + }, + ], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 165..173, + value: Call( + ExprCall { + range: 165..173, + func: Name( + ExprName { + range: 165..169, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 169..173, + args: [ + Starred( + ExprStarred { + range: 170..172, + value: Name( + ExprName { + range: 171..172, + id: "x", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ], + keywords: [], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 174..183, + value: Call( + ExprCall { + range: 174..183, + func: Name( + ExprName { + range: 174..178, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 178..183, + args: [], + keywords: [ + Keyword { + range: 179..182, + arg: None, + value: Name( + ExprName { + range: 181..182, + id: "x", + ctx: Load, + }, + ), + }, + ], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 193..205, + value: Call( + ExprCall { + range: 193..205, + func: Name( + ExprName { + range: 193..197, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 197..205, + args: [ + Name( + ExprName { + range: 198..199, + id: "x", + ctx: Load, + }, + ), + ], + keywords: [ + Keyword { + range: 201..204, + arg: Some( + Identifier { + id: "y", + range: 201..202, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 203..204, + value: Int( + 1, + ), + }, + ), + }, + ], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 206..217, + value: Call( + ExprCall { + range: 206..217, + func: Name( + ExprName { + range: 206..210, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 210..217, + args: [ + Name( + ExprName { + range: 211..212, + id: "x", + ctx: Load, + }, + ), + Starred( + ExprStarred { + range: 214..216, + value: Name( + ExprName { + range: 215..216, + id: "y", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ], + keywords: [], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 218..230, + value: Call( + ExprCall { + range: 218..230, + func: Name( + ExprName { + range: 218..222, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 222..230, + args: [ + Name( + ExprName { + range: 223..224, + id: "x", + ctx: Load, + }, + ), + ], + keywords: [ + Keyword { + range: 226..229, + arg: None, + value: Name( + ExprName { + range: 228..229, + id: "y", + ctx: Load, + }, + ), + }, + ], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 231..244, + value: Call( + ExprCall { + range: 231..244, + func: Name( + ExprName { + range: 231..235, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 235..244, + args: [ + Starred( + ExprStarred { + range: 241..243, + value: Name( + ExprName { + range: 242..243, + id: "y", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ], + keywords: [ + Keyword { + range: 236..239, + arg: Some( + Identifier { + id: "x", + range: 236..237, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 238..239, + value: Int( + 1, + ), + }, + ), + }, + ], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 245..259, + value: Call( + ExprCall { + range: 245..259, + func: Name( + ExprName { + range: 245..249, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 249..259, + args: [], + keywords: [ + Keyword { + range: 250..253, + arg: Some( + Identifier { + id: "x", + range: 250..251, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 252..253, + value: Int( + 1, + ), + }, + ), + }, + Keyword { + range: 255..258, + arg: None, + value: Name( + ExprName { + range: 257..258, + id: "y", + ctx: Load, + }, + ), + }, + ], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 260..273, + value: Call( + ExprCall { + range: 260..273, + func: Name( + ExprName { + range: 260..264, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 264..273, + args: [ + Starred( + ExprStarred { + range: 265..267, + value: Name( + ExprName { + range: 266..267, + id: "x", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ], + keywords: [ + Keyword { + range: 269..272, + arg: None, + value: Name( + ExprName { + range: 271..272, + id: "y", + ctx: Load, + }, + ), + }, + ], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 274..288, + value: Call( + ExprCall { + range: 274..288, + func: Name( + ExprName { + range: 274..278, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 278..288, + args: [ + Starred( + ExprStarred { + range: 279..281, + value: Name( + ExprName { + range: 280..281, + id: "x", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + Name( + ExprName { + range: 283..284, + id: "y", + ctx: Load, + }, + ), + Name( + ExprName { + range: 286..287, + id: "z", + ctx: Load, + }, + ), + ], + keywords: [], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 289..308, + value: Call( + ExprCall { + range: 289..308, + func: Name( + ExprName { + range: 289..293, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 293..308, + args: [], + keywords: [ + Keyword { + range: 294..297, + arg: None, + value: Name( + ExprName { + range: 296..297, + id: "x", + ctx: Load, + }, + ), + }, + Keyword { + range: 299..302, + arg: Some( + Identifier { + id: "y", + range: 299..300, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 301..302, + value: Int( + 1, + ), + }, + ), + }, + Keyword { + range: 304..307, + arg: Some( + Identifier { + id: "z", + range: 304..305, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 306..307, + value: Int( + 2, + ), + }, + ), + }, + ], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 309..335, + value: Call( + ExprCall { + range: 309..335, + func: Name( + ExprName { + range: 309..313, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 313..335, + args: [ + Starred( + ExprStarred { + range: 314..317, + value: Name( + ExprName { + range: 315..317, + id: "x1", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + Starred( + ExprStarred { + range: 319..322, + value: Name( + ExprName { + range: 320..322, + id: "x2", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ], + keywords: [ + Keyword { + range: 324..328, + arg: None, + value: Name( + ExprName { + range: 326..328, + id: "y1", + ctx: Load, + }, + ), + }, + Keyword { + range: 330..334, + arg: None, + value: Name( + ExprName { + range: 332..334, + id: "y2", + ctx: Load, + }, + ), + }, + ], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 336..355, + value: Call( + ExprCall { + range: 336..355, + func: Name( + ExprName { + range: 336..340, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 340..355, + args: [], + keywords: [ + Keyword { + range: 341..344, + arg: Some( + Identifier { + id: "x", + range: 341..342, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 343..344, + value: Int( + 1, + ), + }, + ), + }, + Keyword { + range: 346..349, + arg: None, + value: Name( + ExprName { + range: 348..349, + id: "y", + ctx: Load, + }, + ), + }, + Keyword { + range: 351..354, + arg: Some( + Identifier { + id: "z", + range: 351..352, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 353..354, + value: Int( + 1, + ), + }, + ), + }, + ], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 378..402, + value: Call( + ExprCall { + range: 378..402, + func: Name( + ExprName { + range: 378..382, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 382..402, + args: [], + keywords: [ + Keyword { + range: 383..401, + arg: Some( + Identifier { + id: "x", + range: 383..384, + }, + ), + value: If( + ExprIf { + range: 385..401, + test: BooleanLiteral( + ExprBooleanLiteral { + range: 390..394, + value: true, + }, + ), + body: NumberLiteral( + ExprNumberLiteral { + range: 385..386, + value: Int( + 1, + ), + }, + ), + orelse: NumberLiteral( + ExprNumberLiteral { + range: 400..401, + value: Int( + 2, + ), + }, + ), + }, + ), + }, + ], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 403..418, + value: Call( + ExprCall { + range: 403..418, + func: Name( + ExprName { + range: 403..407, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 407..418, + args: [], + keywords: [ + Keyword { + range: 408..417, + arg: Some( + Identifier { + id: "x", + range: 408..409, + }, + ), + value: Await( + ExprAwait { + range: 410..417, + value: Name( + ExprName { + range: 416..417, + id: "y", + ctx: Load, + }, + ), + }, + ), + }, + ], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 419..438, + value: Call( + ExprCall { + range: 419..438, + func: Name( + ExprName { + range: 419..423, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 423..438, + args: [], + keywords: [ + Keyword { + range: 424..437, + arg: Some( + Identifier { + id: "x", + range: 424..425, + }, + ), + value: Lambda( + ExprLambda { + range: 426..437, + parameters: Some( + Parameters { + range: 433..434, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 433..434, + parameter: Parameter { + range: 433..434, + name: Identifier { + id: "y", + range: 433..434, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: Name( + ExprName { + range: 436..437, + id: "y", + ctx: Load, + }, + ), + }, + ), + }, + ], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 439..455, + value: Call( + ExprCall { + range: 439..455, + func: Name( + ExprName { + range: 439..443, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 443..455, + args: [], + keywords: [ + Keyword { + range: 444..454, + arg: Some( + Identifier { + id: "x", + range: 444..445, + }, + ), + value: Named( + ExprNamed { + range: 447..453, + target: Name( + ExprName { + range: 447..448, + id: "y", + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 452..453, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 476..491, + value: Call( + ExprCall { + range: 476..491, + func: Name( + ExprName { + range: 476..480, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 480..491, + args: [ + Yield( + ExprYield { + range: 482..489, + value: Some( + Name( + ExprName { + range: 488..489, + id: "x", + ctx: Load, + }, + ), + ), + }, + ), + ], + keywords: [], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 492..512, + value: Call( + ExprCall { + range: 492..512, + func: Name( + ExprName { + range: 492..496, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 496..512, + args: [ + YieldFrom( + ExprYieldFrom { + range: 498..510, + value: Name( + ExprName { + range: 509..510, + id: "x", + ctx: Load, + }, + ), + }, + ), + ], + keywords: [], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 533..545, + value: Call( + ExprCall { + range: 533..545, + func: Name( + ExprName { + range: 533..537, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 537..545, + args: [ + Named( + ExprNamed { + range: 538..544, + target: Name( + ExprName { + range: 538..539, + id: "x", + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 543..544, + value: Int( + 1, + ), + }, + ), + }, + ), + ], + keywords: [], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 546..572, + value: Call( + ExprCall { + range: 546..572, + func: Name( + ExprName { + range: 546..550, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 550..572, + args: [ + Generator( + ExprGenerator { + range: 551..571, + elt: Named( + ExprNamed { + range: 551..557, + target: Name( + ExprName { + range: 551..552, + id: "x", + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 556..557, + value: Int( + 1, + ), + }, + ), + }, + ), + generators: [ + Comprehension { + range: 558..571, + target: Name( + ExprName { + range: 562..563, + id: "x", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 567..571, + id: "iter", + ctx: Load, + }, + ), + ifs: [], + is_async: false, + }, + ], + parenthesized: false, + }, + ), + ], + keywords: [], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 596..610, + value: Call( + ExprCall { + range: 596..610, + func: Name( + ExprName { + range: 596..600, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 600..610, + args: [ + Starred( + ExprStarred { + range: 601..609, + value: BoolOp( + ExprBoolOp { + range: 602..609, + op: And, + values: [ + Name( + ExprName { + range: 602..603, + id: "x", + ctx: Load, + }, + ), + Name( + ExprName { + range: 608..609, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + ctx: Load, + }, + ), + ], + keywords: [], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 611..623, + value: Call( + ExprCall { + range: 611..623, + func: Name( + ExprName { + range: 611..615, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 615..623, + args: [ + Starred( + ExprStarred { + range: 616..622, + value: BinOp( + ExprBinOp { + range: 617..622, + left: Name( + ExprName { + range: 617..618, + id: "x", + ctx: Load, + }, + ), + op: BitOr, + right: Name( + ExprName { + range: 621..622, + id: "y", + ctx: Load, + }, + ), + }, + ), + ctx: Load, + }, + ), + ], + keywords: [], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 624..638, + value: Call( + ExprCall { + range: 624..638, + func: Name( + ExprName { + range: 624..628, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 628..638, + args: [ + Starred( + ExprStarred { + range: 629..637, + value: Await( + ExprAwait { + range: 630..637, + value: Name( + ExprName { + range: 636..637, + id: "x", + ctx: Load, + }, + ), + }, + ), + ctx: Load, + }, + ), + ], + keywords: [], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 639..657, + value: Call( + ExprCall { + range: 639..657, + func: Name( + ExprName { + range: 639..643, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 643..657, + args: [ + Starred( + ExprStarred { + range: 644..656, + value: Lambda( + ExprLambda { + range: 645..656, + parameters: Some( + Parameters { + range: 652..653, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 652..653, + parameter: Parameter { + range: 652..653, + name: Identifier { + id: "x", + range: 652..653, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: Name( + ExprName { + range: 655..656, + id: "x", + ctx: Load, + }, + ), + }, + ), + ctx: Load, + }, + ), + ], + keywords: [], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 658..681, + value: Call( + ExprCall { + range: 658..681, + func: Name( + ExprName { + range: 658..662, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 662..681, + args: [ + Starred( + ExprStarred { + range: 663..680, + value: If( + ExprIf { + range: 664..680, + test: BooleanLiteral( + ExprBooleanLiteral { + range: 669..673, + value: true, + }, + ), + body: Name( + ExprName { + range: 664..665, + id: "x", + ctx: Load, + }, + ), + orelse: Name( + ExprName { + range: 679..680, + id: "y", + ctx: Load, + }, + ), + }, + ), + ctx: Load, + }, + ), + ], + keywords: [], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 700..709, + value: Call( + ExprCall { + range: 700..709, + func: Name( + ExprName { + range: 700..704, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 704..709, + args: [], + keywords: [ + Keyword { + range: 705..708, + arg: None, + value: Name( + ExprName { + range: 707..708, + id: "x", + ctx: Load, + }, + ), + }, + ], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 710..725, + value: Call( + ExprCall { + range: 710..725, + func: Name( + ExprName { + range: 710..714, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 714..725, + args: [], + keywords: [ + Keyword { + range: 715..724, + arg: None, + value: BoolOp( + ExprBoolOp { + range: 717..724, + op: And, + values: [ + Name( + ExprName { + range: 717..718, + id: "x", + ctx: Load, + }, + ), + Name( + ExprName { + range: 723..724, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + }, + ], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 726..741, + value: Call( + ExprCall { + range: 726..741, + func: Name( + ExprName { + range: 726..730, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 730..741, + args: [], + keywords: [ + Keyword { + range: 731..740, + arg: None, + value: Await( + ExprAwait { + range: 733..740, + value: Name( + ExprName { + range: 739..740, + id: "x", + ctx: Load, + }, + ), + }, + ), + }, + ], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 742..766, + value: Call( + ExprCall { + range: 742..766, + func: Name( + ExprName { + range: 742..746, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 746..766, + args: [], + keywords: [ + Keyword { + range: 747..765, + arg: None, + value: If( + ExprIf { + range: 749..765, + test: BooleanLiteral( + ExprBooleanLiteral { + range: 754..758, + value: true, + }, + ), + body: Name( + ExprName { + range: 749..750, + id: "x", + ctx: Load, + }, + ), + orelse: Name( + ExprName { + range: 764..765, + id: "y", + ctx: Load, + }, + ), + }, + ), + }, + ], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 767..784, + value: Call( + ExprCall { + range: 767..784, + func: Name( + ExprName { + range: 767..771, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 771..784, + args: [], + keywords: [ + Keyword { + range: 772..783, + arg: None, + value: Yield( + ExprYield { + range: 775..782, + value: Some( + Name( + ExprName { + range: 781..782, + id: "x", + ctx: Load, + }, + ), + ), + }, + ), + }, + ], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 785..804, + value: Call( + ExprCall { + range: 785..804, + func: Name( + ExprName { + range: 785..789, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 789..804, + args: [], + keywords: [ + Keyword { + range: 790..803, + arg: None, + value: Lambda( + ExprLambda { + range: 792..803, + parameters: Some( + Parameters { + range: 799..800, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 799..800, + parameter: Parameter { + range: 799..800, + name: Identifier { + id: "x", + range: 799..800, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: Name( + ExprName { + range: 802..803, + id: "x", + ctx: Load, + }, + ), + }, + ), + }, + ], + }, + }, + ), + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__attribute.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__attribute.py.snap.new new file mode 100644 index 0000000000..504db03953 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__attribute.py.snap.new @@ -0,0 +1,228 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/valid/expressions/attribute.py +--- +## AST + +``` +Module( + ModModule { + range: 0..90, + body: [ + Expr( + StmtExpr { + range: 0..10, + value: Attribute( + ExprAttribute { + range: 0..10, + value: Name( + ExprName { + range: 0..5, + id: "value", + ctx: Load, + }, + ), + attr: Identifier { + id: "attr", + range: 6..10, + }, + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 11..23, + value: Call( + ExprCall { + range: 11..23, + func: Attribute( + ExprAttribute { + range: 11..21, + value: Name( + ExprName { + range: 11..16, + id: "value", + ctx: Load, + }, + ), + attr: Identifier { + id: "attr", + range: 17..21, + }, + ctx: Load, + }, + ), + arguments: Arguments { + range: 21..23, + args: [], + keywords: [], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 24..36, + value: Attribute( + ExprAttribute { + range: 24..36, + value: Call( + ExprCall { + range: 24..31, + func: Name( + ExprName { + range: 24..29, + id: "value", + ctx: Load, + }, + ), + arguments: Arguments { + range: 29..31, + args: [], + keywords: [], + }, + }, + ), + attr: Identifier { + id: "attr", + range: 32..36, + }, + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 37..55, + value: Attribute( + ExprAttribute { + range: 37..55, + value: Call( + ExprCall { + range: 37..51, + func: Attribute( + ExprAttribute { + range: 37..49, + value: Call( + ExprCall { + range: 37..44, + func: Name( + ExprName { + range: 37..42, + id: "value", + ctx: Load, + }, + ), + arguments: Arguments { + range: 42..44, + args: [], + keywords: [], + }, + }, + ), + attr: Identifier { + id: "attr", + range: 45..49, + }, + ctx: Load, + }, + ), + arguments: Arguments { + range: 49..51, + args: [], + keywords: [], + }, + }, + ), + attr: Identifier { + id: "foo", + range: 52..55, + }, + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 56..70, + value: Attribute( + ExprAttribute { + range: 56..70, + value: Attribute( + ExprAttribute { + range: 56..66, + value: Name( + ExprName { + range: 56..61, + id: "value", + ctx: Load, + }, + ), + attr: Identifier { + id: "attr", + range: 62..66, + }, + ctx: Load, + }, + ), + attr: Identifier { + id: "foo", + range: 67..70, + }, + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 71..89, + value: Attribute( + ExprAttribute { + range: 71..89, + value: Call( + ExprCall { + range: 71..85, + func: Attribute( + ExprAttribute { + range: 71..83, + value: Name( + ExprName { + range: 72..77, + id: "value", + ctx: Load, + }, + ), + attr: Identifier { + id: "attr", + range: 79..83, + }, + ctx: Load, + }, + ), + arguments: Arguments { + range: 83..85, + args: [], + keywords: [], + }, + }, + ), + attr: Identifier { + id: "foo", + range: 86..89, + }, + ctx: Load, + }, + ), + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__await.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__await.py.snap.new new file mode 100644 index 0000000000..47745f02f1 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__await.py.snap.new @@ -0,0 +1,513 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/valid/expressions/await.py +--- +## AST + +``` +Module( + ModModule { + range: 0..211, + body: [ + Expr( + StmtExpr { + range: 0..7, + value: Await( + ExprAwait { + range: 0..7, + value: Name( + ExprName { + range: 6..7, + id: "x", + ctx: Load, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 8..19, + value: BinOp( + ExprBinOp { + range: 8..19, + left: Await( + ExprAwait { + range: 8..15, + value: Name( + ExprName { + range: 14..15, + id: "x", + ctx: Load, + }, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 18..19, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 20..33, + value: BoolOp( + ExprBoolOp { + range: 20..33, + op: And, + values: [ + Await( + ExprAwait { + range: 20..27, + value: Name( + ExprName { + range: 26..27, + id: "a", + ctx: Load, + }, + ), + }, + ), + Name( + ExprName { + range: 32..33, + id: "b", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 34..43, + value: Await( + ExprAwait { + range: 34..43, + value: Call( + ExprCall { + range: 40..43, + func: Name( + ExprName { + range: 40..41, + id: "f", + ctx: Load, + }, + ), + arguments: Arguments { + range: 41..43, + args: [], + keywords: [], + }, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 44..56, + value: Await( + ExprAwait { + range: 44..56, + value: List( + ExprList { + range: 50..56, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 51..52, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 54..55, + value: Int( + 2, + ), + }, + ), + ], + ctx: Load, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 57..69, + value: Await( + ExprAwait { + range: 57..69, + value: Set( + ExprSet { + range: 63..69, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 64..65, + value: Int( + 3, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 67..68, + value: Int( + 4, + ), + }, + ), + ], + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 70..82, + value: Await( + ExprAwait { + range: 70..82, + value: Dict( + ExprDict { + range: 76..82, + items: [ + DictItem { + key: Some( + Name( + ExprName { + range: 77..78, + id: "i", + ctx: Load, + }, + ), + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 80..81, + value: Int( + 5, + ), + }, + ), + }, + ], + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 83..93, + value: Tuple( + ExprTuple { + range: 83..93, + elts: [ + Await( + ExprAwait { + range: 83..90, + value: NumberLiteral( + ExprNumberLiteral { + range: 89..90, + value: Int( + 7, + ), + }, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 92..93, + value: Int( + 8, + ), + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 94..107, + value: Await( + ExprAwait { + range: 94..107, + value: Tuple( + ExprTuple { + range: 100..107, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 101..102, + value: Int( + 9, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 104..106, + value: Int( + 10, + ), + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 108..120, + value: Compare( + ExprCompare { + range: 108..120, + left: Await( + ExprAwait { + range: 108..115, + value: NumberLiteral( + ExprNumberLiteral { + range: 114..115, + value: Int( + 1, + ), + }, + ), + }, + ), + ops: [ + Eq, + ], + comparators: [ + NumberLiteral( + ExprNumberLiteral { + range: 119..120, + value: Int( + 1, + ), + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 121..146, + value: If( + ExprIf { + range: 121..146, + test: BooleanLiteral( + ExprBooleanLiteral { + range: 132..136, + value: true, + }, + ), + body: Await( + ExprAwait { + range: 121..128, + value: Name( + ExprName { + range: 127..128, + id: "x", + ctx: Load, + }, + ), + }, + ), + orelse: NoneLiteral( + ExprNoneLiteral { + range: 142..146, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 147..158, + value: Await( + ExprAwait { + range: 147..158, + value: Tuple( + ExprTuple { + range: 153..158, + elts: [ + Starred( + ExprStarred { + range: 154..156, + value: Name( + ExprName { + range: 155..156, + id: "x", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 159..178, + value: Await( + ExprAwait { + range: 159..178, + value: Lambda( + ExprLambda { + range: 166..177, + parameters: Some( + Parameters { + range: 173..174, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 173..174, + parameter: Parameter { + range: 173..174, + name: Identifier { + id: "x", + range: 173..174, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: Name( + ExprName { + range: 176..177, + id: "x", + ctx: Load, + }, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 179..192, + value: BinOp( + ExprBinOp { + range: 179..192, + left: Await( + ExprAwait { + range: 179..186, + value: Name( + ExprName { + range: 185..186, + id: "x", + ctx: Load, + }, + ), + }, + ), + op: Pow, + right: UnaryOp( + ExprUnaryOp { + range: 190..192, + op: USub, + operand: Name( + ExprName { + range: 191..192, + id: "x", + ctx: Load, + }, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 193..211, + value: BinOp( + ExprBinOp { + range: 193..211, + left: Await( + ExprAwait { + range: 193..200, + value: Name( + ExprName { + range: 199..200, + id: "x", + ctx: Load, + }, + ), + }, + ), + op: Pow, + right: Await( + ExprAwait { + range: 204..211, + value: Name( + ExprName { + range: 210..211, + id: "y", + ctx: Load, + }, + ), + }, + ), + }, + ), + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__bin_op.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__bin_op.py.snap.new new file mode 100644 index 0000000000..12a3cb5eb1 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__bin_op.py.snap.new @@ -0,0 +1,1047 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/valid/expressions/bin_op.py +--- +## AST + +``` +Module( + ModModule { + range: 0..397, + body: [ + Expr( + StmtExpr { + range: 9..14, + value: BinOp( + ExprBinOp { + range: 9..14, + left: NumberLiteral( + ExprNumberLiteral { + range: 9..10, + value: Int( + 1, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 13..14, + value: Int( + 2, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 15..20, + value: BinOp( + ExprBinOp { + range: 15..20, + left: NumberLiteral( + ExprNumberLiteral { + range: 15..16, + value: Int( + 1, + ), + }, + ), + op: Sub, + right: NumberLiteral( + ExprNumberLiteral { + range: 19..20, + value: Int( + 2, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 21..26, + value: BinOp( + ExprBinOp { + range: 21..26, + left: NumberLiteral( + ExprNumberLiteral { + range: 21..22, + value: Int( + 1, + ), + }, + ), + op: Mult, + right: NumberLiteral( + ExprNumberLiteral { + range: 25..26, + value: Int( + 2, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 27..32, + value: BinOp( + ExprBinOp { + range: 27..32, + left: NumberLiteral( + ExprNumberLiteral { + range: 27..28, + value: Int( + 1, + ), + }, + ), + op: Div, + right: NumberLiteral( + ExprNumberLiteral { + range: 31..32, + value: Int( + 2, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 33..39, + value: BinOp( + ExprBinOp { + range: 33..39, + left: NumberLiteral( + ExprNumberLiteral { + range: 33..34, + value: Int( + 1, + ), + }, + ), + op: FloorDiv, + right: NumberLiteral( + ExprNumberLiteral { + range: 38..39, + value: Int( + 2, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 40..45, + value: BinOp( + ExprBinOp { + range: 40..45, + left: NumberLiteral( + ExprNumberLiteral { + range: 40..41, + value: Int( + 1, + ), + }, + ), + op: Mod, + right: NumberLiteral( + ExprNumberLiteral { + range: 44..45, + value: Int( + 2, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 46..52, + value: BinOp( + ExprBinOp { + range: 46..52, + left: NumberLiteral( + ExprNumberLiteral { + range: 46..47, + value: Int( + 1, + ), + }, + ), + op: Pow, + right: NumberLiteral( + ExprNumberLiteral { + range: 51..52, + value: Int( + 2, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 53..58, + value: BinOp( + ExprBinOp { + range: 53..58, + left: NumberLiteral( + ExprNumberLiteral { + range: 53..54, + value: Int( + 1, + ), + }, + ), + op: BitOr, + right: NumberLiteral( + ExprNumberLiteral { + range: 57..58, + value: Int( + 2, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 59..64, + value: BinOp( + ExprBinOp { + range: 59..64, + left: NumberLiteral( + ExprNumberLiteral { + range: 59..60, + value: Int( + 1, + ), + }, + ), + op: BitXor, + right: NumberLiteral( + ExprNumberLiteral { + range: 63..64, + value: Int( + 2, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 65..70, + value: BinOp( + ExprBinOp { + range: 65..70, + left: NumberLiteral( + ExprNumberLiteral { + range: 65..66, + value: Int( + 1, + ), + }, + ), + op: BitAnd, + right: NumberLiteral( + ExprNumberLiteral { + range: 69..70, + value: Int( + 2, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 71..77, + value: BinOp( + ExprBinOp { + range: 71..77, + left: NumberLiteral( + ExprNumberLiteral { + range: 71..72, + value: Int( + 1, + ), + }, + ), + op: RShift, + right: NumberLiteral( + ExprNumberLiteral { + range: 76..77, + value: Int( + 2, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 78..84, + value: BinOp( + ExprBinOp { + range: 78..84, + left: NumberLiteral( + ExprNumberLiteral { + range: 78..79, + value: Int( + 1, + ), + }, + ), + op: LShift, + right: NumberLiteral( + ExprNumberLiteral { + range: 83..84, + value: Int( + 2, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 85..90, + value: BinOp( + ExprBinOp { + range: 85..90, + left: NumberLiteral( + ExprNumberLiteral { + range: 85..86, + value: Int( + 1, + ), + }, + ), + op: MatMult, + right: NumberLiteral( + ExprNumberLiteral { + range: 89..90, + value: Int( + 2, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 110..123, + value: BinOp( + ExprBinOp { + range: 110..123, + left: BinOp( + ExprBinOp { + range: 110..119, + left: BinOp( + ExprBinOp { + range: 110..115, + left: NumberLiteral( + ExprNumberLiteral { + range: 110..111, + value: Int( + 1, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 114..115, + value: Int( + 2, + ), + }, + ), + }, + ), + op: Sub, + right: NumberLiteral( + ExprNumberLiteral { + range: 118..119, + value: Int( + 3, + ), + }, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 122..123, + value: Int( + 4, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 124..146, + value: BinOp( + ExprBinOp { + range: 124..146, + left: BinOp( + ExprBinOp { + range: 124..142, + left: BinOp( + ExprBinOp { + range: 124..138, + left: BinOp( + ExprBinOp { + range: 124..133, + left: BinOp( + ExprBinOp { + range: 124..129, + left: NumberLiteral( + ExprNumberLiteral { + range: 124..125, + value: Int( + 1, + ), + }, + ), + op: Mult, + right: NumberLiteral( + ExprNumberLiteral { + range: 128..129, + value: Int( + 2, + ), + }, + ), + }, + ), + op: Div, + right: NumberLiteral( + ExprNumberLiteral { + range: 132..133, + value: Int( + 3, + ), + }, + ), + }, + ), + op: FloorDiv, + right: NumberLiteral( + ExprNumberLiteral { + range: 137..138, + value: Int( + 4, + ), + }, + ), + }, + ), + op: MatMult, + right: NumberLiteral( + ExprNumberLiteral { + range: 141..142, + value: Int( + 5, + ), + }, + ), + }, + ), + op: Mod, + right: NumberLiteral( + ExprNumberLiteral { + range: 145..146, + value: Int( + 6, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 147..168, + value: BinOp( + ExprBinOp { + range: 147..168, + left: BinOp( + ExprBinOp { + range: 147..163, + left: BinOp( + ExprBinOp { + range: 147..158, + left: BinOp( + ExprBinOp { + range: 147..153, + left: NumberLiteral( + ExprNumberLiteral { + range: 147..148, + value: Int( + 1, + ), + }, + ), + op: LShift, + right: NumberLiteral( + ExprNumberLiteral { + range: 152..153, + value: Int( + 2, + ), + }, + ), + }, + ), + op: RShift, + right: NumberLiteral( + ExprNumberLiteral { + range: 157..158, + value: Int( + 3, + ), + }, + ), + }, + ), + op: RShift, + right: NumberLiteral( + ExprNumberLiteral { + range: 162..163, + value: Int( + 4, + ), + }, + ), + }, + ), + op: LShift, + right: NumberLiteral( + ExprNumberLiteral { + range: 167..168, + value: Int( + 5, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 193..202, + value: BinOp( + ExprBinOp { + range: 193..202, + left: NumberLiteral( + ExprNumberLiteral { + range: 193..194, + value: Int( + 1, + ), + }, + ), + op: Add, + right: BinOp( + ExprBinOp { + range: 197..202, + left: NumberLiteral( + ExprNumberLiteral { + range: 197..198, + value: Int( + 2, + ), + }, + ), + op: Mult, + right: NumberLiteral( + ExprNumberLiteral { + range: 201..202, + value: Int( + 3, + ), + }, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 203..212, + value: BinOp( + ExprBinOp { + range: 203..212, + left: BinOp( + ExprBinOp { + range: 203..208, + left: NumberLiteral( + ExprNumberLiteral { + range: 203..204, + value: Int( + 1, + ), + }, + ), + op: Mult, + right: NumberLiteral( + ExprNumberLiteral { + range: 207..208, + value: Int( + 2, + ), + }, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 211..212, + value: Int( + 3, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 213..244, + value: BinOp( + ExprBinOp { + range: 213..244, + left: BinOp( + ExprBinOp { + range: 213..235, + left: BinOp( + ExprBinOp { + range: 213..231, + left: BinOp( + ExprBinOp { + range: 213..223, + left: BinOp( + ExprBinOp { + range: 213..219, + left: NumberLiteral( + ExprNumberLiteral { + range: 213..214, + value: Int( + 1, + ), + }, + ), + op: Pow, + right: NumberLiteral( + ExprNumberLiteral { + range: 218..219, + value: Int( + 2, + ), + }, + ), + }, + ), + op: Mult, + right: NumberLiteral( + ExprNumberLiteral { + range: 222..223, + value: Int( + 3, + ), + }, + ), + }, + ), + op: Sub, + right: BinOp( + ExprBinOp { + range: 226..231, + left: NumberLiteral( + ExprNumberLiteral { + range: 226..227, + value: Int( + 4, + ), + }, + ), + op: MatMult, + right: NumberLiteral( + ExprNumberLiteral { + range: 230..231, + value: Int( + 5, + ), + }, + ), + }, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 234..235, + value: Int( + 6, + ), + }, + ), + }, + ), + op: Sub, + right: BinOp( + ExprBinOp { + range: 238..244, + left: NumberLiteral( + ExprNumberLiteral { + range: 238..239, + value: Int( + 7, + ), + }, + ), + op: FloorDiv, + right: NumberLiteral( + ExprNumberLiteral { + range: 243..244, + value: Int( + 8, + ), + }, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 270..306, + value: BinOp( + ExprBinOp { + range: 270..306, + left: NumberLiteral( + ExprNumberLiteral { + range: 270..271, + value: Int( + 1, + ), + }, + ), + op: BitOr, + right: BinOp( + ExprBinOp { + range: 274..306, + left: BinOp( + ExprBinOp { + range: 274..279, + left: NumberLiteral( + ExprNumberLiteral { + range: 274..275, + value: Int( + 2, + ), + }, + ), + op: BitAnd, + right: NumberLiteral( + ExprNumberLiteral { + range: 278..279, + value: Int( + 3, + ), + }, + ), + }, + ), + op: BitXor, + right: BinOp( + ExprBinOp { + range: 282..306, + left: BinOp( + ExprBinOp { + range: 282..301, + left: BinOp( + ExprBinOp { + range: 282..291, + left: NumberLiteral( + ExprNumberLiteral { + range: 282..283, + value: Int( + 4, + ), + }, + ), + op: Add, + right: BinOp( + ExprBinOp { + range: 286..291, + left: NumberLiteral( + ExprNumberLiteral { + range: 286..287, + value: Int( + 5, + ), + }, + ), + op: MatMult, + right: NumberLiteral( + ExprNumberLiteral { + range: 290..291, + value: Int( + 6, + ), + }, + ), + }, + ), + }, + ), + op: LShift, + right: BinOp( + ExprBinOp { + range: 295..301, + left: NumberLiteral( + ExprNumberLiteral { + range: 295..296, + value: Int( + 7, + ), + }, + ), + op: FloorDiv, + right: NumberLiteral( + ExprNumberLiteral { + range: 300..301, + value: Int( + 8, + ), + }, + ), + }, + ), + }, + ), + op: RShift, + right: NumberLiteral( + ExprNumberLiteral { + range: 305..306, + value: Int( + 9, + ), + }, + ), + }, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 324..339, + value: BinOp( + ExprBinOp { + range: 324..339, + left: BinOp( + ExprBinOp { + range: 324..335, + left: NumberLiteral( + ExprNumberLiteral { + range: 324..325, + value: Int( + 1, + ), + }, + ), + op: Add, + right: BinOp( + ExprBinOp { + range: 329..334, + left: NumberLiteral( + ExprNumberLiteral { + range: 329..330, + value: Int( + 2, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 333..334, + value: Int( + 3, + ), + }, + ), + }, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 338..339, + value: Int( + 4, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 340..359, + value: BinOp( + ExprBinOp { + range: 340..359, + left: BinOp( + ExprBinOp { + range: 340..345, + left: NumberLiteral( + ExprNumberLiteral { + range: 340..341, + value: Int( + 1, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 344..345, + value: Int( + 2, + ), + }, + ), + }, + ), + op: Add, + right: BinOp( + ExprBinOp { + range: 349..358, + left: BinOp( + ExprBinOp { + range: 349..354, + left: NumberLiteral( + ExprNumberLiteral { + range: 349..350, + value: Int( + 3, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 353..354, + value: Int( + 4, + ), + }, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 357..358, + value: Int( + 5, + ), + }, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 390..396, + value: BinOp( + ExprBinOp { + range: 390..396, + left: Name( + ExprName { + range: 390..391, + id: "x", + ctx: Load, + }, + ), + op: Add, + right: UnaryOp( + ExprUnaryOp { + range: 393..396, + op: UAdd, + operand: Name( + ExprName { + range: 395..396, + id: "y", + ctx: Load, + }, + ), + }, + ), + }, + ), + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__bool_op.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__bool_op.py.snap.new new file mode 100644 index 0000000000..91215dd29e --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__bool_op.py.snap.new @@ -0,0 +1,403 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/valid/expressions/bool_op.py +--- +## AST + +``` +Module( + ModModule { + range: 0..142, + body: [ + Expr( + StmtExpr { + range: 0..7, + value: BoolOp( + ExprBoolOp { + range: 0..7, + op: And, + values: [ + Name( + ExprName { + range: 0..1, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 6..7, + id: "b", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 8..21, + value: BoolOp( + ExprBoolOp { + range: 8..21, + op: And, + values: [ + Name( + ExprName { + range: 8..9, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 14..15, + id: "b", + ctx: Load, + }, + ), + Name( + ExprName { + range: 20..21, + id: "c", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 22..28, + value: BoolOp( + ExprBoolOp { + range: 22..28, + op: Or, + values: [ + Name( + ExprName { + range: 22..23, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 27..28, + id: "b", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 29..40, + value: BoolOp( + ExprBoolOp { + range: 29..40, + op: Or, + values: [ + Name( + ExprName { + range: 29..30, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 34..35, + id: "b", + ctx: Load, + }, + ), + Name( + ExprName { + range: 39..40, + id: "c", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 41..53, + value: BoolOp( + ExprBoolOp { + range: 41..53, + op: Or, + values: [ + BoolOp( + ExprBoolOp { + range: 41..48, + op: And, + values: [ + Name( + ExprName { + range: 41..42, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 47..48, + id: "b", + ctx: Load, + }, + ), + ], + }, + ), + Name( + ExprName { + range: 52..53, + id: "c", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 54..88, + value: BoolOp( + ExprBoolOp { + range: 54..88, + op: Or, + values: [ + BoolOp( + ExprBoolOp { + range: 54..67, + op: And, + values: [ + Name( + ExprName { + range: 54..55, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 60..61, + id: "b", + ctx: Load, + }, + ), + Name( + ExprName { + range: 66..67, + id: "c", + ctx: Load, + }, + ), + ], + }, + ), + Name( + ExprName { + range: 71..72, + id: "d", + ctx: Load, + }, + ), + BoolOp( + ExprBoolOp { + range: 76..83, + op: And, + values: [ + Name( + ExprName { + range: 76..77, + id: "e", + ctx: Load, + }, + ), + Name( + ExprName { + range: 82..83, + id: "f", + ctx: Load, + }, + ), + ], + }, + ), + Name( + ExprName { + range: 87..88, + id: "g", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 89..105, + value: BoolOp( + ExprBoolOp { + range: 89..105, + op: Or, + values: [ + BoolOp( + ExprBoolOp { + range: 89..100, + op: And, + values: [ + Name( + ExprName { + range: 89..90, + id: "a", + ctx: Load, + }, + ), + UnaryOp( + ExprUnaryOp { + range: 95..100, + op: Not, + operand: Name( + ExprName { + range: 99..100, + id: "b", + ctx: Load, + }, + ), + }, + ), + ], + }, + ), + Name( + ExprName { + range: 104..105, + id: "c", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 106..124, + value: Yield( + ExprYield { + range: 106..124, + value: Some( + BoolOp( + ExprBoolOp { + range: 112..124, + op: Or, + values: [ + BoolOp( + ExprBoolOp { + range: 112..119, + op: And, + values: [ + Name( + ExprName { + range: 112..113, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 118..119, + id: "b", + ctx: Load, + }, + ), + ], + }, + ), + Name( + ExprName { + range: 123..124, + id: "c", + ctx: Load, + }, + ), + ], + }, + ), + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 125..141, + value: BoolOp( + ExprBoolOp { + range: 125..141, + op: Or, + values: [ + BoolOp( + ExprBoolOp { + range: 125..136, + op: And, + values: [ + UnaryOp( + ExprUnaryOp { + range: 125..130, + op: Not, + operand: Name( + ExprName { + range: 129..130, + id: "a", + ctx: Load, + }, + ), + }, + ), + Name( + ExprName { + range: 135..136, + id: "b", + ctx: Load, + }, + ), + ], + }, + ), + Name( + ExprName { + range: 140..141, + id: "c", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__call.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__call.py.snap.new new file mode 100644 index 0000000000..a859123843 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__call.py.snap.new @@ -0,0 +1,590 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/valid/expressions/call.py +--- +## AST + +``` +Module( + ModModule { + range: 0..349, + body: [ + Expr( + StmtExpr { + range: 114..120, + value: Call( + ExprCall { + range: 114..120, + func: Name( + ExprName { + range: 114..118, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 118..120, + args: [], + keywords: [], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 121..132, + value: Call( + ExprCall { + range: 121..132, + func: Attribute( + ExprAttribute { + range: 121..130, + value: Name( + ExprName { + range: 121..125, + id: "attr", + ctx: Load, + }, + ), + attr: Identifier { + id: "expr", + range: 126..130, + }, + ctx: Load, + }, + ), + arguments: Arguments { + range: 130..132, + args: [], + keywords: [], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 133..150, + value: Call( + ExprCall { + range: 133..150, + func: Subscript( + ExprSubscript { + range: 133..148, + value: Name( + ExprName { + range: 133..142, + id: "subscript", + ctx: Load, + }, + ), + slice: Tuple( + ExprTuple { + range: 143..147, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 143..144, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 146..147, + value: Int( + 2, + ), + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + ctx: Load, + }, + ), + arguments: Arguments { + range: 148..150, + args: [], + keywords: [], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 151..162, + value: Call( + ExprCall { + range: 151..162, + func: Subscript( + ExprSubscript { + range: 151..160, + value: Name( + ExprName { + range: 151..156, + id: "slice", + ctx: Load, + }, + ), + slice: Slice( + ExprSlice { + range: 157..159, + lower: None, + upper: Some( + NumberLiteral( + ExprNumberLiteral { + range: 158..159, + value: Int( + 1, + ), + }, + ), + ), + step: None, + }, + ), + ctx: Load, + }, + ), + arguments: Arguments { + range: 160..162, + args: [], + keywords: [], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 163..174, + value: Call( + ExprCall { + range: 163..174, + func: List( + ExprList { + range: 163..172, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 164..165, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 167..168, + value: Int( + 2, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 170..171, + value: Int( + 3, + ), + }, + ), + ], + ctx: Load, + }, + ), + arguments: Arguments { + range: 172..174, + args: [], + keywords: [], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 175..186, + value: Call( + ExprCall { + range: 175..186, + func: Tuple( + ExprTuple { + range: 175..184, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 176..177, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 179..180, + value: Int( + 2, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 182..183, + value: Int( + 3, + ), + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + arguments: Arguments { + range: 184..186, + args: [], + keywords: [], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 187..206, + value: Call( + ExprCall { + range: 187..206, + func: Generator( + ExprGenerator { + range: 187..204, + elt: Name( + ExprName { + range: 188..189, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 190..203, + target: Name( + ExprName { + range: 194..195, + id: "x", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 199..203, + id: "iter", + ctx: Load, + }, + ), + ifs: [], + is_async: false, + }, + ], + parenthesized: true, + }, + ), + arguments: Arguments { + range: 204..206, + args: [], + keywords: [], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 207..218, + value: Call( + ExprCall { + range: 207..218, + func: Set( + ExprSet { + range: 207..216, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 208..209, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 211..212, + value: Int( + 2, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 214..215, + value: Int( + 3, + ), + }, + ), + ], + }, + ), + arguments: Arguments { + range: 216..218, + args: [], + keywords: [], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 219..233, + value: Call( + ExprCall { + range: 219..233, + func: Dict( + ExprDict { + range: 219..231, + items: [ + DictItem { + key: Some( + NumberLiteral( + ExprNumberLiteral { + range: 220..221, + value: Int( + 1, + ), + }, + ), + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 223..224, + value: Int( + 2, + ), + }, + ), + }, + DictItem { + key: Some( + NumberLiteral( + ExprNumberLiteral { + range: 226..227, + value: Int( + 3, + ), + }, + ), + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 229..230, + value: Int( + 4, + ), + }, + ), + }, + ], + }, + ), + arguments: Arguments { + range: 231..233, + args: [], + keywords: [], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 234..245, + value: Call( + ExprCall { + range: 234..245, + func: Yield( + ExprYield { + range: 235..242, + value: Some( + Name( + ExprName { + range: 241..242, + id: "x", + ctx: Load, + }, + ), + ), + }, + ), + arguments: Arguments { + range: 243..245, + args: [], + keywords: [], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 306..312, + value: Call( + ExprCall { + range: 306..312, + func: BooleanLiteral( + ExprBooleanLiteral { + range: 306..310, + value: true, + }, + ), + arguments: Arguments { + range: 310..312, + args: [], + keywords: [], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 313..320, + value: Call( + ExprCall { + range: 313..320, + func: BooleanLiteral( + ExprBooleanLiteral { + range: 313..318, + value: false, + }, + ), + arguments: Arguments { + range: 318..320, + args: [], + keywords: [], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 321..327, + value: Call( + ExprCall { + range: 321..327, + func: NoneLiteral( + ExprNoneLiteral { + range: 321..325, + }, + ), + arguments: Arguments { + range: 325..327, + args: [], + keywords: [], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 328..338, + value: Call( + ExprCall { + range: 328..338, + func: StringLiteral( + ExprStringLiteral { + range: 328..336, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 328..336, + value: "string", + flags: StringLiteralFlags { + quote_style: Double, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + arguments: Arguments { + range: 336..338, + args: [], + keywords: [], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 339..342, + value: Call( + ExprCall { + range: 339..342, + func: NumberLiteral( + ExprNumberLiteral { + range: 339..340, + value: Int( + 1, + ), + }, + ), + arguments: Arguments { + range: 340..342, + args: [], + keywords: [], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 343..348, + value: Call( + ExprCall { + range: 343..348, + func: NumberLiteral( + ExprNumberLiteral { + range: 343..346, + value: Float( + 1.0, + ), + }, + ), + arguments: Arguments { + range: 346..348, + args: [], + keywords: [], + }, + }, + ), + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__compare.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__compare.py.snap.new new file mode 100644 index 0000000000..7517ff9952 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__compare.py.snap.new @@ -0,0 +1,698 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/valid/expressions/compare.py +--- +## AST + +``` +Module( + ModModule { + range: 0..542, + body: [ + Expr( + StmtExpr { + range: 9..15, + value: Compare( + ExprCompare { + range: 9..15, + left: Name( + ExprName { + range: 9..10, + id: "a", + ctx: Load, + }, + ), + ops: [ + Eq, + ], + comparators: [ + Name( + ExprName { + range: 14..15, + id: "b", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 16..21, + value: Compare( + ExprCompare { + range: 16..21, + left: Name( + ExprName { + range: 16..17, + id: "b", + ctx: Load, + }, + ), + ops: [ + Lt, + ], + comparators: [ + Name( + ExprName { + range: 20..21, + id: "a", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 22..27, + value: Compare( + ExprCompare { + range: 22..27, + left: Name( + ExprName { + range: 22..23, + id: "b", + ctx: Load, + }, + ), + ops: [ + Gt, + ], + comparators: [ + Name( + ExprName { + range: 26..27, + id: "a", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 28..34, + value: Compare( + ExprCompare { + range: 28..34, + left: Name( + ExprName { + range: 28..29, + id: "a", + ctx: Load, + }, + ), + ops: [ + GtE, + ], + comparators: [ + Name( + ExprName { + range: 33..34, + id: "b", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 35..41, + value: Compare( + ExprCompare { + range: 35..41, + left: Name( + ExprName { + range: 35..36, + id: "a", + ctx: Load, + }, + ), + ops: [ + LtE, + ], + comparators: [ + Name( + ExprName { + range: 40..41, + id: "b", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 42..48, + value: Compare( + ExprCompare { + range: 42..48, + left: Name( + ExprName { + range: 42..43, + id: "a", + ctx: Load, + }, + ), + ops: [ + NotEq, + ], + comparators: [ + Name( + ExprName { + range: 47..48, + id: "b", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 49..55, + value: Compare( + ExprCompare { + range: 49..55, + left: Name( + ExprName { + range: 49..50, + id: "a", + ctx: Load, + }, + ), + ops: [ + Is, + ], + comparators: [ + Name( + ExprName { + range: 54..55, + id: "c", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 56..62, + value: Compare( + ExprCompare { + range: 56..62, + left: Name( + ExprName { + range: 56..57, + id: "a", + ctx: Load, + }, + ), + ops: [ + In, + ], + comparators: [ + Name( + ExprName { + range: 61..62, + id: "b", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 63..73, + value: Compare( + ExprCompare { + range: 63..73, + left: Name( + ExprName { + range: 63..64, + id: "a", + ctx: Load, + }, + ), + ops: [ + NotIn, + ], + comparators: [ + Name( + ExprName { + range: 72..73, + id: "c", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 74..84, + value: Compare( + ExprCompare { + range: 74..84, + left: Name( + ExprName { + range: 74..75, + id: "a", + ctx: Load, + }, + ), + ops: [ + IsNot, + ], + comparators: [ + Name( + ExprName { + range: 83..84, + id: "b", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 110..156, + value: Compare( + ExprCompare { + range: 110..156, + left: Name( + ExprName { + range: 110..111, + id: "a", + ctx: Load, + }, + ), + ops: [ + NotIn, + IsNot, + NotIn, + NotIn, + IsNot, + ], + comparators: [ + Name( + ExprName { + range: 119..120, + id: "b", + ctx: Load, + }, + ), + Name( + ExprName { + range: 128..129, + id: "c", + ctx: Load, + }, + ), + Name( + ExprName { + range: 137..138, + id: "d", + ctx: Load, + }, + ), + Name( + ExprName { + range: 146..147, + id: "e", + ctx: Load, + }, + ), + Name( + ExprName { + range: 155..156, + id: "f", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 177..203, + value: Compare( + ExprCompare { + range: 177..203, + left: BinOp( + ExprBinOp { + range: 177..182, + left: Name( + ExprName { + range: 177..178, + id: "a", + ctx: Load, + }, + ), + op: BitOr, + right: Name( + ExprName { + range: 181..182, + id: "b", + ctx: Load, + }, + ), + }, + ), + ops: [ + Lt, + NotIn, + ], + comparators: [ + BinOp( + ExprBinOp { + range: 185..190, + left: Name( + ExprName { + range: 185..186, + id: "c", + ctx: Load, + }, + ), + op: BitOr, + right: Name( + ExprName { + range: 189..190, + id: "d", + ctx: Load, + }, + ), + }, + ), + BinOp( + ExprBinOp { + range: 198..203, + left: Name( + ExprName { + range: 198..199, + id: "e", + ctx: Load, + }, + ), + op: BitAnd, + right: Name( + ExprName { + range: 202..203, + id: "f", + ctx: Load, + }, + ), + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 379..393, + value: UnaryOp( + ExprUnaryOp { + range: 379..393, + op: Not, + operand: Compare( + ExprCompare { + range: 383..393, + left: Name( + ExprName { + range: 383..384, + id: "x", + ctx: Load, + }, + ), + ops: [ + NotIn, + ], + comparators: [ + Name( + ExprName { + range: 392..393, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 395..416, + value: BoolOp( + ExprBoolOp { + range: 395..416, + op: Or, + values: [ + Name( + ExprName { + range: 395..396, + id: "x", + ctx: Load, + }, + ), + BoolOp( + ExprBoolOp { + range: 400..416, + op: And, + values: [ + Compare( + ExprCompare { + range: 400..410, + left: Name( + ExprName { + range: 400..401, + id: "y", + ctx: Load, + }, + ), + ops: [ + NotIn, + ], + comparators: [ + Name( + ExprName { + range: 409..410, + id: "z", + ctx: Load, + }, + ), + ], + }, + ), + Name( + ExprName { + range: 415..416, + id: "a", + ctx: Load, + }, + ), + ], + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 417..429, + value: Compare( + ExprCompare { + range: 417..429, + left: Name( + ExprName { + range: 417..418, + id: "x", + ctx: Load, + }, + ), + ops: [ + Eq, + ], + comparators: [ + Await( + ExprAwait { + range: 422..429, + value: Name( + ExprName { + range: 428..429, + id: "y", + ctx: Load, + }, + ), + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 430..446, + value: Compare( + ExprCompare { + range: 430..446, + left: Name( + ExprName { + range: 430..431, + id: "x", + ctx: Load, + }, + ), + ops: [ + IsNot, + ], + comparators: [ + Await( + ExprAwait { + range: 439..446, + value: Name( + ExprName { + range: 445..446, + id: "y", + ctx: Load, + }, + ), + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 489..541, + value: Compare( + ExprCompare { + range: 489..541, + left: Name( + ExprName { + range: 489..490, + id: "a", + ctx: Load, + }, + ), + ops: [ + Lt, + Eq, + Gt, + Is, + NotIn, + IsNot, + LtE, + GtE, + NotEq, + ], + comparators: [ + Name( + ExprName { + range: 493..494, + id: "b", + ctx: Load, + }, + ), + Name( + ExprName { + range: 498..499, + id: "c", + ctx: Load, + }, + ), + Name( + ExprName { + range: 502..503, + id: "d", + ctx: Load, + }, + ), + Name( + ExprName { + range: 507..508, + id: "e", + ctx: Load, + }, + ), + Name( + ExprName { + range: 516..517, + id: "f", + ctx: Load, + }, + ), + Name( + ExprName { + range: 525..526, + id: "g", + ctx: Load, + }, + ), + Name( + ExprName { + range: 530..531, + id: "h", + ctx: Load, + }, + ), + Name( + ExprName { + range: 535..536, + id: "i", + ctx: Load, + }, + ), + Name( + ExprName { + range: 540..541, + id: "j", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__dictionary.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__dictionary.py.snap.new new file mode 100644 index 0000000000..4eb2784604 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__dictionary.py.snap.new @@ -0,0 +1,1231 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/valid/expressions/dictionary.py +--- +## AST + +``` +Module( + ModModule { + range: 0..622, + body: [ + Expr( + StmtExpr { + range: 9..11, + value: Dict( + ExprDict { + range: 9..11, + items: [], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 12..18, + value: Dict( + ExprDict { + range: 12..18, + items: [ + DictItem { + key: Some( + NumberLiteral( + ExprNumberLiteral { + range: 13..14, + value: Int( + 1, + ), + }, + ), + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 16..17, + value: Int( + 2, + ), + }, + ), + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 19..43, + value: Dict( + ExprDict { + range: 19..43, + items: [ + DictItem { + key: Some( + NumberLiteral( + ExprNumberLiteral { + range: 20..21, + value: Int( + 1, + ), + }, + ), + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 23..24, + value: Int( + 2, + ), + }, + ), + }, + DictItem { + key: Some( + Name( + ExprName { + range: 26..27, + id: "a", + ctx: Load, + }, + ), + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 29..30, + value: Int( + 1, + ), + }, + ), + }, + DictItem { + key: Some( + Name( + ExprName { + range: 32..33, + id: "b", + ctx: Load, + }, + ), + ), + value: StringLiteral( + ExprStringLiteral { + range: 35..42, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 35..42, + value: "hello", + flags: StringLiteralFlags { + quote_style: Single, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 66..69, + value: Dict( + ExprDict { + range: 66..69, + items: [], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 70..100, + value: Dict( + ExprDict { + range: 70..100, + items: [ + DictItem { + key: Some( + NumberLiteral( + ExprNumberLiteral { + range: 76..77, + value: Int( + 1, + ), + }, + ), + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 83..84, + value: Int( + 2, + ), + }, + ), + }, + DictItem { + key: Some( + NumberLiteral( + ExprNumberLiteral { + range: 90..91, + value: Int( + 3, + ), + }, + ), + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 97..98, + value: Int( + 4, + ), + }, + ), + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 111..132, + value: Dict( + ExprDict { + range: 111..132, + items: [ + DictItem { + key: Some( + Dict( + ExprDict { + range: 112..118, + items: [ + DictItem { + key: Some( + NumberLiteral( + ExprNumberLiteral { + range: 113..114, + value: Int( + 1, + ), + }, + ), + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 116..117, + value: Int( + 2, + ), + }, + ), + }, + ], + }, + ), + ), + value: Dict( + ExprDict { + range: 120..131, + items: [ + DictItem { + key: Some( + NumberLiteral( + ExprNumberLiteral { + range: 121..122, + value: Int( + 3, + ), + }, + ), + ), + value: Dict( + ExprDict { + range: 124..130, + items: [ + DictItem { + key: Some( + NumberLiteral( + ExprNumberLiteral { + range: 125..126, + value: Int( + 4, + ), + }, + ), + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 128..129, + value: Int( + 5, + ), + }, + ), + }, + ], + }, + ), + }, + ], + }, + ), + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 155..171, + value: Dict( + ExprDict { + range: 155..171, + items: [ + DictItem { + key: Some( + Lambda( + ExprLambda { + range: 156..167, + parameters: Some( + Parameters { + range: 163..164, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 163..164, + parameter: Parameter { + range: 163..164, + name: Identifier { + id: "x", + range: 163..164, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: Name( + ExprName { + range: 166..167, + id: "x", + ctx: Load, + }, + ), + }, + ), + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 169..170, + value: Int( + 1, + ), + }, + ), + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 172..202, + value: Dict( + ExprDict { + range: 172..202, + items: [ + DictItem { + key: Some( + StringLiteral( + ExprStringLiteral { + range: 173..176, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 173..176, + value: "A", + flags: StringLiteralFlags { + quote_style: Single, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + ), + value: Lambda( + ExprLambda { + range: 178..192, + parameters: Some( + Parameters { + range: 185..186, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 185..186, + parameter: Parameter { + range: 185..186, + name: Identifier { + id: "p", + range: 185..186, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: NoneLiteral( + ExprNoneLiteral { + range: 188..192, + }, + ), + }, + ), + }, + DictItem { + key: Some( + StringLiteral( + ExprStringLiteral { + range: 194..197, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 194..197, + value: "B", + flags: StringLiteralFlags { + quote_style: Single, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + ), + value: Name( + ExprName { + range: 199..200, + id: "C", + ctx: Load, + }, + ), + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 224..237, + value: Dict( + ExprDict { + range: 224..237, + items: [ + DictItem { + key: Some( + Named( + ExprNamed { + range: 226..232, + target: Name( + ExprName { + range: 226..227, + id: "x", + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 231..232, + value: Int( + 1, + ), + }, + ), + }, + ), + ), + value: Name( + ExprName { + range: 235..236, + id: "y", + ctx: Load, + }, + ), + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 238..258, + value: Dict( + ExprDict { + range: 238..258, + items: [ + DictItem { + key: Some( + Named( + ExprNamed { + range: 240..246, + target: Name( + ExprName { + range: 240..241, + id: "x", + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 245..246, + value: Int( + 1, + ), + }, + ), + }, + ), + ), + value: Named( + ExprNamed { + range: 250..256, + target: Name( + ExprName { + range: 250..251, + id: "y", + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 255..256, + value: Int( + 2, + ), + }, + ), + }, + ), + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 284..289, + value: Dict( + ExprDict { + range: 284..289, + items: [ + DictItem { + key: None, + value: Name( + ExprName { + range: 287..288, + id: "d", + ctx: Load, + }, + ), + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 290..301, + value: Dict( + ExprDict { + range: 290..301, + items: [ + DictItem { + key: Some( + Name( + ExprName { + range: 291..292, + id: "a", + ctx: Load, + }, + ), + ), + value: Name( + ExprName { + range: 294..295, + id: "b", + ctx: Load, + }, + ), + }, + DictItem { + key: None, + value: Name( + ExprName { + range: 299..300, + id: "d", + ctx: Load, + }, + ), + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 302..312, + value: Dict( + ExprDict { + range: 302..312, + items: [ + DictItem { + key: None, + value: Name( + ExprName { + range: 305..306, + id: "a", + ctx: Load, + }, + ), + }, + DictItem { + key: None, + value: Name( + ExprName { + range: 310..311, + id: "b", + ctx: Load, + }, + ), + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 313..338, + value: Dict( + ExprDict { + range: 313..338, + items: [ + DictItem { + key: Some( + StringLiteral( + ExprStringLiteral { + range: 314..317, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 314..317, + value: "a", + flags: StringLiteralFlags { + quote_style: Double, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + ), + value: StringLiteral( + ExprStringLiteral { + range: 319..322, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 319..322, + value: "b", + flags: StringLiteralFlags { + quote_style: Double, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + }, + DictItem { + key: None, + value: Name( + ExprName { + range: 326..327, + id: "c", + ctx: Load, + }, + ), + }, + DictItem { + key: Some( + StringLiteral( + ExprStringLiteral { + range: 329..332, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 329..332, + value: "d", + flags: StringLiteralFlags { + quote_style: Double, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + ), + value: StringLiteral( + ExprStringLiteral { + range: 334..337, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 334..337, + value: "e", + flags: StringLiteralFlags { + quote_style: Double, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 339..367, + value: Dict( + ExprDict { + range: 339..367, + items: [ + DictItem { + key: Some( + NumberLiteral( + ExprNumberLiteral { + range: 340..341, + value: Int( + 1, + ), + }, + ), + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 343..344, + value: Int( + 2, + ), + }, + ), + }, + DictItem { + key: None, + value: Dict( + ExprDict { + range: 348..366, + items: [ + DictItem { + key: Some( + StringLiteral( + ExprStringLiteral { + range: 349..357, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 349..357, + value: "nested", + flags: StringLiteralFlags { + quote_style: Single, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + ), + value: StringLiteral( + ExprStringLiteral { + range: 359..365, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 359..365, + value: "dict", + flags: StringLiteralFlags { + quote_style: Single, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + }, + ], + }, + ), + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 368..393, + value: Dict( + ExprDict { + range: 368..393, + items: [ + DictItem { + key: Some( + BinOp( + ExprBinOp { + range: 369..374, + left: Name( + ExprName { + range: 369..370, + id: "x", + ctx: Load, + }, + ), + op: Mult, + right: NumberLiteral( + ExprNumberLiteral { + range: 373..374, + value: Int( + 1, + ), + }, + ), + }, + ), + ), + value: BinOp( + ExprBinOp { + range: 376..382, + left: Name( + ExprName { + range: 376..377, + id: "y", + ctx: Load, + }, + ), + op: Pow, + right: NumberLiteral( + ExprNumberLiteral { + range: 381..382, + value: Int( + 2, + ), + }, + ), + }, + ), + }, + DictItem { + key: None, + value: Call( + ExprCall { + range: 386..392, + func: Name( + ExprName { + range: 386..390, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 390..392, + args: [], + keywords: [], + }, + }, + ), + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 460..471, + value: Dict( + ExprDict { + range: 460..471, + items: [ + DictItem { + key: None, + value: UnaryOp( + ExprUnaryOp { + range: 464..469, + op: Not, + operand: Name( + ExprName { + range: 468..469, + id: "x", + ctx: Load, + }, + ), + }, + ), + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 494..515, + value: Dict( + ExprDict { + range: 494..515, + items: [ + DictItem { + key: Some( + NumberLiteral( + ExprNumberLiteral { + range: 495..496, + value: Int( + 1, + ), + }, + ), + ), + value: If( + ExprIf { + range: 498..514, + test: BooleanLiteral( + ExprBooleanLiteral { + range: 503..507, + value: true, + }, + ), + body: Name( + ExprName { + range: 498..499, + id: "x", + ctx: Load, + }, + ), + orelse: Name( + ExprName { + range: 513..514, + id: "y", + ctx: Load, + }, + ), + }, + ), + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 516..575, + value: DictComp( + ExprDictComp { + range: 516..575, + key: If( + ExprIf { + range: 517..533, + test: BooleanLiteral( + ExprBooleanLiteral { + range: 522..526, + value: true, + }, + ), + body: Name( + ExprName { + range: 517..518, + id: "x", + ctx: Load, + }, + ), + orelse: Name( + ExprName { + range: 532..533, + id: "y", + ctx: Load, + }, + ), + }, + ), + value: Name( + ExprName { + range: 535..536, + id: "y", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 537..555, + target: Name( + ExprName { + range: 541..542, + id: "x", + ctx: Store, + }, + ), + iter: Call( + ExprCall { + range: 546..555, + func: Name( + ExprName { + range: 546..551, + id: "range", + ctx: Load, + }, + ), + arguments: Arguments { + range: 551..555, + args: [ + NumberLiteral( + ExprNumberLiteral { + range: 552..554, + value: Int( + 10, + ), + }, + ), + ], + keywords: [], + }, + }, + ), + ifs: [], + is_async: false, + }, + Comprehension { + range: 556..574, + target: Name( + ExprName { + range: 560..561, + id: "y", + ctx: Store, + }, + ), + iter: Call( + ExprCall { + range: 565..574, + func: Name( + ExprName { + range: 565..570, + id: "range", + ctx: Load, + }, + ), + arguments: Arguments { + range: 570..574, + args: [ + NumberLiteral( + ExprNumberLiteral { + range: 571..573, + value: Int( + 10, + ), + }, + ), + ], + keywords: [], + }, + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 576..600, + value: Dict( + ExprDict { + range: 576..600, + items: [ + DictItem { + key: Some( + Set( + ExprSet { + range: 577..583, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 578..579, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 581..582, + value: Int( + 2, + ), + }, + ), + ], + }, + ), + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 585..586, + value: Int( + 3, + ), + }, + ), + }, + DictItem { + key: Some( + Name( + ExprName { + range: 588..589, + id: "x", + ctx: Load, + }, + ), + ), + value: Dict( + ExprDict { + range: 591..598, + items: [ + DictItem { + key: Some( + NumberLiteral( + ExprNumberLiteral { + range: 592..593, + value: Int( + 1, + ), + }, + ), + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 595..596, + value: Int( + 2, + ), + }, + ), + }, + ], + }, + ), + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 601..621, + value: Dict( + ExprDict { + range: 601..621, + items: [ + DictItem { + key: Some( + Name( + ExprName { + range: 603..604, + id: "x", + ctx: Load, + }, + ), + ), + value: Name( + ExprName { + range: 608..609, + id: "y", + ctx: Load, + }, + ), + }, + DictItem { + key: Some( + Name( + ExprName { + range: 613..614, + id: "z", + ctx: Load, + }, + ), + ), + value: Name( + ExprName { + range: 618..619, + id: "a", + ctx: Load, + }, + ), + }, + ], + }, + ), + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__dictionary_comprehension.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__dictionary_comprehension.py.snap.new new file mode 100644 index 0000000000..e4e593371a --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__dictionary_comprehension.py.snap.new @@ -0,0 +1,1000 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/valid/expressions/dictionary_comprehension.py +--- +## AST + +``` +Module( + ModModule { + range: 0..589, + body: [ + Expr( + StmtExpr { + range: 0..22, + value: SetComp( + ExprSetComp { + range: 0..22, + elt: Name( + ExprName { + range: 1..2, + id: "y", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 3..21, + target: Name( + ExprName { + range: 7..8, + id: "y", + ctx: Store, + }, + ), + iter: Tuple( + ExprTuple { + range: 12..21, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 13..14, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 16..17, + value: Int( + 2, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 19..20, + value: Int( + 3, + ), + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 23..42, + value: DictComp( + ExprDictComp { + range: 23..42, + key: Name( + ExprName { + range: 24..26, + id: "x1", + ctx: Load, + }, + ), + value: Name( + ExprName { + range: 28..30, + id: "x2", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 31..41, + target: Name( + ExprName { + range: 35..36, + id: "y", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 40..41, + id: "z", + ctx: Load, + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 43..73, + value: DictComp( + ExprDictComp { + range: 43..73, + key: BinOp( + ExprBinOp { + range: 44..49, + left: Name( + ExprName { + range: 44..45, + id: "x", + ctx: Load, + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 48..49, + value: Int( + 1, + ), + }, + ), + }, + ), + value: StringLiteral( + ExprStringLiteral { + range: 51..54, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 51..54, + value: "x", + flags: StringLiteralFlags { + quote_style: Single, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + generators: [ + Comprehension { + range: 55..72, + target: Name( + ExprName { + range: 59..60, + id: "i", + ctx: Store, + }, + ), + iter: Call( + ExprCall { + range: 64..72, + func: Name( + ExprName { + range: 64..69, + id: "range", + ctx: Load, + }, + ), + arguments: Arguments { + range: 69..72, + args: [ + NumberLiteral( + ExprNumberLiteral { + range: 70..71, + value: Int( + 5, + ), + }, + ), + ], + keywords: [], + }, + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 74..122, + value: DictComp( + ExprDictComp { + range: 74..122, + key: Name( + ExprName { + range: 75..76, + id: "b", + ctx: Load, + }, + ), + value: BinOp( + ExprBinOp { + range: 78..83, + left: Name( + ExprName { + range: 78..79, + id: "c", + ctx: Load, + }, + ), + op: Mult, + right: NumberLiteral( + ExprNumberLiteral { + range: 82..83, + value: Int( + 2, + ), + }, + ), + }, + ), + generators: [ + Comprehension { + range: 84..121, + target: Name( + ExprName { + range: 88..89, + id: "c", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 93..94, + id: "d", + ctx: Load, + }, + ), + ifs: [ + Compare( + ExprCompare { + range: 98..104, + left: Name( + ExprName { + range: 98..99, + id: "x", + ctx: Load, + }, + ), + ops: [ + In, + ], + comparators: [ + Name( + ExprName { + range: 103..104, + id: "w", + ctx: Load, + }, + ), + ], + }, + ), + BoolOp( + ExprBoolOp { + range: 108..116, + op: And, + values: [ + Name( + ExprName { + range: 108..109, + id: "y", + ctx: Load, + }, + ), + Name( + ExprName { + range: 114..116, + id: "yy", + ctx: Load, + }, + ), + ], + }, + ), + Name( + ExprName { + range: 120..121, + id: "z", + ctx: Load, + }, + ), + ], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 123..176, + value: DictComp( + ExprDictComp { + range: 123..176, + key: Name( + ExprName { + range: 124..125, + id: "a", + ctx: Load, + }, + ), + value: BinOp( + ExprBinOp { + range: 127..133, + left: Name( + ExprName { + range: 127..128, + id: "a", + ctx: Load, + }, + ), + op: Pow, + right: NumberLiteral( + ExprNumberLiteral { + range: 132..133, + value: Int( + 2, + ), + }, + ), + }, + ), + generators: [ + Comprehension { + range: 134..155, + target: Name( + ExprName { + range: 138..139, + id: "b", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 143..144, + id: "c", + ctx: Load, + }, + ), + ifs: [ + BoolOp( + ExprBoolOp { + range: 148..155, + op: And, + values: [ + Name( + ExprName { + range: 148..149, + id: "d", + ctx: Load, + }, + ), + Name( + ExprName { + range: 154..155, + id: "e", + ctx: Load, + }, + ), + ], + }, + ), + ], + is_async: false, + }, + Comprehension { + range: 156..175, + target: Name( + ExprName { + range: 160..161, + id: "f", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 165..166, + id: "j", + ctx: Load, + }, + ), + ifs: [ + Compare( + ExprCompare { + range: 170..175, + left: Name( + ExprName { + range: 170..171, + id: "k", + ctx: Load, + }, + ), + ops: [ + Gt, + ], + comparators: [ + Name( + ExprName { + range: 174..175, + id: "h", + ctx: Load, + }, + ), + ], + }, + ), + ], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 177..231, + value: DictComp( + ExprDictComp { + range: 177..231, + key: Name( + ExprName { + range: 178..179, + id: "a", + ctx: Load, + }, + ), + value: Name( + ExprName { + range: 181..182, + id: "b", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 183..204, + target: Name( + ExprName { + range: 187..188, + id: "b", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 192..193, + id: "c", + ctx: Load, + }, + ), + ifs: [ + BoolOp( + ExprBoolOp { + range: 197..204, + op: And, + values: [ + Name( + ExprName { + range: 197..198, + id: "d", + ctx: Load, + }, + ), + Name( + ExprName { + range: 203..204, + id: "e", + ctx: Load, + }, + ), + ], + }, + ), + ], + is_async: false, + }, + Comprehension { + range: 205..230, + target: Name( + ExprName { + range: 215..216, + id: "f", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 220..221, + id: "j", + ctx: Load, + }, + ), + ifs: [ + Compare( + ExprCompare { + range: 225..230, + left: Name( + ExprName { + range: 225..226, + id: "k", + ctx: Load, + }, + ), + ops: [ + Gt, + ], + comparators: [ + Name( + ExprName { + range: 229..230, + id: "h", + ctx: Load, + }, + ), + ], + }, + ), + ], + is_async: true, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 232..252, + value: DictComp( + ExprDictComp { + range: 232..252, + key: Name( + ExprName { + range: 233..234, + id: "a", + ctx: Load, + }, + ), + value: Name( + ExprName { + range: 236..237, + id: "a", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 238..251, + target: Tuple( + ExprTuple { + range: 242..246, + elts: [ + Name( + ExprName { + range: 242..243, + id: "b", + ctx: Store, + }, + ), + Name( + ExprName { + range: 245..246, + id: "c", + ctx: Store, + }, + ), + ], + ctx: Store, + parenthesized: false, + }, + ), + iter: Name( + ExprName { + range: 250..251, + id: "d", + ctx: Load, + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 391..416, + value: DictComp( + ExprDictComp { + range: 391..416, + key: Name( + ExprName { + range: 392..393, + id: "x", + ctx: Load, + }, + ), + value: Name( + ExprName { + range: 395..396, + id: "y", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 397..415, + target: Name( + ExprName { + range: 401..402, + id: "x", + ctx: Store, + }, + ), + iter: Yield( + ExprYield { + range: 407..414, + value: Some( + Name( + ExprName { + range: 413..414, + id: "y", + ctx: Load, + }, + ), + ), + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 417..447, + value: DictComp( + ExprDictComp { + range: 417..447, + key: Name( + ExprName { + range: 418..419, + id: "x", + ctx: Load, + }, + ), + value: Name( + ExprName { + range: 421..422, + id: "y", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 423..446, + target: Name( + ExprName { + range: 427..428, + id: "x", + ctx: Store, + }, + ), + iter: YieldFrom( + ExprYieldFrom { + range: 433..445, + value: Name( + ExprName { + range: 444..445, + id: "y", + ctx: Load, + }, + ), + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 448..477, + value: DictComp( + ExprDictComp { + range: 448..477, + key: Name( + ExprName { + range: 449..450, + id: "x", + ctx: Load, + }, + ), + value: Name( + ExprName { + range: 452..453, + id: "y", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 454..476, + target: Name( + ExprName { + range: 458..459, + id: "x", + ctx: Store, + }, + ), + iter: Lambda( + ExprLambda { + range: 464..475, + parameters: Some( + Parameters { + range: 471..472, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 471..472, + parameter: Parameter { + range: 471..472, + name: Identifier { + id: "y", + range: 471..472, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: Name( + ExprName { + range: 474..475, + id: "y", + ctx: Load, + }, + ), + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 478..511, + value: DictComp( + ExprDictComp { + range: 478..511, + key: Name( + ExprName { + range: 479..480, + id: "x", + ctx: Load, + }, + ), + value: Name( + ExprName { + range: 482..483, + id: "y", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 484..510, + target: Name( + ExprName { + range: 488..489, + id: "x", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 493..497, + id: "data", + ctx: Load, + }, + ), + ifs: [ + Yield( + ExprYield { + range: 502..509, + value: Some( + Name( + ExprName { + range: 508..509, + id: "y", + ctx: Load, + }, + ), + ), + }, + ), + ], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 512..550, + value: DictComp( + ExprDictComp { + range: 512..550, + key: Name( + ExprName { + range: 513..514, + id: "x", + ctx: Load, + }, + ), + value: Name( + ExprName { + range: 516..517, + id: "y", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 518..549, + target: Name( + ExprName { + range: 522..523, + id: "x", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 527..531, + id: "data", + ctx: Load, + }, + ), + ifs: [ + YieldFrom( + ExprYieldFrom { + range: 536..548, + value: Name( + ExprName { + range: 547..548, + id: "y", + ctx: Load, + }, + ), + }, + ), + ], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 551..588, + value: DictComp( + ExprDictComp { + range: 551..588, + key: Name( + ExprName { + range: 552..553, + id: "x", + ctx: Load, + }, + ), + value: Name( + ExprName { + range: 555..556, + id: "y", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 557..587, + target: Name( + ExprName { + range: 561..562, + id: "x", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 566..570, + id: "data", + ctx: Load, + }, + ), + ifs: [ + Lambda( + ExprLambda { + range: 575..586, + parameters: Some( + Parameters { + range: 582..583, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 582..583, + parameter: Parameter { + range: 582..583, + name: Identifier { + id: "y", + range: 582..583, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: Name( + ExprName { + range: 585..586, + id: "y", + ctx: Load, + }, + ), + }, + ), + ], + is_async: false, + }, + ], + }, + ), + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__f_string.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__f_string.py.snap.new new file mode 100644 index 0000000000..a423e07d4f --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__f_string.py.snap.new @@ -0,0 +1,2703 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/valid/expressions/f_string.py +--- +## AST + +``` +Module( + ModModule { + range: 0..979, + body: [ + Expr( + StmtExpr { + range: 18..21, + value: FString( + ExprFString { + range: 18..21, + value: FStringValue { + inner: Single( + FString( + FString { + range: 18..21, + elements: [], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 22..25, + value: FString( + ExprFString { + range: 22..25, + value: FStringValue { + inner: Single( + FString( + FString { + range: 22..25, + elements: [], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 26..29, + value: FString( + ExprFString { + range: 26..29, + value: FStringValue { + inner: Single( + FString( + FString { + range: 26..29, + elements: [], + flags: FStringFlags { + quote_style: Single, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 30..37, + value: FString( + ExprFString { + range: 30..37, + value: FStringValue { + inner: Single( + FString( + FString { + range: 30..37, + elements: [], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: true, + }, + }, + ), + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 38..45, + value: FString( + ExprFString { + range: 38..45, + value: FStringValue { + inner: Single( + FString( + FString { + range: 38..45, + elements: [], + flags: FStringFlags { + quote_style: Single, + prefix: Regular, + triple_quoted: true, + }, + }, + ), + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 47..56, + value: FString( + ExprFString { + range: 47..56, + value: FStringValue { + inner: Single( + FString( + FString { + range: 47..56, + elements: [ + Expression( + FStringExpressionElement { + range: 49..55, + expression: StringLiteral( + ExprStringLiteral { + range: 50..54, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 50..54, + value: " f", + flags: StringLiteralFlags { + quote_style: Double, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 57..67, + value: FString( + ExprFString { + range: 57..67, + value: FStringValue { + inner: Single( + FString( + FString { + range: 57..67, + elements: [ + Expression( + FStringExpressionElement { + range: 59..66, + expression: Name( + ExprName { + range: 60..63, + id: "foo", + ctx: Load, + }, + ), + debug_text: None, + conversion: Str, + format_spec: None, + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 68..75, + value: FString( + ExprFString { + range: 68..75, + value: FStringValue { + inner: Single( + FString( + FString { + range: 68..75, + elements: [ + Expression( + FStringExpressionElement { + range: 70..74, + expression: Tuple( + ExprTuple { + range: 71..73, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 71..72, + value: Int( + 3, + ), + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 76..86, + value: FString( + ExprFString { + range: 76..86, + value: FStringValue { + inner: Single( + FString( + FString { + range: 76..86, + elements: [ + Expression( + FStringExpressionElement { + range: 78..85, + expression: Compare( + ExprCompare { + range: 79..83, + left: NumberLiteral( + ExprNumberLiteral { + range: 79..80, + value: Int( + 3, + ), + }, + ), + ops: [ + NotEq, + ], + comparators: [ + NumberLiteral( + ExprNumberLiteral { + range: 82..83, + value: Int( + 4, + ), + }, + ), + ], + }, + ), + debug_text: None, + conversion: None, + format_spec: Some( + FStringFormatSpec { + range: 84..84, + elements: [], + }, + ), + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 87..102, + value: FString( + ExprFString { + range: 87..102, + value: FStringValue { + inner: Single( + FString( + FString { + range: 87..102, + elements: [ + Expression( + FStringExpressionElement { + range: 89..101, + expression: NumberLiteral( + ExprNumberLiteral { + range: 90..91, + value: Int( + 3, + ), + }, + ), + debug_text: None, + conversion: None, + format_spec: Some( + FStringFormatSpec { + range: 92..100, + elements: [ + Expression( + FStringExpressionElement { + range: 92..97, + expression: StringLiteral( + ExprStringLiteral { + range: 93..96, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 93..96, + value: "}", + flags: StringLiteralFlags { + quote_style: Double, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + Literal( + FStringLiteralElement { + range: 97..100, + value: ">10", + }, + ), + ], + }, + ), + }, + ), + ], + flags: FStringFlags { + quote_style: Single, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 103..118, + value: FString( + ExprFString { + range: 103..118, + value: FStringValue { + inner: Single( + FString( + FString { + range: 103..118, + elements: [ + Expression( + FStringExpressionElement { + range: 105..117, + expression: NumberLiteral( + ExprNumberLiteral { + range: 106..107, + value: Int( + 3, + ), + }, + ), + debug_text: None, + conversion: None, + format_spec: Some( + FStringFormatSpec { + range: 108..116, + elements: [ + Expression( + FStringExpressionElement { + range: 108..113, + expression: StringLiteral( + ExprStringLiteral { + range: 109..112, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 109..112, + value: "{", + flags: StringLiteralFlags { + quote_style: Double, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + Literal( + FStringLiteralElement { + range: 113..116, + value: ">10", + }, + ), + ], + }, + ), + }, + ), + ], + flags: FStringFlags { + quote_style: Single, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 119..133, + value: FString( + ExprFString { + range: 119..133, + value: FStringValue { + inner: Single( + FString( + FString { + range: 119..133, + elements: [ + Expression( + FStringExpressionElement { + range: 121..132, + expression: Name( + ExprName { + range: 124..127, + id: "foo", + ctx: Load, + }, + ), + debug_text: Some( + DebugText { + leading: " ", + trailing: " = ", + }, + ), + conversion: None, + format_spec: None, + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 134..154, + value: FString( + ExprFString { + range: 134..154, + value: FStringValue { + inner: Single( + FString( + FString { + range: 134..154, + elements: [ + Expression( + FStringExpressionElement { + range: 136..153, + expression: Name( + ExprName { + range: 139..142, + id: "foo", + ctx: Load, + }, + ), + debug_text: Some( + DebugText { + leading: " ", + trailing: " = ", + }, + ), + conversion: None, + format_spec: Some( + FStringFormatSpec { + range: 147..152, + elements: [ + Literal( + FStringLiteralElement { + range: 147..152, + value: ".3f ", + }, + ), + ], + }, + ), + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 155..173, + value: FString( + ExprFString { + range: 155..173, + value: FStringValue { + inner: Single( + FString( + FString { + range: 155..173, + elements: [ + Expression( + FStringExpressionElement { + range: 157..172, + expression: Name( + ExprName { + range: 160..163, + id: "foo", + ctx: Load, + }, + ), + debug_text: Some( + DebugText { + leading: " ", + trailing: " = ", + }, + ), + conversion: Str, + format_spec: None, + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 174..190, + value: FString( + ExprFString { + range: 174..190, + value: FStringValue { + inner: Single( + FString( + FString { + range: 174..190, + elements: [ + Expression( + FStringExpressionElement { + range: 176..189, + expression: Tuple( + ExprTuple { + range: 179..183, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 179..180, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 182..183, + value: Int( + 2, + ), + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + debug_text: Some( + DebugText { + leading: " ", + trailing: " = ", + }, + ), + conversion: None, + format_spec: None, + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 191..217, + value: FString( + ExprFString { + range: 191..217, + value: FStringValue { + inner: Single( + FString( + FString { + range: 191..217, + elements: [ + Expression( + FStringExpressionElement { + range: 193..216, + expression: FString( + ExprFString { + range: 194..210, + value: FStringValue { + inner: Single( + FString( + FString { + range: 194..210, + elements: [ + Expression( + FStringExpressionElement { + range: 196..209, + expression: NumberLiteral( + ExprNumberLiteral { + range: 197..203, + value: Float( + 3.1415, + ), + }, + ), + debug_text: Some( + DebugText { + leading: "", + trailing: "=", + }, + ), + conversion: None, + format_spec: Some( + FStringFormatSpec { + range: 205..208, + elements: [ + Literal( + FStringLiteralElement { + range: 205..208, + value: ".1f", + }, + ), + ], + }, + ), + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + debug_text: None, + conversion: None, + format_spec: Some( + FStringFormatSpec { + range: 211..215, + elements: [ + Literal( + FStringLiteralElement { + range: 211..215, + value: "*^20", + }, + ), + ], + }, + ), + }, + ), + ], + flags: FStringFlags { + quote_style: Single, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 219..253, + value: Dict( + ExprDict { + range: 219..253, + items: [ + DictItem { + key: Some( + FString( + ExprFString { + range: 220..248, + value: FStringValue { + inner: Concatenated( + [ + Literal( + StringLiteral { + range: 220..226, + value: "foo ", + flags: StringLiteralFlags { + quote_style: Double, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + FString( + FString { + range: 227..242, + elements: [ + Literal( + FStringLiteralElement { + range: 229..233, + value: "bar ", + }, + ), + Expression( + FStringExpressionElement { + range: 233..240, + expression: BinOp( + ExprBinOp { + range: 234..239, + left: Name( + ExprName { + range: 234..235, + id: "x", + ctx: Load, + }, + ), + op: Add, + right: Name( + ExprName { + range: 238..239, + id: "y", + ctx: Load, + }, + ), + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + Literal( + FStringLiteralElement { + range: 240..241, + value: " ", + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + Literal( + StringLiteral { + range: 243..248, + value: "baz", + flags: StringLiteralFlags { + quote_style: Double, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + ], + ), + }, + }, + ), + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 250..252, + value: Int( + 10, + ), + }, + ), + }, + ], + }, + ), + }, + ), + Match( + StmtMatch { + range: 254..345, + subject: Name( + ExprName { + range: 260..263, + id: "foo", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 269..293, + pattern: MatchValue( + PatternMatchValue { + range: 274..279, + value: StringLiteral( + ExprStringLiteral { + range: 274..279, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 274..279, + value: "one", + flags: StringLiteralFlags { + quote_style: Double, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + }, + ), + guard: None, + body: [ + Pass( + StmtPass { + range: 289..293, + }, + ), + ], + }, + MatchCase { + range: 298..345, + pattern: MatchValue( + PatternMatchValue { + range: 303..331, + value: StringLiteral( + ExprStringLiteral { + range: 303..331, + value: StringLiteralValue { + inner: Concatenated( + ConcatenatedStringLiteral { + strings: [ + StringLiteral { + range: 303..316, + value: "implicitly ", + flags: StringLiteralFlags { + quote_style: Double, + prefix: Empty, + triple_quoted: false, + }, + }, + StringLiteral { + range: 317..331, + value: "concatenated", + flags: StringLiteralFlags { + quote_style: Double, + prefix: Empty, + triple_quoted: false, + }, + }, + ], + value: "implicitly concatenated", + }, + ), + }, + }, + ), + }, + ), + guard: None, + body: [ + Pass( + StmtPass { + range: 341..345, + }, + ), + ], + }, + ], + }, + ), + Expr( + StmtExpr { + range: 347..364, + value: FString( + ExprFString { + range: 347..364, + value: FStringValue { + inner: Single( + FString( + FString { + range: 347..364, + elements: [ + Literal( + FStringLiteralElement { + range: 349..350, + value: "\\", + }, + ), + Expression( + FStringExpressionElement { + range: 350..355, + expression: Name( + ExprName { + range: 351..354, + id: "foo", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + Literal( + FStringLiteralElement { + range: 355..356, + value: "\\", + }, + ), + Expression( + FStringExpressionElement { + range: 356..363, + expression: Name( + ExprName { + range: 357..360, + id: "bar", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: Some( + FStringFormatSpec { + range: 361..362, + elements: [ + Literal( + FStringLiteralElement { + range: 361..362, + value: "\\", + }, + ), + ], + }, + ), + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 365..379, + value: FString( + ExprFString { + range: 365..379, + value: FStringValue { + inner: Single( + FString( + FString { + range: 365..379, + elements: [ + Literal( + FStringLiteralElement { + range: 367..378, + value: "\\{foo\\}", + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 380..420, + value: FString( + ExprFString { + range: 380..420, + value: FStringValue { + inner: Single( + FString( + FString { + range: 380..420, + elements: [ + Expression( + FStringExpressionElement { + range: 384..417, + expression: Name( + ExprName { + range: 390..393, + id: "foo", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: Some( + FStringFormatSpec { + range: 394..416, + elements: [ + Literal( + FStringLiteralElement { + range: 394..416, + value: "x\n y\n z\n", + }, + ), + ], + }, + ), + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: true, + }, + }, + ), + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 421..439, + value: FString( + ExprFString { + range: 421..439, + value: FStringValue { + inner: Single( + FString( + FString { + range: 421..439, + elements: [ + Expression( + FStringExpressionElement { + range: 423..438, + expression: Name( + ExprName { + range: 428..431, + id: "foo", + ctx: Load, + }, + ), + debug_text: Some( + DebugText { + leading: " ( ", + trailing: " ) = ", + }, + ), + conversion: None, + format_spec: None, + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 441..486, + value: FString( + ExprFString { + range: 441..486, + value: FStringValue { + inner: Single( + FString( + FString { + range: 441..486, + elements: [ + Literal( + FStringLiteralElement { + range: 443..450, + value: "normal ", + }, + ), + Expression( + FStringExpressionElement { + range: 450..455, + expression: Name( + ExprName { + range: 451..454, + id: "foo", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + Literal( + FStringLiteralElement { + range: 455..468, + value: " {another} ", + }, + ), + Expression( + FStringExpressionElement { + range: 468..473, + expression: Name( + ExprName { + range: 469..472, + id: "bar", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + Literal( + FStringLiteralElement { + range: 473..476, + value: " {", + }, + ), + Expression( + FStringExpressionElement { + range: 476..483, + expression: Name( + ExprName { + range: 477..482, + id: "three", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + Literal( + FStringLiteralElement { + range: 483..485, + value: "}", + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 487..529, + value: FString( + ExprFString { + range: 487..529, + value: FStringValue { + inner: Single( + FString( + FString { + range: 487..529, + elements: [ + Literal( + FStringLiteralElement { + range: 489..496, + value: "normal ", + }, + ), + Expression( + FStringExpressionElement { + range: 496..503, + expression: Name( + ExprName { + range: 497..500, + id: "foo", + ctx: Load, + }, + ), + debug_text: None, + conversion: Ascii, + format_spec: None, + }, + ), + Literal( + FStringLiteralElement { + range: 503..504, + value: " ", + }, + ), + Expression( + FStringExpressionElement { + range: 504..511, + expression: Name( + ExprName { + range: 505..508, + id: "bar", + ctx: Load, + }, + ), + debug_text: None, + conversion: Str, + format_spec: None, + }, + ), + Literal( + FStringLiteralElement { + range: 511..512, + value: " ", + }, + ), + Expression( + FStringExpressionElement { + range: 512..519, + expression: Name( + ExprName { + range: 513..516, + id: "baz", + ctx: Load, + }, + ), + debug_text: None, + conversion: Repr, + format_spec: None, + }, + ), + Literal( + FStringLiteralElement { + range: 519..520, + value: " ", + }, + ), + Expression( + FStringExpressionElement { + range: 520..528, + expression: Name( + ExprName { + range: 521..527, + id: "foobar", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 530..549, + value: FString( + ExprFString { + range: 530..549, + value: FStringValue { + inner: Single( + FString( + FString { + range: 530..549, + elements: [ + Literal( + FStringLiteralElement { + range: 532..539, + value: "normal ", + }, + ), + Expression( + FStringExpressionElement { + range: 539..548, + expression: Name( + ExprName { + range: 540..541, + id: "x", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: Some( + FStringFormatSpec { + range: 542..547, + elements: [ + Literal( + FStringLiteralElement { + range: 542..547, + value: "y + 2", + }, + ), + ], + }, + ), + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 550..568, + value: FString( + ExprFString { + range: 550..568, + value: FStringValue { + inner: Single( + FString( + FString { + range: 550..568, + elements: [ + Expression( + FStringExpressionElement { + range: 552..567, + expression: Name( + ExprName { + range: 553..554, + id: "x", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: Some( + FStringFormatSpec { + range: 555..566, + elements: [ + Expression( + FStringExpressionElement { + range: 555..566, + expression: Call( + ExprCall { + range: 556..565, + func: Attribute( + ExprAttribute { + range: 556..563, + value: Set( + ExprSet { + range: 556..559, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 557..558, + value: Int( + 1, + ), + }, + ), + ], + }, + ), + attr: Identifier { + id: "pop", + range: 560..563, + }, + ctx: Load, + }, + ), + arguments: Arguments { + range: 563..565, + args: [], + keywords: [], + }, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + ], + }, + ), + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 569..588, + value: FString( + ExprFString { + range: 569..588, + value: FStringValue { + inner: Single( + FString( + FString { + range: 569..588, + elements: [ + Expression( + FStringExpressionElement { + range: 571..587, + expression: Lambda( + ExprLambda { + range: 573..585, + parameters: Some( + Parameters { + range: 580..581, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 580..581, + parameter: Parameter { + range: 580..581, + name: Identifier { + id: "x", + range: 580..581, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: Set( + ExprSet { + range: 582..585, + elts: [ + Name( + ExprName { + range: 583..584, + id: "x", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 589..597, + value: FString( + ExprFString { + range: 589..597, + value: FStringValue { + inner: Single( + FString( + FString { + range: 589..597, + elements: [ + Expression( + FStringExpressionElement { + range: 591..596, + expression: Name( + ExprName { + range: 592..593, + id: "x", + ctx: Load, + }, + ), + debug_text: Some( + DebugText { + leading: "", + trailing: " =", + }, + ), + conversion: None, + format_spec: None, + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 598..611, + value: FString( + ExprFString { + range: 598..611, + value: FStringValue { + inner: Single( + FString( + FString { + range: 598..611, + elements: [ + Expression( + FStringExpressionElement { + range: 600..610, + expression: Name( + ExprName { + range: 605..606, + id: "x", + ctx: Load, + }, + ), + debug_text: Some( + DebugText { + leading: " ", + trailing: " = ", + }, + ), + conversion: None, + format_spec: None, + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 612..621, + value: FString( + ExprFString { + range: 612..621, + value: FStringValue { + inner: Single( + FString( + FString { + range: 612..621, + elements: [ + Expression( + FStringExpressionElement { + range: 614..620, + expression: Name( + ExprName { + range: 615..616, + id: "x", + ctx: Load, + }, + ), + debug_text: Some( + DebugText { + leading: "", + trailing: "=", + }, + ), + conversion: Ascii, + format_spec: None, + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 622..636, + value: FString( + ExprFString { + range: 622..636, + value: FStringValue { + inner: Single( + FString( + FString { + range: 622..636, + elements: [ + Expression( + FStringExpressionElement { + range: 624..635, + expression: Name( + ExprName { + range: 625..626, + id: "x", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: Some( + FStringFormatSpec { + range: 627..634, + elements: [ + Literal( + FStringLiteralElement { + range: 627..634, + value: ".3f!r =", + }, + ), + ], + }, + ), + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 637..653, + value: FString( + ExprFString { + range: 637..653, + value: FStringValue { + inner: Single( + FString( + FString { + range: 637..653, + elements: [ + Expression( + FStringExpressionElement { + range: 639..652, + expression: Name( + ExprName { + range: 640..641, + id: "x", + ctx: Load, + }, + ), + debug_text: Some( + DebugText { + leading: "", + trailing: " = ", + }, + ), + conversion: Repr, + format_spec: Some( + FStringFormatSpec { + range: 648..651, + elements: [ + Literal( + FStringLiteralElement { + range: 648..651, + value: ".3f", + }, + ), + ], + }, + ), + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 654..667, + value: FString( + ExprFString { + range: 654..667, + value: FStringValue { + inner: Single( + FString( + FString { + range: 654..667, + elements: [ + Expression( + FStringExpressionElement { + range: 656..666, + expression: Name( + ExprName { + range: 657..658, + id: "x", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: Some( + FStringFormatSpec { + range: 659..665, + elements: [ + Literal( + FStringLiteralElement { + range: 659..665, + value: ".3f=!r", + }, + ), + ], + }, + ), + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 668..682, + value: FString( + ExprFString { + range: 668..682, + value: FStringValue { + inner: Concatenated( + [ + Literal( + StringLiteral { + range: 668..675, + value: "hello", + flags: StringLiteralFlags { + quote_style: Double, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + FString( + FString { + range: 676..682, + elements: [ + Expression( + FStringExpressionElement { + range: 678..681, + expression: Name( + ExprName { + range: 679..680, + id: "x", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ], + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 683..696, + value: FString( + ExprFString { + range: 683..696, + value: FStringValue { + inner: Concatenated( + [ + FString( + FString { + range: 683..689, + elements: [ + Expression( + FStringExpressionElement { + range: 685..688, + expression: Name( + ExprName { + range: 686..687, + id: "x", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + FString( + FString { + range: 690..696, + elements: [ + Expression( + FStringExpressionElement { + range: 692..695, + expression: Name( + ExprName { + range: 693..694, + id: "y", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ], + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 697..711, + value: FString( + ExprFString { + range: 697..711, + value: FStringValue { + inner: Concatenated( + [ + FString( + FString { + range: 697..703, + elements: [ + Expression( + FStringExpressionElement { + range: 699..702, + expression: Name( + ExprName { + range: 700..701, + id: "x", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + Literal( + StringLiteral { + range: 704..711, + value: "world", + flags: StringLiteralFlags { + quote_style: Double, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + ], + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 712..756, + value: FString( + ExprFString { + range: 712..756, + value: FStringValue { + inner: Single( + FString( + FString { + range: 712..756, + elements: [ + Literal( + FStringLiteralElement { + range: 714..739, + value: "Invalid args in command: ", + }, + ), + Expression( + FStringExpressionElement { + range: 739..755, + expression: Tuple( + ExprTuple { + range: 740..754, + elts: [ + Name( + ExprName { + range: 740..747, + id: "command", + ctx: Load, + }, + ), + Starred( + ExprStarred { + range: 749..754, + value: Name( + ExprName { + range: 750..754, + id: "args", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 757..775, + value: FString( + ExprFString { + range: 757..775, + value: FStringValue { + inner: Concatenated( + [ + Literal( + StringLiteral { + range: 757..762, + value: "foo", + flags: StringLiteralFlags { + quote_style: Double, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + FString( + FString { + range: 763..769, + elements: [ + Expression( + FStringExpressionElement { + range: 765..768, + expression: Name( + ExprName { + range: 766..767, + id: "x", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + Literal( + StringLiteral { + range: 770..775, + value: "bar", + flags: StringLiteralFlags { + quote_style: Double, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + ], + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 776..825, + value: FString( + ExprFString { + range: 782..823, + value: FStringValue { + inner: Concatenated( + [ + FString( + FString { + range: 782..786, + elements: [ + Literal( + FStringLiteralElement { + range: 784..785, + value: "a", + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + FString( + FString { + range: 791..795, + elements: [ + Literal( + FStringLiteralElement { + range: 793..794, + value: "b", + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + Literal( + StringLiteral { + range: 800..803, + value: "c", + flags: StringLiteralFlags { + quote_style: Double, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + FString( + FString { + range: 808..813, + elements: [ + Literal( + FStringLiteralElement { + range: 811..812, + value: "d", + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Raw { + uppercase_r: false, + }, + triple_quoted: false, + }, + }, + ), + FString( + FString { + range: 818..823, + elements: [ + Literal( + FStringLiteralElement { + range: 821..822, + value: "e", + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Raw { + uppercase_r: false, + }, + triple_quoted: false, + }, + }, + ), + ], + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 850..879, + value: FString( + ExprFString { + range: 850..879, + value: FStringValue { + inner: Concatenated( + [ + Literal( + StringLiteral { + range: 850..856, + value: "foo", + flags: StringLiteralFlags { + quote_style: Double, + prefix: Unicode, + triple_quoted: false, + }, + }, + ), + FString( + FString { + range: 857..865, + elements: [ + Expression( + FStringExpressionElement { + range: 859..864, + expression: Name( + ExprName { + range: 860..863, + id: "bar", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + Literal( + StringLiteral { + range: 866..871, + value: "baz", + flags: StringLiteralFlags { + quote_style: Double, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + Literal( + StringLiteral { + range: 872..879, + value: " some", + flags: StringLiteralFlags { + quote_style: Double, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + ], + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 880..909, + value: FString( + ExprFString { + range: 880..909, + value: FStringValue { + inner: Concatenated( + [ + Literal( + StringLiteral { + range: 880..885, + value: "foo", + flags: StringLiteralFlags { + quote_style: Double, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + FString( + FString { + range: 886..894, + elements: [ + Expression( + FStringExpressionElement { + range: 888..893, + expression: Name( + ExprName { + range: 889..892, + id: "bar", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + Literal( + StringLiteral { + range: 895..901, + value: "baz", + flags: StringLiteralFlags { + quote_style: Double, + prefix: Unicode, + triple_quoted: false, + }, + }, + ), + Literal( + StringLiteral { + range: 902..909, + value: " some", + flags: StringLiteralFlags { + quote_style: Double, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + ], + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 910..939, + value: FString( + ExprFString { + range: 910..939, + value: FStringValue { + inner: Concatenated( + [ + Literal( + StringLiteral { + range: 910..915, + value: "foo", + flags: StringLiteralFlags { + quote_style: Double, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + FString( + FString { + range: 916..924, + elements: [ + Expression( + FStringExpressionElement { + range: 918..923, + expression: Name( + ExprName { + range: 919..922, + id: "bar", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + Literal( + StringLiteral { + range: 925..930, + value: "baz", + flags: StringLiteralFlags { + quote_style: Double, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + Literal( + StringLiteral { + range: 931..939, + value: " some", + flags: StringLiteralFlags { + quote_style: Double, + prefix: Unicode, + triple_quoted: false, + }, + }, + ), + ], + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 940..978, + value: FString( + ExprFString { + range: 940..978, + value: FStringValue { + inner: Concatenated( + [ + Literal( + StringLiteral { + range: 940..946, + value: "foo", + flags: StringLiteralFlags { + quote_style: Double, + prefix: Unicode, + triple_quoted: false, + }, + }, + ), + FString( + FString { + range: 947..966, + elements: [ + Literal( + FStringLiteralElement { + range: 949..953, + value: "bar ", + }, + ), + Expression( + FStringExpressionElement { + range: 953..958, + expression: Name( + ExprName { + range: 954..957, + id: "baz", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + Literal( + FStringLiteralElement { + range: 958..965, + value: " really", + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + Literal( + StringLiteral { + range: 967..973, + value: "bar", + flags: StringLiteralFlags { + quote_style: Double, + prefix: Unicode, + triple_quoted: false, + }, + }, + ), + Literal( + StringLiteral { + range: 974..978, + value: "no", + flags: StringLiteralFlags { + quote_style: Double, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + ], + ), + }, + }, + ), + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__generator.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__generator.py.snap.new new file mode 100644 index 0000000000..90cc91cdec --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__generator.py.snap.new @@ -0,0 +1,698 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/valid/expressions/generator.py +--- +## AST + +``` +Module( + ModModule { + range: 0..482, + body: [ + Expr( + StmtExpr { + range: 0..22, + value: Generator( + ExprGenerator { + range: 0..22, + elt: Name( + ExprName { + range: 1..2, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 3..21, + target: Name( + ExprName { + range: 7..13, + id: "target", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 17..21, + id: "iter", + ctx: Load, + }, + ), + ifs: [], + is_async: false, + }, + ], + parenthesized: true, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 23..51, + value: Generator( + ExprGenerator { + range: 23..51, + elt: Name( + ExprName { + range: 24..25, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 26..50, + target: Name( + ExprName { + range: 36..42, + id: "target", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 46..50, + id: "iter", + ctx: Load, + }, + ), + ifs: [], + is_async: true, + }, + ], + parenthesized: true, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 52..100, + value: Generator( + ExprGenerator { + range: 52..100, + elt: Name( + ExprName { + range: 53..54, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 55..99, + target: Name( + ExprName { + range: 59..65, + id: "target", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 69..73, + id: "iter", + ctx: Load, + }, + ), + ifs: [ + Compare( + ExprCompare { + range: 77..83, + left: Name( + ExprName { + range: 77..78, + id: "x", + ctx: Load, + }, + ), + ops: [ + In, + ], + comparators: [ + Name( + ExprName { + range: 82..83, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + BoolOp( + ExprBoolOp { + range: 87..94, + op: And, + values: [ + Name( + ExprName { + range: 87..88, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 93..94, + id: "b", + ctx: Load, + }, + ), + ], + }, + ), + Name( + ExprName { + range: 98..99, + id: "c", + ctx: Load, + }, + ), + ], + is_async: false, + }, + ], + parenthesized: true, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 101..166, + value: Generator( + ExprGenerator { + range: 101..166, + elt: Name( + ExprName { + range: 102..103, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 104..135, + target: Name( + ExprName { + range: 108..115, + id: "target1", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 119..124, + id: "iter1", + ctx: Load, + }, + ), + ifs: [ + BoolOp( + ExprBoolOp { + range: 128..135, + op: And, + values: [ + Name( + ExprName { + range: 128..129, + id: "x", + ctx: Load, + }, + ), + Name( + ExprName { + range: 134..135, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + ], + is_async: false, + }, + Comprehension { + range: 136..165, + target: Name( + ExprName { + range: 140..147, + id: "target2", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 151..156, + id: "iter2", + ctx: Load, + }, + ), + ifs: [ + Compare( + ExprCompare { + range: 160..165, + left: Name( + ExprName { + range: 160..161, + id: "a", + ctx: Load, + }, + ), + ops: [ + Gt, + ], + comparators: [ + Name( + ExprName { + range: 164..165, + id: "b", + ctx: Load, + }, + ), + ], + }, + ), + ], + is_async: false, + }, + ], + parenthesized: true, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 167..238, + value: Generator( + ExprGenerator { + range: 167..238, + elt: Name( + ExprName { + range: 168..169, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 170..201, + target: Name( + ExprName { + range: 174..181, + id: "target1", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 185..190, + id: "iter1", + ctx: Load, + }, + ), + ifs: [ + BoolOp( + ExprBoolOp { + range: 194..201, + op: And, + values: [ + Name( + ExprName { + range: 194..195, + id: "x", + ctx: Load, + }, + ), + Name( + ExprName { + range: 200..201, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + ], + is_async: false, + }, + Comprehension { + range: 202..237, + target: Name( + ExprName { + range: 212..219, + id: "target2", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 223..228, + id: "iter2", + ctx: Load, + }, + ), + ifs: [ + Compare( + ExprCompare { + range: 232..237, + left: Name( + ExprName { + range: 232..233, + id: "a", + ctx: Load, + }, + ), + ops: [ + Gt, + ], + comparators: [ + Name( + ExprName { + range: 236..237, + id: "b", + ctx: Load, + }, + ), + ], + }, + ), + ], + is_async: true, + }, + ], + parenthesized: true, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 259..282, + value: Generator( + ExprGenerator { + range: 259..282, + elt: Named( + ExprNamed { + range: 260..270, + target: Name( + ExprName { + range: 260..261, + id: "x", + ctx: Store, + }, + ), + value: BinOp( + ExprBinOp { + range: 265..270, + left: Name( + ExprName { + range: 265..266, + id: "y", + ctx: Load, + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 269..270, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + generators: [ + Comprehension { + range: 271..281, + target: Name( + ExprName { + range: 275..276, + id: "y", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 280..281, + id: "z", + ctx: Load, + }, + ), + ifs: [], + is_async: false, + }, + ], + parenthesized: true, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 300..326, + value: Generator( + ExprGenerator { + range: 300..326, + elt: If( + ExprIf { + range: 301..314, + test: Name( + ExprName { + range: 306..307, + id: "y", + ctx: Load, + }, + ), + body: Name( + ExprName { + range: 301..302, + id: "x", + ctx: Load, + }, + ), + orelse: Name( + ExprName { + range: 313..314, + id: "y", + ctx: Load, + }, + ), + }, + ), + generators: [ + Comprehension { + range: 315..325, + target: Name( + ExprName { + range: 319..320, + id: "y", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 324..325, + id: "z", + ctx: Load, + }, + ), + ifs: [], + is_async: false, + }, + ], + parenthesized: true, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 340..481, + value: Call( + ExprCall { + range: 340..481, + func: Attribute( + ExprAttribute { + range: 340..348, + value: StringLiteral( + ExprStringLiteral { + range: 340..343, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 340..343, + value: " ", + flags: StringLiteralFlags { + quote_style: Double, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + attr: Identifier { + id: "join", + range: 344..348, + }, + ctx: Load, + }, + ), + arguments: Arguments { + range: 348..481, + args: [ + Generator( + ExprGenerator { + range: 354..479, + elt: Name( + ExprName { + range: 354..357, + id: "sql", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 362..479, + target: Name( + ExprName { + range: 366..369, + id: "sql", + ctx: Store, + }, + ), + iter: Tuple( + ExprTuple { + range: 373..479, + elts: [ + If( + ExprIf { + range: 383..420, + test: Name( + ExprName { + range: 405..410, + id: "limit", + ctx: Load, + }, + ), + body: BinOp( + ExprBinOp { + range: 383..401, + left: StringLiteral( + ExprStringLiteral { + range: 383..393, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 383..393, + value: "LIMIT %d", + flags: StringLiteralFlags { + quote_style: Double, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + op: Mod, + right: Name( + ExprName { + range: 396..401, + id: "limit", + ctx: Load, + }, + ), + }, + ), + orelse: NoneLiteral( + ExprNoneLiteral { + range: 416..420, + }, + ), + }, + ), + If( + ExprIf { + range: 430..472, + test: Name( + ExprName { + range: 456..462, + id: "offset", + ctx: Load, + }, + ), + body: BinOp( + ExprBinOp { + range: 431..451, + left: StringLiteral( + ExprStringLiteral { + range: 431..442, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 431..442, + value: "OFFSET %d", + flags: StringLiteralFlags { + quote_style: Double, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + op: Mod, + right: Name( + ExprName { + range: 445..451, + id: "offset", + ctx: Load, + }, + ), + }, + ), + orelse: NoneLiteral( + ExprNoneLiteral { + range: 468..472, + }, + ), + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + ifs: [], + is_async: false, + }, + ], + parenthesized: false, + }, + ), + ], + keywords: [], + }, + }, + ), + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__if.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__if.py.snap.new new file mode 100644 index 0000000000..1c3527c88c --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__if.py.snap.new @@ -0,0 +1,614 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/valid/expressions/if.py +--- +## AST + +``` +Module( + ModModule { + range: 0..423, + body: [ + Expr( + StmtExpr { + range: 0..16, + value: If( + ExprIf { + range: 0..16, + test: BooleanLiteral( + ExprBooleanLiteral { + range: 5..9, + value: true, + }, + ), + body: Name( + ExprName { + range: 0..1, + id: "a", + ctx: Load, + }, + ), + orelse: Name( + ExprName { + range: 15..16, + id: "b", + ctx: Load, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 17..35, + value: If( + ExprIf { + range: 17..35, + test: Name( + ExprName { + range: 24..25, + id: "x", + ctx: Load, + }, + ), + body: Call( + ExprCall { + range: 17..20, + func: Name( + ExprName { + range: 17..18, + id: "f", + ctx: Load, + }, + ), + arguments: Arguments { + range: 18..20, + args: [], + keywords: [], + }, + }, + ), + orelse: NoneLiteral( + ExprNoneLiteral { + range: 31..35, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 36..61, + value: If( + ExprIf { + range: 36..61, + test: Name( + ExprName { + range: 41..42, + id: "b", + ctx: Load, + }, + ), + body: Name( + ExprName { + range: 36..37, + id: "a", + ctx: Load, + }, + ), + orelse: If( + ExprIf { + range: 48..61, + test: Name( + ExprName { + range: 53..54, + id: "d", + ctx: Load, + }, + ), + body: Name( + ExprName { + range: 48..49, + id: "c", + ctx: Load, + }, + ), + orelse: Name( + ExprName { + range: 60..61, + id: "e", + ctx: Load, + }, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 62..84, + value: If( + ExprIf { + range: 62..84, + test: Compare( + ExprCompare { + range: 71..76, + left: NumberLiteral( + ExprNumberLiteral { + range: 71..72, + value: Int( + 1, + ), + }, + ), + ops: [ + Lt, + ], + comparators: [ + NumberLiteral( + ExprNumberLiteral { + range: 75..76, + value: Int( + 0, + ), + }, + ), + ], + }, + ), + body: BinOp( + ExprBinOp { + range: 62..67, + left: NumberLiteral( + ExprNumberLiteral { + range: 62..63, + value: Int( + 1, + ), + }, + ), + op: Add, + right: Name( + ExprName { + range: 66..67, + id: "x", + ctx: Load, + }, + ), + }, + ), + orelse: UnaryOp( + ExprUnaryOp { + range: 82..84, + op: USub, + operand: NumberLiteral( + ExprNumberLiteral { + range: 83..84, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 85..108, + value: If( + ExprIf { + range: 85..108, + test: Name( + ExprName { + range: 96..97, + id: "x", + ctx: Load, + }, + ), + body: BoolOp( + ExprBoolOp { + range: 85..92, + op: And, + values: [ + Name( + ExprName { + range: 85..86, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 91..92, + id: "b", + ctx: Load, + }, + ), + ], + }, + ), + orelse: BooleanLiteral( + ExprBooleanLiteral { + range: 103..108, + value: false, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 109..127, + value: If( + ExprIf { + range: 109..127, + test: Name( + ExprName { + range: 119..120, + id: "y", + ctx: Load, + }, + ), + body: Compare( + ExprCompare { + range: 109..115, + left: Name( + ExprName { + range: 109..110, + id: "x", + ctx: Load, + }, + ), + ops: [ + LtE, + ], + comparators: [ + Name( + ExprName { + range: 114..115, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + orelse: Name( + ExprName { + range: 126..127, + id: "x", + ctx: Load, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 128..154, + value: If( + ExprIf { + range: 128..154, + test: BoolOp( + ExprBoolOp { + range: 136..143, + op: And, + values: [ + Name( + ExprName { + range: 136..137, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 142..143, + id: "b", + ctx: Load, + }, + ), + ], + }, + ), + body: BooleanLiteral( + ExprBooleanLiteral { + range: 128..132, + value: true, + }, + ), + orelse: BooleanLiteral( + ExprBooleanLiteral { + range: 149..154, + value: false, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 155..171, + value: Tuple( + ExprTuple { + range: 155..171, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 155..156, + value: Int( + 1, + ), + }, + ), + If( + ExprIf { + range: 158..171, + test: Name( + ExprName { + range: 163..164, + id: "a", + ctx: Load, + }, + ), + body: NumberLiteral( + ExprNumberLiteral { + range: 158..159, + value: Int( + 1, + ), + }, + ), + orelse: Name( + ExprName { + range: 170..171, + id: "c", + ctx: Load, + }, + ), + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 214..240, + value: If( + ExprIf { + range: 214..240, + test: BooleanLiteral( + ExprBooleanLiteral { + range: 219..223, + value: true, + }, + ), + body: Name( + ExprName { + range: 214..215, + id: "x", + ctx: Load, + }, + ), + orelse: Lambda( + ExprLambda { + range: 229..240, + parameters: Some( + Parameters { + range: 236..237, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 236..237, + parameter: Parameter { + range: 236..237, + name: Identifier { + id: "y", + range: 236..237, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: Name( + ExprName { + range: 239..240, + id: "y", + ctx: Load, + }, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 302..323, + value: If( + ExprIf { + range: 302..323, + test: Yield( + ExprYield { + range: 308..315, + value: Some( + Name( + ExprName { + range: 314..315, + id: "x", + ctx: Load, + }, + ), + ), + }, + ), + body: Name( + ExprName { + range: 302..303, + id: "x", + ctx: Load, + }, + ), + orelse: Name( + ExprName { + range: 322..323, + id: "y", + ctx: Load, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 324..350, + value: If( + ExprIf { + range: 324..350, + test: YieldFrom( + ExprYieldFrom { + range: 330..342, + value: Name( + ExprName { + range: 341..342, + id: "x", + ctx: Load, + }, + ), + }, + ), + body: Name( + ExprName { + range: 324..325, + id: "x", + ctx: Load, + }, + ), + orelse: Name( + ExprName { + range: 349..350, + id: "y", + ctx: Load, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 351..376, + value: If( + ExprIf { + range: 351..376, + test: Lambda( + ExprLambda { + range: 357..368, + parameters: Some( + Parameters { + range: 364..365, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 364..365, + parameter: Parameter { + range: 364..365, + name: Identifier { + id: "x", + range: 364..365, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: Name( + ExprName { + range: 367..368, + id: "x", + ctx: Load, + }, + ), + }, + ), + body: Name( + ExprName { + range: 351..352, + id: "x", + ctx: Load, + }, + ), + orelse: Name( + ExprName { + range: 375..376, + id: "y", + ctx: Load, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 408..423, + value: If( + ExprIf { + range: 409..422, + test: Name( + ExprName { + range: 414..415, + id: "y", + ctx: Load, + }, + ), + body: Name( + ExprName { + range: 409..410, + id: "x", + ctx: Load, + }, + ), + orelse: Name( + ExprName { + range: 421..422, + id: "z", + ctx: Load, + }, + ), + }, + ), + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__lambda.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__lambda.py.snap.new new file mode 100644 index 0000000000..794f58e6ae --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__lambda.py.snap.new @@ -0,0 +1,1522 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/valid/expressions/lambda.py +--- +## AST + +``` +Module( + ModModule { + range: 0..530, + body: [ + Expr( + StmtExpr { + range: 0..9, + value: Lambda( + ExprLambda { + range: 0..9, + parameters: None, + body: Name( + ExprName { + range: 8..9, + id: "a", + ctx: Load, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 10..19, + value: Lambda( + ExprLambda { + range: 10..19, + parameters: None, + body: NumberLiteral( + ExprNumberLiteral { + range: 18..19, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 20..31, + value: Lambda( + ExprLambda { + range: 20..31, + parameters: Some( + Parameters { + range: 27..28, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 27..28, + parameter: Parameter { + range: 27..28, + name: Identifier { + id: "x", + range: 27..28, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: NumberLiteral( + ExprNumberLiteral { + range: 30..31, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 32..48, + value: Lambda( + ExprLambda { + range: 32..48, + parameters: Some( + Parameters { + range: 39..43, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 39..40, + parameter: Parameter { + range: 39..40, + name: Identifier { + id: "x", + range: 39..40, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 42..43, + parameter: Parameter { + range: 42..43, + name: Identifier { + id: "y", + range: 42..43, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: EllipsisLiteral( + ExprEllipsisLiteral { + range: 45..48, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 49..66, + value: Lambda( + ExprLambda { + range: 49..66, + parameters: Some( + Parameters { + range: 56..63, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 56..57, + parameter: Parameter { + range: 56..57, + name: Identifier { + id: "a", + range: 56..57, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 59..60, + parameter: Parameter { + range: 59..60, + name: Identifier { + id: "b", + range: 59..60, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 62..63, + parameter: Parameter { + range: 62..63, + name: Identifier { + id: "c", + range: 62..63, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: NumberLiteral( + ExprNumberLiteral { + range: 65..66, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 67..90, + value: Lambda( + ExprLambda { + range: 67..90, + parameters: Some( + Parameters { + range: 74..87, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 74..75, + parameter: Parameter { + range: 74..75, + name: Identifier { + id: "a", + range: 74..75, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 77..81, + parameter: Parameter { + range: 77..78, + name: Identifier { + id: "b", + range: 77..78, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 79..81, + value: Int( + 20, + ), + }, + ), + ), + }, + ParameterWithDefault { + range: 83..87, + parameter: Parameter { + range: 83..84, + name: Identifier { + id: "c", + range: 83..84, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 85..87, + value: Int( + 30, + ), + }, + ), + ), + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: NumberLiteral( + ExprNumberLiteral { + range: 89..90, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 91..109, + value: Lambda( + ExprLambda { + range: 91..109, + parameters: Some( + Parameters { + range: 98..102, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 98..99, + parameter: Parameter { + range: 98..99, + name: Identifier { + id: "x", + range: 98..99, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 101..102, + parameter: Parameter { + range: 101..102, + name: Identifier { + id: "y", + range: 101..102, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: BinOp( + ExprBinOp { + range: 104..109, + left: Name( + ExprName { + range: 104..105, + id: "x", + ctx: Load, + }, + ), + op: Mult, + right: Name( + ExprName { + range: 108..109, + id: "y", + ctx: Load, + }, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 110..130, + value: Lambda( + ExprLambda { + range: 110..130, + parameters: Some( + Parameters { + range: 117..123, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 117..118, + parameter: Parameter { + range: 117..118, + name: Identifier { + id: "y", + range: 117..118, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 120..123, + parameter: Parameter { + range: 120..121, + name: Identifier { + id: "z", + range: 120..121, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 122..123, + value: Int( + 1, + ), + }, + ), + ), + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: BinOp( + ExprBinOp { + range: 125..130, + left: Name( + ExprName { + range: 125..126, + id: "z", + ctx: Load, + }, + ), + op: Mult, + right: Name( + ExprName { + range: 129..130, + id: "y", + ctx: Load, + }, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 131..143, + value: Lambda( + ExprLambda { + range: 131..143, + parameters: Some( + Parameters { + range: 138..140, + posonlyargs: [], + args: [], + vararg: Some( + Parameter { + range: 138..140, + name: Identifier { + id: "a", + range: 139..140, + }, + annotation: None, + }, + ), + kwonlyargs: [], + kwarg: None, + }, + ), + body: Name( + ExprName { + range: 142..143, + id: "a", + ctx: Load, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 144..166, + value: Lambda( + ExprLambda { + range: 144..166, + parameters: Some( + Parameters { + range: 151..161, + posonlyargs: [], + args: [], + vararg: Some( + Parameter { + range: 151..153, + name: Identifier { + id: "a", + range: 152..153, + }, + annotation: None, + }, + ), + kwonlyargs: [ + ParameterWithDefault { + range: 155..156, + parameter: Parameter { + range: 155..156, + name: Identifier { + id: "z", + range: 155..156, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 158..161, + parameter: Parameter { + range: 158..159, + name: Identifier { + id: "x", + range: 158..159, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 160..161, + value: Int( + 0, + ), + }, + ), + ), + }, + ], + kwarg: None, + }, + ), + body: EllipsisLiteral( + ExprEllipsisLiteral { + range: 163..166, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 167..187, + value: Lambda( + ExprLambda { + range: 167..187, + parameters: Some( + Parameters { + range: 174..184, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [ + ParameterWithDefault { + range: 177..178, + parameter: Parameter { + range: 177..178, + name: Identifier { + id: "a", + range: 177..178, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 180..181, + parameter: Parameter { + range: 180..181, + name: Identifier { + id: "b", + range: 180..181, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 183..184, + parameter: Parameter { + range: 183..184, + name: Identifier { + id: "c", + range: 183..184, + }, + annotation: None, + }, + default: None, + }, + ], + kwarg: None, + }, + ), + body: NumberLiteral( + ExprNumberLiteral { + range: 186..187, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 188..214, + value: Lambda( + ExprLambda { + range: 188..214, + parameters: Some( + Parameters { + range: 195..211, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [ + ParameterWithDefault { + range: 198..199, + parameter: Parameter { + range: 198..199, + name: Identifier { + id: "a", + range: 198..199, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 201..205, + parameter: Parameter { + range: 201..202, + name: Identifier { + id: "b", + range: 201..202, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 203..205, + value: Int( + 20, + ), + }, + ), + ), + }, + ParameterWithDefault { + range: 207..211, + parameter: Parameter { + range: 207..208, + name: Identifier { + id: "c", + range: 207..208, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 209..211, + value: Int( + 30, + ), + }, + ), + ), + }, + ], + kwarg: None, + }, + ), + body: NumberLiteral( + ExprNumberLiteral { + range: 213..214, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 215..241, + value: Lambda( + ExprLambda { + range: 215..241, + parameters: Some( + Parameters { + range: 222..238, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 222..223, + parameter: Parameter { + range: 222..223, + name: Identifier { + id: "a", + range: 222..223, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 225..226, + parameter: Parameter { + range: 225..226, + name: Identifier { + id: "b", + range: 225..226, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 228..229, + parameter: Parameter { + range: 228..229, + name: Identifier { + id: "c", + range: 228..229, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [ + ParameterWithDefault { + range: 234..235, + parameter: Parameter { + range: 234..235, + name: Identifier { + id: "d", + range: 234..235, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 237..238, + parameter: Parameter { + range: 237..238, + name: Identifier { + id: "e", + range: 237..238, + }, + annotation: None, + }, + default: None, + }, + ], + kwarg: None, + }, + ), + body: NumberLiteral( + ExprNumberLiteral { + range: 240..241, + value: Int( + 0, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 242..262, + value: Lambda( + ExprLambda { + range: 242..262, + parameters: Some( + Parameters { + range: 249..257, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: Some( + Parameter { + range: 249..257, + name: Identifier { + id: "kwargs", + range: 251..257, + }, + annotation: None, + }, + ), + }, + ), + body: Call( + ExprCall { + range: 259..262, + func: Name( + ExprName { + range: 259..260, + id: "f", + ctx: Load, + }, + ), + arguments: Arguments { + range: 260..262, + args: [], + keywords: [], + }, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 263..294, + value: Lambda( + ExprLambda { + range: 263..294, + parameters: Some( + Parameters { + range: 270..285, + posonlyargs: [], + args: [], + vararg: Some( + Parameter { + range: 270..275, + name: Identifier { + id: "args", + range: 271..275, + }, + annotation: None, + }, + ), + kwonlyargs: [], + kwarg: Some( + Parameter { + range: 277..285, + name: Identifier { + id: "kwargs", + range: 279..285, + }, + annotation: None, + }, + ), + }, + ), + body: BinOp( + ExprBinOp { + range: 287..294, + left: Call( + ExprCall { + range: 287..290, + func: Name( + ExprName { + range: 287..288, + id: "f", + ctx: Load, + }, + ), + arguments: Arguments { + range: 288..290, + args: [], + keywords: [], + }, + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 293..294, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 295..334, + value: Lambda( + ExprLambda { + range: 295..334, + parameters: Some( + Parameters { + range: 302..325, + posonlyargs: [], + args: [], + vararg: Some( + Parameter { + range: 302..307, + name: Identifier { + id: "args", + range: 303..307, + }, + annotation: None, + }, + ), + kwonlyargs: [ + ParameterWithDefault { + range: 309..310, + parameter: Parameter { + range: 309..310, + name: Identifier { + id: "a", + range: 309..310, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 312..315, + parameter: Parameter { + range: 312..313, + name: Identifier { + id: "b", + range: 312..313, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 314..315, + value: Int( + 1, + ), + }, + ), + ), + }, + ], + kwarg: Some( + Parameter { + range: 317..325, + name: Identifier { + id: "kwargs", + range: 319..325, + }, + annotation: None, + }, + ), + }, + ), + body: BinOp( + ExprBinOp { + range: 327..334, + left: Call( + ExprCall { + range: 327..330, + func: Name( + ExprName { + range: 327..328, + id: "f", + ctx: Load, + }, + ), + arguments: Arguments { + range: 328..330, + args: [], + keywords: [], + }, + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 333..334, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 335..351, + value: Lambda( + ExprLambda { + range: 335..351, + parameters: Some( + Parameters { + range: 342..346, + posonlyargs: [ + ParameterWithDefault { + range: 342..343, + parameter: Parameter { + range: 342..343, + name: Identifier { + id: "a", + range: 342..343, + }, + annotation: None, + }, + default: None, + }, + ], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: EllipsisLiteral( + ExprEllipsisLiteral { + range: 348..351, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 352..371, + value: Lambda( + ExprLambda { + range: 352..371, + parameters: Some( + Parameters { + range: 359..366, + posonlyargs: [ + ParameterWithDefault { + range: 359..360, + parameter: Parameter { + range: 359..360, + name: Identifier { + id: "a", + range: 359..360, + }, + annotation: None, + }, + default: None, + }, + ], + args: [ + ParameterWithDefault { + range: 365..366, + parameter: Parameter { + range: 365..366, + name: Identifier { + id: "b", + range: 365..366, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: EllipsisLiteral( + ExprEllipsisLiteral { + range: 368..371, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 372..391, + value: Lambda( + ExprLambda { + range: 372..391, + parameters: Some( + Parameters { + range: 379..386, + posonlyargs: [ + ParameterWithDefault { + range: 379..382, + parameter: Parameter { + range: 379..380, + name: Identifier { + id: "a", + range: 379..380, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 381..382, + value: Int( + 1, + ), + }, + ), + ), + }, + ], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: EllipsisLiteral( + ExprEllipsisLiteral { + range: 388..391, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 392..417, + value: Lambda( + ExprLambda { + range: 392..417, + parameters: Some( + Parameters { + range: 399..412, + posonlyargs: [ + ParameterWithDefault { + range: 399..400, + parameter: Parameter { + range: 399..400, + name: Identifier { + id: "a", + range: 399..400, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 402..403, + parameter: Parameter { + range: 402..403, + name: Identifier { + id: "b", + range: 402..403, + }, + annotation: None, + }, + default: None, + }, + ], + args: [], + vararg: None, + kwonlyargs: [ + ParameterWithDefault { + range: 411..412, + parameter: Parameter { + range: 411..412, + name: Identifier { + id: "c", + range: 411..412, + }, + annotation: None, + }, + default: None, + }, + ], + kwarg: None, + }, + ), + body: EllipsisLiteral( + ExprEllipsisLiteral { + range: 414..417, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 418..440, + value: Lambda( + ExprLambda { + range: 418..440, + parameters: Some( + Parameters { + range: 425..435, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 425..429, + parameter: Parameter { + range: 425..427, + name: Identifier { + id: "kw", + range: 425..427, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 428..429, + value: Int( + 1, + ), + }, + ), + ), + }, + ], + vararg: None, + kwonlyargs: [ + ParameterWithDefault { + range: 434..435, + parameter: Parameter { + range: 434..435, + name: Identifier { + id: "a", + range: 434..435, + }, + annotation: None, + }, + default: None, + }, + ], + kwarg: None, + }, + ), + body: EllipsisLiteral( + ExprEllipsisLiteral { + range: 437..440, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 441..467, + value: Lambda( + ExprLambda { + range: 441..467, + parameters: Some( + Parameters { + range: 448..464, + posonlyargs: [ + ParameterWithDefault { + range: 448..449, + parameter: Parameter { + range: 448..449, + name: Identifier { + id: "a", + range: 448..449, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 451..455, + parameter: Parameter { + range: 451..452, + name: Identifier { + id: "b", + range: 451..452, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 453..455, + value: Int( + 20, + ), + }, + ), + ), + }, + ], + args: [ + ParameterWithDefault { + range: 460..464, + parameter: Parameter { + range: 460..461, + name: Identifier { + id: "c", + range: 460..461, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 462..464, + value: Int( + 30, + ), + }, + ), + ), + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: NumberLiteral( + ExprNumberLiteral { + range: 466..467, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 468..497, + value: Lambda( + ExprLambda { + range: 468..497, + parameters: Some( + Parameters { + range: 475..494, + posonlyargs: [ + ParameterWithDefault { + range: 475..476, + parameter: Parameter { + range: 475..476, + name: Identifier { + id: "a", + range: 475..476, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 478..479, + parameter: Parameter { + range: 478..479, + name: Identifier { + id: "b", + range: 478..479, + }, + annotation: None, + }, + default: None, + }, + ], + args: [ + ParameterWithDefault { + range: 484..485, + parameter: Parameter { + range: 484..485, + name: Identifier { + id: "c", + range: 484..485, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [ + ParameterWithDefault { + range: 490..491, + parameter: Parameter { + range: 490..491, + name: Identifier { + id: "d", + range: 490..491, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 493..494, + parameter: Parameter { + range: 493..494, + name: Identifier { + id: "e", + range: 493..494, + }, + annotation: None, + }, + default: None, + }, + ], + kwarg: None, + }, + ), + body: NumberLiteral( + ExprNumberLiteral { + range: 496..497, + value: Int( + 0, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 498..530, + value: Lambda( + ExprLambda { + range: 498..530, + parameters: Some( + Parameters { + range: 505..527, + posonlyargs: [ + ParameterWithDefault { + range: 505..506, + parameter: Parameter { + range: 505..506, + name: Identifier { + id: "a", + range: 505..506, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 508..509, + parameter: Parameter { + range: 508..509, + name: Identifier { + id: "b", + range: 508..509, + }, + annotation: None, + }, + default: None, + }, + ], + args: [ + ParameterWithDefault { + range: 514..515, + parameter: Parameter { + range: 514..515, + name: Identifier { + id: "c", + range: 514..515, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: Some( + Parameter { + range: 517..519, + name: Identifier { + id: "d", + range: 518..519, + }, + annotation: None, + }, + ), + kwonlyargs: [ + ParameterWithDefault { + range: 521..522, + parameter: Parameter { + range: 521..522, + name: Identifier { + id: "e", + range: 521..522, + }, + annotation: None, + }, + default: None, + }, + ], + kwarg: Some( + Parameter { + range: 524..527, + name: Identifier { + id: "f", + range: 526..527, + }, + annotation: None, + }, + ), + }, + ), + body: NumberLiteral( + ExprNumberLiteral { + range: 529..530, + value: Int( + 0, + ), + }, + ), + }, + ), + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__list.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__list.py.snap.new new file mode 100644 index 0000000000..112bf2de0c --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__list.py.snap.new @@ -0,0 +1,811 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/valid/expressions/list.py +--- +## AST + +``` +Module( + ModModule { + range: 0..384, + body: [ + Expr( + StmtExpr { + range: 15..17, + value: List( + ExprList { + range: 15..17, + elts: [], + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 18..21, + value: List( + ExprList { + range: 18..21, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 19..20, + value: Int( + 1, + ), + }, + ), + ], + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 22..26, + value: List( + ExprList { + range: 22..26, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 23..24, + value: Int( + 1, + ), + }, + ), + ], + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 27..36, + value: List( + ExprList { + range: 27..36, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 28..29, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 31..32, + value: Int( + 2, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 34..35, + value: Int( + 3, + ), + }, + ), + ], + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 37..47, + value: List( + ExprList { + range: 37..47, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 38..39, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 41..42, + value: Int( + 2, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 44..45, + value: Int( + 3, + ), + }, + ), + ], + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 75..78, + value: List( + ExprList { + range: 75..78, + elts: [], + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 79..92, + value: List( + ExprList { + range: 79..92, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 89..90, + value: Int( + 1, + ), + }, + ), + ], + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 93..114, + value: List( + ExprList { + range: 93..114, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 99..100, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 110..111, + value: Int( + 2, + ), + }, + ), + ], + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 125..132, + value: List( + ExprList { + range: 125..132, + elts: [ + List( + ExprList { + range: 126..131, + elts: [ + List( + ExprList { + range: 127..130, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 128..129, + value: Int( + 1, + ), + }, + ), + ], + ctx: Load, + }, + ), + ], + ctx: Load, + }, + ), + ], + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 133..149, + value: List( + ExprList { + range: 133..149, + elts: [ + List( + ExprList { + range: 134..140, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 135..136, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 138..139, + value: Int( + 2, + ), + }, + ), + ], + ctx: Load, + }, + ), + List( + ExprList { + range: 142..148, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 143..144, + value: Int( + 3, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 146..147, + value: Int( + 4, + ), + }, + ), + ], + ctx: Load, + }, + ), + ], + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 170..178, + value: List( + ExprList { + range: 170..178, + elts: [ + Named( + ExprNamed { + range: 171..177, + target: Name( + ExprName { + range: 171..172, + id: "x", + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 176..177, + value: Int( + 2, + ), + }, + ), + }, + ), + ], + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 179..188, + value: List( + ExprList { + range: 179..188, + elts: [ + Named( + ExprNamed { + range: 180..186, + target: Name( + ExprName { + range: 180..181, + id: "x", + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 185..186, + value: Int( + 2, + ), + }, + ), + }, + ), + ], + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 189..203, + value: List( + ExprList { + range: 189..203, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 190..191, + value: Int( + 1, + ), + }, + ), + Named( + ExprNamed { + range: 193..199, + target: Name( + ExprName { + range: 193..194, + id: "x", + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 198..199, + value: Int( + 2, + ), + }, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 201..202, + value: Int( + 3, + ), + }, + ), + ], + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 223..233, + value: List( + ExprList { + range: 223..233, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 224..225, + value: Int( + 1, + ), + }, + ), + Starred( + ExprStarred { + range: 227..229, + value: Name( + ExprName { + range: 228..229, + id: "x", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 231..232, + value: Int( + 3, + ), + }, + ), + ], + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 234..248, + value: List( + ExprList { + range: 234..248, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 235..236, + value: Int( + 1, + ), + }, + ), + Starred( + ExprStarred { + range: 238..244, + value: BinOp( + ExprBinOp { + range: 239..244, + left: Name( + ExprName { + range: 239..240, + id: "x", + ctx: Load, + }, + ), + op: BitOr, + right: Name( + ExprName { + range: 243..244, + id: "y", + ctx: Load, + }, + ), + }, + ), + ctx: Load, + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 246..247, + value: Int( + 3, + ), + }, + ), + ], + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 271..334, + value: List( + ExprList { + range: 271..334, + elts: [ + BinOp( + ExprBinOp { + range: 272..277, + left: NumberLiteral( + ExprNumberLiteral { + range: 272..273, + value: Int( + 1, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 276..277, + value: Int( + 2, + ), + }, + ), + }, + ), + List( + ExprList { + range: 279..291, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 280..281, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 283..284, + value: Int( + 2, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 286..287, + value: Int( + 3, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 289..290, + value: Int( + 4, + ), + }, + ), + ], + ctx: Load, + }, + ), + Tuple( + ExprTuple { + range: 293..306, + elts: [ + Name( + ExprName { + range: 294..295, + id: "a", + ctx: Load, + }, + ), + BinOp( + ExprBinOp { + range: 297..302, + left: Name( + ExprName { + range: 297..298, + id: "b", + ctx: Load, + }, + ), + op: Add, + right: Name( + ExprName { + range: 301..302, + id: "c", + ctx: Load, + }, + ), + }, + ), + Name( + ExprName { + range: 304..305, + id: "d", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + Set( + ExprSet { + range: 308..317, + elts: [ + Name( + ExprName { + range: 309..310, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 312..313, + id: "b", + ctx: Load, + }, + ), + Name( + ExprName { + range: 315..316, + id: "c", + ctx: Load, + }, + ), + ], + }, + ), + Dict( + ExprDict { + range: 319..325, + items: [ + DictItem { + key: Some( + Name( + ExprName { + range: 320..321, + id: "a", + ctx: Load, + }, + ), + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 323..324, + value: Int( + 1, + ), + }, + ), + }, + ], + }, + ), + Named( + ExprNamed { + range: 327..333, + target: Name( + ExprName { + range: 327..328, + id: "x", + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 332..333, + value: Int( + 2, + ), + }, + ), + }, + ), + ], + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 335..383, + value: List( + ExprList { + range: 335..383, + elts: [ + Call( + ExprCall { + range: 336..382, + func: Name( + ExprName { + range: 336..341, + id: "call1", + ctx: Load, + }, + ), + arguments: Arguments { + range: 341..382, + args: [ + Generator( + ExprGenerator { + range: 342..381, + elt: Call( + ExprCall { + range: 342..361, + func: Name( + ExprName { + range: 342..347, + id: "call2", + ctx: Load, + }, + ), + arguments: Arguments { + range: 347..361, + args: [ + Call( + ExprCall { + range: 348..360, + func: Attribute( + ExprAttribute { + range: 348..358, + value: Name( + ExprName { + range: 348..353, + id: "value", + ctx: Load, + }, + ), + attr: Identifier { + id: "attr", + range: 354..358, + }, + ctx: Load, + }, + ), + arguments: Arguments { + range: 358..360, + args: [], + keywords: [], + }, + }, + ), + ], + keywords: [], + }, + }, + ), + generators: [ + Comprehension { + range: 362..381, + target: Name( + ExprName { + range: 366..373, + id: "element", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 377..381, + id: "iter", + ctx: Load, + }, + ), + ifs: [], + is_async: false, + }, + ], + parenthesized: false, + }, + ), + ], + keywords: [], + }, + }, + ), + ], + ctx: Load, + }, + ), + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__list_comprehension.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__list_comprehension.py.snap.new new file mode 100644 index 0000000000..a7a0f6e28b --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__list_comprehension.py.snap.new @@ -0,0 +1,1291 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/valid/expressions/list_comprehension.py +--- +## AST + +``` +Module( + ModModule { + range: 0..776, + body: [ + Assign( + StmtAssign { + range: 0..26, + targets: [ + Name( + ExprName { + range: 0..1, + id: "x", + ctx: Store, + }, + ), + ], + value: ListComp( + ExprListComp { + range: 4..26, + elt: Name( + ExprName { + range: 5..6, + id: "y", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 7..25, + target: Name( + ExprName { + range: 11..12, + id: "y", + ctx: Store, + }, + ), + iter: Tuple( + ExprTuple { + range: 16..25, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 17..18, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 20..21, + value: Int( + 2, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 23..24, + value: Int( + 3, + ), + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 28..49, + value: ListComp( + ExprListComp { + range: 28..49, + elt: Name( + ExprName { + range: 29..30, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 31..48, + target: Name( + ExprName { + range: 35..36, + id: "i", + ctx: Store, + }, + ), + iter: Call( + ExprCall { + range: 40..48, + func: Name( + ExprName { + range: 40..45, + id: "range", + ctx: Load, + }, + ), + arguments: Arguments { + range: 45..48, + args: [ + NumberLiteral( + ExprNumberLiteral { + range: 46..47, + value: Int( + 5, + ), + }, + ), + ], + keywords: [], + }, + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 50..91, + value: ListComp( + ExprListComp { + range: 50..91, + elt: Name( + ExprName { + range: 51..52, + id: "b", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 53..90, + target: Name( + ExprName { + range: 57..58, + id: "c", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 62..63, + id: "d", + ctx: Load, + }, + ), + ifs: [ + Compare( + ExprCompare { + range: 67..73, + left: Name( + ExprName { + range: 67..68, + id: "x", + ctx: Load, + }, + ), + ops: [ + In, + ], + comparators: [ + Name( + ExprName { + range: 72..73, + id: "w", + ctx: Load, + }, + ), + ], + }, + ), + BoolOp( + ExprBoolOp { + range: 77..85, + op: And, + values: [ + Name( + ExprName { + range: 77..78, + id: "y", + ctx: Load, + }, + ), + Name( + ExprName { + range: 83..85, + id: "yy", + ctx: Load, + }, + ), + ], + }, + ), + Name( + ExprName { + range: 89..90, + id: "z", + ctx: Load, + }, + ), + ], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 92..137, + value: ListComp( + ExprListComp { + range: 92..137, + elt: Name( + ExprName { + range: 93..94, + id: "a", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 95..116, + target: Name( + ExprName { + range: 99..100, + id: "b", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 104..105, + id: "c", + ctx: Load, + }, + ), + ifs: [ + BoolOp( + ExprBoolOp { + range: 109..116, + op: And, + values: [ + Name( + ExprName { + range: 109..110, + id: "d", + ctx: Load, + }, + ), + Name( + ExprName { + range: 115..116, + id: "e", + ctx: Load, + }, + ), + ], + }, + ), + ], + is_async: false, + }, + Comprehension { + range: 117..136, + target: Name( + ExprName { + range: 121..122, + id: "f", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 126..127, + id: "j", + ctx: Load, + }, + ), + ifs: [ + Compare( + ExprCompare { + range: 131..136, + left: Name( + ExprName { + range: 131..132, + id: "k", + ctx: Load, + }, + ), + ops: [ + Gt, + ], + comparators: [ + Name( + ExprName { + range: 135..136, + id: "h", + ctx: Load, + }, + ), + ], + }, + ), + ], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 138..189, + value: ListComp( + ExprListComp { + range: 138..189, + elt: Name( + ExprName { + range: 139..140, + id: "a", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 141..162, + target: Name( + ExprName { + range: 145..146, + id: "b", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 150..151, + id: "c", + ctx: Load, + }, + ), + ifs: [ + BoolOp( + ExprBoolOp { + range: 155..162, + op: And, + values: [ + Name( + ExprName { + range: 155..156, + id: "d", + ctx: Load, + }, + ), + Name( + ExprName { + range: 161..162, + id: "e", + ctx: Load, + }, + ), + ], + }, + ), + ], + is_async: false, + }, + Comprehension { + range: 163..188, + target: Name( + ExprName { + range: 173..174, + id: "f", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 178..179, + id: "j", + ctx: Load, + }, + ), + ifs: [ + Compare( + ExprCompare { + range: 183..188, + left: Name( + ExprName { + range: 183..184, + id: "k", + ctx: Load, + }, + ), + ops: [ + Gt, + ], + comparators: [ + Name( + ExprName { + range: 187..188, + id: "h", + ctx: Load, + }, + ), + ], + }, + ), + ], + is_async: true, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 190..209, + value: ListComp( + ExprListComp { + range: 190..209, + elt: NumberLiteral( + ExprNumberLiteral { + range: 191..192, + value: Int( + 1, + ), + }, + ), + generators: [ + Comprehension { + range: 193..208, + target: Name( + ExprName { + range: 197..198, + id: "i", + ctx: Store, + }, + ), + iter: Compare( + ExprCompare { + range: 202..208, + left: Name( + ExprName { + range: 202..203, + id: "x", + ctx: Load, + }, + ), + ops: [ + In, + ], + comparators: [ + Name( + ExprName { + range: 207..208, + id: "a", + ctx: Load, + }, + ), + ], + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 210..227, + value: ListComp( + ExprListComp { + range: 210..227, + elt: Name( + ExprName { + range: 211..212, + id: "a", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 213..226, + target: Tuple( + ExprTuple { + range: 217..221, + elts: [ + Name( + ExprName { + range: 217..218, + id: "a", + ctx: Store, + }, + ), + Name( + ExprName { + range: 220..221, + id: "b", + ctx: Store, + }, + ), + ], + ctx: Store, + parenthesized: false, + }, + ), + iter: Name( + ExprName { + range: 225..226, + id: "G", + ctx: Load, + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 228..257, + value: ListComp( + ExprListComp { + range: 228..257, + elt: Await( + ExprAwait { + range: 234..241, + value: Name( + ExprName { + range: 240..241, + id: "x", + ctx: Load, + }, + ), + }, + ), + generators: [ + Comprehension { + range: 242..255, + target: Tuple( + ExprTuple { + range: 246..250, + elts: [ + Name( + ExprName { + range: 246..247, + id: "a", + ctx: Store, + }, + ), + Name( + ExprName { + range: 249..250, + id: "b", + ctx: Store, + }, + ), + ], + ctx: Store, + parenthesized: false, + }, + ), + iter: Name( + ExprName { + range: 254..255, + id: "C", + ctx: Load, + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 258..300, + value: ListComp( + ExprListComp { + range: 258..300, + elt: Name( + ExprName { + range: 259..260, + id: "i", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 261..299, + target: Name( + ExprName { + range: 265..266, + id: "i", + ctx: Store, + }, + ), + iter: Await( + ExprAwait { + range: 270..277, + value: Name( + ExprName { + range: 276..277, + id: "x", + ctx: Load, + }, + ), + }, + ), + ifs: [ + Compare( + ExprCompare { + range: 281..299, + left: Name( + ExprName { + range: 281..287, + id: "entity", + ctx: Load, + }, + ), + ops: [ + IsNot, + ], + comparators: [ + NoneLiteral( + ExprNoneLiteral { + range: 295..299, + }, + ), + ], + }, + ), + ], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 301..337, + value: ListComp( + ExprListComp { + range: 301..337, + elt: Name( + ExprName { + range: 302..303, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 304..336, + target: Name( + ExprName { + range: 308..309, + id: "x", + ctx: Store, + }, + ), + iter: If( + ExprIf { + range: 314..330, + test: BooleanLiteral( + ExprBooleanLiteral { + range: 319..323, + value: true, + }, + ), + body: Name( + ExprName { + range: 314..315, + id: "l", + ctx: Load, + }, + ), + orelse: Name( + ExprName { + range: 329..330, + id: "L", + ctx: Load, + }, + ), + }, + ), + ifs: [ + Name( + ExprName { + range: 335..336, + id: "T", + ctx: Load, + }, + ), + ], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 338..380, + value: ListComp( + ExprListComp { + range: 338..380, + elt: Name( + ExprName { + range: 339..340, + id: "i", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 341..379, + target: Name( + ExprName { + range: 345..346, + id: "i", + ctx: Store, + }, + ), + iter: If( + ExprIf { + range: 351..373, + test: BooleanLiteral( + ExprBooleanLiteral { + range: 362..366, + value: true, + }, + ), + body: Await( + ExprAwait { + range: 351..358, + value: Name( + ExprName { + range: 357..358, + id: "x", + ctx: Load, + }, + ), + }, + ), + orelse: Name( + ExprName { + range: 372..373, + id: "X", + ctx: Load, + }, + ), + }, + ), + ifs: [ + Name( + ExprName { + range: 378..379, + id: "F", + ctx: Load, + }, + ), + ], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 381..423, + value: ListComp( + ExprListComp { + range: 381..423, + elt: Name( + ExprName { + range: 382..383, + id: "i", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 384..422, + target: Name( + ExprName { + range: 388..389, + id: "i", + ctx: Store, + }, + ), + iter: Await( + ExprAwait { + range: 393..417, + value: If( + ExprIf { + range: 400..416, + test: BooleanLiteral( + ExprBooleanLiteral { + range: 405..409, + value: true, + }, + ), + body: Name( + ExprName { + range: 400..401, + id: "x", + ctx: Load, + }, + ), + orelse: Name( + ExprName { + range: 415..416, + id: "X", + ctx: Load, + }, + ), + }, + ), + }, + ), + ifs: [ + Name( + ExprName { + range: 421..422, + id: "F", + ctx: Load, + }, + ), + ], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 424..457, + value: ListComp( + ExprListComp { + range: 424..457, + elt: Name( + ExprName { + range: 425..426, + id: "f", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 427..456, + target: Name( + ExprName { + range: 431..432, + id: "f", + ctx: Store, + }, + ), + iter: Call( + ExprCall { + range: 436..456, + func: Name( + ExprName { + range: 436..437, + id: "c", + ctx: Load, + }, + ), + arguments: Arguments { + range: 437..456, + args: [ + If( + ExprIf { + range: 438..455, + test: BooleanLiteral( + ExprBooleanLiteral { + range: 443..447, + value: true, + }, + ), + body: Name( + ExprName { + range: 438..439, + id: "x", + ctx: Load, + }, + ), + orelse: List( + ExprList { + range: 453..455, + elts: [], + ctx: Load, + }, + ), + }, + ), + ], + keywords: [], + }, + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 596..618, + value: ListComp( + ExprListComp { + range: 596..618, + elt: Name( + ExprName { + range: 597..598, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 599..617, + target: Name( + ExprName { + range: 603..604, + id: "x", + ctx: Store, + }, + ), + iter: Yield( + ExprYield { + range: 609..616, + value: Some( + Name( + ExprName { + range: 615..616, + id: "y", + ctx: Load, + }, + ), + ), + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 619..646, + value: ListComp( + ExprListComp { + range: 619..646, + elt: Name( + ExprName { + range: 620..621, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 622..645, + target: Name( + ExprName { + range: 626..627, + id: "x", + ctx: Store, + }, + ), + iter: YieldFrom( + ExprYieldFrom { + range: 632..644, + value: Name( + ExprName { + range: 643..644, + id: "y", + ctx: Load, + }, + ), + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 647..673, + value: ListComp( + ExprListComp { + range: 647..673, + elt: Name( + ExprName { + range: 648..649, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 650..672, + target: Name( + ExprName { + range: 654..655, + id: "x", + ctx: Store, + }, + ), + iter: Lambda( + ExprLambda { + range: 660..671, + parameters: Some( + Parameters { + range: 667..668, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 667..668, + parameter: Parameter { + range: 667..668, + name: Identifier { + id: "y", + range: 667..668, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: Name( + ExprName { + range: 670..671, + id: "y", + ctx: Load, + }, + ), + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 674..704, + value: ListComp( + ExprListComp { + range: 674..704, + elt: Name( + ExprName { + range: 675..676, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 677..703, + target: Name( + ExprName { + range: 681..682, + id: "x", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 686..690, + id: "data", + ctx: Load, + }, + ), + ifs: [ + Yield( + ExprYield { + range: 695..702, + value: Some( + Name( + ExprName { + range: 701..702, + id: "y", + ctx: Load, + }, + ), + ), + }, + ), + ], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 705..740, + value: ListComp( + ExprListComp { + range: 705..740, + elt: Name( + ExprName { + range: 706..707, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 708..739, + target: Name( + ExprName { + range: 712..713, + id: "x", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 717..721, + id: "data", + ctx: Load, + }, + ), + ifs: [ + YieldFrom( + ExprYieldFrom { + range: 726..738, + value: Name( + ExprName { + range: 737..738, + id: "y", + ctx: Load, + }, + ), + }, + ), + ], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 741..775, + value: ListComp( + ExprListComp { + range: 741..775, + elt: Name( + ExprName { + range: 742..743, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 744..774, + target: Name( + ExprName { + range: 748..749, + id: "x", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 753..757, + id: "data", + ctx: Load, + }, + ), + ifs: [ + Lambda( + ExprLambda { + range: 762..773, + parameters: Some( + Parameters { + range: 769..770, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 769..770, + parameter: Parameter { + range: 769..770, + name: Identifier { + id: "y", + range: 769..770, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: Name( + ExprName { + range: 772..773, + id: "y", + ctx: Load, + }, + ), + }, + ), + ], + is_async: false, + }, + ], + }, + ), + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__name.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__name.py.snap.new new file mode 100644 index 0000000000..513c29c5d8 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__name.py.snap.new @@ -0,0 +1,124 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/valid/expressions/name.py +--- +## AST + +``` +Module( + ModModule { + range: 0..76, + body: [ + Expr( + StmtExpr { + range: 0..1, + value: Name( + ExprName { + range: 0..1, + id: "_", + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 2..5, + value: Name( + ExprName { + range: 3..4, + id: "_", + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 6..8, + value: Name( + ExprName { + range: 6..8, + id: "__", + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 9..17, + value: Name( + ExprName { + range: 9..17, + id: "__init__", + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 18..22, + value: Name( + ExprName { + range: 18..22, + id: "name", + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 23..29, + value: Name( + ExprName { + range: 24..28, + id: "name", + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 60..65, + value: Name( + ExprName { + range: 60..65, + id: "match", + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 66..70, + value: Name( + ExprName { + range: 66..70, + id: "case", + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 71..75, + value: Name( + ExprName { + range: 71..75, + id: "type", + ctx: Load, + }, + ), + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__named.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__named.py.snap.new new file mode 100644 index 0000000000..d72d477b11 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__named.py.snap.new @@ -0,0 +1,317 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/valid/expressions/named.py +--- +## AST + +``` +Module( + ModModule { + range: 0..157, + body: [ + Expr( + StmtExpr { + range: 0..11, + value: Named( + ExprNamed { + range: 1..10, + target: Name( + ExprName { + range: 1..5, + id: "name", + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 9..10, + value: Int( + 0, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 12..29, + value: Named( + ExprNamed { + range: 13..28, + target: Name( + ExprName { + range: 13..17, + id: "name", + ctx: Store, + }, + ), + value: BinOp( + ExprBinOp { + range: 22..27, + left: Name( + ExprName { + range: 22..23, + id: "x", + ctx: Load, + }, + ), + op: Mult, + right: Name( + ExprName { + range: 26..27, + id: "y", + ctx: Load, + }, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 30..45, + value: Named( + ExprNamed { + range: 31..44, + target: Name( + ExprName { + range: 31..35, + id: "name", + ctx: Store, + }, + ), + value: BinOp( + ExprBinOp { + range: 39..44, + left: NumberLiteral( + ExprNumberLiteral { + range: 39..40, + value: Int( + 1, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 43..44, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 46..63, + value: Named( + ExprNamed { + range: 47..62, + target: Name( + ExprName { + range: 47..51, + id: "name", + ctx: Store, + }, + ), + value: Tuple( + ExprTuple { + range: 55..62, + elts: [ + Starred( + ExprStarred { + range: 56..58, + value: Name( + ExprName { + range: 57..58, + id: "x", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + Name( + ExprName { + range: 60..61, + id: "y", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 64..90, + value: Named( + ExprNamed { + range: 65..89, + target: Name( + ExprName { + range: 65..69, + id: "name", + ctx: Store, + }, + ), + value: If( + ExprIf { + range: 73..89, + test: BooleanLiteral( + ExprBooleanLiteral { + range: 78..82, + value: true, + }, + ), + body: Name( + ExprName { + range: 73..74, + id: "x", + ctx: Load, + }, + ), + orelse: Name( + ExprName { + range: 88..89, + id: "y", + ctx: Load, + }, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 91..112, + value: Named( + ExprNamed { + range: 92..111, + target: Name( + ExprName { + range: 92..96, + id: "name", + ctx: Store, + }, + ), + value: Lambda( + ExprLambda { + range: 100..111, + parameters: Some( + Parameters { + range: 107..108, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 107..108, + parameter: Parameter { + range: 107..108, + name: Identifier { + id: "x", + range: 107..108, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: Name( + ExprName { + range: 110..111, + id: "x", + ctx: Load, + }, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 113..132, + value: Named( + ExprNamed { + range: 114..131, + target: Name( + ExprName { + range: 114..118, + id: "name", + ctx: Store, + }, + ), + value: Yield( + ExprYield { + range: 123..130, + value: Some( + Name( + ExprName { + range: 129..130, + id: "x", + ctx: Load, + }, + ), + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 133..157, + value: Named( + ExprNamed { + range: 134..156, + target: Name( + ExprName { + range: 134..138, + id: "name", + ctx: Store, + }, + ), + value: YieldFrom( + ExprYieldFrom { + range: 143..155, + value: Name( + ExprName { + range: 154..155, + id: "x", + ctx: Load, + }, + ), + }, + ), + }, + ), + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__number_literal.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__number_literal.py.snap.new new file mode 100644 index 0000000000..5cc14031b7 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__number_literal.py.snap.new @@ -0,0 +1,1029 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/valid/expressions/number_literal.py +--- +## AST + +``` +Module( + ModModule { + range: 0..700, + body: [ + Assign( + StmtAssign { + range: 0..13, + targets: [ + Name( + ExprName { + range: 0..1, + id: "x", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 4..13, + value: Int( + 123456789, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 14..24, + targets: [ + Name( + ExprName { + range: 14..15, + id: "x", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 18..24, + value: Int( + 123456, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 25..31, + targets: [ + Name( + ExprName { + range: 25..26, + id: "x", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 29..31, + value: Float( + 0.1, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 32..38, + targets: [ + Name( + ExprName { + range: 32..33, + id: "x", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 36..38, + value: Float( + 1.0, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 39..47, + targets: [ + Name( + ExprName { + range: 39..40, + id: "x", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 43..47, + value: Float( + 10.0, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 48..56, + targets: [ + Name( + ExprName { + range: 48..49, + id: "x", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 52..56, + value: Float( + 0.1, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 57..73, + targets: [ + Name( + ExprName { + range: 57..58, + id: "x", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 61..73, + value: Float( + 1.00000001, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 74..97, + targets: [ + Name( + ExprName { + range: 74..75, + id: "x", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 78..97, + value: Float( + 123456789.12345679, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 98..131, + targets: [ + Name( + ExprName { + range: 98..99, + id: "x", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 102..131, + value: Float( + inf, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 132..155, + targets: [ + Name( + ExprName { + range: 132..133, + id: "x", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 136..155, + value: Float( + inf, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 156..170, + targets: [ + Name( + ExprName { + range: 156..157, + id: "x", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 160..170, + value: Complex { + real: 0.0, + imag: 123456789.0, + }, + }, + ), + }, + ), + Assign( + StmtAssign { + range: 171..195, + targets: [ + Name( + ExprName { + range: 171..172, + id: "x", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 175..195, + value: Complex { + real: 0.0, + imag: 123456789.12345679, + }, + }, + ), + }, + ), + Assign( + StmtAssign { + range: 196..207, + targets: [ + Name( + ExprName { + range: 196..197, + id: "x", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 200..207, + value: Int( + 727756, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 208..218, + targets: [ + Name( + ExprName { + range: 208..209, + id: "x", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 212..218, + value: Int( + 11, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 219..228, + targets: [ + Name( + ExprName { + range: 219..220, + id: "x", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 223..228, + value: Int( + 511, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 229..244, + targets: [ + Name( + ExprName { + range: 229..230, + id: "x", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 233..244, + value: Float( + 6e-9, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 245..254, + targets: [ + Name( + ExprName { + range: 245..246, + id: "x", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 249..254, + value: Int( + 10000, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 255..265, + targets: [ + Name( + ExprName { + range: 255..256, + id: "x", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 259..265, + value: Int( + 133333, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 286..298, + targets: [ + Name( + ExprName { + range: 286..287, + id: "x", + ctx: Store, + }, + ), + ], + value: Attribute( + ExprAttribute { + range: 290..298, + value: NumberLiteral( + ExprNumberLiteral { + range: 290..292, + value: Float( + 1.0, + ), + }, + ), + attr: Identifier { + id: "imag", + range: 294..298, + }, + ctx: Load, + }, + ), + }, + ), + Assign( + StmtAssign { + range: 299..312, + targets: [ + Name( + ExprName { + range: 299..300, + id: "x", + ctx: Store, + }, + ), + ], + value: Attribute( + ExprAttribute { + range: 303..312, + value: NumberLiteral( + ExprNumberLiteral { + range: 303..307, + value: Float( + 10.0, + ), + }, + ), + attr: Identifier { + id: "imag", + range: 308..312, + }, + ctx: Load, + }, + ), + }, + ), + Assign( + StmtAssign { + range: 313..326, + targets: [ + Name( + ExprName { + range: 313..314, + id: "x", + ctx: Store, + }, + ), + ], + value: Attribute( + ExprAttribute { + range: 317..326, + value: NumberLiteral( + ExprNumberLiteral { + range: 317..321, + value: Float( + 0.1, + ), + }, + ), + attr: Identifier { + id: "real", + range: 322..326, + }, + ctx: Load, + }, + ), + }, + ), + Assign( + StmtAssign { + range: 327..356, + targets: [ + Name( + ExprName { + range: 327..328, + id: "x", + ctx: Store, + }, + ), + ], + value: Call( + ExprCall { + range: 331..356, + func: Attribute( + ExprAttribute { + range: 331..354, + value: NumberLiteral( + ExprNumberLiteral { + range: 331..350, + value: Float( + 123456789.12345679, + ), + }, + ), + attr: Identifier { + id: "hex", + range: 351..354, + }, + ctx: Load, + }, + ), + arguments: Arguments { + range: 354..356, + args: [], + keywords: [], + }, + }, + ), + }, + ), + Assign( + StmtAssign { + range: 357..396, + targets: [ + Name( + ExprName { + range: 357..358, + id: "x", + ctx: Store, + }, + ), + ], + value: Attribute( + ExprAttribute { + range: 361..396, + value: NumberLiteral( + ExprNumberLiteral { + range: 361..390, + value: Float( + inf, + ), + }, + ), + attr: Identifier { + id: "real", + range: 392..396, + }, + ctx: Load, + }, + ), + }, + ), + Assign( + StmtAssign { + range: 397..433, + targets: [ + Name( + ExprName { + range: 397..398, + id: "x", + ctx: Store, + }, + ), + ], + value: Call( + ExprCall { + range: 401..433, + func: Attribute( + ExprAttribute { + range: 401..431, + value: NumberLiteral( + ExprNumberLiteral { + range: 401..420, + value: Float( + inf, + ), + }, + ), + attr: Identifier { + id: "conjugate", + range: 422..431, + }, + ctx: Load, + }, + ), + arguments: Arguments { + range: 431..433, + args: [], + keywords: [], + }, + }, + ), + }, + ), + Assign( + StmtAssign { + range: 434..453, + targets: [ + Name( + ExprName { + range: 434..435, + id: "x", + ctx: Store, + }, + ), + ], + value: Attribute( + ExprAttribute { + range: 438..453, + value: NumberLiteral( + ExprNumberLiteral { + range: 438..448, + value: Complex { + real: 0.0, + imag: 123456789.0, + }, + }, + ), + attr: Identifier { + id: "real", + range: 449..453, + }, + ctx: Load, + }, + ), + }, + ), + Assign( + StmtAssign { + range: 454..507, + targets: [ + Name( + ExprName { + range: 454..455, + id: "x", + ctx: Store, + }, + ), + ], + value: Call( + ExprCall { + range: 458..507, + func: Attribute( + ExprAttribute { + range: 458..486, + value: NumberLiteral( + ExprNumberLiteral { + range: 458..478, + value: Complex { + real: 0.0, + imag: 123456789.12345679, + }, + }, + ), + attr: Identifier { + id: "__add__", + range: 479..486, + }, + ctx: Load, + }, + ), + arguments: Arguments { + range: 486..507, + args: [ + Call( + ExprCall { + range: 487..506, + func: Attribute( + ExprAttribute { + range: 487..504, + value: NumberLiteral( + ExprNumberLiteral { + range: 487..493, + value: Int( + 11, + ), + }, + ), + attr: Identifier { + id: "bit_length", + range: 494..504, + }, + ctx: Load, + }, + ), + arguments: Arguments { + range: 504..506, + args: [], + keywords: [], + }, + }, + ), + ], + keywords: [], + }, + }, + ), + }, + ), + Assign( + StmtAssign { + range: 508..531, + targets: [ + Name( + ExprName { + range: 508..509, + id: "x", + ctx: Store, + }, + ), + ], + value: Call( + ExprCall { + range: 512..531, + func: Attribute( + ExprAttribute { + range: 512..529, + value: NumberLiteral( + ExprNumberLiteral { + range: 512..519, + value: Int( + 727756, + ), + }, + ), + attr: Identifier { + id: "conjugate", + range: 520..529, + }, + ctx: Load, + }, + ), + arguments: Arguments { + range: 529..531, + args: [], + keywords: [], + }, + }, + ), + }, + ), + Assign( + StmtAssign { + range: 532..555, + targets: [ + Name( + ExprName { + range: 532..533, + id: "x", + ctx: Store, + }, + ), + ], + value: Call( + ExprCall { + range: 536..555, + func: Attribute( + ExprAttribute { + range: 536..553, + value: NumberLiteral( + ExprNumberLiteral { + range: 536..542, + value: Int( + 11, + ), + }, + ), + attr: Identifier { + id: "conjugate", + range: 544..553, + }, + ctx: Load, + }, + ), + arguments: Arguments { + range: 553..555, + args: [], + keywords: [], + }, + }, + ), + }, + ), + Assign( + StmtAssign { + range: 556..571, + targets: [ + Name( + ExprName { + range: 556..557, + id: "x", + ctx: Store, + }, + ), + ], + value: Attribute( + ExprAttribute { + range: 560..571, + value: NumberLiteral( + ExprNumberLiteral { + range: 560..565, + value: Int( + 511, + ), + }, + ), + attr: Identifier { + id: "real", + range: 567..571, + }, + ctx: Load, + }, + ), + }, + ), + Assign( + StmtAssign { + range: 572..595, + targets: [ + Name( + ExprName { + range: 572..573, + id: "x", + ctx: Store, + }, + ), + ], + value: Call( + ExprCall { + range: 576..595, + func: Attribute( + ExprAttribute { + range: 576..593, + value: NumberLiteral( + ExprNumberLiteral { + range: 576..587, + value: Float( + 6e-9, + ), + }, + ), + attr: Identifier { + id: "hex", + range: 590..593, + }, + ctx: Load, + }, + ), + arguments: Arguments { + range: 593..595, + args: [], + keywords: [], + }, + }, + ), + }, + ), + Assign( + StmtAssign { + range: 596..610, + targets: [ + Name( + ExprName { + range: 596..597, + id: "x", + ctx: Store, + }, + ), + ], + value: UnaryOp( + ExprUnaryOp { + range: 600..610, + op: USub, + operand: NumberLiteral( + ExprNumberLiteral { + range: 601..610, + value: Complex { + real: 0.0, + imag: 100.0, + }, + }, + ), + }, + ), + }, + ), + If( + StmtIf { + range: 612..632, + test: Attribute( + ExprAttribute { + range: 615..623, + value: NumberLiteral( + ExprNumberLiteral { + range: 615..617, + value: Int( + 10, + ), + }, + ), + attr: Identifier { + id: "real", + range: 619..623, + }, + ctx: Load, + }, + ), + body: [ + Expr( + StmtExpr { + range: 629..632, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 629..632, + }, + ), + }, + ), + ], + elif_else_clauses: [], + }, + ), + Assign( + StmtAssign { + range: 677..688, + targets: [ + Name( + ExprName { + range: 677..678, + id: "y", + ctx: Store, + }, + ), + ], + value: Subscript( + ExprSubscript { + range: 681..688, + value: NumberLiteral( + ExprNumberLiteral { + range: 681..684, + value: Int( + 100, + ), + }, + ), + slice: Name( + ExprName { + range: 685..687, + id: "no", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + }, + ), + Assign( + StmtAssign { + range: 689..700, + targets: [ + Name( + ExprName { + range: 689..690, + id: "y", + ctx: Store, + }, + ), + ], + value: Call( + ExprCall { + range: 693..700, + func: NumberLiteral( + ExprNumberLiteral { + range: 693..696, + value: Int( + 100, + ), + }, + ), + arguments: Arguments { + range: 696..700, + args: [ + Name( + ExprName { + range: 697..699, + id: "no", + ctx: Load, + }, + ), + ], + keywords: [], + }, + }, + ), + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__parenthesized.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__parenthesized.py.snap.new new file mode 100644 index 0000000000..e2d96cfc0c --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__parenthesized.py.snap.new @@ -0,0 +1,235 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/valid/expressions/parenthesized.py +--- +## AST + +``` +Module( + ModModule { + range: 0..92, + body: [ + Expr( + StmtExpr { + range: 0..6, + value: Name( + ExprName { + range: 1..5, + id: "expr", + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 7..15, + value: Call( + ExprCall { + range: 7..15, + func: Name( + ExprName { + range: 8..12, + id: "expr", + ctx: Load, + }, + ), + arguments: Arguments { + range: 13..15, + args: [], + keywords: [], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 16..28, + value: Call( + ExprCall { + range: 16..28, + func: Call( + ExprCall { + range: 16..26, + func: Call( + ExprCall { + range: 16..24, + func: Name( + ExprName { + range: 17..21, + id: "expr", + ctx: Load, + }, + ), + arguments: Arguments { + range: 22..24, + args: [], + keywords: [], + }, + }, + ), + arguments: Arguments { + range: 24..26, + args: [], + keywords: [], + }, + }, + ), + arguments: Arguments { + range: 26..28, + args: [], + keywords: [], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 30..44, + value: BoolOp( + ExprBoolOp { + range: 31..43, + op: Or, + values: [ + BoolOp( + ExprBoolOp { + range: 31..38, + op: And, + values: [ + Name( + ExprName { + range: 31..32, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 37..38, + id: "b", + ctx: Load, + }, + ), + ], + }, + ), + Name( + ExprName { + range: 42..43, + id: "c", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 45..58, + value: Lambda( + ExprLambda { + range: 46..57, + parameters: Some( + Parameters { + range: 53..54, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 53..54, + parameter: Parameter { + range: 53..54, + name: Identifier { + id: "x", + range: 53..54, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: Name( + ExprName { + range: 56..57, + id: "x", + ctx: Load, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 59..67, + value: Named( + ExprNamed { + range: 60..66, + target: Name( + ExprName { + range: 60..61, + id: "x", + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 65..66, + value: Int( + 2, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 68..77, + value: Yield( + ExprYield { + range: 69..76, + value: Some( + Name( + ExprName { + range: 75..76, + id: "x", + ctx: Load, + }, + ), + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 78..92, + value: YieldFrom( + ExprYieldFrom { + range: 79..91, + value: Name( + ExprName { + range: 90..91, + id: "x", + ctx: Load, + }, + ), + }, + ), + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__set.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__set.py.snap.new new file mode 100644 index 0000000000..823932b2d4 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__set.py.snap.new @@ -0,0 +1,621 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/valid/expressions/set.py +--- +## AST + +``` +Module( + ModModule { + range: 0..313, + body: [ + Expr( + StmtExpr { + range: 14..16, + value: Dict( + ExprDict { + range: 14..16, + items: [], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 17..20, + value: Set( + ExprSet { + range: 17..20, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 18..19, + value: Int( + 1, + ), + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 21..25, + value: Set( + ExprSet { + range: 21..25, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 22..23, + value: Int( + 1, + ), + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 26..35, + value: Set( + ExprSet { + range: 26..35, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 27..28, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 30..31, + value: Int( + 2, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 33..34, + value: Int( + 3, + ), + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 36..46, + value: Set( + ExprSet { + range: 36..46, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 37..38, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 40..41, + value: Int( + 2, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 43..44, + value: Int( + 3, + ), + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 74..77, + value: Dict( + ExprDict { + range: 74..77, + items: [], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 78..91, + value: Set( + ExprSet { + range: 78..91, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 88..89, + value: Int( + 1, + ), + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 92..113, + value: Set( + ExprSet { + range: 92..113, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 98..99, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 109..110, + value: Int( + 2, + ), + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 124..129, + value: Set( + ExprSet { + range: 124..129, + elts: [ + Set( + ExprSet { + range: 125..128, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 126..127, + value: Int( + 1, + ), + }, + ), + ], + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 130..146, + value: Set( + ExprSet { + range: 130..146, + elts: [ + Set( + ExprSet { + range: 131..137, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 132..133, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 135..136, + value: Int( + 2, + ), + }, + ), + ], + }, + ), + Set( + ExprSet { + range: 139..145, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 140..141, + value: Int( + 3, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 143..144, + value: Int( + 4, + ), + }, + ), + ], + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 167..175, + value: Set( + ExprSet { + range: 167..175, + elts: [ + Named( + ExprNamed { + range: 168..174, + target: Name( + ExprName { + range: 168..169, + id: "x", + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 173..174, + value: Int( + 2, + ), + }, + ), + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 176..190, + value: Set( + ExprSet { + range: 176..190, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 177..178, + value: Int( + 1, + ), + }, + ), + Named( + ExprNamed { + range: 180..186, + target: Name( + ExprName { + range: 180..181, + id: "x", + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 185..186, + value: Int( + 2, + ), + }, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 188..189, + value: Int( + 3, + ), + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 191..205, + value: Set( + ExprSet { + range: 191..205, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 192..193, + value: Int( + 1, + ), + }, + ), + Named( + ExprNamed { + range: 196..202, + target: Name( + ExprName { + range: 196..197, + id: "x", + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 201..202, + value: Int( + 2, + ), + }, + ), + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 225..235, + value: Set( + ExprSet { + range: 225..235, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 226..227, + value: Int( + 1, + ), + }, + ), + Starred( + ExprStarred { + range: 229..231, + value: Name( + ExprName { + range: 230..231, + id: "x", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 233..234, + value: Int( + 3, + ), + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 236..250, + value: Set( + ExprSet { + range: 236..250, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 237..238, + value: Int( + 1, + ), + }, + ), + Starred( + ExprStarred { + range: 240..246, + value: BinOp( + ExprBinOp { + range: 241..246, + left: Name( + ExprName { + range: 241..242, + id: "x", + ctx: Load, + }, + ), + op: BitOr, + right: Name( + ExprName { + range: 245..246, + id: "y", + ctx: Load, + }, + ), + }, + ), + ctx: Load, + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 248..249, + value: Int( + 3, + ), + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 273..312, + value: Set( + ExprSet { + range: 273..312, + elts: [ + BinOp( + ExprBinOp { + range: 274..279, + left: NumberLiteral( + ExprNumberLiteral { + range: 274..275, + value: Int( + 1, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 278..279, + value: Int( + 2, + ), + }, + ), + }, + ), + Tuple( + ExprTuple { + range: 281..287, + elts: [ + Name( + ExprName { + range: 282..283, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 285..286, + id: "b", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + Set( + ExprSet { + range: 289..298, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 290..291, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 293..294, + value: Int( + 2, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 296..297, + value: Int( + 3, + ), + }, + ), + ], + }, + ), + Dict( + ExprDict { + range: 300..311, + items: [ + DictItem { + key: Some( + Name( + ExprName { + range: 301..302, + id: "a", + ctx: Load, + }, + ), + ), + value: Name( + ExprName { + range: 304..305, + id: "b", + ctx: Load, + }, + ), + }, + DictItem { + key: None, + value: Name( + ExprName { + range: 309..310, + id: "d", + ctx: Load, + }, + ), + }, + ], + }, + ), + ], + }, + ), + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__set_comprehension.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__set_comprehension.py.snap.new new file mode 100644 index 0000000000..4cd0c84214 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__set_comprehension.py.snap.new @@ -0,0 +1,742 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/valid/expressions/set_comprehension.py +--- +## AST + +``` +Module( + ModModule { + range: 0..492, + body: [ + Expr( + StmtExpr { + range: 0..15, + value: SetComp( + ExprSetComp { + range: 0..15, + elt: Name( + ExprName { + range: 1..2, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 3..14, + target: Name( + ExprName { + range: 7..8, + id: "i", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 12..14, + id: "ll", + ctx: Load, + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 16..57, + value: SetComp( + ExprSetComp { + range: 16..57, + elt: Name( + ExprName { + range: 17..18, + id: "b", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 19..56, + target: Name( + ExprName { + range: 23..24, + id: "c", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 28..29, + id: "d", + ctx: Load, + }, + ), + ifs: [ + Compare( + ExprCompare { + range: 33..39, + left: Name( + ExprName { + range: 33..34, + id: "x", + ctx: Load, + }, + ), + ops: [ + In, + ], + comparators: [ + Name( + ExprName { + range: 38..39, + id: "w", + ctx: Load, + }, + ), + ], + }, + ), + BoolOp( + ExprBoolOp { + range: 43..51, + op: And, + values: [ + Name( + ExprName { + range: 43..44, + id: "y", + ctx: Load, + }, + ), + Name( + ExprName { + range: 49..51, + id: "yy", + ctx: Load, + }, + ), + ], + }, + ), + Name( + ExprName { + range: 55..56, + id: "z", + ctx: Load, + }, + ), + ], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 58..103, + value: SetComp( + ExprSetComp { + range: 58..103, + elt: Name( + ExprName { + range: 59..60, + id: "a", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 61..82, + target: Name( + ExprName { + range: 65..66, + id: "b", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 70..71, + id: "c", + ctx: Load, + }, + ), + ifs: [ + BoolOp( + ExprBoolOp { + range: 75..82, + op: And, + values: [ + Name( + ExprName { + range: 75..76, + id: "d", + ctx: Load, + }, + ), + Name( + ExprName { + range: 81..82, + id: "e", + ctx: Load, + }, + ), + ], + }, + ), + ], + is_async: false, + }, + Comprehension { + range: 83..102, + target: Name( + ExprName { + range: 87..88, + id: "f", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 92..93, + id: "j", + ctx: Load, + }, + ), + ifs: [ + Compare( + ExprCompare { + range: 97..102, + left: Name( + ExprName { + range: 97..98, + id: "k", + ctx: Load, + }, + ), + ops: [ + Gt, + ], + comparators: [ + Name( + ExprName { + range: 101..102, + id: "h", + ctx: Load, + }, + ), + ], + }, + ), + ], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 104..155, + value: SetComp( + ExprSetComp { + range: 104..155, + elt: Name( + ExprName { + range: 105..106, + id: "a", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 107..128, + target: Name( + ExprName { + range: 111..112, + id: "b", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 116..117, + id: "c", + ctx: Load, + }, + ), + ifs: [ + BoolOp( + ExprBoolOp { + range: 121..128, + op: And, + values: [ + Name( + ExprName { + range: 121..122, + id: "d", + ctx: Load, + }, + ), + Name( + ExprName { + range: 127..128, + id: "e", + ctx: Load, + }, + ), + ], + }, + ), + ], + is_async: false, + }, + Comprehension { + range: 129..154, + target: Name( + ExprName { + range: 139..140, + id: "f", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 144..145, + id: "j", + ctx: Load, + }, + ), + ifs: [ + Compare( + ExprCompare { + range: 149..154, + left: Name( + ExprName { + range: 149..150, + id: "k", + ctx: Load, + }, + ), + ops: [ + Gt, + ], + comparators: [ + Name( + ExprName { + range: 153..154, + id: "h", + ctx: Load, + }, + ), + ], + }, + ), + ], + is_async: true, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 156..173, + value: SetComp( + ExprSetComp { + range: 156..173, + elt: Name( + ExprName { + range: 157..158, + id: "a", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 159..172, + target: Tuple( + ExprTuple { + range: 163..167, + elts: [ + Name( + ExprName { + range: 163..164, + id: "a", + ctx: Store, + }, + ), + Name( + ExprName { + range: 166..167, + id: "b", + ctx: Store, + }, + ), + ], + ctx: Store, + parenthesized: false, + }, + ), + iter: Name( + ExprName { + range: 171..172, + id: "G", + ctx: Load, + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 312..334, + value: SetComp( + ExprSetComp { + range: 312..334, + elt: Name( + ExprName { + range: 313..314, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 315..333, + target: Name( + ExprName { + range: 319..320, + id: "x", + ctx: Store, + }, + ), + iter: Yield( + ExprYield { + range: 325..332, + value: Some( + Name( + ExprName { + range: 331..332, + id: "y", + ctx: Load, + }, + ), + ), + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 335..362, + value: SetComp( + ExprSetComp { + range: 335..362, + elt: Name( + ExprName { + range: 336..337, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 338..361, + target: Name( + ExprName { + range: 342..343, + id: "x", + ctx: Store, + }, + ), + iter: YieldFrom( + ExprYieldFrom { + range: 348..360, + value: Name( + ExprName { + range: 359..360, + id: "y", + ctx: Load, + }, + ), + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 363..389, + value: SetComp( + ExprSetComp { + range: 363..389, + elt: Name( + ExprName { + range: 364..365, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 366..388, + target: Name( + ExprName { + range: 370..371, + id: "x", + ctx: Store, + }, + ), + iter: Lambda( + ExprLambda { + range: 376..387, + parameters: Some( + Parameters { + range: 383..384, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 383..384, + parameter: Parameter { + range: 383..384, + name: Identifier { + id: "y", + range: 383..384, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: Name( + ExprName { + range: 386..387, + id: "y", + ctx: Load, + }, + ), + }, + ), + ifs: [], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 390..420, + value: SetComp( + ExprSetComp { + range: 390..420, + elt: Name( + ExprName { + range: 391..392, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 393..419, + target: Name( + ExprName { + range: 397..398, + id: "x", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 402..406, + id: "data", + ctx: Load, + }, + ), + ifs: [ + Yield( + ExprYield { + range: 411..418, + value: Some( + Name( + ExprName { + range: 417..418, + id: "y", + ctx: Load, + }, + ), + ), + }, + ), + ], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 421..456, + value: SetComp( + ExprSetComp { + range: 421..456, + elt: Name( + ExprName { + range: 422..423, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 424..455, + target: Name( + ExprName { + range: 428..429, + id: "x", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 433..437, + id: "data", + ctx: Load, + }, + ), + ifs: [ + YieldFrom( + ExprYieldFrom { + range: 442..454, + value: Name( + ExprName { + range: 453..454, + id: "y", + ctx: Load, + }, + ), + }, + ), + ], + is_async: false, + }, + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 457..491, + value: SetComp( + ExprSetComp { + range: 457..491, + elt: Name( + ExprName { + range: 458..459, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 460..490, + target: Name( + ExprName { + range: 464..465, + id: "x", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 469..473, + id: "data", + ctx: Load, + }, + ), + ifs: [ + Lambda( + ExprLambda { + range: 478..489, + parameters: Some( + Parameters { + range: 485..486, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 485..486, + parameter: Parameter { + range: 485..486, + name: Identifier { + id: "y", + range: 485..486, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: Name( + ExprName { + range: 488..489, + id: "y", + ctx: Load, + }, + ), + }, + ), + ], + is_async: false, + }, + ], + }, + ), + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__slice.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__slice.py.snap.new new file mode 100644 index 0000000000..de1e06c063 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__slice.py.snap.new @@ -0,0 +1,656 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/valid/expressions/slice.py +--- +## AST + +``` +Module( + ModModule { + range: 0..211, + body: [ + Expr( + StmtExpr { + range: 23..27, + value: Subscript( + ExprSubscript { + range: 23..27, + value: Name( + ExprName { + range: 23..24, + id: "x", + ctx: Load, + }, + ), + slice: Slice( + ExprSlice { + range: 25..26, + lower: None, + upper: None, + step: None, + }, + ), + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 28..33, + value: Subscript( + ExprSubscript { + range: 28..33, + value: Name( + ExprName { + range: 28..29, + id: "x", + ctx: Load, + }, + ), + slice: Slice( + ExprSlice { + range: 30..32, + lower: Some( + NumberLiteral( + ExprNumberLiteral { + range: 30..31, + value: Int( + 1, + ), + }, + ), + ), + upper: None, + step: None, + }, + ), + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 34..39, + value: Subscript( + ExprSubscript { + range: 34..39, + value: Name( + ExprName { + range: 34..35, + id: "x", + ctx: Load, + }, + ), + slice: Slice( + ExprSlice { + range: 36..38, + lower: None, + upper: Some( + NumberLiteral( + ExprNumberLiteral { + range: 37..38, + value: Int( + 2, + ), + }, + ), + ), + step: None, + }, + ), + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 40..46, + value: Subscript( + ExprSubscript { + range: 40..46, + value: Name( + ExprName { + range: 40..41, + id: "x", + ctx: Load, + }, + ), + slice: Slice( + ExprSlice { + range: 42..45, + lower: Some( + NumberLiteral( + ExprNumberLiteral { + range: 42..43, + value: Int( + 1, + ), + }, + ), + ), + upper: Some( + NumberLiteral( + ExprNumberLiteral { + range: 44..45, + value: Int( + 2, + ), + }, + ), + ), + step: None, + }, + ), + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 47..52, + value: Subscript( + ExprSubscript { + range: 47..52, + value: Name( + ExprName { + range: 47..48, + id: "x", + ctx: Load, + }, + ), + slice: Slice( + ExprSlice { + range: 49..51, + lower: None, + upper: None, + step: None, + }, + ), + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 53..59, + value: Subscript( + ExprSubscript { + range: 53..59, + value: Name( + ExprName { + range: 53..54, + id: "x", + ctx: Load, + }, + ), + slice: Slice( + ExprSlice { + range: 55..58, + lower: Some( + NumberLiteral( + ExprNumberLiteral { + range: 55..56, + value: Int( + 1, + ), + }, + ), + ), + upper: None, + step: None, + }, + ), + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 60..66, + value: Subscript( + ExprSubscript { + range: 60..66, + value: Name( + ExprName { + range: 60..61, + id: "x", + ctx: Load, + }, + ), + slice: Slice( + ExprSlice { + range: 62..65, + lower: None, + upper: Some( + NumberLiteral( + ExprNumberLiteral { + range: 63..64, + value: Int( + 2, + ), + }, + ), + ), + step: None, + }, + ), + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 67..74, + value: Subscript( + ExprSubscript { + range: 67..74, + value: Name( + ExprName { + range: 67..68, + id: "x", + ctx: Load, + }, + ), + slice: Slice( + ExprSlice { + range: 69..73, + lower: Some( + NumberLiteral( + ExprNumberLiteral { + range: 69..70, + value: Int( + 1, + ), + }, + ), + ), + upper: Some( + NumberLiteral( + ExprNumberLiteral { + range: 71..72, + value: Int( + 2, + ), + }, + ), + ), + step: None, + }, + ), + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 75..81, + value: Subscript( + ExprSubscript { + range: 75..81, + value: Name( + ExprName { + range: 75..76, + id: "x", + ctx: Load, + }, + ), + slice: Slice( + ExprSlice { + range: 77..80, + lower: None, + upper: None, + step: Some( + NumberLiteral( + ExprNumberLiteral { + range: 79..80, + value: Int( + 3, + ), + }, + ), + ), + }, + ), + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 82..89, + value: Subscript( + ExprSubscript { + range: 82..89, + value: Name( + ExprName { + range: 82..83, + id: "x", + ctx: Load, + }, + ), + slice: Slice( + ExprSlice { + range: 84..88, + lower: Some( + NumberLiteral( + ExprNumberLiteral { + range: 84..85, + value: Int( + 1, + ), + }, + ), + ), + upper: None, + step: Some( + NumberLiteral( + ExprNumberLiteral { + range: 87..88, + value: Int( + 3, + ), + }, + ), + ), + }, + ), + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 90..97, + value: Subscript( + ExprSubscript { + range: 90..97, + value: Name( + ExprName { + range: 90..91, + id: "x", + ctx: Load, + }, + ), + slice: Slice( + ExprSlice { + range: 92..96, + lower: None, + upper: Some( + NumberLiteral( + ExprNumberLiteral { + range: 93..94, + value: Int( + 2, + ), + }, + ), + ), + step: Some( + NumberLiteral( + ExprNumberLiteral { + range: 95..96, + value: Int( + 3, + ), + }, + ), + ), + }, + ), + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 98..106, + value: Subscript( + ExprSubscript { + range: 98..106, + value: Name( + ExprName { + range: 98..99, + id: "x", + ctx: Load, + }, + ), + slice: Slice( + ExprSlice { + range: 100..105, + lower: Some( + NumberLiteral( + ExprNumberLiteral { + range: 100..101, + value: Int( + 1, + ), + }, + ), + ), + upper: Some( + NumberLiteral( + ExprNumberLiteral { + range: 102..103, + value: Int( + 2, + ), + }, + ), + ), + step: Some( + NumberLiteral( + ExprNumberLiteral { + range: 104..105, + value: Int( + 3, + ), + }, + ), + ), + }, + ), + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 127..136, + value: Subscript( + ExprSubscript { + range: 127..136, + value: Name( + ExprName { + range: 127..128, + id: "x", + ctx: Load, + }, + ), + slice: Named( + ExprNamed { + range: 129..135, + target: Name( + ExprName { + range: 129..130, + id: "y", + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 134..135, + value: Int( + 2, + ), + }, + ), + }, + ), + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 137..149, + value: Subscript( + ExprSubscript { + range: 137..149, + value: Name( + ExprName { + range: 137..138, + id: "x", + ctx: Load, + }, + ), + slice: Slice( + ExprSlice { + range: 139..148, + lower: Some( + Named( + ExprNamed { + range: 140..146, + target: Name( + ExprName { + range: 140..141, + id: "y", + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 145..146, + value: Int( + 2, + ), + }, + ), + }, + ), + ), + upper: None, + step: None, + }, + ), + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 150..160, + value: Subscript( + ExprSubscript { + range: 150..160, + value: Name( + ExprName { + range: 150..151, + id: "x", + ctx: Load, + }, + ), + slice: Tuple( + ExprTuple { + range: 152..159, + elts: [ + Named( + ExprNamed { + range: 152..158, + target: Name( + ExprName { + range: 152..153, + id: "y", + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 157..158, + value: Int( + 2, + ), + }, + ), + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 202..210, + value: Subscript( + ExprSubscript { + range: 202..210, + value: Name( + ExprName { + range: 202..203, + id: "x", + ctx: Load, + }, + ), + slice: Tuple( + ExprTuple { + range: 204..209, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 204..205, + value: Int( + 1, + ), + }, + ), + Slice( + ExprSlice { + range: 206..208, + lower: None, + upper: Some( + NumberLiteral( + ExprNumberLiteral { + range: 207..208, + value: Int( + 2, + ), + }, + ), + ), + step: None, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + ctx: Load, + }, + ), + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__starred.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__starred.py.snap.new new file mode 100644 index 0000000000..d310ab195a --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__starred.py.snap.new @@ -0,0 +1,355 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/valid/expressions/starred.py +--- +## AST + +``` +Module( + ModModule { + range: 0..172, + body: [ + Expr( + StmtExpr { + range: 0..2, + value: Starred( + ExprStarred { + range: 0..2, + value: Name( + ExprName { + range: 1..2, + id: "a", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 3..11, + value: Starred( + ExprStarred { + range: 3..11, + value: BinOp( + ExprBinOp { + range: 5..10, + left: Name( + ExprName { + range: 5..6, + id: "a", + ctx: Load, + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 9..10, + value: Int( + 1, + ), + }, + ), + }, + ), + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 12..19, + value: Starred( + ExprStarred { + range: 12..19, + value: Attribute( + ExprAttribute { + range: 13..19, + value: Name( + ExprName { + range: 13..14, + id: "x", + ctx: Load, + }, + ), + attr: Identifier { + id: "attr", + range: 15..19, + }, + ctx: Load, + }, + ), + ctx: Load, + }, + ), + }, + ), + Assign( + StmtAssign { + range: 21..57, + targets: [ + Name( + ExprName { + range: 21..32, + id: "array_slice", + ctx: Store, + }, + ), + ], + value: Subscript( + ExprSubscript { + range: 35..57, + value: Name( + ExprName { + range: 35..40, + id: "array", + ctx: Load, + }, + ), + slice: Tuple( + ExprTuple { + range: 41..56, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 41..42, + value: Int( + 0, + ), + }, + ), + Starred( + ExprStarred { + range: 44..52, + value: Name( + ExprName { + range: 45..52, + id: "indexes", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + UnaryOp( + ExprUnaryOp { + range: 54..56, + op: USub, + operand: NumberLiteral( + ExprNumberLiteral { + range: 55..56, + value: Int( + 1, + ), + }, + ), + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + ctx: Load, + }, + ), + }, + ), + Assign( + StmtAssign { + range: 58..94, + targets: [ + Subscript( + ExprSubscript { + range: 58..80, + value: Name( + ExprName { + range: 58..63, + id: "array", + ctx: Load, + }, + ), + slice: Tuple( + ExprTuple { + range: 64..79, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 64..65, + value: Int( + 0, + ), + }, + ), + Starred( + ExprStarred { + range: 67..75, + value: Name( + ExprName { + range: 68..75, + id: "indexes", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + UnaryOp( + ExprUnaryOp { + range: 77..79, + op: USub, + operand: NumberLiteral( + ExprNumberLiteral { + range: 78..79, + value: Int( + 1, + ), + }, + ), + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + ctx: Store, + }, + ), + ], + value: Name( + ExprName { + range: 83..94, + id: "array_slice", + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 95..140, + value: Subscript( + ExprSubscript { + range: 95..140, + value: Name( + ExprName { + range: 95..100, + id: "array", + ctx: Load, + }, + ), + slice: Tuple( + ExprTuple { + range: 101..139, + elts: [ + Starred( + ExprStarred { + range: 101..119, + value: Name( + ExprName { + range: 102..119, + id: "indexes_to_select", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + Starred( + ExprStarred { + range: 121..139, + value: Name( + ExprName { + range: 122..139, + id: "indexes_to_select", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 141..171, + value: Subscript( + ExprSubscript { + range: 141..171, + value: Name( + ExprName { + range: 141..146, + id: "array", + ctx: Load, + }, + ), + slice: Tuple( + ExprTuple { + range: 147..170, + elts: [ + Slice( + ExprSlice { + range: 147..150, + lower: Some( + NumberLiteral( + ExprNumberLiteral { + range: 147..148, + value: Int( + 3, + ), + }, + ), + ), + upper: Some( + NumberLiteral( + ExprNumberLiteral { + range: 149..150, + value: Int( + 5, + ), + }, + ), + ), + step: None, + }, + ), + Starred( + ExprStarred { + range: 152..170, + value: Name( + ExprName { + range: 153..170, + id: "indexes_to_select", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + ctx: Load, + }, + ), + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__subscript.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__subscript.py.snap.new new file mode 100644 index 0000000000..cddc33cc34 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__subscript.py.snap.new @@ -0,0 +1,784 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/valid/expressions/subscript.py +--- +## AST + +``` +Module( + ModModule { + range: 0..266, + body: [ + Expr( + StmtExpr { + range: 0..10, + value: Subscript( + ExprSubscript { + range: 0..10, + value: Subscript( + ExprSubscript { + range: 0..7, + value: Name( + ExprName { + range: 0..4, + id: "data", + ctx: Load, + }, + ), + slice: NumberLiteral( + ExprNumberLiteral { + range: 5..6, + value: Int( + 0, + ), + }, + ), + ctx: Load, + }, + ), + slice: NumberLiteral( + ExprNumberLiteral { + range: 8..9, + value: Int( + 0, + ), + }, + ), + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 11..21, + value: Subscript( + ExprSubscript { + range: 11..21, + value: Name( + ExprName { + range: 11..15, + id: "data", + ctx: Load, + }, + ), + slice: Tuple( + ExprTuple { + range: 16..20, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 16..17, + value: Int( + 0, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 19..20, + value: Int( + 1, + ), + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 22..31, + value: Subscript( + ExprSubscript { + range: 22..31, + value: Name( + ExprName { + range: 22..26, + id: "data", + ctx: Load, + }, + ), + slice: Tuple( + ExprTuple { + range: 27..30, + elts: [ + Slice( + ExprSlice { + range: 27..29, + lower: Some( + NumberLiteral( + ExprNumberLiteral { + range: 27..28, + value: Int( + 0, + ), + }, + ), + ), + upper: None, + step: None, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 32..43, + value: Subscript( + ExprSubscript { + range: 32..43, + value: Name( + ExprName { + range: 32..36, + id: "data", + ctx: Load, + }, + ), + slice: Tuple( + ExprTuple { + range: 37..42, + elts: [ + Slice( + ExprSlice { + range: 37..39, + lower: Some( + NumberLiteral( + ExprNumberLiteral { + range: 37..38, + value: Int( + 0, + ), + }, + ), + ), + upper: None, + step: None, + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 41..42, + value: Int( + 1, + ), + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 44..56, + value: Subscript( + ExprSubscript { + range: 44..56, + value: Name( + ExprName { + range: 44..48, + id: "data", + ctx: Load, + }, + ), + slice: Tuple( + ExprTuple { + range: 49..55, + elts: [ + Slice( + ExprSlice { + range: 49..52, + lower: Some( + NumberLiteral( + ExprNumberLiteral { + range: 49..50, + value: Int( + 0, + ), + }, + ), + ), + upper: Some( + NumberLiteral( + ExprNumberLiteral { + range: 51..52, + value: Int( + 1, + ), + }, + ), + ), + step: None, + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 54..55, + value: Int( + 2, + ), + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 57..80, + value: Subscript( + ExprSubscript { + range: 57..80, + value: Name( + ExprName { + range: 57..61, + id: "data", + ctx: Load, + }, + ), + slice: Tuple( + ExprTuple { + range: 62..79, + elts: [ + Slice( + ExprSlice { + range: 62..67, + lower: Some( + NumberLiteral( + ExprNumberLiteral { + range: 62..63, + value: Int( + 0, + ), + }, + ), + ), + upper: Some( + NumberLiteral( + ExprNumberLiteral { + range: 64..65, + value: Int( + 1, + ), + }, + ), + ), + step: Some( + NumberLiteral( + ExprNumberLiteral { + range: 66..67, + value: Int( + 2, + ), + }, + ), + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 69..70, + value: Int( + 3, + ), + }, + ), + Slice( + ExprSlice { + range: 72..79, + lower: Some( + Name( + ExprName { + range: 72..73, + id: "a", + ctx: Load, + }, + ), + ), + upper: Some( + BinOp( + ExprBinOp { + range: 74..79, + left: Name( + ExprName { + range: 74..75, + id: "b", + ctx: Load, + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 78..79, + value: Int( + 1, + ), + }, + ), + }, + ), + ), + step: None, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 81..93, + value: Subscript( + ExprSubscript { + range: 81..93, + value: Name( + ExprName { + range: 81..85, + id: "data", + ctx: Load, + }, + ), + slice: Named( + ExprNamed { + range: 86..92, + target: Name( + ExprName { + range: 86..87, + id: "a", + ctx: Store, + }, + ), + value: Name( + ExprName { + range: 91..92, + id: "b", + ctx: Load, + }, + ), + }, + ), + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 94..106, + value: Subscript( + ExprSubscript { + range: 94..106, + value: Name( + ExprName { + range: 94..98, + id: "data", + ctx: Load, + }, + ), + slice: Tuple( + ExprTuple { + range: 99..105, + elts: [ + Slice( + ExprSlice { + range: 99..100, + lower: None, + upper: None, + step: None, + }, + ), + Slice( + ExprSlice { + range: 102..105, + lower: None, + upper: Some( + NumberLiteral( + ExprNumberLiteral { + range: 103..105, + value: Int( + 11, + ), + }, + ), + ), + step: None, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 107..120, + value: Subscript( + ExprSubscript { + range: 107..120, + value: Name( + ExprName { + range: 107..111, + id: "data", + ctx: Load, + }, + ), + slice: Tuple( + ExprTuple { + range: 112..119, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 112..113, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 115..116, + value: Int( + 2, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 118..119, + value: Int( + 3, + ), + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 121..132, + value: Subscript( + ExprSubscript { + range: 121..132, + value: Name( + ExprName { + range: 121..125, + id: "data", + ctx: Load, + }, + ), + slice: UnaryOp( + ExprUnaryOp { + range: 126..131, + op: Invert, + operand: Name( + ExprName { + range: 127..131, + id: "flag", + ctx: Load, + }, + ), + }, + ), + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 133..148, + value: Subscript( + ExprSubscript { + range: 133..148, + value: Name( + ExprName { + range: 133..137, + id: "data", + ctx: Load, + }, + ), + slice: Slice( + ExprSlice { + range: 138..147, + lower: Some( + Named( + ExprNamed { + range: 139..145, + target: Name( + ExprName { + range: 139..140, + id: "a", + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 144..145, + value: Int( + 0, + ), + }, + ), + }, + ), + ), + upper: None, + step: None, + }, + ), + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 149..165, + value: Subscript( + ExprSubscript { + range: 149..165, + value: Name( + ExprName { + range: 149..153, + id: "data", + ctx: Load, + }, + ), + slice: Slice( + ExprSlice { + range: 154..164, + lower: Some( + Named( + ExprNamed { + range: 155..161, + target: Name( + ExprName { + range: 155..156, + id: "a", + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 160..161, + value: Int( + 0, + ), + }, + ), + }, + ), + ), + upper: Some( + Name( + ExprName { + range: 163..164, + id: "y", + ctx: Load, + }, + ), + ), + step: None, + }, + ), + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 226..234, + value: Subscript( + ExprSubscript { + range: 226..234, + value: Name( + ExprName { + range: 226..230, + id: "data", + ctx: Load, + }, + ), + slice: Tuple( + ExprTuple { + range: 231..233, + elts: [ + Starred( + ExprStarred { + range: 231..233, + value: Name( + ExprName { + range: 232..233, + id: "x", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 235..249, + value: Subscript( + ExprSubscript { + range: 235..249, + value: Name( + ExprName { + range: 235..239, + id: "data", + ctx: Load, + }, + ), + slice: Tuple( + ExprTuple { + range: 240..248, + elts: [ + Starred( + ExprStarred { + range: 240..248, + value: BoolOp( + ExprBoolOp { + range: 241..248, + op: And, + values: [ + Name( + ExprName { + range: 241..242, + id: "x", + ctx: Load, + }, + ), + Name( + ExprName { + range: 247..248, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 250..265, + value: Subscript( + ExprSubscript { + range: 250..265, + value: Name( + ExprName { + range: 250..254, + id: "data", + ctx: Load, + }, + ), + slice: Tuple( + ExprTuple { + range: 255..264, + elts: [ + Starred( + ExprStarred { + range: 255..264, + value: Named( + ExprNamed { + range: 257..263, + target: Name( + ExprName { + range: 257..258, + id: "x", + ctx: Store, + }, + ), + value: Name( + ExprName { + range: 262..263, + id: "y", + ctx: Load, + }, + ), + }, + ), + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + ctx: Load, + }, + ), + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__tuple.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__tuple.py.snap.new new file mode 100644 index 0000000000..da4b9fcf9d --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__tuple.py.snap.new @@ -0,0 +1,707 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/valid/expressions/tuple.py +--- +## AST + +``` +Module( + ModModule { + range: 0..276, + body: [ + Expr( + StmtExpr { + range: 19..21, + value: Tuple( + ExprTuple { + range: 19..21, + elts: [], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 22..26, + value: Tuple( + ExprTuple { + range: 23..25, + elts: [], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 27..37, + value: Tuple( + ExprTuple { + range: 27..37, + elts: [ + Tuple( + ExprTuple { + range: 29..31, + elts: [], + ctx: Load, + parenthesized: true, + }, + ), + Tuple( + ExprTuple { + range: 34..36, + elts: [], + ctx: Load, + parenthesized: true, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 38..42, + value: Tuple( + ExprTuple { + range: 38..42, + elts: [ + Name( + ExprName { + range: 39..40, + id: "a", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 43..49, + value: Tuple( + ExprTuple { + range: 43..49, + elts: [ + Name( + ExprName { + range: 44..45, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 47..48, + id: "b", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 50..57, + value: Tuple( + ExprTuple { + range: 50..57, + elts: [ + Name( + ExprName { + range: 51..52, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 54..55, + id: "b", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 58..66, + value: Tuple( + ExprTuple { + range: 59..65, + elts: [ + Name( + ExprName { + range: 60..61, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 63..64, + id: "b", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 90..92, + value: Tuple( + ExprTuple { + range: 90..92, + elts: [ + Name( + ExprName { + range: 90..91, + id: "a", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 93..97, + value: Tuple( + ExprTuple { + range: 93..97, + elts: [ + Name( + ExprName { + range: 93..94, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 96..97, + id: "b", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 98..103, + value: Tuple( + ExprTuple { + range: 98..103, + elts: [ + Name( + ExprName { + range: 98..99, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 101..102, + id: "b", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 126..129, + value: Tuple( + ExprTuple { + range: 126..129, + elts: [ + Starred( + ExprStarred { + range: 126..128, + value: Name( + ExprName { + range: 127..128, + id: "a", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 130..135, + value: Tuple( + ExprTuple { + range: 130..135, + elts: [ + Name( + ExprName { + range: 130..131, + id: "a", + ctx: Load, + }, + ), + Starred( + ExprStarred { + range: 133..135, + value: Name( + ExprName { + range: 134..135, + id: "b", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 136..161, + value: Tuple( + ExprTuple { + range: 136..161, + elts: [ + Starred( + ExprStarred { + range: 136..142, + value: BinOp( + ExprBinOp { + range: 137..142, + left: Name( + ExprName { + range: 137..138, + id: "a", + ctx: Load, + }, + ), + op: BitOr, + right: Name( + ExprName { + range: 141..142, + id: "b", + ctx: Load, + }, + ), + }, + ), + ctx: Load, + }, + ), + Starred( + ExprStarred { + range: 144..152, + value: Await( + ExprAwait { + range: 145..152, + value: Name( + ExprName { + range: 151..152, + id: "x", + ctx: Load, + }, + ), + }, + ), + ctx: Load, + }, + ), + Tuple( + ExprTuple { + range: 154..156, + elts: [], + ctx: Load, + parenthesized: true, + }, + ), + Starred( + ExprStarred { + range: 158..161, + value: Tuple( + ExprTuple { + range: 159..161, + elts: [], + ctx: Load, + parenthesized: true, + }, + ), + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 162..167, + value: Tuple( + ExprTuple { + range: 162..167, + elts: [ + Starred( + ExprStarred { + range: 163..165, + value: Name( + ExprName { + range: 164..165, + id: "a", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 168..175, + value: Tuple( + ExprTuple { + range: 168..175, + elts: [ + Name( + ExprName { + range: 169..170, + id: "a", + ctx: Load, + }, + ), + Starred( + ExprStarred { + range: 172..174, + value: Name( + ExprName { + range: 173..174, + id: "b", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 176..203, + value: Tuple( + ExprTuple { + range: 176..203, + elts: [ + Starred( + ExprStarred { + range: 177..183, + value: BinOp( + ExprBinOp { + range: 178..183, + left: Name( + ExprName { + range: 178..179, + id: "a", + ctx: Load, + }, + ), + op: BitOr, + right: Name( + ExprName { + range: 182..183, + id: "b", + ctx: Load, + }, + ), + }, + ), + ctx: Load, + }, + ), + Starred( + ExprStarred { + range: 185..193, + value: Await( + ExprAwait { + range: 186..193, + value: Name( + ExprName { + range: 192..193, + id: "x", + ctx: Load, + }, + ), + }, + ), + ctx: Load, + }, + ), + Tuple( + ExprTuple { + range: 195..197, + elts: [], + ctx: Load, + parenthesized: true, + }, + ), + Starred( + ExprStarred { + range: 199..202, + value: Tuple( + ExprTuple { + range: 200..202, + elts: [], + ctx: Load, + parenthesized: true, + }, + ), + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 224..233, + value: Tuple( + ExprTuple { + range: 224..233, + elts: [ + Named( + ExprNamed { + range: 225..231, + target: Name( + ExprName { + range: 225..226, + id: "x", + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 230..231, + value: Int( + 1, + ), + }, + ), + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 234..245, + value: Tuple( + ExprTuple { + range: 234..245, + elts: [ + Name( + ExprName { + range: 235..236, + id: "x", + ctx: Load, + }, + ), + Named( + ExprNamed { + range: 238..244, + target: Name( + ExprName { + range: 238..239, + id: "y", + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 243..244, + value: Int( + 2, + ), + }, + ), + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 246..260, + value: Tuple( + ExprTuple { + range: 246..260, + elts: [ + Name( + ExprName { + range: 247..248, + id: "x", + ctx: Load, + }, + ), + Named( + ExprNamed { + range: 250..256, + target: Name( + ExprName { + range: 250..251, + id: "y", + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 255..256, + value: Int( + 2, + ), + }, + ), + }, + ), + Name( + ExprName { + range: 258..259, + id: "z", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 261..275, + value: Tuple( + ExprTuple { + range: 261..275, + elts: [ + Name( + ExprName { + range: 261..262, + id: "x", + ctx: Load, + }, + ), + Named( + ExprNamed { + range: 265..271, + target: Name( + ExprName { + range: 265..266, + id: "y", + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 270..271, + value: Int( + 2, + ), + }, + ), + }, + ), + Name( + ExprName { + range: 274..275, + id: "z", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__unary_op.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__unary_op.py.snap.new new file mode 100644 index 0000000000..46f0b9200f --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__unary_op.py.snap.new @@ -0,0 +1,507 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/valid/expressions/unary_op.py +--- +## AST + +``` +Module( + ModModule { + range: 0..276, + body: [ + Expr( + StmtExpr { + range: 9..11, + value: UnaryOp( + ExprUnaryOp { + range: 9..11, + op: USub, + operand: NumberLiteral( + ExprNumberLiteral { + range: 10..11, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 12..14, + value: UnaryOp( + ExprUnaryOp { + range: 12..14, + op: UAdd, + operand: NumberLiteral( + ExprNumberLiteral { + range: 13..14, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 15..17, + value: UnaryOp( + ExprUnaryOp { + range: 15..17, + op: Invert, + operand: NumberLiteral( + ExprNumberLiteral { + range: 16..17, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 18..23, + value: UnaryOp( + ExprUnaryOp { + range: 18..23, + op: Not, + operand: Name( + ExprName { + range: 22..23, + id: "x", + ctx: Load, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 36..40, + value: UnaryOp( + ExprUnaryOp { + range: 36..40, + op: USub, + operand: UnaryOp( + ExprUnaryOp { + range: 37..40, + op: USub, + operand: UnaryOp( + ExprUnaryOp { + range: 38..40, + op: USub, + operand: NumberLiteral( + ExprNumberLiteral { + range: 39..40, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 41..45, + value: UnaryOp( + ExprUnaryOp { + range: 41..45, + op: USub, + operand: UnaryOp( + ExprUnaryOp { + range: 42..45, + op: UAdd, + operand: UnaryOp( + ExprUnaryOp { + range: 43..45, + op: Invert, + operand: NumberLiteral( + ExprNumberLiteral { + range: 44..45, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 46..53, + value: UnaryOp( + ExprUnaryOp { + range: 46..53, + op: Not, + operand: UnaryOp( + ExprUnaryOp { + range: 49..53, + op: USub, + operand: UnaryOp( + ExprUnaryOp { + range: 50..53, + op: UAdd, + operand: UnaryOp( + ExprUnaryOp { + range: 51..53, + op: Invert, + operand: NumberLiteral( + ExprNumberLiteral { + range: 52..53, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 54..63, + value: UnaryOp( + ExprUnaryOp { + range: 54..63, + op: Not, + operand: UnaryOp( + ExprUnaryOp { + range: 58..63, + op: Not, + operand: Name( + ExprName { + range: 62..63, + id: "x", + ctx: Load, + }, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 84..93, + value: UnaryOp( + ExprUnaryOp { + range: 84..93, + op: USub, + operand: Await( + ExprAwait { + range: 86..93, + value: NumberLiteral( + ExprNumberLiteral { + range: 92..93, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 94..109, + value: UnaryOp( + ExprUnaryOp { + range: 94..109, + op: UAdd, + operand: BinOp( + ExprBinOp { + range: 96..109, + left: Await( + ExprAwait { + range: 96..103, + value: NumberLiteral( + ExprNumberLiteral { + range: 102..103, + value: Int( + 1, + ), + }, + ), + }, + ), + op: Pow, + right: UnaryOp( + ExprUnaryOp { + range: 107..109, + op: USub, + operand: NumberLiteral( + ExprNumberLiteral { + range: 108..109, + value: Int( + 2, + ), + }, + ), + }, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 110..117, + value: UnaryOp( + ExprUnaryOp { + range: 110..117, + op: Invert, + operand: Tuple( + ExprTuple { + range: 111..117, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 112..113, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 115..116, + value: Int( + 2, + ), + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 118..124, + value: BinOp( + ExprBinOp { + range: 118..124, + left: UnaryOp( + ExprUnaryOp { + range: 118..120, + op: USub, + operand: NumberLiteral( + ExprNumberLiteral { + range: 119..120, + value: Int( + 1, + ), + }, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 123..124, + value: Int( + 2, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 212..246, + value: BoolOp( + ExprBoolOp { + range: 212..246, + op: Or, + values: [ + BoolOp( + ExprBoolOp { + range: 212..223, + op: And, + values: [ + UnaryOp( + ExprUnaryOp { + range: 212..217, + op: Not, + operand: Name( + ExprName { + range: 216..217, + id: "a", + ctx: Load, + }, + ), + }, + ), + Name( + ExprName { + range: 222..223, + id: "b", + ctx: Load, + }, + ), + ], + }, + ), + BoolOp( + ExprBoolOp { + range: 227..246, + op: And, + values: [ + UnaryOp( + ExprUnaryOp { + range: 227..236, + op: Not, + operand: BinOp( + ExprBinOp { + range: 231..236, + left: Name( + ExprName { + range: 231..232, + id: "c", + ctx: Load, + }, + ), + op: BitOr, + right: Name( + ExprName { + range: 235..236, + id: "d", + ctx: Load, + }, + ), + }, + ), + }, + ), + UnaryOp( + ExprUnaryOp { + range: 241..246, + op: Not, + operand: Name( + ExprName { + range: 245..246, + id: "e", + ctx: Load, + }, + ), + }, + ), + ], + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 247..259, + value: UnaryOp( + ExprUnaryOp { + range: 247..259, + op: Not, + operand: Named( + ExprNamed { + range: 252..258, + target: Name( + ExprName { + range: 252..253, + id: "x", + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 257..258, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 260..275, + value: UnaryOp( + ExprUnaryOp { + range: 260..275, + op: Not, + operand: BinOp( + ExprBinOp { + range: 264..275, + left: Name( + ExprName { + range: 264..265, + id: "a", + ctx: Load, + }, + ), + op: BitOr, + right: UnaryOp( + ExprUnaryOp { + range: 269..274, + op: Not, + operand: Name( + ExprName { + range: 273..274, + id: "b", + ctx: Load, + }, + ), + }, + ), + }, + ), + }, + ), + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__yield.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__yield.py.snap.new new file mode 100644 index 0000000000..2337519f10 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__yield.py.snap.new @@ -0,0 +1,527 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/valid/expressions/yield.py +--- +## AST + +``` +Module( + ModModule { + range: 0..188, + body: [ + Expr( + StmtExpr { + range: 0..5, + value: Yield( + ExprYield { + range: 0..5, + value: None, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 6..13, + value: Yield( + ExprYield { + range: 6..13, + value: Some( + Name( + ExprName { + range: 12..13, + id: "x", + ctx: Load, + }, + ), + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 14..25, + value: Yield( + ExprYield { + range: 14..25, + value: Some( + BinOp( + ExprBinOp { + range: 20..25, + left: Name( + ExprName { + range: 20..21, + id: "x", + ctx: Load, + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 24..25, + value: Int( + 1, + ), + }, + ), + }, + ), + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 26..39, + value: Yield( + ExprYield { + range: 26..39, + value: Some( + BoolOp( + ExprBoolOp { + range: 32..39, + op: And, + values: [ + Name( + ExprName { + range: 32..33, + id: "x", + ctx: Load, + }, + ), + Name( + ExprName { + range: 38..39, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 40..52, + value: Yield( + ExprYield { + range: 40..52, + value: Some( + Call( + ExprCall { + range: 46..52, + func: Name( + ExprName { + range: 46..50, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 50..52, + args: [], + keywords: [], + }, + }, + ), + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 53..65, + value: Yield( + ExprYield { + range: 53..65, + value: Some( + List( + ExprList { + range: 59..65, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 60..61, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 63..64, + value: Int( + 2, + ), + }, + ), + ], + ctx: Load, + }, + ), + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 66..78, + value: Yield( + ExprYield { + range: 66..78, + value: Some( + Set( + ExprSet { + range: 72..78, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 73..74, + value: Int( + 3, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 76..77, + value: Int( + 4, + ), + }, + ), + ], + }, + ), + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 79..91, + value: Yield( + ExprYield { + range: 79..91, + value: Some( + Dict( + ExprDict { + range: 85..91, + items: [ + DictItem { + key: Some( + Name( + ExprName { + range: 86..87, + id: "x", + ctx: Load, + }, + ), + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 89..90, + value: Int( + 5, + ), + }, + ), + }, + ], + }, + ), + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 92..102, + value: Yield( + ExprYield { + range: 92..102, + value: Some( + Tuple( + ExprTuple { + range: 98..102, + elts: [ + Name( + ExprName { + range: 98..99, + id: "x", + ctx: Load, + }, + ), + Name( + ExprName { + range: 101..102, + id: "y", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 103..115, + value: Yield( + ExprYield { + range: 103..115, + value: Some( + Tuple( + ExprTuple { + range: 109..115, + elts: [ + Name( + ExprName { + range: 110..111, + id: "x", + ctx: Load, + }, + ), + Name( + ExprName { + range: 113..114, + id: "y", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 116..128, + value: Yield( + ExprYield { + range: 116..128, + value: Some( + Compare( + ExprCompare { + range: 122..128, + left: Name( + ExprName { + range: 122..123, + id: "x", + ctx: Load, + }, + ), + ops: [ + Eq, + ], + comparators: [ + Name( + ExprName { + range: 127..128, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 129..143, + value: Yield( + ExprYield { + range: 129..143, + value: Some( + Named( + ExprNamed { + range: 136..142, + target: Name( + ExprName { + range: 136..137, + id: "x", + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 141..142, + value: Int( + 1, + ), + }, + ), + }, + ), + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 144..152, + value: Yield( + ExprYield { + range: 144..152, + value: Some( + Starred( + ExprStarred { + range: 150..152, + value: Name( + ExprName { + range: 151..152, + id: "y", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 153..164, + value: Yield( + ExprYield { + range: 153..164, + value: Some( + Tuple( + ExprTuple { + range: 159..164, + elts: [ + Name( + ExprName { + range: 159..160, + id: "x", + ctx: Load, + }, + ), + Starred( + ExprStarred { + range: 162..164, + value: Name( + ExprName { + range: 163..164, + id: "y", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 165..174, + value: Yield( + ExprYield { + range: 165..174, + value: Some( + Tuple( + ExprTuple { + range: 171..174, + elts: [ + Starred( + ExprStarred { + range: 171..173, + value: Name( + ExprName { + range: 172..173, + id: "x", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 175..187, + value: Yield( + ExprYield { + range: 175..187, + value: Some( + Starred( + ExprStarred { + range: 181..187, + value: BinOp( + ExprBinOp { + range: 182..187, + left: Name( + ExprName { + range: 182..183, + id: "x", + ctx: Load, + }, + ), + op: BitOr, + right: Name( + ExprName { + range: 186..187, + id: "y", + ctx: Load, + }, + ), + }, + ), + ctx: Load, + }, + ), + ), + }, + ), + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__yield_from.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__yield_from.py.snap.new new file mode 100644 index 0000000000..2d62cd9ff0 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@expressions__yield_from.py.snap.new @@ -0,0 +1,375 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/valid/expressions/yield_from.py +--- +## AST + +``` +Module( + ModModule { + range: 0..199, + body: [ + Expr( + StmtExpr { + range: 0..12, + value: YieldFrom( + ExprYieldFrom { + range: 0..12, + value: Name( + ExprName { + range: 11..12, + id: "x", + ctx: Load, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 13..29, + value: YieldFrom( + ExprYieldFrom { + range: 13..29, + value: BinOp( + ExprBinOp { + range: 24..29, + left: Name( + ExprName { + range: 24..25, + id: "x", + ctx: Load, + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 28..29, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 30..48, + value: YieldFrom( + ExprYieldFrom { + range: 30..48, + value: BoolOp( + ExprBoolOp { + range: 41..48, + op: And, + values: [ + Name( + ExprName { + range: 41..42, + id: "x", + ctx: Load, + }, + ), + Name( + ExprName { + range: 47..48, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 49..66, + value: YieldFrom( + ExprYieldFrom { + range: 49..66, + value: Call( + ExprCall { + range: 60..66, + func: Name( + ExprName { + range: 60..64, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 64..66, + args: [], + keywords: [], + }, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 67..84, + value: YieldFrom( + ExprYieldFrom { + range: 67..84, + value: List( + ExprList { + range: 78..84, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 79..80, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 82..83, + value: Int( + 2, + ), + }, + ), + ], + ctx: Load, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 85..102, + value: YieldFrom( + ExprYieldFrom { + range: 85..102, + value: Set( + ExprSet { + range: 96..102, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 97..98, + value: Int( + 3, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 100..101, + value: Int( + 4, + ), + }, + ), + ], + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 103..120, + value: YieldFrom( + ExprYieldFrom { + range: 103..120, + value: Dict( + ExprDict { + range: 114..120, + items: [ + DictItem { + key: Some( + Name( + ExprName { + range: 115..116, + id: "x", + ctx: Load, + }, + ), + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 118..119, + value: Int( + 5, + ), + }, + ), + }, + ], + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 121..138, + value: YieldFrom( + ExprYieldFrom { + range: 121..138, + value: Tuple( + ExprTuple { + range: 132..138, + elts: [ + Name( + ExprName { + range: 133..134, + id: "x", + ctx: Load, + }, + ), + Name( + ExprName { + range: 136..137, + id: "y", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 139..156, + value: YieldFrom( + ExprYieldFrom { + range: 139..156, + value: Compare( + ExprCompare { + range: 150..156, + left: Name( + ExprName { + range: 150..151, + id: "x", + ctx: Load, + }, + ), + ops: [ + Eq, + ], + comparators: [ + Name( + ExprName { + range: 155..156, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 157..176, + value: YieldFrom( + ExprYieldFrom { + range: 157..176, + value: Named( + ExprNamed { + range: 169..175, + target: Name( + ExprName { + range: 169..170, + id: "x", + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 174..175, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 177..199, + value: YieldFrom( + ExprYieldFrom { + range: 177..199, + value: Tuple( + ExprTuple { + range: 188..199, + elts: [ + Name( + ExprName { + range: 189..190, + id: "x", + ctx: Load, + }, + ), + Starred( + ExprStarred { + range: 192..198, + value: BinOp( + ExprBinOp { + range: 193..198, + left: Name( + ExprName { + range: 193..194, + id: "x", + ctx: Load, + }, + ), + op: BitOr, + right: Name( + ExprName { + range: 197..198, + id: "y", + ctx: Load, + }, + ), + }, + ), + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@for_in_target_valid_expr.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@for_in_target_valid_expr.py.snap.new new file mode 100644 index 0000000000..93a247d626 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@for_in_target_valid_expr.py.snap.new @@ -0,0 +1,203 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/inline/ok/for_in_target_valid_expr.py +--- +## AST + +``` +Module( + ModModule { + range: 0..89, + body: [ + For( + StmtFor { + range: 0..28, + is_async: false, + target: Subscript( + ExprSubscript { + range: 4..13, + value: Name( + ExprName { + range: 4..5, + id: "d", + ctx: Load, + }, + ), + slice: Compare( + ExprCompare { + range: 6..12, + left: Name( + ExprName { + range: 6..7, + id: "x", + ctx: Load, + }, + ), + ops: [ + In, + ], + comparators: [ + Name( + ExprName { + range: 11..12, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 17..23, + id: "target", + ctx: Load, + }, + ), + body: [ + Expr( + StmtExpr { + range: 25..28, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 25..28, + }, + ), + }, + ), + ], + orelse: [], + }, + ), + For( + StmtFor { + range: 29..57, + is_async: false, + target: Subscript( + ExprSubscript { + range: 33..44, + value: Compare( + ExprCompare { + range: 34..40, + left: Name( + ExprName { + range: 34..35, + id: "x", + ctx: Load, + }, + ), + ops: [ + In, + ], + comparators: [ + Name( + ExprName { + range: 39..40, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + slice: NumberLiteral( + ExprNumberLiteral { + range: 42..43, + value: Int( + 0, + ), + }, + ), + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 48..52, + id: "iter", + ctx: Load, + }, + ), + body: [ + Expr( + StmtExpr { + range: 54..57, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 54..57, + }, + ), + }, + ), + ], + orelse: [], + }, + ), + For( + StmtFor { + range: 58..88, + is_async: false, + target: Attribute( + ExprAttribute { + range: 62..75, + value: Compare( + ExprCompare { + range: 63..69, + left: Name( + ExprName { + range: 63..64, + id: "x", + ctx: Load, + }, + ), + ops: [ + In, + ], + comparators: [ + Name( + ExprName { + range: 68..69, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + attr: Identifier { + id: "attr", + range: 71..75, + }, + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 79..83, + id: "iter", + ctx: Load, + }, + ), + body: [ + Expr( + StmtExpr { + range: 85..88, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 85..88, + }, + ), + }, + ), + ], + orelse: [], + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@from_import_no_space.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@from_import_no_space.py.snap.new new file mode 100644 index 0000000000..18bf1eae9b --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@from_import_no_space.py.snap.new @@ -0,0 +1,50 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/inline/ok/from_import_no_space.py +--- +## AST + +``` +Module( + ModModule { + range: 0..30, + body: [ + ImportFrom( + StmtImportFrom { + range: 0..13, + module: None, + names: [ + Alias { + range: 12..13, + name: Identifier { + id: "x", + range: 12..13, + }, + asname: None, + }, + ], + level: 1, + }, + ), + ImportFrom( + StmtImportFrom { + range: 14..29, + module: None, + names: [ + Alias { + range: 28..29, + name: Identifier { + id: "x", + range: 28..29, + }, + asname: None, + }, + ], + level: 3, + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@from_import_soft_keyword_module_name.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@from_import_soft_keyword_module_name.py.snap.new new file mode 100644 index 0000000000..5200ccac1c --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@from_import_soft_keyword_module_name.py.snap.new @@ -0,0 +1,104 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/inline/ok/from_import_soft_keyword_module_name.py +--- +## AST + +``` +Module( + ModModule { + range: 0..104, + body: [ + ImportFrom( + StmtImportFrom { + range: 0..25, + module: Some( + Identifier { + id: "match", + range: 5..10, + }, + ), + names: [ + Alias { + range: 18..25, + name: Identifier { + id: "pattern", + range: 18..25, + }, + asname: None, + }, + ], + level: 0, + }, + ), + ImportFrom( + StmtImportFrom { + range: 26..46, + module: Some( + Identifier { + id: "type", + range: 31..35, + }, + ), + names: [ + Alias { + range: 43..46, + name: Identifier { + id: "bar", + range: 43..46, + }, + asname: None, + }, + ], + level: 0, + }, + ), + ImportFrom( + StmtImportFrom { + range: 47..71, + module: Some( + Identifier { + id: "case", + range: 52..56, + }, + ), + names: [ + Alias { + range: 64..71, + name: Identifier { + id: "pattern", + range: 64..71, + }, + asname: None, + }, + ], + level: 0, + }, + ), + ImportFrom( + StmtImportFrom { + range: 72..103, + module: Some( + Identifier { + id: "match.type.case", + range: 77..92, + }, + ), + names: [ + Alias { + range: 100..103, + name: Identifier { + id: "foo", + range: 100..103, + }, + asname: None, + }, + ], + level: 0, + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@from_import_stmt_terminator.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@from_import_stmt_terminator.py.snap.new new file mode 100644 index 0000000000..8466d4837b --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@from_import_stmt_terminator.py.snap.new @@ -0,0 +1,220 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/inline/ok/from_import_stmt_terminator.py +--- +## AST + +``` +Module( + ModModule { + range: 0..97, + body: [ + ImportFrom( + StmtImportFrom { + range: 0..20, + module: Some( + Identifier { + id: "a", + range: 5..6, + }, + ), + names: [ + Alias { + range: 15..16, + name: Identifier { + id: "b", + range: 15..16, + }, + asname: None, + }, + Alias { + range: 18..19, + name: Identifier { + id: "c", + range: 18..19, + }, + asname: None, + }, + ], + level: 0, + }, + ), + ImportFrom( + StmtImportFrom { + range: 21..41, + module: Some( + Identifier { + id: "a", + range: 26..27, + }, + ), + names: [ + Alias { + range: 36..37, + name: Identifier { + id: "b", + range: 36..37, + }, + asname: None, + }, + Alias { + range: 39..40, + name: Identifier { + id: "c", + range: 39..40, + }, + asname: None, + }, + ], + level: 0, + }, + ), + Expr( + StmtExpr { + range: 43..47, + value: Tuple( + ExprTuple { + range: 43..47, + elts: [ + Name( + ExprName { + range: 43..44, + id: "x", + ctx: Load, + }, + ), + Name( + ExprName { + range: 46..47, + id: "y", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + }, + ), + ImportFrom( + StmtImportFrom { + range: 48..66, + module: Some( + Identifier { + id: "a", + range: 53..54, + }, + ), + names: [ + Alias { + range: 62..63, + name: Identifier { + id: "b", + range: 62..63, + }, + asname: None, + }, + Alias { + range: 65..66, + name: Identifier { + id: "c", + range: 65..66, + }, + asname: None, + }, + ], + level: 0, + }, + ), + Expr( + StmtExpr { + range: 68..72, + value: Tuple( + ExprTuple { + range: 68..72, + elts: [ + Name( + ExprName { + range: 68..69, + id: "x", + ctx: Load, + }, + ), + Name( + ExprName { + range: 71..72, + id: "y", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + }, + ), + ImportFrom( + StmtImportFrom { + range: 73..91, + module: Some( + Identifier { + id: "a", + range: 78..79, + }, + ), + names: [ + Alias { + range: 87..88, + name: Identifier { + id: "b", + range: 87..88, + }, + asname: None, + }, + Alias { + range: 90..91, + name: Identifier { + id: "c", + range: 90..91, + }, + asname: None, + }, + ], + level: 0, + }, + ), + Expr( + StmtExpr { + range: 92..96, + value: Tuple( + ExprTuple { + range: 92..96, + elts: [ + Name( + ExprName { + range: 92..93, + id: "x", + ctx: Load, + }, + ), + Name( + ExprName { + range: 95..96, + id: "y", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@fstring_format_spec_terminator.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@fstring_format_spec_terminator.py.snap.new new file mode 100644 index 0000000000..51acdd78c2 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@fstring_format_spec_terminator.py.snap.new @@ -0,0 +1,139 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/inline/ok/fstring_format_spec_terminator.py +--- +## AST + +``` +Module( + ModModule { + range: 0..43, + body: [ + Expr( + StmtExpr { + range: 0..19, + value: FString( + ExprFString { + range: 0..19, + value: FStringValue { + inner: Single( + FString( + FString { + range: 0..19, + elements: [ + Literal( + FStringLiteralElement { + range: 2..8, + value: "hello ", + }, + ), + Expression( + FStringExpressionElement { + range: 8..12, + expression: Name( + ExprName { + range: 9..10, + id: "x", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: Some( + FStringFormatSpec { + range: 11..11, + elements: [], + }, + ), + }, + ), + Literal( + FStringLiteralElement { + range: 12..18, + value: " world", + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 20..42, + value: FString( + ExprFString { + range: 20..42, + value: FStringValue { + inner: Single( + FString( + FString { + range: 20..42, + elements: [ + Literal( + FStringLiteralElement { + range: 22..28, + value: "hello ", + }, + ), + Expression( + FStringExpressionElement { + range: 28..35, + expression: Name( + ExprName { + range: 29..30, + id: "x", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: Some( + FStringFormatSpec { + range: 31..34, + elements: [ + Literal( + FStringLiteralElement { + range: 31..34, + value: ".3f", + }, + ), + ], + }, + ), + }, + ), + Literal( + FStringLiteralElement { + range: 35..41, + value: " world", + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@function_def_parameter_range.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@function_def_parameter_range.py.snap.new new file mode 100644 index 0000000000..7593631eef --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@function_def_parameter_range.py.snap.new @@ -0,0 +1,98 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/inline/ok/function_def_parameter_range.py +--- +## AST + +``` +Module( + ModModule { + range: 0..56, + body: [ + FunctionDef( + StmtFunctionDef { + range: 0..55, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 4..7, + }, + type_params: None, + parameters: Parameters { + range: 7..43, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 13..23, + parameter: Parameter { + range: 13..23, + name: Identifier { + id: "first", + range: 13..18, + }, + annotation: Some( + Name( + ExprName { + range: 20..23, + id: "int", + ctx: Load, + }, + ), + ), + }, + default: None, + }, + ParameterWithDefault { + range: 29..40, + parameter: Parameter { + range: 29..40, + name: Identifier { + id: "second", + range: 29..35, + }, + annotation: Some( + Name( + ExprName { + range: 37..40, + id: "int", + ctx: Load, + }, + ), + ), + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: Some( + Name( + ExprName { + range: 47..50, + id: "int", + ctx: Load, + }, + ), + ), + body: [ + Expr( + StmtExpr { + range: 52..55, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 52..55, + }, + ), + }, + ), + ], + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@function_def_parenthesized_return_types.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@function_def_parenthesized_return_types.py.snap.new new file mode 100644 index 0000000000..6e93831eef --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@function_def_parenthesized_return_types.py.snap.new @@ -0,0 +1,123 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/inline/ok/function_def_parenthesized_return_types.py +--- +## AST + +``` +Module( + ModModule { + range: 0..54, + body: [ + FunctionDef( + StmtFunctionDef { + range: 0..24, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 4..7, + }, + type_params: None, + parameters: Parameters { + range: 7..9, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: Some( + Tuple( + ExprTuple { + range: 13..19, + elts: [ + Name( + ExprName { + range: 14..17, + id: "int", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + ), + body: [ + Expr( + StmtExpr { + range: 21..24, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 21..24, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 25..53, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 29..32, + }, + type_params: None, + parameters: Parameters { + range: 32..34, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: Some( + Tuple( + ExprTuple { + range: 38..48, + elts: [ + Name( + ExprName { + range: 39..42, + id: "int", + ctx: Load, + }, + ), + Name( + ExprName { + range: 44..47, + id: "str", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + ), + body: [ + Expr( + StmtExpr { + range: 50..53, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 50..53, + }, + ), + }, + ), + ], + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@function_def_valid_return_expr.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@function_def_valid_return_expr.py.snap.new new file mode 100644 index 0000000000..85f4269018 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@function_def_valid_return_expr.py.snap.new @@ -0,0 +1,246 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/inline/ok/function_def_valid_return_expr.py +--- +## AST + +``` +Module( + ModModule { + range: 0..125, + body: [ + FunctionDef( + StmtFunctionDef { + range: 0..27, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 4..7, + }, + type_params: None, + parameters: Parameters { + range: 7..9, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: Some( + BinOp( + ExprBinOp { + range: 13..22, + left: Name( + ExprName { + range: 13..16, + id: "int", + ctx: Load, + }, + ), + op: BitOr, + right: Name( + ExprName { + range: 19..22, + id: "str", + ctx: Load, + }, + ), + }, + ), + ), + body: [ + Expr( + StmtExpr { + range: 24..27, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 24..27, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 28..57, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 32..35, + }, + type_params: None, + parameters: Parameters { + range: 35..37, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: Some( + Lambda( + ExprLambda { + range: 41..52, + parameters: Some( + Parameters { + range: 48..49, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 48..49, + parameter: Parameter { + range: 48..49, + name: Identifier { + id: "x", + range: 48..49, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: Name( + ExprName { + range: 51..52, + id: "x", + ctx: Load, + }, + ), + }, + ), + ), + body: [ + Expr( + StmtExpr { + range: 54..57, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 54..57, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 58..85, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 62..65, + }, + type_params: None, + parameters: Parameters { + range: 65..67, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: Some( + Yield( + ExprYield { + range: 72..79, + value: Some( + Name( + ExprName { + range: 78..79, + id: "x", + ctx: Load, + }, + ), + ), + }, + ), + ), + body: [ + Expr( + StmtExpr { + range: 82..85, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 82..85, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 86..124, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 90..93, + }, + type_params: None, + parameters: Parameters { + range: 93..95, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: Some( + If( + ExprIf { + range: 99..119, + test: BooleanLiteral( + ExprBooleanLiteral { + range: 106..110, + value: true, + }, + ), + body: Name( + ExprName { + range: 99..102, + id: "int", + ctx: Load, + }, + ), + orelse: Name( + ExprName { + range: 116..119, + id: "str", + ctx: Load, + }, + ), + }, + ), + ), + body: [ + Expr( + StmtExpr { + range: 121..124, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 121..124, + }, + ), + }, + ), + ], + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@global_stmt.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@global_stmt.py.snap.new new file mode 100644 index 0000000000..99892c69e6 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@global_stmt.py.snap.new @@ -0,0 +1,46 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/inline/ok/global_stmt.py +--- +## AST + +``` +Module( + ModModule { + range: 0..24, + body: [ + Global( + StmtGlobal { + range: 0..8, + names: [ + Identifier { + id: "x", + range: 7..8, + }, + ], + }, + ), + Global( + StmtGlobal { + range: 9..23, + names: [ + Identifier { + id: "x", + range: 16..17, + }, + Identifier { + id: "y", + range: 19..20, + }, + Identifier { + id: "z", + range: 22..23, + }, + ], + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@import_as_name_soft_keyword.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@import_as_name_soft_keyword.py.snap.new new file mode 100644 index 0000000000..692f881262 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@import_as_name_soft_keyword.py.snap.new @@ -0,0 +1,76 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/inline/ok/import_as_name_soft_keyword.py +--- +## AST + +``` +Module( + ModModule { + range: 0..58, + body: [ + Import( + StmtImport { + range: 0..19, + names: [ + Alias { + range: 7..19, + name: Identifier { + id: "foo", + range: 7..10, + }, + asname: Some( + Identifier { + id: "match", + range: 14..19, + }, + ), + }, + ], + }, + ), + Import( + StmtImport { + range: 20..38, + names: [ + Alias { + range: 27..38, + name: Identifier { + id: "bar", + range: 27..30, + }, + asname: Some( + Identifier { + id: "case", + range: 34..38, + }, + ), + }, + ], + }, + ), + Import( + StmtImport { + range: 39..57, + names: [ + Alias { + range: 46..57, + name: Identifier { + id: "baz", + range: 46..49, + }, + asname: Some( + Identifier { + id: "type", + range: 53..57, + }, + ), + }, + ], + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@import_stmt_terminator.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@import_stmt_terminator.py.snap.new new file mode 100644 index 0000000000..c773d9340a --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@import_stmt_terminator.py.snap.new @@ -0,0 +1,113 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/inline/ok/import_stmt_terminator.py +--- +## AST + +``` +Module( + ModModule { + range: 0..42, + body: [ + Import( + StmtImport { + range: 0..11, + names: [ + Alias { + range: 7..8, + name: Identifier { + id: "a", + range: 7..8, + }, + asname: None, + }, + Alias { + range: 10..11, + name: Identifier { + id: "b", + range: 10..11, + }, + asname: None, + }, + ], + }, + ), + Import( + StmtImport { + range: 13..24, + names: [ + Alias { + range: 20..21, + name: Identifier { + id: "c", + range: 20..21, + }, + asname: None, + }, + Alias { + range: 23..24, + name: Identifier { + id: "d", + range: 23..24, + }, + asname: None, + }, + ], + }, + ), + Import( + StmtImport { + range: 25..36, + names: [ + Alias { + range: 32..33, + name: Identifier { + id: "a", + range: 32..33, + }, + asname: None, + }, + Alias { + range: 35..36, + name: Identifier { + id: "b", + range: 35..36, + }, + asname: None, + }, + ], + }, + ), + Expr( + StmtExpr { + range: 37..41, + value: Tuple( + ExprTuple { + range: 37..41, + elts: [ + Name( + ExprName { + range: 37..38, + id: "c", + ctx: Load, + }, + ), + Name( + ExprName { + range: 40..41, + id: "d", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@lambda_with_valid_body.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@lambda_with_valid_body.py.snap.new new file mode 100644 index 0000000000..373b7f1e0c --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@lambda_with_valid_body.py.snap.new @@ -0,0 +1,349 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/inline/ok/lambda_with_valid_body.py +--- +## AST + +``` +Module( + ModModule { + range: 0..152, + body: [ + Expr( + StmtExpr { + range: 0..11, + value: Lambda( + ExprLambda { + range: 0..11, + parameters: Some( + Parameters { + range: 7..8, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 7..8, + parameter: Parameter { + range: 7..8, + name: Identifier { + id: "x", + range: 7..8, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: Name( + ExprName { + range: 10..11, + id: "x", + ctx: Load, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 12..38, + value: Lambda( + ExprLambda { + range: 12..38, + parameters: Some( + Parameters { + range: 19..20, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 19..20, + parameter: Parameter { + range: 19..20, + name: Identifier { + id: "x", + range: 19..20, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: If( + ExprIf { + range: 22..38, + test: BooleanLiteral( + ExprBooleanLiteral { + range: 27..31, + value: true, + }, + ), + body: Name( + ExprName { + range: 22..23, + id: "x", + ctx: Load, + }, + ), + orelse: Name( + ExprName { + range: 37..38, + id: "y", + ctx: Load, + }, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 39..56, + value: Lambda( + ExprLambda { + range: 39..56, + parameters: Some( + Parameters { + range: 46..47, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 46..47, + parameter: Parameter { + range: 46..47, + name: Identifier { + id: "x", + range: 46..47, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: Await( + ExprAwait { + range: 49..56, + value: Name( + ExprName { + range: 55..56, + id: "x", + ctx: Load, + }, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 57..82, + value: Lambda( + ExprLambda { + range: 57..82, + parameters: Some( + Parameters { + range: 64..65, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 64..65, + parameter: Parameter { + range: 64..65, + name: Identifier { + id: "x", + range: 64..65, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: Lambda( + ExprLambda { + range: 67..82, + parameters: Some( + Parameters { + range: 74..75, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 74..75, + parameter: Parameter { + range: 74..75, + name: Identifier { + id: "y", + range: 74..75, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: BinOp( + ExprBinOp { + range: 77..82, + left: Name( + ExprName { + range: 77..78, + id: "x", + ctx: Load, + }, + ), + op: Add, + right: Name( + ExprName { + range: 81..82, + id: "y", + ctx: Load, + }, + ), + }, + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 83..102, + value: Lambda( + ExprLambda { + range: 83..102, + parameters: Some( + Parameters { + range: 90..91, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 90..91, + parameter: Parameter { + range: 90..91, + name: Identifier { + id: "x", + range: 90..91, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: Yield( + ExprYield { + range: 94..101, + value: Some( + Name( + ExprName { + range: 100..101, + id: "x", + ctx: Load, + }, + ), + ), + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 136..151, + value: Tuple( + ExprTuple { + range: 136..151, + elts: [ + Lambda( + ExprLambda { + range: 136..147, + parameters: Some( + Parameters { + range: 143..144, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 143..144, + parameter: Parameter { + range: 143..144, + name: Identifier { + id: "x", + range: 143..144, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: Name( + ExprName { + range: 146..147, + id: "x", + ctx: Load, + }, + ), + }, + ), + Starred( + ExprStarred { + range: 149..151, + value: Name( + ExprName { + range: 150..151, + id: "y", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@match_as_pattern.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@match_as_pattern.py.snap.new new file mode 100644 index 0000000000..9b8e4496b6 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@match_as_pattern.py.snap.new @@ -0,0 +1,81 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/inline/ok/match_as_pattern.py +--- +## AST + +``` +Module( + ModModule { + range: 0..49, + body: [ + Match( + StmtMatch { + range: 0..48, + subject: Name( + ExprName { + range: 6..9, + id: "foo", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 15..32, + pattern: MatchAs( + PatternMatchAs { + range: 20..27, + pattern: None, + name: Some( + Identifier { + id: "foo_bar", + range: 20..27, + }, + ), + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 29..32, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 29..32, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 37..48, + pattern: MatchAs( + PatternMatchAs { + range: 42..43, + pattern: None, + name: None, + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 45..48, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 45..48, + }, + ), + }, + ), + ], + }, + ], + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@match_as_pattern_soft_keyword.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@match_as_pattern_soft_keyword.py.snap.new new file mode 100644 index 0000000000..1fb9f6ec9d --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@match_as_pattern_soft_keyword.py.snap.new @@ -0,0 +1,114 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/inline/ok/match_as_pattern_soft_keyword.py +--- +## AST + +``` +Module( + ModModule { + range: 0..69, + body: [ + Match( + StmtMatch { + range: 0..68, + subject: Name( + ExprName { + range: 6..9, + id: "foo", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 15..29, + pattern: MatchAs( + PatternMatchAs { + range: 20..24, + pattern: None, + name: Some( + Identifier { + id: "case", + range: 20..24, + }, + ), + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 26..29, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 26..29, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 34..49, + pattern: MatchAs( + PatternMatchAs { + range: 39..44, + pattern: None, + name: Some( + Identifier { + id: "match", + range: 39..44, + }, + ), + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 46..49, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 46..49, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 54..68, + pattern: MatchAs( + PatternMatchAs { + range: 59..63, + pattern: None, + name: Some( + Identifier { + id: "type", + range: 59..63, + }, + ), + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 65..68, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 65..68, + }, + ), + }, + ), + ], + }, + ], + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@match_attr_pattern_soft_keyword.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@match_attr_pattern_soft_keyword.py.snap.new new file mode 100644 index 0000000000..d89bfac720 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@match_attr_pattern_soft_keyword.py.snap.new @@ -0,0 +1,232 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/inline/ok/match_attr_pattern_soft_keyword.py +--- +## AST + +``` +Module( + ModModule { + range: 0..131, + body: [ + Match( + StmtMatch { + range: 0..130, + subject: Name( + ExprName { + range: 6..9, + id: "foo", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 15..34, + pattern: MatchValue( + PatternMatchValue { + range: 20..29, + value: Attribute( + ExprAttribute { + range: 20..29, + value: Name( + ExprName { + range: 20..25, + id: "match", + ctx: Load, + }, + ), + attr: Identifier { + id: "bar", + range: 26..29, + }, + ctx: Load, + }, + ), + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 31..34, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 31..34, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 39..57, + pattern: MatchValue( + PatternMatchValue { + range: 44..52, + value: Attribute( + ExprAttribute { + range: 44..52, + value: Name( + ExprName { + range: 44..48, + id: "case", + ctx: Load, + }, + ), + attr: Identifier { + id: "bar", + range: 49..52, + }, + ctx: Load, + }, + ), + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 54..57, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 54..57, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 62..80, + pattern: MatchValue( + PatternMatchValue { + range: 67..75, + value: Attribute( + ExprAttribute { + range: 67..75, + value: Name( + ExprName { + range: 67..71, + id: "type", + ctx: Load, + }, + ), + attr: Identifier { + id: "bar", + range: 72..75, + }, + ctx: Load, + }, + ), + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 77..80, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 77..80, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 85..130, + pattern: MatchValue( + PatternMatchValue { + range: 90..125, + value: Attribute( + ExprAttribute { + range: 90..125, + value: Attribute( + ExprAttribute { + range: 90..119, + value: Attribute( + ExprAttribute { + range: 90..114, + value: Attribute( + ExprAttribute { + range: 90..109, + value: Attribute( + ExprAttribute { + range: 90..105, + value: Attribute( + ExprAttribute { + range: 90..100, + value: Name( + ExprName { + range: 90..95, + id: "match", + ctx: Load, + }, + ), + attr: Identifier { + id: "case", + range: 96..100, + }, + ctx: Load, + }, + ), + attr: Identifier { + id: "type", + range: 101..105, + }, + ctx: Load, + }, + ), + attr: Identifier { + id: "bar", + range: 106..109, + }, + ctx: Load, + }, + ), + attr: Identifier { + id: "type", + range: 110..114, + }, + ctx: Load, + }, + ), + attr: Identifier { + id: "case", + range: 115..119, + }, + ctx: Load, + }, + ), + attr: Identifier { + id: "match", + range: 120..125, + }, + ctx: Load, + }, + ), + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 127..130, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 127..130, + }, + ), + }, + ), + ], + }, + ], + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@match_classify_as_identifier_1.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@match_classify_as_identifier_1.py.snap.new new file mode 100644 index 0000000000..e8d80959f1 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@match_classify_as_identifier_1.py.snap.new @@ -0,0 +1,45 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/inline/ok/match_classify_as_identifier_1.py +--- +## AST + +``` +Module( + ModModule { + range: 0..18, + body: [ + Expr( + StmtExpr { + range: 0..17, + value: Compare( + ExprCompare { + range: 0..17, + left: Name( + ExprName { + range: 0..5, + id: "match", + ctx: Load, + }, + ), + ops: [ + NotIn, + ], + comparators: [ + Name( + ExprName { + range: 13..17, + id: "case", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@match_classify_as_identifier_2.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@match_classify_as_identifier_2.py.snap.new new file mode 100644 index 0000000000..6931cb7b3c --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@match_classify_as_identifier_2.py.snap.new @@ -0,0 +1,320 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/inline/ok/match_classify_as_identifier_2.py +--- +## AST + +``` +Module( + ModModule { + range: 0..149, + body: [ + Expr( + StmtExpr { + range: 0..5, + value: Name( + ExprName { + range: 0..5, + id: "match", + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 6..18, + value: Compare( + ExprCompare { + range: 6..18, + left: Name( + ExprName { + range: 6..11, + id: "match", + ctx: Load, + }, + ), + ops: [ + NotEq, + ], + comparators: [ + Name( + ExprName { + range: 15..18, + id: "foo", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 19..31, + value: Tuple( + ExprTuple { + range: 19..31, + elts: [ + Name( + ExprName { + range: 20..23, + id: "foo", + ctx: Load, + }, + ), + Name( + ExprName { + range: 25..30, + id: "match", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 32..44, + value: List( + ExprList { + range: 32..44, + elts: [ + Name( + ExprName { + range: 33..36, + id: "foo", + ctx: Load, + }, + ), + Name( + ExprName { + range: 38..43, + id: "match", + ctx: Load, + }, + ), + ], + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 45..57, + value: Set( + ExprSet { + range: 45..57, + elts: [ + Name( + ExprName { + range: 46..49, + id: "foo", + ctx: Load, + }, + ), + Name( + ExprName { + range: 51..56, + id: "match", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 58..63, + value: Name( + ExprName { + range: 58..63, + id: "match", + ctx: Load, + }, + ), + }, + ), + AnnAssign( + StmtAnnAssign { + range: 65..75, + target: Name( + ExprName { + range: 65..70, + id: "match", + ctx: Store, + }, + ), + annotation: Name( + ExprName { + range: 72..75, + id: "int", + ctx: Load, + }, + ), + value: None, + simple: true, + }, + ), + Expr( + StmtExpr { + range: 76..82, + value: Tuple( + ExprTuple { + range: 76..82, + elts: [ + Name( + ExprName { + range: 76..81, + id: "match", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 83..92, + value: Attribute( + ExprAttribute { + range: 83..92, + value: Name( + ExprName { + range: 83..88, + id: "match", + ctx: Load, + }, + ), + attr: Identifier { + id: "foo", + range: 89..92, + }, + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 93..104, + value: BinOp( + ExprBinOp { + range: 93..104, + left: Name( + ExprName { + range: 93..98, + id: "match", + ctx: Load, + }, + ), + op: Div, + right: Name( + ExprName { + range: 101..104, + id: "foo", + ctx: Load, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 105..117, + value: BinOp( + ExprBinOp { + range: 105..117, + left: Name( + ExprName { + range: 105..110, + id: "match", + ctx: Load, + }, + ), + op: LShift, + right: Name( + ExprName { + range: 114..117, + id: "foo", + ctx: Load, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 118..131, + value: BoolOp( + ExprBoolOp { + range: 118..131, + op: And, + values: [ + Name( + ExprName { + range: 118..123, + id: "match", + ctx: Load, + }, + ), + Name( + ExprName { + range: 128..131, + id: "foo", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + Expr( + StmtExpr { + range: 132..148, + value: Compare( + ExprCompare { + range: 132..148, + left: Name( + ExprName { + range: 132..137, + id: "match", + ctx: Load, + }, + ), + ops: [ + IsNot, + ], + comparators: [ + Name( + ExprName { + range: 145..148, + id: "foo", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@match_classify_as_keyword_1.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@match_classify_as_keyword_1.py.snap.new new file mode 100644 index 0000000000..c617621a23 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@match_classify_as_keyword_1.py.snap.new @@ -0,0 +1,579 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/inline/ok/match_classify_as_keyword_1.py +--- +## AST + +``` +Module( + ModModule { + range: 0..358, + body: [ + Match( + StmtMatch { + range: 0..26, + subject: Name( + ExprName { + range: 6..9, + id: "foo", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 15..26, + pattern: MatchAs( + PatternMatchAs { + range: 20..21, + pattern: None, + name: None, + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 23..26, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 23..26, + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 27..51, + subject: NumberLiteral( + ExprNumberLiteral { + range: 33..34, + value: Int( + 1, + ), + }, + ), + cases: [ + MatchCase { + range: 40..51, + pattern: MatchAs( + PatternMatchAs { + range: 45..46, + pattern: None, + name: None, + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 48..51, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 48..51, + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 52..78, + subject: NumberLiteral( + ExprNumberLiteral { + range: 58..61, + value: Float( + 1.0, + ), + }, + ), + cases: [ + MatchCase { + range: 67..78, + pattern: MatchAs( + PatternMatchAs { + range: 72..73, + pattern: None, + name: None, + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 75..78, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 75..78, + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 79..104, + subject: NumberLiteral( + ExprNumberLiteral { + range: 85..87, + value: Complex { + real: 0.0, + imag: 1.0, + }, + }, + ), + cases: [ + MatchCase { + range: 93..104, + pattern: MatchAs( + PatternMatchAs { + range: 98..99, + pattern: None, + name: None, + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 101..104, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 101..104, + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 105..133, + subject: StringLiteral( + ExprStringLiteral { + range: 111..116, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 111..116, + value: "foo", + flags: StringLiteralFlags { + quote_style: Double, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + cases: [ + MatchCase { + range: 122..133, + pattern: MatchAs( + PatternMatchAs { + range: 127..128, + pattern: None, + name: None, + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 130..133, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 130..133, + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 134..167, + subject: FString( + ExprFString { + range: 140..150, + value: FStringValue { + inner: Single( + FString( + FString { + range: 140..150, + elements: [ + Literal( + FStringLiteralElement { + range: 142..146, + value: "foo ", + }, + ), + Expression( + FStringExpressionElement { + range: 146..149, + expression: Name( + ExprName { + range: 147..148, + id: "x", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + cases: [ + MatchCase { + range: 156..167, + pattern: MatchAs( + PatternMatchAs { + range: 161..162, + pattern: None, + name: None, + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 164..167, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 164..167, + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 168..197, + subject: Set( + ExprSet { + range: 174..180, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 175..176, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 178..179, + value: Int( + 2, + ), + }, + ), + ], + }, + ), + cases: [ + MatchCase { + range: 186..197, + pattern: MatchAs( + PatternMatchAs { + range: 191..192, + pattern: None, + name: None, + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 194..197, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 194..197, + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 198..225, + subject: UnaryOp( + ExprUnaryOp { + range: 204..208, + op: Invert, + operand: Name( + ExprName { + range: 205..208, + id: "foo", + ctx: Load, + }, + ), + }, + ), + cases: [ + MatchCase { + range: 214..225, + pattern: MatchAs( + PatternMatchAs { + range: 219..220, + pattern: None, + name: None, + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 222..225, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 222..225, + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 226..252, + subject: EllipsisLiteral( + ExprEllipsisLiteral { + range: 232..235, + }, + ), + cases: [ + MatchCase { + range: 241..252, + pattern: MatchAs( + PatternMatchAs { + range: 246..247, + pattern: None, + name: None, + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 249..252, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 249..252, + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 253..283, + subject: UnaryOp( + ExprUnaryOp { + range: 259..266, + op: Not, + operand: Name( + ExprName { + range: 263..266, + id: "foo", + ctx: Load, + }, + ), + }, + ), + cases: [ + MatchCase { + range: 272..283, + pattern: MatchAs( + PatternMatchAs { + range: 277..278, + pattern: None, + name: None, + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 280..283, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 280..283, + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 284..318, + subject: Await( + ExprAwait { + range: 290..301, + value: Call( + ExprCall { + range: 296..301, + func: Name( + ExprName { + range: 296..299, + id: "foo", + ctx: Load, + }, + ), + arguments: Arguments { + range: 299..301, + args: [], + keywords: [], + }, + }, + ), + }, + ), + cases: [ + MatchCase { + range: 307..318, + pattern: MatchAs( + PatternMatchAs { + range: 312..313, + pattern: None, + name: None, + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 315..318, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 315..318, + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 319..357, + subject: Lambda( + ExprLambda { + range: 325..340, + parameters: Some( + Parameters { + range: 332..335, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 332..335, + parameter: Parameter { + range: 332..335, + name: Identifier { + id: "foo", + range: 332..335, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: Name( + ExprName { + range: 337..340, + id: "foo", + ctx: Load, + }, + ), + }, + ), + cases: [ + MatchCase { + range: 346..357, + pattern: MatchAs( + PatternMatchAs { + range: 351..352, + pattern: None, + name: None, + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 354..357, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 354..357, + }, + ), + }, + ), + ], + }, + ], + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@match_classify_as_keyword_2.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@match_classify_as_keyword_2.py.snap.new new file mode 100644 index 0000000000..77e047d7ab --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@match_classify_as_keyword_2.py.snap.new @@ -0,0 +1,234 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/inline/ok/match_classify_as_keyword_2.py +--- +## AST + +``` +Module( + ModModule { + range: 0..170, + body: [ + Match( + StmtMatch { + range: 0..28, + subject: Name( + ExprName { + range: 6..11, + id: "match", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 17..28, + pattern: MatchAs( + PatternMatchAs { + range: 22..23, + pattern: None, + name: None, + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 25..28, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 25..28, + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 29..56, + subject: Name( + ExprName { + range: 35..39, + id: "case", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 45..56, + pattern: MatchAs( + PatternMatchAs { + range: 50..51, + pattern: None, + name: None, + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 53..56, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 53..56, + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 57..84, + subject: Name( + ExprName { + range: 63..67, + id: "type", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 73..84, + pattern: MatchAs( + PatternMatchAs { + range: 78..79, + pattern: None, + name: None, + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 81..84, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 81..84, + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 85..112, + subject: NoneLiteral( + ExprNoneLiteral { + range: 91..95, + }, + ), + cases: [ + MatchCase { + range: 101..112, + pattern: MatchAs( + PatternMatchAs { + range: 106..107, + pattern: None, + name: None, + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 109..112, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 109..112, + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 113..140, + subject: BooleanLiteral( + ExprBooleanLiteral { + range: 119..123, + value: true, + }, + ), + cases: [ + MatchCase { + range: 129..140, + pattern: MatchAs( + PatternMatchAs { + range: 134..135, + pattern: None, + name: None, + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 137..140, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 137..140, + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 141..169, + subject: BooleanLiteral( + ExprBooleanLiteral { + range: 147..152, + value: false, + }, + ), + cases: [ + MatchCase { + range: 158..169, + pattern: MatchAs( + PatternMatchAs { + range: 163..164, + pattern: None, + name: None, + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 166..169, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 166..169, + }, + ), + }, + ), + ], + }, + ], + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@match_classify_as_keyword_or_identifier.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@match_classify_as_keyword_or_identifier.py.snap.new new file mode 100644 index 0000000000..628e4e577d --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@match_classify_as_keyword_or_identifier.py.snap.new @@ -0,0 +1,292 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/inline/ok/match_classify_as_keyword_or_identifier.py +--- +## AST + +``` +Module( + ModModule { + range: 0..225, + body: [ + Expr( + StmtExpr { + range: 0..12, + value: Call( + ExprCall { + range: 0..12, + func: Name( + ExprName { + range: 0..5, + id: "match", + ctx: Load, + }, + ), + arguments: Arguments { + range: 6..12, + args: [ + NumberLiteral( + ExprNumberLiteral { + range: 7..8, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 10..11, + value: Int( + 2, + ), + }, + ), + ], + keywords: [], + }, + }, + ), + }, + ), + Match( + StmtMatch { + range: 27..67, + subject: Tuple( + ExprTuple { + range: 33..39, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 34..35, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 37..38, + value: Int( + 2, + ), + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + cases: [ + MatchCase { + range: 56..67, + pattern: MatchAs( + PatternMatchAs { + range: 61..62, + pattern: None, + name: None, + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 64..67, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 64..67, + }, + ), + }, + ), + ], + }, + ], + }, + ), + Expr( + StmtExpr { + range: 68..78, + value: Subscript( + ExprSubscript { + range: 68..78, + value: Name( + ExprName { + range: 68..73, + id: "match", + ctx: Load, + }, + ), + slice: Slice( + ExprSlice { + range: 75..77, + lower: Some( + NumberLiteral( + ExprNumberLiteral { + range: 75..76, + value: Int( + 1, + ), + }, + ), + ), + upper: None, + step: None, + }, + ), + ctx: Load, + }, + ), + }, + ), + Match( + StmtMatch { + range: 93..133, + subject: List( + ExprList { + range: 99..105, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 100..101, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 103..104, + value: Int( + 2, + ), + }, + ), + ], + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 122..133, + pattern: MatchAs( + PatternMatchAs { + range: 127..128, + pattern: None, + name: None, + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 130..133, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 130..133, + }, + ), + }, + ), + ], + }, + ], + }, + ), + Expr( + StmtExpr { + range: 134..145, + value: BinOp( + ExprBinOp { + range: 134..145, + left: Name( + ExprName { + range: 134..139, + id: "match", + ctx: Load, + }, + ), + op: Mult, + right: Name( + ExprName { + range: 142..145, + id: "foo", + ctx: Load, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 160..171, + value: BinOp( + ExprBinOp { + range: 160..171, + left: Name( + ExprName { + range: 160..165, + id: "match", + ctx: Load, + }, + ), + op: Sub, + right: Name( + ExprName { + range: 168..171, + id: "foo", + ctx: Load, + }, + ), + }, + ), + }, + ), + Match( + StmtMatch { + range: 186..224, + subject: UnaryOp( + ExprUnaryOp { + range: 192..196, + op: USub, + operand: Name( + ExprName { + range: 193..196, + id: "foo", + ctx: Load, + }, + ), + }, + ), + cases: [ + MatchCase { + range: 213..224, + pattern: MatchAs( + PatternMatchAs { + range: 218..219, + pattern: None, + name: None, + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 221..224, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 221..224, + }, + ), + }, + ), + ], + }, + ], + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@match_sequence_pattern_parentheses_terminator.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@match_sequence_pattern_parentheses_terminator.py.snap.new new file mode 100644 index 0000000000..9ae0863b8a --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@match_sequence_pattern_parentheses_terminator.py.snap.new @@ -0,0 +1,124 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/inline/ok/match_sequence_pattern_parentheses_terminator.py +--- +## AST + +``` +Module( + ModModule { + range: 0..57, + body: [ + Match( + StmtMatch { + range: 0..56, + subject: Name( + ExprName { + range: 6..13, + id: "subject", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 19..35, + pattern: MatchSequence( + PatternMatchSequence { + range: 24..30, + patterns: [ + MatchAs( + PatternMatchAs { + range: 25..26, + pattern: None, + name: Some( + Identifier { + id: "a", + range: 25..26, + }, + ), + }, + ), + MatchAs( + PatternMatchAs { + range: 28..29, + pattern: None, + name: Some( + Identifier { + id: "b", + range: 28..29, + }, + ), + }, + ), + ], + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 32..35, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 32..35, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 40..56, + pattern: MatchSequence( + PatternMatchSequence { + range: 45..51, + patterns: [ + MatchAs( + PatternMatchAs { + range: 46..47, + pattern: None, + name: Some( + Identifier { + id: "a", + range: 46..47, + }, + ), + }, + ), + MatchAs( + PatternMatchAs { + range: 49..50, + pattern: None, + name: Some( + Identifier { + id: "b", + range: 49..50, + }, + ), + }, + ), + ], + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 53..56, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 53..56, + }, + ), + }, + ), + ], + }, + ], + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@match_sequence_pattern_terminator.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@match_sequence_pattern_terminator.py.snap.new new file mode 100644 index 0000000000..47e6c9393b --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@match_sequence_pattern_terminator.py.snap.new @@ -0,0 +1,196 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/inline/ok/match_sequence_pattern_terminator.py +--- +## AST + +``` +Module( + ModModule { + range: 0..95, + body: [ + Match( + StmtMatch { + range: 0..94, + subject: Name( + ExprName { + range: 6..13, + id: "subject", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 19..30, + pattern: MatchAs( + PatternMatchAs { + range: 24..25, + pattern: None, + name: Some( + Identifier { + id: "a", + range: 24..25, + }, + ), + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 27..30, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 27..30, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 35..51, + pattern: MatchAs( + PatternMatchAs { + range: 40..41, + pattern: None, + name: Some( + Identifier { + id: "a", + range: 40..41, + }, + ), + }, + ), + guard: Some( + Name( + ExprName { + range: 45..46, + id: "x", + ctx: Load, + }, + ), + ), + body: [ + Expr( + StmtExpr { + range: 48..51, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 48..51, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 56..70, + pattern: MatchSequence( + PatternMatchSequence { + range: 61..65, + patterns: [ + MatchAs( + PatternMatchAs { + range: 61..62, + pattern: None, + name: Some( + Identifier { + id: "a", + range: 61..62, + }, + ), + }, + ), + MatchAs( + PatternMatchAs { + range: 64..65, + pattern: None, + name: Some( + Identifier { + id: "b", + range: 64..65, + }, + ), + }, + ), + ], + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 67..70, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 67..70, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 75..94, + pattern: MatchSequence( + PatternMatchSequence { + range: 80..84, + patterns: [ + MatchAs( + PatternMatchAs { + range: 80..81, + pattern: None, + name: Some( + Identifier { + id: "a", + range: 80..81, + }, + ), + }, + ), + MatchAs( + PatternMatchAs { + range: 83..84, + pattern: None, + name: Some( + Identifier { + id: "b", + range: 83..84, + }, + ), + }, + ), + ], + }, + ), + guard: Some( + Name( + ExprName { + range: 88..89, + id: "x", + ctx: Load, + }, + ), + ), + body: [ + Expr( + StmtExpr { + range: 91..94, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 91..94, + }, + ), + }, + ), + ], + }, + ], + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@match_stmt_subject_expr.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@match_stmt_subject_expr.py.snap.new new file mode 100644 index 0000000000..cb7dd6a691 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@match_stmt_subject_expr.py.snap.new @@ -0,0 +1,230 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/inline/ok/match_stmt_subject_expr.py +--- +## AST + +``` +Module( + ModModule { + range: 0..185, + body: [ + Match( + StmtMatch { + range: 0..29, + subject: Named( + ExprNamed { + range: 6..12, + target: Name( + ExprName { + range: 6..7, + id: "x", + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 11..12, + value: Int( + 1, + ), + }, + ), + }, + ), + cases: [ + MatchCase { + range: 18..29, + pattern: MatchAs( + PatternMatchAs { + range: 23..24, + pattern: None, + name: None, + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 26..29, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 26..29, + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 30..61, + subject: Named( + ExprNamed { + range: 37..43, + target: Name( + ExprName { + range: 37..38, + id: "x", + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 42..43, + value: Int( + 1, + ), + }, + ), + }, + ), + cases: [ + MatchCase { + range: 50..61, + pattern: MatchAs( + PatternMatchAs { + range: 55..56, + pattern: None, + name: None, + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 58..61, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 58..61, + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 121..153, + subject: Tuple( + ExprTuple { + range: 127..136, + elts: [ + Starred( + ExprStarred { + range: 127..133, + value: BinOp( + ExprBinOp { + range: 128..133, + left: Name( + ExprName { + range: 128..129, + id: "x", + ctx: Load, + }, + ), + op: BitOr, + right: Name( + ExprName { + range: 132..133, + id: "y", + ctx: Load, + }, + ), + }, + ), + ctx: Load, + }, + ), + Name( + ExprName { + range: 135..136, + id: "z", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + cases: [ + MatchCase { + range: 142..153, + pattern: MatchAs( + PatternMatchAs { + range: 147..148, + pattern: None, + name: None, + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 150..153, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 150..153, + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 154..184, + subject: Await( + ExprAwait { + range: 160..167, + value: Name( + ExprName { + range: 166..167, + id: "x", + ctx: Load, + }, + ), + }, + ), + cases: [ + MatchCase { + range: 173..184, + pattern: MatchAs( + PatternMatchAs { + range: 178..179, + pattern: None, + name: None, + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 181..184, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 181..184, + }, + ), + }, + ), + ], + }, + ], + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@match_stmt_valid_guard_expr.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@match_stmt_valid_guard_expr.py.snap.new new file mode 100644 index 0000000000..28cf8498a5 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@match_stmt_valid_guard_expr.py.snap.new @@ -0,0 +1,282 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/inline/ok/match_stmt_valid_guard_expr.py +--- +## AST + +``` +Module( + ModModule { + range: 0..158, + body: [ + Match( + StmtMatch { + range: 0..34, + subject: Name( + ExprName { + range: 6..7, + id: "x", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 13..34, + pattern: MatchAs( + PatternMatchAs { + range: 18..19, + pattern: None, + name: Some( + Identifier { + id: "y", + range: 18..19, + }, + ), + }, + ), + guard: Some( + Named( + ExprNamed { + range: 23..29, + target: Name( + ExprName { + range: 23..24, + id: "a", + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 28..29, + value: Int( + 1, + ), + }, + ), + }, + ), + ), + body: [ + Expr( + StmtExpr { + range: 31..34, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 31..34, + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 35..79, + subject: Name( + ExprName { + range: 41..42, + id: "x", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 48..79, + pattern: MatchAs( + PatternMatchAs { + range: 53..54, + pattern: None, + name: Some( + Identifier { + id: "y", + range: 53..54, + }, + ), + }, + ), + guard: Some( + If( + ExprIf { + range: 58..74, + test: BooleanLiteral( + ExprBooleanLiteral { + range: 63..67, + value: true, + }, + ), + body: Name( + ExprName { + range: 58..59, + id: "a", + ctx: Load, + }, + ), + orelse: Name( + ExprName { + range: 73..74, + id: "b", + ctx: Load, + }, + ), + }, + ), + ), + body: [ + Expr( + StmtExpr { + range: 76..79, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 76..79, + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 80..119, + subject: Name( + ExprName { + range: 86..87, + id: "x", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 93..119, + pattern: MatchAs( + PatternMatchAs { + range: 98..99, + pattern: None, + name: Some( + Identifier { + id: "y", + range: 98..99, + }, + ), + }, + ), + guard: Some( + Lambda( + ExprLambda { + range: 103..114, + parameters: Some( + Parameters { + range: 110..111, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 110..111, + parameter: Parameter { + range: 110..111, + name: Identifier { + id: "a", + range: 110..111, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: Name( + ExprName { + range: 113..114, + id: "b", + ctx: Load, + }, + ), + }, + ), + ), + body: [ + Expr( + StmtExpr { + range: 116..119, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 116..119, + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 120..157, + subject: Name( + ExprName { + range: 126..127, + id: "x", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 133..157, + pattern: MatchAs( + PatternMatchAs { + range: 138..139, + pattern: None, + name: Some( + Identifier { + id: "y", + range: 138..139, + }, + ), + }, + ), + guard: Some( + Yield( + ExprYield { + range: 144..151, + value: Some( + Name( + ExprName { + range: 150..151, + id: "x", + ctx: Load, + }, + ), + ), + }, + ), + ), + body: [ + Expr( + StmtExpr { + range: 154..157, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 154..157, + }, + ), + }, + ), + ], + }, + ], + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@nonlocal_stmt.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@nonlocal_stmt.py.snap.new new file mode 100644 index 0000000000..76a2e4f5cb --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@nonlocal_stmt.py.snap.new @@ -0,0 +1,46 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/inline/ok/nonlocal_stmt.py +--- +## AST + +``` +Module( + ModModule { + range: 0..28, + body: [ + Nonlocal( + StmtNonlocal { + range: 0..10, + names: [ + Identifier { + id: "x", + range: 9..10, + }, + ], + }, + ), + Nonlocal( + StmtNonlocal { + range: 11..27, + names: [ + Identifier { + id: "x", + range: 20..21, + }, + Identifier { + id: "y", + range: 23..24, + }, + Identifier { + id: "z", + range: 26..27, + }, + ], + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@other__decorator.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@other__decorator.py.snap.new new file mode 100644 index 0000000000..e1c86276c2 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@other__decorator.py.snap.new @@ -0,0 +1,671 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/valid/other/decorator.py +--- +## AST + +``` +Module( + ModModule { + range: 0..407, + body: [ + FunctionDef( + StmtFunctionDef { + range: 0..40, + is_async: false, + decorator_list: [ + Decorator { + range: 0..19, + expression: Name( + ExprName { + range: 1..19, + id: "function_decorator", + ctx: Load, + }, + ), + }, + ], + name: Identifier { + id: "test", + range: 24..28, + }, + type_params: None, + parameters: Parameters { + range: 28..30, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 36..40, + }, + ), + ], + }, + ), + ClassDef( + StmtClassDef { + range: 43..80, + decorator_list: [ + Decorator { + range: 43..59, + expression: Name( + ExprName { + range: 44..59, + id: "class_decorator", + ctx: Load, + }, + ), + }, + ], + name: Identifier { + id: "Test", + range: 66..70, + }, + type_params: None, + arguments: None, + body: [ + Pass( + StmtPass { + range: 76..80, + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 83..106, + is_async: false, + decorator_list: [ + Decorator { + range: 83..93, + expression: Name( + ExprName { + range: 84..93, + id: "decorator", + ctx: Load, + }, + ), + }, + ], + name: Identifier { + id: "f", + range: 98..99, + }, + type_params: None, + parameters: Parameters { + range: 99..101, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 103..106, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 103..106, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 109..128, + is_async: false, + decorator_list: [ + Decorator { + range: 109..115, + expression: Attribute( + ExprAttribute { + range: 110..115, + value: Attribute( + ExprAttribute { + range: 110..113, + value: Name( + ExprName { + range: 110..111, + id: "a", + ctx: Load, + }, + ), + attr: Identifier { + id: "b", + range: 112..113, + }, + ctx: Load, + }, + ), + attr: Identifier { + id: "c", + range: 114..115, + }, + ctx: Load, + }, + ), + }, + ], + name: Identifier { + id: "f", + range: 120..121, + }, + type_params: None, + parameters: Parameters { + range: 121..123, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 125..128, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 125..128, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 131..153, + is_async: false, + decorator_list: [ + Decorator { + range: 131..133, + expression: Name( + ExprName { + range: 132..133, + id: "a", + ctx: Load, + }, + ), + }, + Decorator { + range: 134..140, + expression: Attribute( + ExprAttribute { + range: 135..140, + value: Attribute( + ExprAttribute { + range: 135..138, + value: Name( + ExprName { + range: 135..136, + id: "a", + ctx: Load, + }, + ), + attr: Identifier { + id: "b", + range: 137..138, + }, + ctx: Load, + }, + ), + attr: Identifier { + id: "c", + range: 139..140, + }, + ctx: Load, + }, + ), + }, + ], + name: Identifier { + id: "f", + range: 145..146, + }, + type_params: None, + parameters: Parameters { + range: 146..148, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 150..153, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 150..153, + }, + ), + }, + ), + ], + }, + ), + ClassDef( + StmtClassDef { + range: 156..185, + decorator_list: [ + Decorator { + range: 156..158, + expression: Name( + ExprName { + range: 157..158, + id: "a", + ctx: Load, + }, + ), + }, + Decorator { + range: 159..165, + expression: BinOp( + ExprBinOp { + range: 160..165, + left: NumberLiteral( + ExprNumberLiteral { + range: 160..161, + value: Int( + 1, + ), + }, + ), + op: BitOr, + right: NumberLiteral( + ExprNumberLiteral { + range: 164..165, + value: Int( + 2, + ), + }, + ), + }, + ), + }, + Decorator { + range: 166..172, + expression: Attribute( + ExprAttribute { + range: 167..172, + value: Attribute( + ExprAttribute { + range: 167..170, + value: Name( + ExprName { + range: 167..168, + id: "a", + ctx: Load, + }, + ), + attr: Identifier { + id: "b", + range: 169..170, + }, + ctx: Load, + }, + ), + attr: Identifier { + id: "c", + range: 171..172, + }, + ctx: Load, + }, + ), + }, + ], + name: Identifier { + id: "T", + range: 179..180, + }, + type_params: None, + arguments: None, + body: [ + Expr( + StmtExpr { + range: 182..185, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 182..185, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 188..269, + is_async: false, + decorator_list: [ + Decorator { + range: 188..195, + expression: Named( + ExprNamed { + range: 189..195, + target: Name( + ExprName { + range: 189..190, + id: "x", + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 194..195, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + Decorator { + range: 196..213, + expression: If( + ExprIf { + range: 197..213, + test: BooleanLiteral( + ExprBooleanLiteral { + range: 202..206, + value: true, + }, + ), + body: Name( + ExprName { + range: 197..198, + id: "x", + ctx: Load, + }, + ), + orelse: Name( + ExprName { + range: 212..213, + id: "y", + ctx: Load, + }, + ), + }, + ), + }, + Decorator { + range: 214..226, + expression: Lambda( + ExprLambda { + range: 215..226, + parameters: Some( + Parameters { + range: 222..223, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 222..223, + parameter: Parameter { + range: 222..223, + name: Identifier { + id: "x", + range: 222..223, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: Name( + ExprName { + range: 225..226, + id: "x", + ctx: Load, + }, + ), + }, + ), + }, + Decorator { + range: 227..235, + expression: BoolOp( + ExprBoolOp { + range: 228..235, + op: And, + values: [ + Name( + ExprName { + range: 228..229, + id: "x", + ctx: Load, + }, + ), + Name( + ExprName { + range: 234..235, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + }, + Decorator { + range: 236..246, + expression: Yield( + ExprYield { + range: 238..245, + value: Some( + Name( + ExprName { + range: 244..245, + id: "x", + ctx: Load, + }, + ), + ), + }, + ), + }, + Decorator { + range: 247..256, + expression: Tuple( + ExprTuple { + range: 248..256, + elts: [ + Starred( + ExprStarred { + range: 249..251, + value: Name( + ExprName { + range: 250..251, + id: "x", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + Starred( + ExprStarred { + range: 253..255, + value: Name( + ExprName { + range: 254..255, + id: "y", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + }, + ], + name: Identifier { + id: "f", + range: 261..262, + }, + type_params: None, + parameters: Parameters { + range: 262..264, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 266..269, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 266..269, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 360..380, + is_async: false, + decorator_list: [ + Decorator { + range: 360..365, + expression: BinOp( + ExprBinOp { + range: 361..365, + left: Name( + ExprName { + range: 361..362, + id: "x", + ctx: Load, + }, + ), + op: MatMult, + right: Name( + ExprName { + range: 364..365, + id: "y", + ctx: Load, + }, + ), + }, + ), + }, + ], + name: Identifier { + id: "foo", + range: 370..373, + }, + type_params: None, + parameters: Parameters { + range: 373..375, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 377..380, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 377..380, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 383..407, + is_async: false, + decorator_list: [ + Decorator { + range: 383..385, + expression: Name( + ExprName { + range: 384..385, + id: "x", + ctx: Load, + }, + ), + }, + Decorator { + range: 388..390, + expression: Name( + ExprName { + range: 389..390, + id: "y", + ctx: Load, + }, + ), + }, + ], + name: Identifier { + id: "foo", + range: 397..400, + }, + type_params: None, + parameters: Parameters { + range: 400..402, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 404..407, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 404..407, + }, + ), + }, + ), + ], + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@param_with_annotation.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@param_with_annotation.py.snap.new new file mode 100644 index 0000000000..4268c5e059 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@param_with_annotation.py.snap.new @@ -0,0 +1,279 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/inline/ok/param_with_annotation.py +--- +## AST + +``` +Module( + ModModule { + range: 0..113, + body: [ + FunctionDef( + StmtFunctionDef { + range: 0..22, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 4..7, + }, + type_params: None, + parameters: Parameters { + range: 7..17, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 8..16, + parameter: Parameter { + range: 8..16, + name: Identifier { + id: "arg", + range: 8..11, + }, + annotation: Some( + Name( + ExprName { + range: 13..16, + id: "int", + ctx: Load, + }, + ), + ), + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 19..22, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 19..22, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 23..53, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 27..30, + }, + type_params: None, + parameters: Parameters { + range: 30..48, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 31..47, + parameter: Parameter { + range: 31..47, + name: Identifier { + id: "arg", + range: 31..34, + }, + annotation: Some( + Lambda( + ExprLambda { + range: 36..47, + parameters: Some( + Parameters { + range: 43..44, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 43..44, + parameter: Parameter { + range: 43..44, + name: Identifier { + id: "x", + range: 43..44, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: Name( + ExprName { + range: 46..47, + id: "x", + ctx: Load, + }, + ), + }, + ), + ), + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 50..53, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 50..53, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 54..82, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 58..61, + }, + type_params: None, + parameters: Parameters { + range: 61..77, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 62..76, + parameter: Parameter { + range: 62..76, + name: Identifier { + id: "arg", + range: 62..65, + }, + annotation: Some( + Yield( + ExprYield { + range: 68..75, + value: Some( + Name( + ExprName { + range: 74..75, + id: "x", + ctx: Load, + }, + ), + ), + }, + ), + ), + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 79..82, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 79..82, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 83..112, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 87..90, + }, + type_params: None, + parameters: Parameters { + range: 90..107, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 91..106, + parameter: Parameter { + range: 91..106, + name: Identifier { + id: "arg", + range: 91..94, + }, + annotation: Some( + Named( + ExprNamed { + range: 97..105, + target: Name( + ExprName { + range: 97..98, + id: "x", + ctx: Store, + }, + ), + value: Name( + ExprName { + range: 102..105, + id: "int", + ctx: Load, + }, + ), + }, + ), + ), + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 109..112, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 109..112, + }, + ), + }, + ), + ], + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@param_with_default.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@param_with_default.py.snap.new new file mode 100644 index 0000000000..10aeb8efeb --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@param_with_default.py.snap.new @@ -0,0 +1,292 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/inline/ok/param_with_default.py +--- +## AST + +``` +Module( + ModModule { + range: 0..111, + body: [ + FunctionDef( + StmtFunctionDef { + range: 0..27, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 4..7, + }, + type_params: None, + parameters: Parameters { + range: 7..22, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 8..21, + parameter: Parameter { + range: 8..9, + name: Identifier { + id: "x", + range: 8..9, + }, + annotation: None, + }, + default: Some( + Lambda( + ExprLambda { + range: 10..21, + parameters: Some( + Parameters { + range: 17..18, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 17..18, + parameter: Parameter { + range: 17..18, + name: Identifier { + id: "y", + range: 17..18, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: Name( + ExprName { + range: 20..21, + id: "y", + ctx: Load, + }, + ), + }, + ), + ), + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 24..27, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 24..27, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 28..60, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 32..35, + }, + type_params: None, + parameters: Parameters { + range: 35..55, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 36..54, + parameter: Parameter { + range: 36..37, + name: Identifier { + id: "x", + range: 36..37, + }, + annotation: None, + }, + default: Some( + If( + ExprIf { + range: 38..54, + test: BooleanLiteral( + ExprBooleanLiteral { + range: 43..47, + value: true, + }, + ), + body: NumberLiteral( + ExprNumberLiteral { + range: 38..39, + value: Int( + 1, + ), + }, + ), + orelse: NumberLiteral( + ExprNumberLiteral { + range: 53..54, + value: Int( + 2, + ), + }, + ), + }, + ), + ), + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 57..60, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 57..60, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 61..84, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 65..68, + }, + type_params: None, + parameters: Parameters { + range: 68..79, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 69..78, + parameter: Parameter { + range: 69..70, + name: Identifier { + id: "x", + range: 69..70, + }, + annotation: None, + }, + default: Some( + Await( + ExprAwait { + range: 71..78, + value: Name( + ExprName { + range: 77..78, + id: "y", + ctx: Load, + }, + ), + }, + ), + ), + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 81..84, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 81..84, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 85..110, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 89..92, + }, + type_params: None, + parameters: Parameters { + range: 92..105, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 93..104, + parameter: Parameter { + range: 93..94, + name: Identifier { + id: "x", + range: 93..94, + }, + annotation: None, + }, + default: Some( + Yield( + ExprYield { + range: 96..103, + value: Some( + Name( + ExprName { + range: 102..103, + id: "y", + ctx: Load, + }, + ), + ), + }, + ), + ), + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 107..110, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 107..110, + }, + ), + }, + ), + ], + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@param_with_star_annotation.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@param_with_star_annotation.py.snap.new new file mode 100644 index 0000000000..3fdeb787cd --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@param_with_star_annotation.py.snap.new @@ -0,0 +1,156 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/inline/ok/param_with_star_annotation.py +--- +## AST + +``` +Module( + ModModule { + range: 0..67, + body: [ + FunctionDef( + StmtFunctionDef { + range: 0..31, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 4..7, + }, + type_params: None, + parameters: Parameters { + range: 7..26, + posonlyargs: [], + args: [], + vararg: Some( + Parameter { + range: 8..25, + name: Identifier { + id: "args", + range: 9..13, + }, + annotation: Some( + Starred( + ExprStarred { + range: 15..25, + value: BinOp( + ExprBinOp { + range: 16..25, + left: Name( + ExprName { + range: 16..19, + id: "int", + ctx: Load, + }, + ), + op: BitOr, + right: Name( + ExprName { + range: 22..25, + id: "str", + ctx: Load, + }, + ), + }, + ), + ctx: Load, + }, + ), + ), + }, + ), + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 28..31, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 28..31, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 32..66, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 36..39, + }, + type_params: None, + parameters: Parameters { + range: 39..61, + posonlyargs: [], + args: [], + vararg: Some( + Parameter { + range: 40..60, + name: Identifier { + id: "args", + range: 41..45, + }, + annotation: Some( + Starred( + ExprStarred { + range: 47..60, + value: BoolOp( + ExprBoolOp { + range: 49..59, + op: Or, + values: [ + Name( + ExprName { + range: 49..52, + id: "int", + ctx: Load, + }, + ), + Name( + ExprName { + range: 56..59, + id: "str", + ctx: Load, + }, + ), + ], + }, + ), + ctx: Load, + }, + ), + ), + }, + ), + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 63..66, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 63..66, + }, + ), + }, + ), + ], + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@params_non_default_after_star.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@params_non_default_after_star.py.snap.new new file mode 100644 index 0000000000..1fdb0a95c6 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@params_non_default_after_star.py.snap.new @@ -0,0 +1,227 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/inline/ok/params_non_default_after_star.py +--- +## AST + +``` +Module( + ModModule { + range: 0..72, + body: [ + FunctionDef( + StmtFunctionDef { + range: 0..33, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 4..7, + }, + type_params: None, + parameters: Parameters { + range: 7..28, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 8..12, + parameter: Parameter { + range: 8..9, + name: Identifier { + id: "a", + range: 8..9, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 10..12, + value: Int( + 10, + ), + }, + ), + ), + }, + ], + vararg: None, + kwonlyargs: [ + ParameterWithDefault { + range: 17..18, + parameter: Parameter { + range: 17..18, + name: Identifier { + id: "b", + range: 17..18, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 20..24, + parameter: Parameter { + range: 20..21, + name: Identifier { + id: "c", + range: 20..21, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 22..24, + value: Int( + 11, + ), + }, + ), + ), + }, + ParameterWithDefault { + range: 26..27, + parameter: Parameter { + range: 26..27, + name: Identifier { + id: "d", + range: 26..27, + }, + annotation: None, + }, + default: None, + }, + ], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 30..33, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 30..33, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 34..71, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 38..41, + }, + type_params: None, + parameters: Parameters { + range: 41..66, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 42..46, + parameter: Parameter { + range: 42..43, + name: Identifier { + id: "a", + range: 42..43, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 44..46, + value: Int( + 10, + ), + }, + ), + ), + }, + ], + vararg: Some( + Parameter { + range: 48..53, + name: Identifier { + id: "args", + range: 49..53, + }, + annotation: None, + }, + ), + kwonlyargs: [ + ParameterWithDefault { + range: 55..56, + parameter: Parameter { + range: 55..56, + name: Identifier { + id: "b", + range: 55..56, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 58..62, + parameter: Parameter { + range: 58..59, + name: Identifier { + id: "c", + range: 58..59, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 60..62, + value: Int( + 11, + ), + }, + ), + ), + }, + ParameterWithDefault { + range: 64..65, + parameter: Parameter { + range: 64..65, + name: Identifier { + id: "d", + range: 64..65, + }, + annotation: None, + }, + default: None, + }, + ], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 68..71, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 68..71, + }, + ), + }, + ), + ], + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@params_seen_keyword_only_param_after_star.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@params_seen_keyword_only_param_after_star.py.snap.new new file mode 100644 index 0000000000..31b2f5cdc3 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@params_seen_keyword_only_param_after_star.py.snap.new @@ -0,0 +1,135 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/inline/ok/params_seen_keyword_only_param_after_star.py +--- +## AST + +``` +Module( + ModModule { + range: 0..61, + body: [ + FunctionDef( + StmtFunctionDef { + range: 0..28, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 4..7, + }, + type_params: None, + parameters: Parameters { + range: 7..23, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [ + ParameterWithDefault { + range: 11..12, + parameter: Parameter { + range: 11..12, + name: Identifier { + id: "a", + range: 11..12, + }, + annotation: None, + }, + default: None, + }, + ], + kwarg: Some( + Parameter { + range: 14..22, + name: Identifier { + id: "kwargs", + range: 16..22, + }, + annotation: None, + }, + ), + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 25..28, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 25..28, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 29..60, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 33..36, + }, + type_params: None, + parameters: Parameters { + range: 36..55, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [ + ParameterWithDefault { + range: 40..44, + parameter: Parameter { + range: 40..41, + name: Identifier { + id: "a", + range: 40..41, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 42..44, + value: Int( + 10, + ), + }, + ), + ), + }, + ], + kwarg: Some( + Parameter { + range: 46..54, + name: Identifier { + id: "kwargs", + range: 48..54, + }, + annotation: None, + }, + ), + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 57..60, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 57..60, + }, + ), + }, + ), + ], + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@simple_stmts_in_block.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@simple_stmts_in_block.py.snap.new new file mode 100644 index 0000000000..bf1e717b88 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@simple_stmts_in_block.py.snap.new @@ -0,0 +1,124 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/inline/ok/simple_stmts_in_block.py +--- +## AST + +``` +Module( + ModModule { + range: 0..84, + body: [ + If( + StmtIf { + range: 0..13, + test: BooleanLiteral( + ExprBooleanLiteral { + range: 3..7, + value: true, + }, + ), + body: [ + Pass( + StmtPass { + range: 9..13, + }, + ), + ], + elif_else_clauses: [], + }, + ), + If( + StmtIf { + range: 14..27, + test: BooleanLiteral( + ExprBooleanLiteral { + range: 17..21, + value: true, + }, + ), + body: [ + Pass( + StmtPass { + range: 23..27, + }, + ), + ], + elif_else_clauses: [], + }, + ), + If( + StmtIf { + range: 29..52, + test: BooleanLiteral( + ExprBooleanLiteral { + range: 32..36, + value: true, + }, + ), + body: [ + Pass( + StmtPass { + range: 38..42, + }, + ), + Continue( + StmtContinue { + range: 44..52, + }, + ), + ], + elif_else_clauses: [], + }, + ), + If( + StmtIf { + range: 53..76, + test: BooleanLiteral( + ExprBooleanLiteral { + range: 56..60, + value: true, + }, + ), + body: [ + Pass( + StmtPass { + range: 62..66, + }, + ), + Continue( + StmtContinue { + range: 68..76, + }, + ), + ], + elif_else_clauses: [], + }, + ), + Assign( + StmtAssign { + range: 78..83, + targets: [ + Name( + ExprName { + range: 78..79, + id: "x", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 82..83, + value: Int( + 1, + ), + }, + ), + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@simple_stmts_with_semicolons.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@simple_stmts_with_semicolons.py.snap.new new file mode 100644 index 0000000000..76fa27529c --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@simple_stmts_with_semicolons.py.snap.new @@ -0,0 +1,91 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/inline/ok/simple_stmts_with_semicolons.py +--- +## AST + +``` +Module( + ModModule { + range: 0..51, + body: [ + Return( + StmtReturn { + range: 0..6, + value: None, + }, + ), + Import( + StmtImport { + range: 8..16, + names: [ + Alias { + range: 15..16, + name: Identifier { + id: "a", + range: 15..16, + }, + asname: None, + }, + ], + }, + ), + ImportFrom( + StmtImportFrom { + range: 18..33, + module: Some( + Identifier { + id: "x", + range: 23..24, + }, + ), + names: [ + Alias { + range: 32..33, + name: Identifier { + id: "y", + range: 32..33, + }, + asname: None, + }, + ], + level: 0, + }, + ), + Expr( + StmtExpr { + range: 35..36, + value: Name( + ExprName { + range: 35..36, + id: "z", + ctx: Load, + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 38..50, + name: Name( + ExprName { + range: 43..44, + id: "T", + ctx: Store, + }, + ), + type_params: None, + value: Name( + ExprName { + range: 47..50, + id: "int", + ctx: Load, + }, + ), + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__ambiguous_lpar_with_items.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__ambiguous_lpar_with_items.py.snap.new new file mode 100644 index 0000000000..429a1e332d --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__ambiguous_lpar_with_items.py.snap.new @@ -0,0 +1,4032 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/valid/statement/ambiguous_lpar_with_items.py +--- +## AST + +``` +Module( + ModModule { + range: 0..3620, + body: [ + With( + StmtWith { + range: 588..604, + is_async: false, + items: [ + WithItem { + range: 594..598, + context_expr: Name( + ExprName { + range: 594..598, + id: "item", + ctx: Load, + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 601..604, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 601..604, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 605..622, + is_async: false, + items: [ + WithItem { + range: 611..615, + context_expr: Name( + ExprName { + range: 611..615, + id: "item", + ctx: Load, + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 619..622, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 619..622, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 648..668, + is_async: false, + items: [ + WithItem { + range: 654..662, + context_expr: Name( + ExprName { + range: 656..660, + id: "item", + ctx: Load, + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 665..668, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 665..668, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 669..693, + is_async: false, + items: [ + WithItem { + range: 675..680, + context_expr: Name( + ExprName { + range: 675..680, + id: "item1", + ctx: Load, + }, + ), + optional_vars: None, + }, + WithItem { + range: 682..687, + context_expr: Name( + ExprName { + range: 682..687, + id: "item2", + ctx: Load, + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 690..693, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 690..693, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 694..719, + is_async: false, + items: [ + WithItem { + range: 700..705, + context_expr: Name( + ExprName { + range: 700..705, + id: "item1", + ctx: Load, + }, + ), + optional_vars: None, + }, + WithItem { + range: 707..712, + context_expr: Name( + ExprName { + range: 707..712, + id: "item2", + ctx: Load, + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 716..719, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 716..719, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 745..794, + is_async: false, + items: [ + WithItem { + range: 751..758, + context_expr: Name( + ExprName { + range: 752..757, + id: "item1", + ctx: Load, + }, + ), + optional_vars: None, + }, + WithItem { + range: 760..767, + context_expr: Name( + ExprName { + range: 761..766, + id: "item2", + ctx: Load, + }, + ), + optional_vars: None, + }, + WithItem { + range: 769..779, + context_expr: Name( + ExprName { + range: 769..774, + id: "item3", + ctx: Load, + }, + ), + optional_vars: Some( + Name( + ExprName { + range: 778..779, + id: "f", + ctx: Store, + }, + ), + ), + }, + WithItem { + range: 781..788, + context_expr: Name( + ExprName { + range: 782..787, + id: "item4", + ctx: Load, + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 791..794, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 791..794, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 795..828, + is_async: false, + items: [ + WithItem { + range: 801..815, + context_expr: Tuple( + ExprTuple { + range: 801..815, + elts: [ + Name( + ExprName { + range: 802..807, + id: "item1", + ctx: Load, + }, + ), + Name( + ExprName { + range: 809..814, + id: "item2", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + optional_vars: None, + }, + WithItem { + range: 817..822, + context_expr: Name( + ExprName { + range: 817..822, + id: "item3", + ctx: Load, + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 825..828, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 825..828, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 829..852, + is_async: false, + items: [ + WithItem { + range: 835..846, + context_expr: Tuple( + ExprTuple { + range: 835..841, + elts: [ + Name( + ExprName { + range: 836..837, + id: "x", + ctx: Load, + }, + ), + Name( + ExprName { + range: 839..840, + id: "y", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + optional_vars: Some( + Name( + ExprName { + range: 845..846, + id: "f", + ctx: Store, + }, + ), + ), + }, + ], + body: [ + Expr( + StmtExpr { + range: 849..852, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 849..852, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 853..889, + is_async: false, + items: [ + WithItem { + range: 859..870, + context_expr: Name( + ExprName { + range: 859..864, + id: "item1", + ctx: Load, + }, + ), + optional_vars: Some( + Name( + ExprName { + range: 868..870, + id: "f1", + ctx: Store, + }, + ), + ), + }, + WithItem { + range: 872..883, + context_expr: Name( + ExprName { + range: 872..877, + id: "item2", + ctx: Load, + }, + ), + optional_vars: Some( + Name( + ExprName { + range: 881..883, + id: "f2", + ctx: Store, + }, + ), + ), + }, + ], + body: [ + Expr( + StmtExpr { + range: 886..889, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 886..889, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 890..927, + is_async: false, + items: [ + WithItem { + range: 896..907, + context_expr: Name( + ExprName { + range: 896..901, + id: "item1", + ctx: Load, + }, + ), + optional_vars: Some( + Name( + ExprName { + range: 905..907, + id: "f1", + ctx: Store, + }, + ), + ), + }, + WithItem { + range: 909..920, + context_expr: Name( + ExprName { + range: 909..914, + id: "item2", + ctx: Load, + }, + ), + optional_vars: Some( + Name( + ExprName { + range: 918..920, + id: "f2", + ctx: Store, + }, + ), + ), + }, + ], + body: [ + Expr( + StmtExpr { + range: 924..927, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 924..927, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 953..976, + is_async: false, + items: [ + WithItem { + range: 959..969, + context_expr: Compare( + ExprCompare { + range: 959..969, + left: Name( + ExprName { + range: 959..963, + id: "item", + ctx: Load, + }, + ), + ops: [ + Eq, + ], + comparators: [ + NumberLiteral( + ExprNumberLiteral { + range: 967..969, + value: Int( + 10, + ), + }, + ), + ], + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 973..976, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 973..976, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 977..1001, + is_async: false, + items: [ + WithItem { + range: 983..995, + context_expr: Named( + ExprNamed { + range: 984..994, + target: Name( + ExprName { + range: 984..988, + id: "item", + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 992..994, + value: Int( + 10, + ), + }, + ), + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 998..1001, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 998..1001, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 1002..1027, + is_async: false, + items: [ + WithItem { + range: 1008..1021, + context_expr: Tuple( + ExprTuple { + range: 1008..1021, + elts: [ + Named( + ExprNamed { + range: 1009..1019, + target: Name( + ExprName { + range: 1009..1013, + id: "item", + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 1017..1019, + value: Int( + 10, + ), + }, + ), + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 1024..1027, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 1024..1027, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 1028..1048, + is_async: false, + items: [ + WithItem { + range: 1034..1042, + context_expr: Tuple( + ExprTuple { + range: 1034..1042, + elts: [ + Starred( + ExprStarred { + range: 1035..1040, + value: Name( + ExprName { + range: 1036..1040, + id: "item", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 1045..1048, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 1045..1048, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 1049..1081, + is_async: false, + items: [ + WithItem { + range: 1055..1068, + context_expr: Named( + ExprNamed { + range: 1056..1067, + target: Name( + ExprName { + range: 1056..1061, + id: "item1", + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 1065..1067, + value: Int( + 10, + ), + }, + ), + }, + ), + optional_vars: None, + }, + WithItem { + range: 1070..1075, + context_expr: Name( + ExprName { + range: 1070..1075, + id: "item2", + ctx: Load, + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 1078..1081, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 1078..1081, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 1082..1119, + is_async: false, + items: [ + WithItem { + range: 1088..1098, + context_expr: Name( + ExprName { + range: 1088..1093, + id: "item1", + ctx: Load, + }, + ), + optional_vars: Some( + Name( + ExprName { + range: 1097..1098, + id: "f", + ctx: Store, + }, + ), + ), + }, + WithItem { + range: 1100..1113, + context_expr: Named( + ExprNamed { + range: 1101..1112, + target: Name( + ExprName { + range: 1101..1106, + id: "item2", + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 1110..1112, + value: Int( + 10, + ), + }, + ), + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 1116..1119, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 1116..1119, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 1120..1137, + is_async: false, + items: [ + WithItem { + range: 1126..1131, + context_expr: Call( + ExprCall { + range: 1126..1131, + func: Name( + ExprName { + range: 1126..1129, + id: "foo", + ctx: Load, + }, + ), + arguments: Arguments { + range: 1129..1131, + args: [], + keywords: [], + }, + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 1134..1137, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 1134..1137, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 1138..1156, + is_async: false, + items: [ + WithItem { + range: 1144..1149, + context_expr: Call( + ExprCall { + range: 1144..1149, + func: Name( + ExprName { + range: 1144..1147, + id: "foo", + ctx: Load, + }, + ), + arguments: Arguments { + range: 1147..1149, + args: [], + keywords: [], + }, + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 1153..1156, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 1153..1156, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 1157..1179, + is_async: false, + items: [ + WithItem { + range: 1163..1173, + context_expr: Call( + ExprCall { + range: 1163..1168, + func: Name( + ExprName { + range: 1163..1166, + id: "foo", + ctx: Load, + }, + ), + arguments: Arguments { + range: 1166..1168, + args: [], + keywords: [], + }, + }, + ), + optional_vars: Some( + Name( + ExprName { + range: 1172..1173, + id: "f", + ctx: Store, + }, + ), + ), + }, + ], + body: [ + Expr( + StmtExpr { + range: 1176..1179, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 1176..1179, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 1180..1207, + is_async: false, + items: [ + WithItem { + range: 1186..1201, + context_expr: FString( + ExprFString { + range: 1186..1201, + value: FStringValue { + inner: Single( + FString( + FString { + range: 1186..1201, + elements: [ + Expression( + FStringExpressionElement { + range: 1188..1200, + expression: Name( + ExprName { + range: 1189..1193, + id: "item", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: Some( + FStringFormatSpec { + range: 1195..1199, + elements: [ + Literal( + FStringLiteralElement { + range: 1195..1199, + value: "= 42", + }, + ), + ], + }, + ), + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 1204..1207, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 1204..1207, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 1208..1237, + is_async: false, + items: [ + WithItem { + range: 1214..1231, + context_expr: FString( + ExprFString { + range: 1214..1231, + value: FStringValue { + inner: Single( + FString( + FString { + range: 1214..1231, + elements: [ + Expression( + FStringExpressionElement { + range: 1216..1230, + expression: Named( + ExprNamed { + range: 1218..1228, + target: Name( + ExprName { + range: 1218..1222, + id: "item", + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 1226..1228, + value: Int( + 42, + ), + }, + ), + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 1234..1237, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 1234..1237, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 1238..1278, + is_async: false, + items: [ + WithItem { + range: 1244..1266, + context_expr: Generator( + ExprGenerator { + range: 1244..1266, + elt: Name( + ExprName { + range: 1245..1246, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 1247..1265, + target: Name( + ExprName { + range: 1251..1252, + id: "x", + ctx: Store, + }, + ), + iter: Call( + ExprCall { + range: 1256..1265, + func: Name( + ExprName { + range: 1256..1261, + id: "range", + ctx: Load, + }, + ), + arguments: Arguments { + range: 1261..1265, + args: [ + NumberLiteral( + ExprNumberLiteral { + range: 1262..1264, + value: Int( + 10, + ), + }, + ), + ], + keywords: [], + }, + }, + ), + ifs: [], + is_async: false, + }, + ], + parenthesized: true, + }, + ), + optional_vars: None, + }, + WithItem { + range: 1268..1272, + context_expr: Name( + ExprName { + range: 1268..1272, + id: "item", + ctx: Load, + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 1275..1278, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 1275..1278, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 1279..1319, + is_async: false, + items: [ + WithItem { + range: 1285..1289, + context_expr: Name( + ExprName { + range: 1285..1289, + id: "item", + ctx: Load, + }, + ), + optional_vars: None, + }, + WithItem { + range: 1291..1313, + context_expr: Generator( + ExprGenerator { + range: 1291..1313, + elt: Name( + ExprName { + range: 1292..1293, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 1294..1312, + target: Name( + ExprName { + range: 1298..1299, + id: "x", + ctx: Store, + }, + ), + iter: Call( + ExprCall { + range: 1303..1312, + func: Name( + ExprName { + range: 1303..1308, + id: "range", + ctx: Load, + }, + ), + arguments: Arguments { + range: 1308..1312, + args: [ + NumberLiteral( + ExprNumberLiteral { + range: 1309..1311, + value: Int( + 10, + ), + }, + ), + ], + keywords: [], + }, + }, + ), + ifs: [], + is_async: false, + }, + ], + parenthesized: true, + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 1316..1319, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 1316..1319, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 1320..1366, + is_async: false, + items: [ + WithItem { + range: 1326..1330, + context_expr: Name( + ExprName { + range: 1326..1330, + id: "item", + ctx: Load, + }, + ), + optional_vars: None, + }, + WithItem { + range: 1332..1354, + context_expr: Generator( + ExprGenerator { + range: 1332..1354, + elt: Name( + ExprName { + range: 1333..1334, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 1335..1353, + target: Name( + ExprName { + range: 1339..1340, + id: "x", + ctx: Store, + }, + ), + iter: Call( + ExprCall { + range: 1344..1353, + func: Name( + ExprName { + range: 1344..1349, + id: "range", + ctx: Load, + }, + ), + arguments: Arguments { + range: 1349..1353, + args: [ + NumberLiteral( + ExprNumberLiteral { + range: 1350..1352, + value: Int( + 10, + ), + }, + ), + ], + keywords: [], + }, + }, + ), + ifs: [], + is_async: false, + }, + ], + parenthesized: true, + }, + ), + optional_vars: None, + }, + WithItem { + range: 1356..1360, + context_expr: Name( + ExprName { + range: 1356..1360, + id: "item", + ctx: Load, + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 1363..1366, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 1363..1366, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 1367..1388, + is_async: false, + items: [ + WithItem { + range: 1373..1382, + context_expr: Subscript( + ExprSubscript { + range: 1373..1382, + value: Name( + ExprName { + range: 1373..1377, + id: "data", + ctx: Load, + }, + ), + slice: Slice( + ExprSlice { + range: 1378..1381, + lower: Some( + NumberLiteral( + ExprNumberLiteral { + range: 1378..1379, + value: Int( + 1, + ), + }, + ), + ), + upper: Some( + NumberLiteral( + ExprNumberLiteral { + range: 1380..1381, + value: Int( + 2, + ), + }, + ), + ), + step: None, + }, + ), + ctx: Load, + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 1385..1388, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 1385..1388, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 1389..1415, + is_async: false, + items: [ + WithItem { + range: 1395..1409, + context_expr: Subscript( + ExprSubscript { + range: 1395..1404, + value: Name( + ExprName { + range: 1395..1399, + id: "data", + ctx: Load, + }, + ), + slice: Slice( + ExprSlice { + range: 1400..1403, + lower: Some( + NumberLiteral( + ExprNumberLiteral { + range: 1400..1401, + value: Int( + 1, + ), + }, + ), + ), + upper: Some( + NumberLiteral( + ExprNumberLiteral { + range: 1402..1403, + value: Int( + 2, + ), + }, + ), + ), + step: None, + }, + ), + ctx: Load, + }, + ), + optional_vars: Some( + Name( + ExprName { + range: 1408..1409, + id: "f", + ctx: Store, + }, + ), + ), + }, + ], + body: [ + Expr( + StmtExpr { + range: 1412..1415, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 1412..1415, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 1416..1450, + is_async: false, + items: [ + WithItem { + range: 1422..1444, + context_expr: Generator( + ExprGenerator { + range: 1422..1439, + elt: Name( + ExprName { + range: 1423..1424, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 1425..1438, + target: Name( + ExprName { + range: 1429..1430, + id: "x", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 1434..1438, + id: "iter", + ctx: Load, + }, + ), + ifs: [], + is_async: false, + }, + ], + parenthesized: true, + }, + ), + optional_vars: Some( + Name( + ExprName { + range: 1443..1444, + id: "y", + ctx: Store, + }, + ), + ), + }, + ], + body: [ + Expr( + StmtExpr { + range: 1447..1450, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 1447..1450, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 1663..1684, + is_async: false, + items: [ + WithItem { + range: 1668..1679, + context_expr: Name( + ExprName { + range: 1669..1673, + id: "item", + ctx: Load, + }, + ), + optional_vars: Some( + Name( + ExprName { + range: 1678..1679, + id: "f", + ctx: Store, + }, + ), + ), + }, + ], + body: [ + Expr( + StmtExpr { + range: 1681..1684, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 1681..1684, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 1685..1707, + is_async: false, + items: [ + WithItem { + range: 1690..1702, + context_expr: Named( + ExprNamed { + range: 1691..1701, + target: Name( + ExprName { + range: 1691..1695, + id: "item", + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 1699..1701, + value: Int( + 10, + ), + }, + ), + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 1704..1707, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 1704..1707, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 1708..1735, + is_async: false, + items: [ + WithItem { + range: 1713..1730, + context_expr: Named( + ExprNamed { + range: 1714..1724, + target: Name( + ExprName { + range: 1714..1718, + id: "item", + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 1722..1724, + value: Int( + 10, + ), + }, + ), + }, + ), + optional_vars: Some( + Name( + ExprName { + range: 1729..1730, + id: "f", + ctx: Store, + }, + ), + ), + }, + ], + body: [ + Expr( + StmtExpr { + range: 1732..1735, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 1732..1735, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 1736..1762, + is_async: false, + items: [ + WithItem { + range: 1741..1757, + context_expr: Named( + ExprNamed { + range: 1744..1753, + target: Name( + ExprName { + range: 1744..1748, + id: "item", + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 1752..1753, + value: Int( + 1, + ), + }, + ), + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 1759..1762, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 1759..1762, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 1763..1793, + is_async: false, + items: [ + WithItem { + range: 1768..1781, + context_expr: Named( + ExprNamed { + range: 1769..1780, + target: Name( + ExprName { + range: 1769..1774, + id: "item1", + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 1778..1780, + value: Int( + 42, + ), + }, + ), + }, + ), + optional_vars: None, + }, + WithItem { + range: 1783..1788, + context_expr: Name( + ExprName { + range: 1783..1788, + id: "item2", + ctx: Load, + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 1790..1793, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 1790..1793, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 1794..1828, + is_async: false, + items: [ + WithItem { + range: 1799..1823, + context_expr: Call( + ExprCall { + range: 1799..1823, + func: Attribute( + ExprAttribute { + range: 1799..1821, + value: BinOp( + ExprBinOp { + range: 1800..1815, + left: Name( + ExprName { + range: 1800..1804, + id: "root", + ctx: Load, + }, + ), + op: Add, + right: Name( + ExprName { + range: 1807..1815, + id: "filename", + ctx: Load, + }, + ), + }, + ), + attr: Identifier { + id: "read", + range: 1817..1821, + }, + ctx: Load, + }, + ), + arguments: Arguments { + range: 1821..1823, + args: [], + keywords: [], + }, + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 1825..1828, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 1825..1828, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 1851..1890, + is_async: false, + items: [ + WithItem { + range: 1856..1885, + context_expr: Call( + ExprCall { + range: 1856..1880, + func: Attribute( + ExprAttribute { + range: 1856..1878, + value: BinOp( + ExprBinOp { + range: 1857..1872, + left: Name( + ExprName { + range: 1857..1861, + id: "root", + ctx: Load, + }, + ), + op: Add, + right: Name( + ExprName { + range: 1864..1872, + id: "filename", + ctx: Load, + }, + ), + }, + ), + attr: Identifier { + id: "read", + range: 1874..1878, + }, + ctx: Load, + }, + ), + arguments: Arguments { + range: 1878..1880, + args: [], + keywords: [], + }, + }, + ), + optional_vars: Some( + Name( + ExprName { + range: 1884..1885, + id: "f", + ctx: Store, + }, + ), + ), + }, + ], + body: [ + Expr( + StmtExpr { + range: 1887..1890, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 1887..1890, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 1913..1930, + is_async: false, + items: [ + WithItem { + range: 1918..1925, + context_expr: Call( + ExprCall { + range: 1918..1925, + func: Name( + ExprName { + range: 1919..1922, + id: "foo", + ctx: Load, + }, + ), + arguments: Arguments { + range: 1923..1925, + args: [], + keywords: [], + }, + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 1927..1930, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 1927..1930, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 1953..1975, + is_async: false, + items: [ + WithItem { + range: 1958..1970, + context_expr: Call( + ExprCall { + range: 1958..1965, + func: Name( + ExprName { + range: 1959..1962, + id: "foo", + ctx: Load, + }, + ), + arguments: Arguments { + range: 1963..1965, + args: [], + keywords: [], + }, + }, + ), + optional_vars: Some( + Name( + ExprName { + range: 1969..1970, + id: "f", + ctx: Store, + }, + ), + ), + }, + ], + body: [ + Expr( + StmtExpr { + range: 1972..1975, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 1972..1975, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 1998..2020, + is_async: false, + items: [ + WithItem { + range: 2003..2015, + context_expr: Call( + ExprCall { + range: 2004..2009, + func: Name( + ExprName { + range: 2004..2007, + id: "foo", + ctx: Load, + }, + ), + arguments: Arguments { + range: 2007..2009, + args: [], + keywords: [], + }, + }, + ), + optional_vars: Some( + Name( + ExprName { + range: 2014..2015, + id: "f", + ctx: Store, + }, + ), + ), + }, + ], + body: [ + Expr( + StmtExpr { + range: 2017..2020, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 2017..2020, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 2021..2047, + is_async: false, + items: [ + WithItem { + range: 2026..2042, + context_expr: Subscript( + ExprSubscript { + range: 2027..2036, + value: Name( + ExprName { + range: 2027..2031, + id: "data", + ctx: Load, + }, + ), + slice: Slice( + ExprSlice { + range: 2032..2035, + lower: Some( + NumberLiteral( + ExprNumberLiteral { + range: 2032..2033, + value: Int( + 1, + ), + }, + ), + ), + upper: Some( + NumberLiteral( + ExprNumberLiteral { + range: 2034..2035, + value: Int( + 2, + ), + }, + ), + ), + step: None, + }, + ), + ctx: Load, + }, + ), + optional_vars: Some( + Name( + ExprName { + range: 2041..2042, + id: "f", + ctx: Store, + }, + ), + ), + }, + ], + body: [ + Expr( + StmtExpr { + range: 2044..2047, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 2044..2047, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 2048..2070, + is_async: false, + items: [ + WithItem { + range: 2053..2065, + context_expr: Subscript( + ExprSubscript { + range: 2053..2065, + value: Tuple( + ExprTuple { + range: 2053..2062, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 2054..2055, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 2057..2058, + value: Int( + 2, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 2060..2061, + value: Int( + 3, + ), + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + slice: NumberLiteral( + ExprNumberLiteral { + range: 2063..2064, + value: Int( + 0, + ), + }, + ), + ctx: Load, + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 2067..2070, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 2067..2070, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 2093..2120, + is_async: false, + items: [ + WithItem { + range: 2098..2115, + context_expr: Subscript( + ExprSubscript { + range: 2098..2110, + value: Tuple( + ExprTuple { + range: 2098..2107, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 2099..2100, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 2102..2103, + value: Int( + 2, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 2105..2106, + value: Int( + 3, + ), + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + slice: NumberLiteral( + ExprNumberLiteral { + range: 2108..2109, + value: Int( + 0, + ), + }, + ), + ctx: Load, + }, + ), + optional_vars: Some( + Name( + ExprName { + range: 2114..2115, + id: "f", + ctx: Store, + }, + ), + ), + }, + ], + body: [ + Expr( + StmtExpr { + range: 2117..2120, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 2117..2120, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 2143..2169, + is_async: false, + items: [ + WithItem { + range: 2148..2155, + context_expr: Name( + ExprName { + range: 2149..2154, + id: "item1", + ctx: Load, + }, + ), + optional_vars: None, + }, + WithItem { + range: 2157..2164, + context_expr: Name( + ExprName { + range: 2158..2163, + id: "item2", + ctx: Load, + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 2166..2169, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 2166..2169, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 2170..2210, + is_async: false, + items: [ + WithItem { + range: 2175..2189, + context_expr: Call( + ExprCall { + range: 2176..2188, + func: Name( + ExprName { + range: 2176..2180, + id: "open", + ctx: Load, + }, + ), + arguments: Arguments { + range: 2180..2188, + args: [ + StringLiteral( + ExprStringLiteral { + range: 2181..2187, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 2181..2187, + value: "a.py", + flags: StringLiteralFlags { + quote_style: Single, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + ], + keywords: [], + }, + }, + ), + optional_vars: None, + }, + WithItem { + range: 2191..2205, + context_expr: Call( + ExprCall { + range: 2192..2204, + func: Name( + ExprName { + range: 2192..2196, + id: "open", + ctx: Load, + }, + ), + arguments: Arguments { + range: 2196..2204, + args: [ + StringLiteral( + ExprStringLiteral { + range: 2197..2203, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 2197..2203, + value: "b.py", + flags: StringLiteralFlags { + quote_style: Single, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + ], + keywords: [], + }, + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 2207..2210, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 2207..2210, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 2211..2230, + is_async: false, + items: [ + WithItem { + range: 2216..2225, + context_expr: Yield( + ExprYield { + range: 2217..2224, + value: Some( + Name( + ExprName { + range: 2223..2224, + id: "x", + ctx: Load, + }, + ), + ), + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 2227..2230, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 2227..2230, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 2231..2252, + is_async: false, + items: [ + WithItem { + range: 2237..2246, + context_expr: Yield( + ExprYield { + range: 2238..2245, + value: Some( + Name( + ExprName { + range: 2244..2245, + id: "x", + ctx: Load, + }, + ), + ), + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 2249..2252, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 2249..2252, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 2253..2277, + is_async: false, + items: [ + WithItem { + range: 2258..2272, + context_expr: YieldFrom( + ExprYieldFrom { + range: 2259..2271, + value: Name( + ExprName { + range: 2270..2271, + id: "x", + ctx: Load, + }, + ), + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 2274..2277, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 2274..2277, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 2278..2304, + is_async: false, + items: [ + WithItem { + range: 2284..2298, + context_expr: YieldFrom( + ExprYieldFrom { + range: 2285..2297, + value: Name( + ExprName { + range: 2296..2297, + id: "x", + ctx: Load, + }, + ), + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 2301..2304, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 2301..2304, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 2305..2329, + is_async: false, + items: [ + WithItem { + range: 2310..2324, + context_expr: Yield( + ExprYield { + range: 2311..2318, + value: Some( + Name( + ExprName { + range: 2317..2318, + id: "x", + ctx: Load, + }, + ), + ), + }, + ), + optional_vars: Some( + Name( + ExprName { + range: 2323..2324, + id: "f", + ctx: Store, + }, + ), + ), + }, + ], + body: [ + Expr( + StmtExpr { + range: 2326..2329, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 2326..2329, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 2330..2355, + is_async: false, + items: [ + WithItem { + range: 2335..2350, + context_expr: Yield( + ExprYield { + range: 2336..2344, + value: Some( + Tuple( + ExprTuple { + range: 2342..2344, + elts: [ + Name( + ExprName { + range: 2342..2343, + id: "x", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + ), + }, + ), + optional_vars: Some( + Name( + ExprName { + range: 2349..2350, + id: "f", + ctx: Store, + }, + ), + ), + }, + ], + body: [ + Expr( + StmtExpr { + range: 2352..2355, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 2352..2355, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 2741..2753, + is_async: false, + items: [ + WithItem { + range: 2746..2748, + context_expr: Tuple( + ExprTuple { + range: 2746..2748, + elts: [], + ctx: Load, + parenthesized: true, + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 2750..2753, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 2750..2753, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 2754..2771, + is_async: false, + items: [ + WithItem { + range: 2759..2766, + context_expr: Tuple( + ExprTuple { + range: 2759..2761, + elts: [], + ctx: Load, + parenthesized: true, + }, + ), + optional_vars: Some( + Name( + ExprName { + range: 2765..2766, + id: "f", + ctx: Store, + }, + ), + ), + }, + ], + body: [ + Expr( + StmtExpr { + range: 2768..2771, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 2768..2771, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 2772..2795, + is_async: false, + items: [ + WithItem { + range: 2777..2790, + context_expr: Tuple( + ExprTuple { + range: 2777..2790, + elts: [ + Named( + ExprNamed { + range: 2778..2788, + target: Name( + ExprName { + range: 2778..2782, + id: "item", + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 2786..2788, + value: Int( + 42, + ), + }, + ), + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 2792..2795, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 2792..2795, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 2796..2820, + is_async: false, + items: [ + WithItem { + range: 2801..2815, + context_expr: Tuple( + ExprTuple { + range: 2801..2815, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 2802..2803, + value: Int( + 1, + ), + }, + ), + Named( + ExprNamed { + range: 2805..2814, + target: Name( + ExprName { + range: 2805..2809, + id: "item", + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 2813..2814, + value: Int( + 2, + ), + }, + ), + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 2817..2820, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 2817..2820, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 2821..2851, + is_async: false, + items: [ + WithItem { + range: 2826..2846, + context_expr: Tuple( + ExprTuple { + range: 2826..2846, + elts: [ + Named( + ExprNamed { + range: 2827..2838, + target: Name( + ExprName { + range: 2827..2832, + id: "item1", + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 2836..2838, + value: Int( + 10, + ), + }, + ), + }, + ), + Name( + ExprName { + range: 2840..2845, + id: "item2", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 2848..2851, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 2848..2851, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 2852..2893, + is_async: false, + items: [ + WithItem { + range: 2857..2888, + context_expr: Tuple( + ExprTuple { + range: 2857..2883, + elts: [ + Name( + ExprName { + range: 2858..2863, + id: "item1", + ctx: Load, + }, + ), + Named( + ExprNamed { + range: 2865..2875, + target: Name( + ExprName { + range: 2865..2870, + id: "item2", + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 2874..2875, + value: Int( + 2, + ), + }, + ), + }, + ), + Name( + ExprName { + range: 2877..2882, + id: "item3", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + optional_vars: Some( + Name( + ExprName { + range: 2887..2888, + id: "f", + ctx: Store, + }, + ), + ), + }, + ], + body: [ + Expr( + StmtExpr { + range: 2890..2893, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 2890..2893, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 2894..2916, + is_async: false, + items: [ + WithItem { + range: 2899..2911, + context_expr: Tuple( + ExprTuple { + range: 2899..2906, + elts: [ + Name( + ExprName { + range: 2900..2904, + id: "item", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + optional_vars: Some( + Name( + ExprName { + range: 2910..2911, + id: "f", + ctx: Store, + }, + ), + ), + }, + ], + body: [ + Expr( + StmtExpr { + range: 2913..2916, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 2913..2916, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 2917..2935, + is_async: false, + items: [ + WithItem { + range: 2922..2930, + context_expr: Tuple( + ExprTuple { + range: 2922..2930, + elts: [ + Starred( + ExprStarred { + range: 2923..2928, + value: Name( + ExprName { + range: 2924..2928, + id: "item", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 2932..2935, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 2932..2935, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 2936..2959, + is_async: false, + items: [ + WithItem { + range: 2941..2954, + context_expr: Tuple( + ExprTuple { + range: 2941..2949, + elts: [ + Starred( + ExprStarred { + range: 2942..2947, + value: Name( + ExprName { + range: 2943..2947, + id: "item", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + optional_vars: Some( + Name( + ExprName { + range: 2953..2954, + id: "f", + ctx: Store, + }, + ), + ), + }, + ], + body: [ + Expr( + StmtExpr { + range: 2956..2959, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 2956..2959, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 2960..2989, + is_async: false, + items: [ + WithItem { + range: 2965..2984, + context_expr: Tuple( + ExprTuple { + range: 2965..2979, + elts: [ + Name( + ExprName { + range: 2966..2971, + id: "item1", + ctx: Load, + }, + ), + Name( + ExprName { + range: 2973..2978, + id: "item2", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + optional_vars: Some( + Name( + ExprName { + range: 2983..2984, + id: "f", + ctx: Store, + }, + ), + ), + }, + ], + body: [ + Expr( + StmtExpr { + range: 2986..2989, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 2986..2989, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 2990..3020, + is_async: false, + items: [ + WithItem { + range: 2995..3015, + context_expr: Tuple( + ExprTuple { + range: 2995..3010, + elts: [ + Name( + ExprName { + range: 2996..3001, + id: "item1", + ctx: Load, + }, + ), + Name( + ExprName { + range: 3003..3008, + id: "item2", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + optional_vars: Some( + Name( + ExprName { + range: 3014..3015, + id: "f", + ctx: Store, + }, + ), + ), + }, + ], + body: [ + Expr( + StmtExpr { + range: 3017..3020, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 3017..3020, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 3021..3052, + is_async: false, + items: [ + WithItem { + range: 3026..3040, + context_expr: Tuple( + ExprTuple { + range: 3026..3040, + elts: [ + Name( + ExprName { + range: 3027..3032, + id: "item1", + ctx: Load, + }, + ), + Name( + ExprName { + range: 3034..3039, + id: "item2", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + optional_vars: None, + }, + WithItem { + range: 3042..3047, + context_expr: Name( + ExprName { + range: 3042..3047, + id: "item3", + ctx: Load, + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 3049..3052, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 3049..3052, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 3053..3091, + is_async: false, + items: [ + WithItem { + range: 3058..3086, + context_expr: Tuple( + ExprTuple { + range: 3058..3081, + elts: [ + Tuple( + ExprTuple { + range: 3059..3073, + elts: [ + Name( + ExprName { + range: 3060..3065, + id: "item1", + ctx: Load, + }, + ), + Name( + ExprName { + range: 3067..3072, + id: "item2", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + Name( + ExprName { + range: 3075..3080, + id: "item3", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + optional_vars: Some( + Name( + ExprName { + range: 3085..3086, + id: "f", + ctx: Store, + }, + ), + ), + }, + ], + body: [ + Expr( + StmtExpr { + range: 3088..3091, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 3088..3091, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 3092..3138, + is_async: false, + items: [ + WithItem { + range: 3097..3105, + context_expr: Tuple( + ExprTuple { + range: 3097..3105, + elts: [ + Name( + ExprName { + range: 3098..3103, + id: "item1", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + optional_vars: None, + }, + WithItem { + range: 3107..3112, + context_expr: Name( + ExprName { + range: 3107..3112, + id: "item2", + ctx: Load, + }, + ), + optional_vars: None, + }, + WithItem { + range: 3114..3133, + context_expr: Tuple( + ExprTuple { + range: 3114..3128, + elts: [ + Name( + ExprName { + range: 3115..3120, + id: "item3", + ctx: Load, + }, + ), + Name( + ExprName { + range: 3122..3127, + id: "item4", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + optional_vars: Some( + Name( + ExprName { + range: 3132..3133, + id: "f", + ctx: Store, + }, + ), + ), + }, + ], + body: [ + Expr( + StmtExpr { + range: 3135..3138, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 3135..3138, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 3139..3182, + is_async: false, + items: [ + WithItem { + range: 3144..3164, + context_expr: Tuple( + ExprTuple { + range: 3144..3158, + elts: [ + Name( + ExprName { + range: 3145..3150, + id: "item1", + ctx: Load, + }, + ), + Name( + ExprName { + range: 3152..3157, + id: "item2", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + optional_vars: Some( + Name( + ExprName { + range: 3162..3164, + id: "f1", + ctx: Store, + }, + ), + ), + }, + WithItem { + range: 3166..3177, + context_expr: Name( + ExprName { + range: 3166..3171, + id: "item3", + ctx: Load, + }, + ), + optional_vars: Some( + Name( + ExprName { + range: 3175..3177, + id: "f2", + ctx: Store, + }, + ), + ), + }, + ], + body: [ + Expr( + StmtExpr { + range: 3179..3182, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 3179..3182, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 3183..3208, + is_async: false, + items: [ + WithItem { + range: 3188..3203, + context_expr: Tuple( + ExprTuple { + range: 3188..3203, + elts: [ + Name( + ExprName { + range: 3189..3194, + id: "item1", + ctx: Load, + }, + ), + Starred( + ExprStarred { + range: 3196..3202, + value: Name( + ExprName { + range: 3197..3202, + id: "item2", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 3205..3208, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 3205..3208, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 3209..3239, + is_async: false, + items: [ + WithItem { + range: 3214..3234, + context_expr: Tuple( + ExprTuple { + range: 3214..3229, + elts: [ + Name( + ExprName { + range: 3215..3220, + id: "item1", + ctx: Load, + }, + ), + Starred( + ExprStarred { + range: 3222..3228, + value: Name( + ExprName { + range: 3223..3228, + id: "item2", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + optional_vars: Some( + Name( + ExprName { + range: 3233..3234, + id: "f", + ctx: Store, + }, + ), + ), + }, + ], + body: [ + Expr( + StmtExpr { + range: 3236..3239, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 3236..3239, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 3240..3271, + is_async: false, + items: [ + WithItem { + range: 3245..3266, + context_expr: Tuple( + ExprTuple { + range: 3245..3266, + elts: [ + Named( + ExprNamed { + range: 3246..3257, + target: Name( + ExprName { + range: 3246..3251, + id: "item1", + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 3255..3257, + value: Int( + 10, + ), + }, + ), + }, + ), + Starred( + ExprStarred { + range: 3259..3265, + value: Name( + ExprName { + range: 3260..3265, + id: "item2", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 3268..3271, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 3268..3271, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 3272..3305, + is_async: false, + items: [ + WithItem { + range: 3277..3300, + context_expr: Tuple( + ExprTuple { + range: 3277..3300, + elts: [ + Named( + ExprNamed { + range: 3279..3290, + target: Name( + ExprName { + range: 3279..3284, + id: "item1", + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 3288..3290, + value: Int( + 10, + ), + }, + ), + }, + ), + Starred( + ExprStarred { + range: 3293..3299, + value: Name( + ExprName { + range: 3294..3299, + id: "item2", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 3302..3305, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 3302..3305, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 3510..3542, + is_async: false, + items: [ + WithItem { + range: 3515..3537, + context_expr: Generator( + ExprGenerator { + range: 3515..3537, + elt: Name( + ExprName { + range: 3516..3517, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 3518..3536, + target: Name( + ExprName { + range: 3522..3523, + id: "x", + ctx: Store, + }, + ), + iter: Call( + ExprCall { + range: 3527..3536, + func: Name( + ExprName { + range: 3527..3532, + id: "range", + ctx: Load, + }, + ), + arguments: Arguments { + range: 3532..3536, + args: [ + NumberLiteral( + ExprNumberLiteral { + range: 3533..3535, + value: Int( + 10, + ), + }, + ), + ], + keywords: [], + }, + }, + ), + ifs: [], + is_async: false, + }, + ], + parenthesized: true, + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 3539..3542, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 3539..3542, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 3543..3581, + is_async: false, + items: [ + WithItem { + range: 3548..3576, + context_expr: Generator( + ExprGenerator { + range: 3548..3576, + elt: Name( + ExprName { + range: 3549..3550, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 3551..3575, + target: Name( + ExprName { + range: 3561..3562, + id: "x", + ctx: Store, + }, + ), + iter: Call( + ExprCall { + range: 3566..3575, + func: Name( + ExprName { + range: 3566..3571, + id: "range", + ctx: Load, + }, + ), + arguments: Arguments { + range: 3571..3575, + args: [ + NumberLiteral( + ExprNumberLiteral { + range: 3572..3574, + value: Int( + 10, + ), + }, + ), + ], + keywords: [], + }, + }, + ), + ifs: [], + is_async: true, + }, + ], + parenthesized: true, + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 3578..3581, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 3578..3581, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 3582..3620, + is_async: false, + items: [ + WithItem { + range: 3587..3609, + context_expr: Generator( + ExprGenerator { + range: 3587..3609, + elt: Name( + ExprName { + range: 3588..3589, + id: "x", + ctx: Load, + }, + ), + generators: [ + Comprehension { + range: 3590..3608, + target: Name( + ExprName { + range: 3594..3595, + id: "x", + ctx: Store, + }, + ), + iter: Call( + ExprCall { + range: 3599..3608, + func: Name( + ExprName { + range: 3599..3604, + id: "range", + ctx: Load, + }, + ), + arguments: Arguments { + range: 3604..3608, + args: [ + NumberLiteral( + ExprNumberLiteral { + range: 3605..3607, + value: Int( + 10, + ), + }, + ), + ], + keywords: [], + }, + }, + ), + ifs: [], + is_async: false, + }, + ], + parenthesized: true, + }, + ), + optional_vars: None, + }, + WithItem { + range: 3611..3615, + context_expr: Name( + ExprName { + range: 3611..3615, + id: "item", + ctx: Load, + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 3617..3620, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 3617..3620, + }, + ), + }, + ), + ], + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__annotated_assignment.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__annotated_assignment.py.snap.new new file mode 100644 index 0000000000..3bc30454c3 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__annotated_assignment.py.snap.new @@ -0,0 +1,274 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/valid/statement/annotated_assignment.py +--- +## AST + +``` +Module( + ModModule { + range: 0..103, + body: [ + AnnAssign( + StmtAnnAssign { + range: 0..6, + target: Name( + ExprName { + range: 0..1, + id: "x", + ctx: Store, + }, + ), + annotation: Name( + ExprName { + range: 3..6, + id: "int", + ctx: Load, + }, + ), + value: None, + simple: true, + }, + ), + AnnAssign( + StmtAnnAssign { + range: 7..17, + target: Name( + ExprName { + range: 7..8, + id: "x", + ctx: Store, + }, + ), + annotation: Name( + ExprName { + range: 10..13, + id: "int", + ctx: Load, + }, + ), + value: Some( + NumberLiteral( + ExprNumberLiteral { + range: 16..17, + value: Int( + 1, + ), + }, + ), + ), + simple: true, + }, + ), + AnnAssign( + StmtAnnAssign { + range: 18..28, + target: Name( + ExprName { + range: 19..20, + id: "x", + ctx: Store, + }, + ), + annotation: BinOp( + ExprBinOp { + range: 23..28, + left: NumberLiteral( + ExprNumberLiteral { + range: 23..24, + value: Int( + 1, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 27..28, + value: Int( + 2, + ), + }, + ), + }, + ), + value: None, + simple: false, + }, + ), + AnnAssign( + StmtAnnAssign { + range: 29..55, + target: Name( + ExprName { + range: 29..30, + id: "x", + ctx: Store, + }, + ), + annotation: BinOp( + ExprBinOp { + range: 32..48, + left: Subscript( + ExprSubscript { + range: 32..42, + value: Name( + ExprName { + range: 32..37, + id: "tuple", + ctx: Load, + }, + ), + slice: Name( + ExprName { + range: 38..41, + id: "int", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + op: BitOr, + right: Name( + ExprName { + range: 45..48, + id: "int", + ctx: Load, + }, + ), + }, + ), + value: Some( + Tuple( + ExprTuple { + range: 51..55, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 52..53, + value: Int( + 1, + ), + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + ), + simple: true, + }, + ), + AnnAssign( + StmtAnnAssign { + range: 56..83, + target: Name( + ExprName { + range: 56..57, + id: "x", + ctx: Store, + }, + ), + annotation: If( + ExprIf { + range: 59..79, + test: BooleanLiteral( + ExprBooleanLiteral { + range: 66..70, + value: true, + }, + ), + body: Name( + ExprName { + range: 59..62, + id: "int", + ctx: Load, + }, + ), + orelse: Name( + ExprName { + range: 76..79, + id: "str", + ctx: Load, + }, + ), + }, + ), + value: Some( + NumberLiteral( + ExprNumberLiteral { + range: 82..83, + value: Int( + 1, + ), + }, + ), + ), + simple: true, + }, + ), + AnnAssign( + StmtAnnAssign { + range: 84..102, + target: Name( + ExprName { + range: 84..85, + id: "x", + ctx: Store, + }, + ), + annotation: Lambda( + ExprLambda { + range: 87..98, + parameters: Some( + Parameters { + range: 94..95, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 94..95, + parameter: Parameter { + range: 94..95, + name: Identifier { + id: "x", + range: 94..95, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: Name( + ExprName { + range: 97..98, + id: "y", + ctx: Load, + }, + ), + }, + ), + value: Some( + NumberLiteral( + ExprNumberLiteral { + range: 101..102, + value: Int( + 1, + ), + }, + ), + ), + simple: true, + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__assert.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__assert.py.snap.new new file mode 100644 index 0000000000..6c870ff093 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__assert.py.snap.new @@ -0,0 +1,335 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/valid/statement/assert.py +--- +## AST + +``` +Module( + ModModule { + range: 0..186, + body: [ + Assert( + StmtAssert { + range: 0..12, + test: Compare( + ExprCompare { + range: 7..12, + left: NumberLiteral( + ExprNumberLiteral { + range: 7..8, + value: Int( + 1, + ), + }, + ), + ops: [ + Lt, + ], + comparators: [ + NumberLiteral( + ExprNumberLiteral { + range: 11..12, + value: Int( + 2, + ), + }, + ), + ], + }, + ), + msg: None, + }, + ), + Assert( + StmtAssert { + range: 13..26, + test: Call( + ExprCall { + range: 20..26, + func: Name( + ExprName { + range: 20..24, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 24..26, + args: [], + keywords: [], + }, + }, + ), + msg: None, + }, + ), + Assert( + StmtAssert { + range: 27..41, + test: BoolOp( + ExprBoolOp { + range: 34..41, + op: And, + values: [ + Name( + ExprName { + range: 34..35, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 40..41, + id: "b", + ctx: Load, + }, + ), + ], + }, + ), + msg: None, + }, + ), + Assert( + StmtAssert { + range: 42..60, + test: Lambda( + ExprLambda { + range: 49..60, + parameters: Some( + Parameters { + range: 56..57, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 56..57, + parameter: Parameter { + range: 56..57, + name: Identifier { + id: "x", + range: 56..57, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: Name( + ExprName { + range: 59..60, + id: "y", + ctx: Load, + }, + ), + }, + ), + msg: None, + }, + ), + Assert( + StmtAssert { + range: 61..75, + test: Await( + ExprAwait { + range: 68..75, + value: Name( + ExprName { + range: 74..75, + id: "x", + ctx: Load, + }, + ), + }, + ), + msg: None, + }, + ), + Assert( + StmtAssert { + range: 76..99, + test: If( + ExprIf { + range: 83..99, + test: BooleanLiteral( + ExprBooleanLiteral { + range: 88..92, + value: true, + }, + ), + body: Name( + ExprName { + range: 83..84, + id: "x", + ctx: Load, + }, + ), + orelse: Name( + ExprName { + range: 98..99, + id: "y", + ctx: Load, + }, + ), + }, + ), + msg: None, + }, + ), + Assert( + StmtAssert { + range: 101..118, + test: Name( + ExprName { + range: 108..109, + id: "x", + ctx: Load, + }, + ), + msg: Some( + StringLiteral( + ExprStringLiteral { + range: 111..118, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 111..118, + value: "error", + flags: StringLiteralFlags { + quote_style: Double, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + ), + }, + ), + Assert( + StmtAssert { + range: 119..140, + test: Name( + ExprName { + range: 126..127, + id: "x", + ctx: Load, + }, + ), + msg: Some( + Lambda( + ExprLambda { + range: 129..140, + parameters: Some( + Parameters { + range: 136..137, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 136..137, + parameter: Parameter { + range: 136..137, + name: Identifier { + id: "x", + range: 136..137, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: Name( + ExprName { + range: 139..140, + id: "y", + ctx: Load, + }, + ), + }, + ), + ), + }, + ), + Assert( + StmtAssert { + range: 141..158, + test: Name( + ExprName { + range: 148..149, + id: "x", + ctx: Load, + }, + ), + msg: Some( + Await( + ExprAwait { + range: 151..158, + value: Name( + ExprName { + range: 157..158, + id: "x", + ctx: Load, + }, + ), + }, + ), + ), + }, + ), + Assert( + StmtAssert { + range: 159..185, + test: Name( + ExprName { + range: 166..167, + id: "x", + ctx: Load, + }, + ), + msg: Some( + If( + ExprIf { + range: 169..185, + test: BooleanLiteral( + ExprBooleanLiteral { + range: 174..178, + value: true, + }, + ), + body: Name( + ExprName { + range: 169..170, + id: "x", + ctx: Load, + }, + ), + orelse: Name( + ExprName { + range: 184..185, + id: "y", + ctx: Load, + }, + ), + }, + ), + ), + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__assignment.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__assignment.py.snap.new new file mode 100644 index 0000000000..f03fb86219 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__assignment.py.snap.new @@ -0,0 +1,955 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/valid/statement/assignment.py +--- +## AST + +``` +Module( + ModModule { + range: 0..734, + body: [ + Assign( + StmtAssign { + range: 0..13, + targets: [ + Name( + ExprName { + range: 0..1, + id: "x", + ctx: Store, + }, + ), + ], + value: Tuple( + ExprTuple { + range: 4..13, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 5..6, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 8..9, + value: Int( + 2, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 11..12, + value: Int( + 3, + ), + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + Assign( + StmtAssign { + range: 15..33, + targets: [ + Tuple( + ExprTuple { + range: 15..21, + elts: [ + Name( + ExprName { + range: 16..17, + id: "x", + ctx: Store, + }, + ), + Name( + ExprName { + range: 19..20, + id: "y", + ctx: Store, + }, + ), + ], + ctx: Store, + parenthesized: true, + }, + ), + ], + value: Tuple( + ExprTuple { + range: 24..33, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 25..26, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 28..29, + value: Int( + 2, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 31..32, + value: Int( + 3, + ), + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + Assign( + StmtAssign { + range: 35..53, + targets: [ + List( + ExprList { + range: 35..41, + elts: [ + Name( + ExprName { + range: 36..37, + id: "x", + ctx: Store, + }, + ), + Name( + ExprName { + range: 39..40, + id: "y", + ctx: Store, + }, + ), + ], + ctx: Store, + }, + ), + ], + value: Tuple( + ExprTuple { + range: 44..53, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 45..46, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 48..49, + value: Int( + 2, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 51..52, + value: Int( + 3, + ), + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + Assign( + StmtAssign { + range: 55..70, + targets: [ + Attribute( + ExprAttribute { + range: 55..58, + value: Name( + ExprName { + range: 55..56, + id: "x", + ctx: Load, + }, + ), + attr: Identifier { + id: "y", + range: 57..58, + }, + ctx: Store, + }, + ), + ], + value: Tuple( + ExprTuple { + range: 61..70, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 62..63, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 65..66, + value: Int( + 2, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 68..69, + value: Int( + 3, + ), + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + Assign( + StmtAssign { + range: 72..88, + targets: [ + Subscript( + ExprSubscript { + range: 72..76, + value: Name( + ExprName { + range: 72..73, + id: "x", + ctx: Load, + }, + ), + slice: Name( + ExprName { + range: 74..75, + id: "y", + ctx: Load, + }, + ), + ctx: Store, + }, + ), + ], + value: Tuple( + ExprTuple { + range: 79..88, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 80..81, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 83..84, + value: Int( + 2, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 86..87, + value: Int( + 3, + ), + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + Assign( + StmtAssign { + range: 90..109, + targets: [ + Tuple( + ExprTuple { + range: 90..97, + elts: [ + Name( + ExprName { + range: 91..92, + id: "x", + ctx: Store, + }, + ), + Starred( + ExprStarred { + range: 94..96, + value: Name( + ExprName { + range: 95..96, + id: "y", + ctx: Store, + }, + ), + ctx: Store, + }, + ), + ], + ctx: Store, + parenthesized: true, + }, + ), + ], + value: Tuple( + ExprTuple { + range: 100..109, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 101..102, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 104..105, + value: Int( + 2, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 107..108, + value: Int( + 3, + ), + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + Assign( + StmtAssign { + range: 259..268, + targets: [ + Starred( + ExprStarred { + range: 259..263, + value: Name( + ExprName { + range: 260..263, + id: "foo", + ctx: Store, + }, + ), + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 266..268, + value: Int( + 42, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 270..291, + targets: [ + List( + ExprList { + range: 270..279, + elts: [ + Name( + ExprName { + range: 271..272, + id: "x", + ctx: Store, + }, + ), + Name( + ExprName { + range: 274..275, + id: "y", + ctx: Store, + }, + ), + Name( + ExprName { + range: 277..278, + id: "z", + ctx: Store, + }, + ), + ], + ctx: Store, + }, + ), + ], + value: List( + ExprList { + range: 282..291, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 283..284, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 286..287, + value: Int( + 2, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 289..290, + value: Int( + 3, + ), + }, + ), + ], + ctx: Load, + }, + ), + }, + ), + Assign( + StmtAssign { + range: 293..314, + targets: [ + Tuple( + ExprTuple { + range: 293..302, + elts: [ + Name( + ExprName { + range: 294..295, + id: "x", + ctx: Store, + }, + ), + Name( + ExprName { + range: 297..298, + id: "y", + ctx: Store, + }, + ), + Name( + ExprName { + range: 300..301, + id: "z", + ctx: Store, + }, + ), + ], + ctx: Store, + parenthesized: true, + }, + ), + ], + value: Tuple( + ExprTuple { + range: 305..314, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 306..307, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 309..310, + value: Int( + 2, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 312..313, + value: Int( + 3, + ), + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + Assign( + StmtAssign { + range: 315..324, + targets: [ + Subscript( + ExprSubscript { + range: 315..319, + value: Name( + ExprName { + range: 315..316, + id: "x", + ctx: Load, + }, + ), + slice: NumberLiteral( + ExprNumberLiteral { + range: 317..318, + value: Int( + 0, + ), + }, + ), + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 322..324, + value: Int( + 42, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 421..430, + targets: [ + Subscript( + ExprSubscript { + range: 421..425, + value: NumberLiteral( + ExprNumberLiteral { + range: 421..422, + value: Int( + 5, + ), + }, + ), + slice: NumberLiteral( + ExprNumberLiteral { + range: 423..424, + value: Int( + 0, + ), + }, + ), + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 428..430, + value: Int( + 42, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 431..444, + targets: [ + Subscript( + ExprSubscript { + range: 431..437, + value: Name( + ExprName { + range: 431..432, + id: "x", + ctx: Load, + }, + ), + slice: Slice( + ExprSlice { + range: 433..436, + lower: Some( + NumberLiteral( + ExprNumberLiteral { + range: 433..434, + value: Int( + 1, + ), + }, + ), + ), + upper: Some( + NumberLiteral( + ExprNumberLiteral { + range: 435..436, + value: Int( + 2, + ), + }, + ), + ), + step: None, + }, + ), + ctx: Store, + }, + ), + ], + value: List( + ExprList { + range: 440..444, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 441..443, + value: Int( + 42, + ), + }, + ), + ], + ctx: Load, + }, + ), + }, + ), + Assign( + StmtAssign { + range: 540..553, + targets: [ + Subscript( + ExprSubscript { + range: 540..546, + value: NumberLiteral( + ExprNumberLiteral { + range: 540..541, + value: Int( + 5, + ), + }, + ), + slice: Slice( + ExprSlice { + range: 542..545, + lower: Some( + NumberLiteral( + ExprNumberLiteral { + range: 542..543, + value: Int( + 1, + ), + }, + ), + ), + upper: Some( + NumberLiteral( + ExprNumberLiteral { + range: 544..545, + value: Int( + 2, + ), + }, + ), + ), + step: None, + }, + ), + ctx: Store, + }, + ), + ], + value: List( + ExprList { + range: 549..553, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 550..552, + value: Int( + 42, + ), + }, + ), + ], + ctx: Load, + }, + ), + }, + ), + Assign( + StmtAssign { + range: 555..567, + targets: [ + Attribute( + ExprAttribute { + range: 555..562, + value: Name( + ExprName { + range: 555..558, + id: "foo", + ctx: Load, + }, + ), + attr: Identifier { + id: "bar", + range: 559..562, + }, + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 565..567, + value: Int( + 42, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 669..681, + targets: [ + Attribute( + ExprAttribute { + range: 669..676, + value: StringLiteral( + ExprStringLiteral { + range: 669..674, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 669..674, + value: "foo", + flags: StringLiteralFlags { + quote_style: Double, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + attr: Identifier { + id: "y", + range: 675..676, + }, + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 679..681, + value: Int( + 42, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 683..691, + targets: [ + Name( + ExprName { + range: 683..686, + id: "foo", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 689..691, + value: Int( + 42, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 693..703, + targets: [ + List( + ExprList { + range: 693..695, + elts: [], + ctx: Store, + }, + ), + ], + value: Starred( + ExprStarred { + range: 698..703, + value: Name( + ExprName { + range: 699..703, + id: "data", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + }, + ), + Assign( + StmtAssign { + range: 704..714, + targets: [ + Tuple( + ExprTuple { + range: 704..706, + elts: [], + ctx: Store, + parenthesized: true, + }, + ), + ], + value: Starred( + ExprStarred { + range: 709..714, + value: Name( + ExprName { + range: 710..714, + id: "data", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + }, + ), + Assign( + StmtAssign { + range: 715..724, + targets: [ + Tuple( + ExprTuple { + range: 715..719, + elts: [ + Name( + ExprName { + range: 715..716, + id: "a", + ctx: Store, + }, + ), + Name( + ExprName { + range: 718..719, + id: "b", + ctx: Store, + }, + ), + ], + ctx: Store, + parenthesized: false, + }, + ), + ], + value: Name( + ExprName { + range: 722..724, + id: "ab", + ctx: Load, + }, + ), + }, + ), + Assign( + StmtAssign { + range: 725..734, + targets: [ + Name( + ExprName { + range: 725..726, + id: "a", + ctx: Store, + }, + ), + Name( + ExprName { + range: 729..730, + id: "b", + ctx: Store, + }, + ), + ], + value: Name( + ExprName { + range: 733..734, + id: "c", + ctx: Load, + }, + ), + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__augmented_assignment.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__augmented_assignment.py.snap.new new file mode 100644 index 0000000000..7f1a12b114 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__augmented_assignment.py.snap.new @@ -0,0 +1,485 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/valid/statement/augmented_assignment.py +--- +## AST + +``` +Module( + ModModule { + range: 0..212, + body: [ + AugAssign( + StmtAugAssign { + range: 0..6, + target: Name( + ExprName { + range: 0..1, + id: "x", + ctx: Store, + }, + ), + op: Add, + value: NumberLiteral( + ExprNumberLiteral { + range: 5..6, + value: Int( + 1, + ), + }, + ), + }, + ), + AugAssign( + StmtAugAssign { + range: 7..23, + target: Attribute( + ExprAttribute { + range: 7..10, + value: Name( + ExprName { + range: 7..8, + id: "x", + ctx: Load, + }, + ), + attr: Identifier { + id: "y", + range: 9..10, + }, + ctx: Store, + }, + ), + op: Add, + value: Tuple( + ExprTuple { + range: 14..23, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 15..16, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 18..19, + value: Int( + 2, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 21..22, + value: Int( + 3, + ), + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + AugAssign( + StmtAugAssign { + range: 24..41, + target: Subscript( + ExprSubscript { + range: 24..28, + value: Name( + ExprName { + range: 24..25, + id: "x", + ctx: Load, + }, + ), + slice: Name( + ExprName { + range: 26..27, + id: "y", + ctx: Load, + }, + ), + ctx: Store, + }, + ), + op: Add, + value: Tuple( + ExprTuple { + range: 32..41, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 33..34, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 36..37, + value: Int( + 2, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 39..40, + value: Int( + 3, + ), + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + AugAssign( + StmtAugAssign { + range: 86..92, + target: Name( + ExprName { + range: 86..87, + id: "x", + ctx: Store, + }, + ), + op: Add, + value: NumberLiteral( + ExprNumberLiteral { + range: 91..92, + value: Int( + 1, + ), + }, + ), + }, + ), + AugAssign( + StmtAugAssign { + range: 93..99, + target: Name( + ExprName { + range: 93..94, + id: "x", + ctx: Store, + }, + ), + op: Sub, + value: NumberLiteral( + ExprNumberLiteral { + range: 98..99, + value: Int( + 1, + ), + }, + ), + }, + ), + AugAssign( + StmtAugAssign { + range: 100..106, + target: Name( + ExprName { + range: 100..101, + id: "x", + ctx: Store, + }, + ), + op: Mult, + value: NumberLiteral( + ExprNumberLiteral { + range: 105..106, + value: Int( + 1, + ), + }, + ), + }, + ), + AugAssign( + StmtAugAssign { + range: 107..113, + target: Name( + ExprName { + range: 107..108, + id: "x", + ctx: Store, + }, + ), + op: Div, + value: NumberLiteral( + ExprNumberLiteral { + range: 112..113, + value: Int( + 1, + ), + }, + ), + }, + ), + AugAssign( + StmtAugAssign { + range: 114..121, + target: Name( + ExprName { + range: 114..115, + id: "x", + ctx: Store, + }, + ), + op: FloorDiv, + value: NumberLiteral( + ExprNumberLiteral { + range: 120..121, + value: Int( + 1, + ), + }, + ), + }, + ), + AugAssign( + StmtAugAssign { + range: 122..128, + target: Name( + ExprName { + range: 122..123, + id: "x", + ctx: Store, + }, + ), + op: Mod, + value: NumberLiteral( + ExprNumberLiteral { + range: 127..128, + value: Int( + 1, + ), + }, + ), + }, + ), + AugAssign( + StmtAugAssign { + range: 129..136, + target: Name( + ExprName { + range: 129..130, + id: "x", + ctx: Store, + }, + ), + op: Pow, + value: NumberLiteral( + ExprNumberLiteral { + range: 135..136, + value: Int( + 1, + ), + }, + ), + }, + ), + AugAssign( + StmtAugAssign { + range: 137..143, + target: Name( + ExprName { + range: 137..138, + id: "x", + ctx: Store, + }, + ), + op: BitAnd, + value: NumberLiteral( + ExprNumberLiteral { + range: 142..143, + value: Int( + 1, + ), + }, + ), + }, + ), + AugAssign( + StmtAugAssign { + range: 144..150, + target: Name( + ExprName { + range: 144..145, + id: "x", + ctx: Store, + }, + ), + op: BitOr, + value: NumberLiteral( + ExprNumberLiteral { + range: 149..150, + value: Int( + 1, + ), + }, + ), + }, + ), + AugAssign( + StmtAugAssign { + range: 151..157, + target: Name( + ExprName { + range: 151..152, + id: "x", + ctx: Store, + }, + ), + op: BitXor, + value: NumberLiteral( + ExprNumberLiteral { + range: 156..157, + value: Int( + 1, + ), + }, + ), + }, + ), + AugAssign( + StmtAugAssign { + range: 158..165, + target: Name( + ExprName { + range: 158..159, + id: "x", + ctx: Store, + }, + ), + op: LShift, + value: NumberLiteral( + ExprNumberLiteral { + range: 164..165, + value: Int( + 1, + ), + }, + ), + }, + ), + AugAssign( + StmtAugAssign { + range: 166..173, + target: Name( + ExprName { + range: 166..167, + id: "x", + ctx: Store, + }, + ), + op: RShift, + value: NumberLiteral( + ExprNumberLiteral { + range: 172..173, + value: Int( + 1, + ), + }, + ), + }, + ), + AugAssign( + StmtAugAssign { + range: 174..180, + target: Name( + ExprName { + range: 174..175, + id: "x", + ctx: Store, + }, + ), + op: MatMult, + value: NumberLiteral( + ExprNumberLiteral { + range: 179..180, + value: Int( + 1, + ), + }, + ), + }, + ), + AugAssign( + StmtAugAssign { + range: 190..212, + target: Name( + ExprName { + range: 190..191, + id: "a", + ctx: Store, + }, + ), + op: FloorDiv, + value: BinOp( + ExprBinOp { + range: 196..212, + left: BinOp( + ExprBinOp { + range: 197..202, + left: Name( + ExprName { + range: 197..198, + id: "a", + ctx: Load, + }, + ), + op: Add, + right: Name( + ExprName { + range: 201..202, + id: "b", + ctx: Load, + }, + ), + }, + ), + op: Sub, + right: BinOp( + ExprBinOp { + range: 206..212, + left: Name( + ExprName { + range: 206..207, + id: "c", + ctx: Load, + }, + ), + op: Pow, + right: NumberLiteral( + ExprNumberLiteral { + range: 211..212, + value: Int( + 2, + ), + }, + ), + }, + ), + }, + ), + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__class.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__class.py.snap.new new file mode 100644 index 0000000000..8e6242ae81 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__class.py.snap.new @@ -0,0 +1,1243 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/valid/statement/class.py +--- +## AST + +``` +Module( + ModModule { + range: 0..1023, + body: [ + ClassDef( + StmtClassDef { + range: 0..19, + decorator_list: [], + name: Identifier { + id: "Test", + range: 6..10, + }, + type_params: None, + arguments: None, + body: [ + Expr( + StmtExpr { + range: 16..19, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 16..19, + }, + ), + }, + ), + ], + }, + ), + ClassDef( + StmtClassDef { + range: 22..80, + decorator_list: [], + name: Identifier { + id: "Test", + range: 28..32, + }, + type_params: None, + arguments: Some( + Arguments { + range: 32..34, + args: [], + keywords: [], + }, + ), + body: [ + FunctionDef( + StmtFunctionDef { + range: 44..80, + is_async: false, + decorator_list: [], + name: Identifier { + id: "__init__", + range: 48..56, + }, + type_params: None, + parameters: Parameters { + range: 56..62, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 57..61, + parameter: Parameter { + range: 57..61, + name: Identifier { + id: "self", + range: 57..61, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 76..80, + }, + ), + ], + }, + ), + ], + }, + ), + ClassDef( + StmtClassDef { + range: 83..116, + decorator_list: [], + name: Identifier { + id: "Test", + range: 89..93, + }, + type_params: None, + arguments: Some( + Arguments { + range: 93..107, + args: [ + Starred( + ExprStarred { + range: 99..101, + value: Name( + ExprName { + range: 100..101, + id: "A", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ], + keywords: [ + Keyword { + range: 94..97, + arg: Some( + Identifier { + id: "a", + range: 94..95, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 96..97, + value: Int( + 1, + ), + }, + ), + }, + Keyword { + range: 103..106, + arg: None, + value: Name( + ExprName { + range: 105..106, + id: "k", + ctx: Load, + }, + ), + }, + ], + }, + ), + body: [ + Expr( + StmtExpr { + range: 113..116, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 113..116, + }, + ), + }, + ), + ], + }, + ), + ClassDef( + StmtClassDef { + range: 119..168, + decorator_list: [], + name: Identifier { + id: "Test", + range: 125..129, + }, + type_params: None, + arguments: None, + body: [ + FunctionDef( + StmtFunctionDef { + range: 135..168, + is_async: false, + decorator_list: [], + name: Identifier { + id: "method", + range: 139..145, + }, + type_params: None, + parameters: Parameters { + range: 145..147, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Assign( + StmtAssign { + range: 157..168, + targets: [ + Tuple( + ExprTuple { + range: 157..161, + elts: [ + Name( + ExprName { + range: 157..158, + id: "a", + ctx: Store, + }, + ), + Name( + ExprName { + range: 160..161, + id: "b", + ctx: Store, + }, + ), + ], + ctx: Store, + parenthesized: false, + }, + ), + ], + value: Name( + ExprName { + range: 164..168, + id: "data", + ctx: Load, + }, + ), + }, + ), + ], + }, + ), + ], + }, + ), + ClassDef( + StmtClassDef { + range: 171..289, + decorator_list: [], + name: Identifier { + id: "Test", + range: 177..181, + }, + type_params: None, + arguments: Some( + Arguments { + range: 181..187, + args: [ + Name( + ExprName { + range: 182..183, + id: "A", + ctx: Load, + }, + ), + Name( + ExprName { + range: 185..186, + id: "B", + ctx: Load, + }, + ), + ], + keywords: [], + }, + ), + body: [ + FunctionDef( + StmtFunctionDef { + range: 193..225, + is_async: false, + decorator_list: [], + name: Identifier { + id: "__init__", + range: 197..205, + }, + type_params: None, + parameters: Parameters { + range: 205..211, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 206..210, + parameter: Parameter { + range: 206..210, + name: Identifier { + id: "self", + range: 206..210, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 221..225, + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 231..289, + is_async: false, + decorator_list: [], + name: Identifier { + id: "method_with_default", + range: 235..254, + }, + type_params: None, + parameters: Parameters { + range: 254..275, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 255..259, + parameter: Parameter { + range: 255..259, + name: Identifier { + id: "self", + range: 255..259, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 261..274, + parameter: Parameter { + range: 261..264, + name: Identifier { + id: "arg", + range: 261..264, + }, + annotation: None, + }, + default: Some( + StringLiteral( + ExprStringLiteral { + range: 265..274, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 265..274, + value: "default", + flags: StringLiteralFlags { + quote_style: Single, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + ), + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 285..289, + }, + ), + ], + }, + ), + ], + }, + ), + ClassDef( + StmtClassDef { + range: 331..351, + decorator_list: [], + name: Identifier { + id: "Test", + range: 337..341, + }, + type_params: Some( + TypeParams { + range: 341..344, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 342..343, + name: Identifier { + id: "T", + range: 342..343, + }, + bound: None, + default: None, + }, + ), + ], + }, + ), + arguments: Some( + Arguments { + range: 344..346, + args: [], + keywords: [], + }, + ), + body: [ + Expr( + StmtExpr { + range: 348..351, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 348..351, + }, + ), + }, + ), + ], + }, + ), + ClassDef( + StmtClassDef { + range: 376..402, + decorator_list: [], + name: Identifier { + id: "Test", + range: 382..386, + }, + type_params: Some( + TypeParams { + range: 386..395, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 387..394, + name: Identifier { + id: "T", + range: 387..388, + }, + bound: None, + default: Some( + Name( + ExprName { + range: 391..394, + id: "str", + ctx: Load, + }, + ), + ), + }, + ), + ], + }, + ), + arguments: Some( + Arguments { + range: 395..397, + args: [], + keywords: [], + }, + ), + body: [ + Expr( + StmtExpr { + range: 399..402, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 399..402, + }, + ), + }, + ), + ], + }, + ), + ClassDef( + StmtClassDef { + range: 425..450, + decorator_list: [], + name: Identifier { + id: "Test", + range: 431..435, + }, + type_params: Some( + TypeParams { + range: 435..443, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 436..442, + name: Identifier { + id: "T", + range: 436..437, + }, + bound: Some( + Name( + ExprName { + range: 439..442, + id: "str", + ctx: Load, + }, + ), + ), + default: None, + }, + ), + ], + }, + ), + arguments: Some( + Arguments { + range: 443..445, + args: [], + keywords: [], + }, + ), + body: [ + Expr( + StmtExpr { + range: 447..450, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 447..450, + }, + ), + }, + ), + ], + }, + ), + ClassDef( + StmtClassDef { + range: 485..522, + decorator_list: [], + name: Identifier { + id: "Test", + range: 491..495, + }, + type_params: Some( + TypeParams { + range: 495..515, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 496..514, + name: Identifier { + id: "T", + range: 496..497, + }, + bound: Some( + BinOp( + ExprBinOp { + range: 499..508, + left: Name( + ExprName { + range: 499..502, + id: "int", + ctx: Load, + }, + ), + op: BitOr, + right: Name( + ExprName { + range: 505..508, + id: "str", + ctx: Load, + }, + ), + }, + ), + ), + default: Some( + Name( + ExprName { + range: 511..514, + id: "int", + ctx: Load, + }, + ), + ), + }, + ), + ], + }, + ), + arguments: Some( + Arguments { + range: 515..517, + args: [], + keywords: [], + }, + ), + body: [ + Expr( + StmtExpr { + range: 519..522, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 519..522, + }, + ), + }, + ), + ], + }, + ), + ClassDef( + StmtClassDef { + range: 551..585, + decorator_list: [], + name: Identifier { + id: "Test", + range: 557..561, + }, + type_params: Some( + TypeParams { + range: 561..578, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 562..577, + name: Identifier { + id: "T", + range: 562..563, + }, + bound: Some( + Tuple( + ExprTuple { + range: 565..577, + elts: [ + Name( + ExprName { + range: 566..569, + id: "str", + ctx: Load, + }, + ), + Name( + ExprName { + range: 571..576, + id: "bytes", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + ), + default: None, + }, + ), + ], + }, + ), + arguments: Some( + Arguments { + range: 578..580, + args: [], + keywords: [], + }, + ), + body: [ + Expr( + StmtExpr { + range: 582..585, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 582..585, + }, + ), + }, + ), + ], + }, + ), + ClassDef( + StmtClassDef { + range: 606..629, + decorator_list: [], + name: Identifier { + id: "Test", + range: 612..616, + }, + type_params: Some( + TypeParams { + range: 616..622, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 617..618, + name: Identifier { + id: "T", + range: 617..618, + }, + bound: None, + default: None, + }, + ), + TypeVar( + TypeParamTypeVar { + range: 620..621, + name: Identifier { + id: "U", + range: 620..621, + }, + bound: None, + default: None, + }, + ), + ], + }, + ), + arguments: Some( + Arguments { + range: 622..624, + args: [], + keywords: [], + }, + ), + body: [ + Expr( + StmtExpr { + range: 626..629, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 626..629, + }, + ), + }, + ), + ], + }, + ), + ClassDef( + StmtClassDef { + range: 648..672, + decorator_list: [], + name: Identifier { + id: "Test", + range: 654..658, + }, + type_params: Some( + TypeParams { + range: 658..665, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 659..660, + name: Identifier { + id: "T", + range: 659..660, + }, + bound: None, + default: None, + }, + ), + TypeVar( + TypeParamTypeVar { + range: 662..663, + name: Identifier { + id: "U", + range: 662..663, + }, + bound: None, + default: None, + }, + ), + ], + }, + ), + arguments: Some( + Arguments { + range: 665..667, + args: [], + keywords: [], + }, + ), + body: [ + Expr( + StmtExpr { + range: 669..672, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 669..672, + }, + ), + }, + ), + ], + }, + ), + ClassDef( + StmtClassDef { + range: 689..711, + decorator_list: [], + name: Identifier { + id: "Test", + range: 695..699, + }, + type_params: Some( + TypeParams { + range: 699..704, + type_params: [ + TypeVarTuple( + TypeParamTypeVarTuple { + range: 700..703, + name: Identifier { + id: "Ts", + range: 701..703, + }, + default: None, + }, + ), + ], + }, + ), + arguments: Some( + Arguments { + range: 704..706, + args: [], + keywords: [], + }, + ), + body: [ + Expr( + StmtExpr { + range: 708..711, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 708..711, + }, + ), + }, + ), + ], + }, + ), + ClassDef( + StmtClassDef { + range: 741..789, + decorator_list: [], + name: Identifier { + id: "Test", + range: 747..751, + }, + type_params: Some( + TypeParams { + range: 751..782, + type_params: [ + TypeVarTuple( + TypeParamTypeVarTuple { + range: 752..781, + name: Identifier { + id: "Ts", + range: 753..755, + }, + default: Some( + Subscript( + ExprSubscript { + range: 758..781, + value: Name( + ExprName { + range: 758..764, + id: "Unpack", + ctx: Load, + }, + ), + slice: Subscript( + ExprSubscript { + range: 765..780, + value: Name( + ExprName { + range: 765..770, + id: "tuple", + ctx: Load, + }, + ), + slice: Tuple( + ExprTuple { + range: 771..779, + elts: [ + Name( + ExprName { + range: 771..774, + id: "int", + ctx: Load, + }, + ), + Name( + ExprName { + range: 776..779, + id: "str", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ), + }, + ), + ], + }, + ), + arguments: Some( + Arguments { + range: 782..784, + args: [], + keywords: [], + }, + ), + body: [ + Expr( + StmtExpr { + range: 786..789, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 786..789, + }, + ), + }, + ), + ], + }, + ), + ClassDef( + StmtClassDef { + range: 827..868, + decorator_list: [], + name: Identifier { + id: "Test", + range: 833..837, + }, + type_params: Some( + TypeParams { + range: 837..861, + type_params: [ + TypeVarTuple( + TypeParamTypeVarTuple { + range: 838..860, + name: Identifier { + id: "Ts", + range: 839..841, + }, + default: Some( + Starred( + ExprStarred { + range: 844..860, + value: Subscript( + ExprSubscript { + range: 845..860, + value: Name( + ExprName { + range: 845..850, + id: "tuple", + ctx: Load, + }, + ), + slice: Tuple( + ExprTuple { + range: 851..859, + elts: [ + Name( + ExprName { + range: 851..854, + id: "int", + ctx: Load, + }, + ), + Name( + ExprName { + range: 856..859, + id: "str", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ), + }, + ), + ], + }, + ), + arguments: Some( + Arguments { + range: 861..863, + args: [], + keywords: [], + }, + ), + body: [ + Expr( + StmtExpr { + range: 865..868, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 865..868, + }, + ), + }, + ), + ], + }, + ), + ClassDef( + StmtClassDef { + range: 882..904, + decorator_list: [], + name: Identifier { + id: "Test", + range: 888..892, + }, + type_params: Some( + TypeParams { + range: 892..897, + type_params: [ + ParamSpec( + TypeParamParamSpec { + range: 893..896, + name: Identifier { + id: "P", + range: 895..896, + }, + default: None, + }, + ), + ], + }, + ), + arguments: Some( + Arguments { + range: 897..899, + args: [], + keywords: [], + }, + ), + body: [ + Expr( + StmtExpr { + range: 901..904, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 901..904, + }, + ), + }, + ), + ], + }, + ), + ClassDef( + StmtClassDef { + range: 931..966, + decorator_list: [], + name: Identifier { + id: "Test", + range: 937..941, + }, + type_params: Some( + TypeParams { + range: 941..959, + type_params: [ + ParamSpec( + TypeParamParamSpec { + range: 942..958, + name: Identifier { + id: "P", + range: 944..945, + }, + default: Some( + List( + ExprList { + range: 948..958, + elts: [ + Name( + ExprName { + range: 949..952, + id: "int", + ctx: Load, + }, + ), + Name( + ExprName { + range: 954..957, + id: "str", + ctx: Load, + }, + ), + ], + ctx: Load, + }, + ), + ), + }, + ), + ], + }, + ), + arguments: Some( + Arguments { + range: 959..961, + args: [], + keywords: [], + }, + ), + body: [ + Expr( + StmtExpr { + range: 963..966, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 963..966, + }, + ), + }, + ), + ], + }, + ), + ClassDef( + StmtClassDef { + range: 982..1022, + decorator_list: [], + name: Identifier { + id: "Test", + range: 988..992, + }, + type_params: Some( + TypeParams { + range: 992..1012, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 993..994, + name: Identifier { + id: "X", + range: 993..994, + }, + bound: None, + default: None, + }, + ), + TypeVar( + TypeParamTypeVar { + range: 996..1002, + name: Identifier { + id: "Y", + range: 996..997, + }, + bound: Some( + Name( + ExprName { + range: 999..1002, + id: "str", + ctx: Load, + }, + ), + ), + default: None, + }, + ), + TypeVarTuple( + TypeParamTypeVarTuple { + range: 1004..1006, + name: Identifier { + id: "U", + range: 1005..1006, + }, + default: None, + }, + ), + ParamSpec( + TypeParamParamSpec { + range: 1008..1011, + name: Identifier { + id: "P", + range: 1010..1011, + }, + default: None, + }, + ), + ], + }, + ), + arguments: Some( + Arguments { + range: 1012..1014, + args: [], + keywords: [], + }, + ), + body: [ + Pass( + StmtPass { + range: 1018..1022, + }, + ), + ], + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__delete.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__delete.py.snap.new new file mode 100644 index 0000000000..93580cb04f --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__delete.py.snap.new @@ -0,0 +1,300 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/valid/statement/delete.py +--- +## AST + +``` +Module( + ModModule { + range: 0..122, + body: [ + Delete( + StmtDelete { + range: 0..5, + targets: [ + Name( + ExprName { + range: 4..5, + id: "x", + ctx: Del, + }, + ), + ], + }, + ), + Delete( + StmtDelete { + range: 6..13, + targets: [ + Name( + ExprName { + range: 11..12, + id: "x", + ctx: Del, + }, + ), + ], + }, + ), + Delete( + StmtDelete { + range: 14..23, + targets: [ + Name( + ExprName { + range: 18..19, + id: "a", + ctx: Del, + }, + ), + Name( + ExprName { + range: 21..22, + id: "b", + ctx: Del, + }, + ), + ], + }, + ), + Delete( + StmtDelete { + range: 24..40, + targets: [ + Name( + ExprName { + range: 28..29, + id: "a", + ctx: Del, + }, + ), + Tuple( + ExprTuple { + range: 31..37, + elts: [ + Name( + ExprName { + range: 32..33, + id: "b", + ctx: Del, + }, + ), + Name( + ExprName { + range: 35..36, + id: "c", + ctx: Del, + }, + ), + ], + ctx: Del, + parenthesized: true, + }, + ), + Name( + ExprName { + range: 39..40, + id: "d", + ctx: Del, + }, + ), + ], + }, + ), + Delete( + StmtDelete { + range: 41..51, + targets: [ + List( + ExprList { + range: 45..51, + elts: [ + Name( + ExprName { + range: 46..47, + id: "a", + ctx: Del, + }, + ), + Name( + ExprName { + range: 49..50, + id: "b", + ctx: Del, + }, + ), + ], + ctx: Del, + }, + ), + ], + }, + ), + Delete( + StmtDelete { + range: 52..70, + targets: [ + List( + ExprList { + range: 56..70, + elts: [ + Name( + ExprName { + range: 57..58, + id: "a", + ctx: Del, + }, + ), + List( + ExprList { + range: 60..66, + elts: [ + Name( + ExprName { + range: 61..62, + id: "b", + ctx: Del, + }, + ), + Name( + ExprName { + range: 64..65, + id: "c", + ctx: Del, + }, + ), + ], + ctx: Del, + }, + ), + Name( + ExprName { + range: 68..69, + id: "d", + ctx: Del, + }, + ), + ], + ctx: Del, + }, + ), + ], + }, + ), + Delete( + StmtDelete { + range: 71..78, + targets: [ + Attribute( + ExprAttribute { + range: 75..78, + value: Name( + ExprName { + range: 75..76, + id: "x", + ctx: Load, + }, + ), + attr: Identifier { + id: "y", + range: 77..78, + }, + ctx: Del, + }, + ), + ], + }, + ), + Delete( + StmtDelete { + range: 79..87, + targets: [ + Subscript( + ExprSubscript { + range: 83..87, + value: Name( + ExprName { + range: 83..84, + id: "x", + ctx: Load, + }, + ), + slice: Name( + ExprName { + range: 85..86, + id: "y", + ctx: Load, + }, + ), + ctx: Del, + }, + ), + ], + }, + ), + Delete( + StmtDelete { + range: 88..121, + targets: [ + Tuple( + ExprTuple { + range: 92..121, + elts: [ + Name( + ExprName { + range: 98..99, + id: "x", + ctx: Del, + }, + ), + Attribute( + ExprAttribute { + range: 105..108, + value: Name( + ExprName { + range: 105..106, + id: "x", + ctx: Load, + }, + ), + attr: Identifier { + id: "y", + range: 107..108, + }, + ctx: Del, + }, + ), + Subscript( + ExprSubscript { + range: 114..118, + value: Name( + ExprName { + range: 114..115, + id: "x", + ctx: Load, + }, + ), + slice: Name( + ExprName { + range: 116..117, + id: "y", + ctx: Load, + }, + ), + ctx: Del, + }, + ), + ], + ctx: Del, + parenthesized: true, + }, + ), + ], + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__for.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__for.py.snap.new new file mode 100644 index 0000000000..7a0acfce9d --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__for.py.snap.new @@ -0,0 +1,834 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/valid/statement/for.py +--- +## AST + +``` +Module( + ModModule { + range: 0..660, + body: [ + For( + StmtFor { + range: 0..28, + is_async: false, + target: Name( + ExprName { + range: 4..10, + id: "target", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 14..18, + id: "iter", + ctx: Load, + }, + ), + body: [ + Pass( + StmtPass { + range: 24..28, + }, + ), + ], + orelse: [], + }, + ), + For( + StmtFor { + range: 30..63, + is_async: false, + target: Name( + ExprName { + range: 34..40, + id: "target", + ctx: Store, + }, + ), + iter: Tuple( + ExprTuple { + range: 44..53, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 45..46, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 48..49, + value: Int( + 2, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 51..52, + value: Int( + 3, + ), + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + body: [ + Pass( + StmtPass { + range: 59..63, + }, + ), + ], + orelse: [], + }, + ), + For( + StmtFor { + range: 65..100, + is_async: false, + target: Attribute( + ExprAttribute { + range: 69..80, + value: Name( + ExprName { + range: 69..75, + id: "target", + ctx: Load, + }, + ), + attr: Identifier { + id: "attr", + range: 76..80, + }, + ctx: Store, + }, + ), + iter: Call( + ExprCall { + range: 84..90, + func: Name( + ExprName { + range: 84..88, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 88..90, + args: [], + keywords: [], + }, + }, + ), + body: [ + Pass( + StmtPass { + range: 96..100, + }, + ), + ], + orelse: [], + }, + ), + For( + StmtFor { + range: 102..135, + is_async: false, + target: Subscript( + ExprSubscript { + range: 106..115, + value: Name( + ExprName { + range: 106..112, + id: "target", + ctx: Load, + }, + ), + slice: NumberLiteral( + ExprNumberLiteral { + range: 113..114, + value: Int( + 0, + ), + }, + ), + ctx: Store, + }, + ), + iter: Attribute( + ExprAttribute { + range: 119..125, + value: Name( + ExprName { + range: 119..120, + id: "x", + ctx: Load, + }, + ), + attr: Identifier { + id: "attr", + range: 121..125, + }, + ctx: Load, + }, + ), + body: [ + Pass( + StmtPass { + range: 131..135, + }, + ), + ], + orelse: [], + }, + ), + For( + StmtFor { + range: 137..167, + is_async: false, + target: Name( + ExprName { + range: 141..147, + id: "target", + ctx: Store, + }, + ), + iter: Compare( + ExprCompare { + range: 151..157, + left: Name( + ExprName { + range: 151..152, + id: "x", + ctx: Load, + }, + ), + ops: [ + LtE, + ], + comparators: [ + Name( + ExprName { + range: 156..157, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + body: [ + Pass( + StmtPass { + range: 163..167, + }, + ), + ], + orelse: [], + }, + ), + For( + StmtFor { + range: 169..200, + is_async: false, + target: Name( + ExprName { + range: 173..179, + id: "target", + ctx: Store, + }, + ), + iter: BoolOp( + ExprBoolOp { + range: 183..190, + op: And, + values: [ + Name( + ExprName { + range: 183..184, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 189..190, + id: "b", + ctx: Load, + }, + ), + ], + }, + ), + body: [ + Pass( + StmtPass { + range: 196..200, + }, + ), + ], + orelse: [], + }, + ), + For( + StmtFor { + range: 202..232, + is_async: false, + target: Tuple( + ExprTuple { + range: 206..214, + elts: [ + Name( + ExprName { + range: 206..207, + id: "a", + ctx: Store, + }, + ), + Name( + ExprName { + range: 209..210, + id: "b", + ctx: Store, + }, + ), + Name( + ExprName { + range: 212..213, + id: "c", + ctx: Store, + }, + ), + ], + ctx: Store, + parenthesized: false, + }, + ), + iter: Name( + ExprName { + range: 218..222, + id: "iter", + ctx: Load, + }, + ), + body: [ + Pass( + StmtPass { + range: 228..232, + }, + ), + ], + orelse: [], + }, + ), + For( + StmtFor { + range: 234..262, + is_async: false, + target: Tuple( + ExprTuple { + range: 238..244, + elts: [ + Name( + ExprName { + range: 239..240, + id: "a", + ctx: Store, + }, + ), + Name( + ExprName { + range: 242..243, + id: "b", + ctx: Store, + }, + ), + ], + ctx: Store, + parenthesized: true, + }, + ), + iter: Name( + ExprName { + range: 248..252, + id: "iter", + ctx: Load, + }, + ), + body: [ + Pass( + StmtPass { + range: 258..262, + }, + ), + ], + orelse: [], + }, + ), + For( + StmtFor { + range: 264..295, + is_async: false, + target: Name( + ExprName { + range: 268..274, + id: "target", + ctx: Store, + }, + ), + iter: Starred( + ExprStarred { + range: 278..285, + value: Attribute( + ExprAttribute { + range: 279..285, + value: Name( + ExprName { + range: 279..280, + id: "x", + ctx: Load, + }, + ), + attr: Identifier { + id: "attr", + range: 281..285, + }, + ctx: Load, + }, + ), + ctx: Load, + }, + ), + body: [ + Pass( + StmtPass { + range: 291..295, + }, + ), + ], + orelse: [], + }, + ), + For( + StmtFor { + range: 297..327, + is_async: false, + target: Name( + ExprName { + range: 301..307, + id: "target", + ctx: Store, + }, + ), + iter: List( + ExprList { + range: 311..317, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 312..313, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 315..316, + value: Int( + 2, + ), + }, + ), + ], + ctx: Load, + }, + ), + body: [ + Pass( + StmtPass { + range: 323..327, + }, + ), + ], + orelse: [], + }, + ), + For( + StmtFor { + range: 329..377, + is_async: false, + target: Starred( + ExprStarred { + range: 333..340, + value: Name( + ExprName { + range: 334..340, + id: "target", + ctx: Store, + }, + ), + ctx: Store, + }, + ), + iter: Tuple( + ExprTuple { + range: 344..352, + elts: [ + Name( + ExprName { + range: 344..345, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 347..348, + id: "b", + ctx: Load, + }, + ), + Name( + ExprName { + range: 350..351, + id: "c", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + body: [ + Pass( + StmtPass { + range: 358..362, + }, + ), + ], + orelse: [ + Pass( + StmtPass { + range: 373..377, + }, + ), + ], + }, + ), + For( + StmtFor { + range: 379..404, + is_async: false, + target: Name( + ExprName { + range: 383..389, + id: "target", + ctx: Store, + }, + ), + iter: Starred( + ExprStarred { + range: 393..399, + value: BinOp( + ExprBinOp { + range: 394..399, + left: Name( + ExprName { + range: 394..395, + id: "x", + ctx: Load, + }, + ), + op: BitOr, + right: Name( + ExprName { + range: 398..399, + id: "y", + ctx: Load, + }, + ), + }, + ), + ctx: Load, + }, + ), + body: [ + Expr( + StmtExpr { + range: 401..404, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 401..404, + }, + ), + }, + ), + ], + orelse: [], + }, + ), + For( + StmtFor { + range: 405..432, + is_async: false, + target: Name( + ExprName { + range: 409..415, + id: "target", + ctx: Store, + }, + ), + iter: Starred( + ExprStarred { + range: 419..427, + value: Await( + ExprAwait { + range: 420..427, + value: Name( + ExprName { + range: 426..427, + id: "x", + ctx: Load, + }, + ), + }, + ), + ctx: Load, + }, + ), + body: [ + Expr( + StmtExpr { + range: 429..432, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 429..432, + }, + ), + }, + ), + ], + orelse: [], + }, + ), + For( + StmtFor { + range: 433..459, + is_async: false, + target: Name( + ExprName { + range: 437..443, + id: "target", + ctx: Store, + }, + ), + iter: Await( + ExprAwait { + range: 447..454, + value: Name( + ExprName { + range: 453..454, + id: "x", + ctx: Load, + }, + ), + }, + ), + body: [ + Expr( + StmtExpr { + range: 456..459, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 456..459, + }, + ), + }, + ), + ], + orelse: [], + }, + ), + For( + StmtFor { + range: 460..490, + is_async: false, + target: Name( + ExprName { + range: 464..470, + id: "target", + ctx: Store, + }, + ), + iter: Lambda( + ExprLambda { + range: 474..485, + parameters: Some( + Parameters { + range: 481..482, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 481..482, + parameter: Parameter { + range: 481..482, + name: Identifier { + id: "x", + range: 481..482, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: Name( + ExprName { + range: 484..485, + id: "x", + ctx: Load, + }, + ), + }, + ), + body: [ + Expr( + StmtExpr { + range: 487..490, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 487..490, + }, + ), + }, + ), + ], + orelse: [], + }, + ), + For( + StmtFor { + range: 491..526, + is_async: false, + target: Name( + ExprName { + range: 495..501, + id: "target", + ctx: Store, + }, + ), + iter: If( + ExprIf { + range: 505..521, + test: BooleanLiteral( + ExprBooleanLiteral { + range: 510..514, + value: true, + }, + ), + body: Name( + ExprName { + range: 505..506, + id: "x", + ctx: Load, + }, + ), + orelse: Name( + ExprName { + range: 520..521, + id: "y", + ctx: Load, + }, + ), + }, + ), + body: [ + Expr( + StmtExpr { + range: 523..526, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 523..526, + }, + ), + }, + ), + ], + orelse: [], + }, + ), + If( + StmtIf { + range: 528..659, + test: Name( + ExprName { + range: 531..532, + id: "x", + ctx: Load, + }, + ), + body: [ + For( + StmtFor { + range: 538..570, + is_async: false, + target: Name( + ExprName { + range: 542..548, + id: "target", + ctx: Store, + }, + ), + iter: Name( + ExprName { + range: 552..556, + id: "iter", + ctx: Load, + }, + ), + body: [ + Pass( + StmtPass { + range: 566..570, + }, + ), + ], + orelse: [], + }, + ), + ], + elif_else_clauses: [ + ElifElseClause { + range: 645..659, + test: None, + body: [ + Pass( + StmtPass { + range: 655..659, + }, + ), + ], + }, + ], + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__from_import.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__from_import.py.snap.new new file mode 100644 index 0000000000..7952363409 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__from_import.py.snap.new @@ -0,0 +1,238 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/valid/statement/from_import.py +--- +## AST + +``` +Module( + ModModule { + range: 0..259, + body: [ + ImportFrom( + StmtImportFrom { + range: 0..15, + module: Some( + Identifier { + id: "a", + range: 5..6, + }, + ), + names: [ + Alias { + range: 14..15, + name: Identifier { + id: "b", + range: 14..15, + }, + asname: None, + }, + ], + level: 0, + }, + ), + ImportFrom( + StmtImportFrom { + range: 27..42, + module: None, + names: [ + Alias { + range: 41..42, + name: Identifier { + id: "a", + range: 41..42, + }, + asname: None, + }, + ], + level: 1, + }, + ), + ImportFrom( + StmtImportFrom { + range: 43..85, + module: Some( + Identifier { + id: "foo.bar", + range: 48..55, + }, + ), + names: [ + Alias { + range: 63..71, + name: Identifier { + id: "baz", + range: 63..66, + }, + asname: Some( + Identifier { + id: "b", + range: 70..71, + }, + ), + }, + Alias { + range: 73..85, + name: Identifier { + id: "FooBar", + range: 73..79, + }, + asname: Some( + Identifier { + id: "fb", + range: 83..85, + }, + ), + }, + ], + level: 0, + }, + ), + ImportFrom( + StmtImportFrom { + range: 86..102, + module: Some( + Identifier { + id: "a", + range: 92..93, + }, + ), + names: [ + Alias { + range: 101..102, + name: Identifier { + id: "b", + range: 101..102, + }, + asname: None, + }, + ], + level: 1, + }, + ), + ImportFrom( + StmtImportFrom { + range: 103..120, + module: None, + names: [ + Alias { + range: 119..120, + name: Identifier { + id: "c", + range: 119..120, + }, + asname: None, + }, + ], + level: 3, + }, + ), + ImportFrom( + StmtImportFrom { + range: 121..161, + module: None, + names: [ + Alias { + range: 160..161, + name: Identifier { + id: "d", + range: 160..161, + }, + asname: None, + }, + ], + level: 26, + }, + ), + ImportFrom( + StmtImportFrom { + range: 162..207, + module: Some( + Identifier { + id: "a.b.c", + range: 193..198, + }, + ), + names: [ + Alias { + range: 206..207, + name: Identifier { + id: "d", + range: 206..207, + }, + asname: None, + }, + ], + level: 26, + }, + ), + ImportFrom( + StmtImportFrom { + range: 208..242, + module: Some( + Identifier { + id: "module", + range: 213..219, + }, + ), + names: [ + Alias { + range: 228..229, + name: Identifier { + id: "a", + range: 228..229, + }, + asname: None, + }, + Alias { + range: 231..237, + name: Identifier { + id: "b", + range: 231..232, + }, + asname: Some( + Identifier { + id: "B", + range: 236..237, + }, + ), + }, + Alias { + range: 239..240, + name: Identifier { + id: "c", + range: 239..240, + }, + asname: None, + }, + ], + level: 0, + }, + ), + ImportFrom( + StmtImportFrom { + range: 243..258, + module: Some( + Identifier { + id: "a", + range: 248..249, + }, + ), + names: [ + Alias { + range: 257..258, + name: Identifier { + id: "*", + range: 257..258, + }, + asname: None, + }, + ], + level: 0, + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__function.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__function.py.snap.new new file mode 100644 index 0000000000..c4864e78bf --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__function.py.snap.new @@ -0,0 +1,3242 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/valid/statement/function.py +--- +## AST + +``` +Module( + ModModule { + range: 0..2399, + body: [ + FunctionDef( + StmtFunctionDef { + range: 0..29, + is_async: false, + decorator_list: [], + name: Identifier { + id: "no_parameters", + range: 4..17, + }, + type_params: None, + parameters: Parameters { + range: 17..19, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 25..29, + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 32..76, + is_async: false, + decorator_list: [], + name: Identifier { + id: "positional_parameters", + range: 36..57, + }, + type_params: None, + parameters: Parameters { + range: 57..66, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 58..59, + parameter: Parameter { + range: 58..59, + name: Identifier { + id: "a", + range: 58..59, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 61..62, + parameter: Parameter { + range: 61..62, + name: Identifier { + id: "b", + range: 61..62, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 64..65, + parameter: Parameter { + range: 64..65, + name: Identifier { + id: "c", + range: 64..65, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 72..76, + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 79..149, + is_async: false, + decorator_list: [], + name: Identifier { + id: "positional_parameters_with_default_values", + range: 83..124, + }, + type_params: None, + parameters: Parameters { + range: 124..139, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 125..126, + parameter: Parameter { + range: 125..126, + name: Identifier { + id: "a", + range: 125..126, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 128..132, + parameter: Parameter { + range: 128..129, + name: Identifier { + id: "b", + range: 128..129, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 130..132, + value: Int( + 20, + ), + }, + ), + ), + }, + ParameterWithDefault { + range: 134..138, + parameter: Parameter { + range: 134..135, + name: Identifier { + id: "c", + range: 134..135, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 136..138, + value: Int( + 30, + ), + }, + ), + ), + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 145..149, + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 152..226, + is_async: false, + decorator_list: [], + name: Identifier { + id: "positional_parameters_with_default_values2", + range: 156..198, + }, + type_params: None, + parameters: Parameters { + range: 198..216, + posonlyargs: [ + ParameterWithDefault { + range: 199..200, + parameter: Parameter { + range: 199..200, + name: Identifier { + id: "a", + range: 199..200, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 202..206, + parameter: Parameter { + range: 202..203, + name: Identifier { + id: "b", + range: 202..203, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 204..206, + value: Int( + 20, + ), + }, + ), + ), + }, + ], + args: [ + ParameterWithDefault { + range: 211..215, + parameter: Parameter { + range: 211..212, + name: Identifier { + id: "c", + range: 211..212, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 213..215, + value: Int( + 30, + ), + }, + ), + ), + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 222..226, + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 229..296, + is_async: false, + decorator_list: [], + name: Identifier { + id: "positional_only_and_positional_parameters", + range: 233..274, + }, + type_params: None, + parameters: Parameters { + range: 274..286, + posonlyargs: [ + ParameterWithDefault { + range: 275..276, + parameter: Parameter { + range: 275..276, + name: Identifier { + id: "a", + range: 275..276, + }, + annotation: None, + }, + default: None, + }, + ], + args: [ + ParameterWithDefault { + range: 281..282, + parameter: Parameter { + range: 281..282, + name: Identifier { + id: "b", + range: 281..282, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 284..285, + parameter: Parameter { + range: 284..285, + name: Identifier { + id: "c", + range: 284..285, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 292..296, + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 299..393, + is_async: false, + decorator_list: [], + name: Identifier { + id: "pos_args_with_defaults_and_varargs_and_kwargs", + range: 303..348, + }, + type_params: None, + parameters: Parameters { + range: 348..383, + posonlyargs: [ + ParameterWithDefault { + range: 349..350, + parameter: Parameter { + range: 349..350, + name: Identifier { + id: "a", + range: 349..350, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 352..356, + parameter: Parameter { + range: 352..353, + name: Identifier { + id: "b", + range: 352..353, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 354..356, + value: Int( + 20, + ), + }, + ), + ), + }, + ], + args: [ + ParameterWithDefault { + range: 361..365, + parameter: Parameter { + range: 361..362, + name: Identifier { + id: "c", + range: 361..362, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 363..365, + value: Int( + 30, + ), + }, + ), + ), + }, + ], + vararg: Some( + Parameter { + range: 367..372, + name: Identifier { + id: "args", + range: 368..372, + }, + annotation: None, + }, + ), + kwonlyargs: [], + kwarg: Some( + Parameter { + range: 374..382, + name: Identifier { + id: "kwargs", + range: 376..382, + }, + annotation: None, + }, + ), + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 389..393, + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 396..445, + is_async: false, + decorator_list: [], + name: Identifier { + id: "keyword_only_parameters", + range: 400..423, + }, + type_params: None, + parameters: Parameters { + range: 423..435, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [ + ParameterWithDefault { + range: 427..428, + parameter: Parameter { + range: 427..428, + name: Identifier { + id: "a", + range: 427..428, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 430..431, + parameter: Parameter { + range: 430..431, + name: Identifier { + id: "b", + range: 430..431, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 433..434, + parameter: Parameter { + range: 433..434, + name: Identifier { + id: "c", + range: 433..434, + }, + annotation: None, + }, + default: None, + }, + ], + kwarg: None, + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 441..445, + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 448..517, + is_async: false, + decorator_list: [], + name: Identifier { + id: "keyword_only_parameters_with_defaults", + range: 452..489, + }, + type_params: None, + parameters: Parameters { + range: 489..507, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [ + ParameterWithDefault { + range: 493..494, + parameter: Parameter { + range: 493..494, + name: Identifier { + id: "a", + range: 493..494, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 496..500, + parameter: Parameter { + range: 496..497, + name: Identifier { + id: "b", + range: 496..497, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 498..500, + value: Int( + 20, + ), + }, + ), + ), + }, + ParameterWithDefault { + range: 502..506, + parameter: Parameter { + range: 502..503, + name: Identifier { + id: "c", + range: 502..503, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 504..506, + value: Int( + 30, + ), + }, + ), + ), + }, + ], + kwarg: None, + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 513..517, + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 520..594, + is_async: false, + decorator_list: [], + name: Identifier { + id: "kw_only_args_with_defaults_and_varargs", + range: 524..562, + }, + type_params: None, + parameters: Parameters { + range: 562..584, + posonlyargs: [], + args: [], + vararg: Some( + Parameter { + range: 563..568, + name: Identifier { + id: "args", + range: 564..568, + }, + annotation: None, + }, + ), + kwonlyargs: [ + ParameterWithDefault { + range: 570..571, + parameter: Parameter { + range: 570..571, + name: Identifier { + id: "a", + range: 570..571, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 573..577, + parameter: Parameter { + range: 573..574, + name: Identifier { + id: "b", + range: 573..574, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 575..577, + value: Int( + 20, + ), + }, + ), + ), + }, + ParameterWithDefault { + range: 579..583, + parameter: Parameter { + range: 579..580, + name: Identifier { + id: "c", + range: 579..580, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 581..583, + value: Int( + 30, + ), + }, + ), + ), + }, + ], + kwarg: None, + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 590..594, + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 597..676, + is_async: false, + decorator_list: [], + name: Identifier { + id: "kw_only_args_with_defaults_and_kwargs", + range: 601..638, + }, + type_params: None, + parameters: Parameters { + range: 638..666, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [ + ParameterWithDefault { + range: 642..643, + parameter: Parameter { + range: 642..643, + name: Identifier { + id: "a", + range: 642..643, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 645..649, + parameter: Parameter { + range: 645..646, + name: Identifier { + id: "b", + range: 645..646, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 647..649, + value: Int( + 20, + ), + }, + ), + ), + }, + ParameterWithDefault { + range: 651..655, + parameter: Parameter { + range: 651..652, + name: Identifier { + id: "c", + range: 651..652, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 653..655, + value: Int( + 30, + ), + }, + ), + ), + }, + ], + kwarg: Some( + Parameter { + range: 657..665, + name: Identifier { + id: "kwargs", + range: 659..665, + }, + annotation: None, + }, + ), + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 672..676, + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 679..774, + is_async: false, + decorator_list: [], + name: Identifier { + id: "kw_only_args_with_defaults_and_varargs_and_kwargs", + range: 683..732, + }, + type_params: None, + parameters: Parameters { + range: 732..764, + posonlyargs: [], + args: [], + vararg: Some( + Parameter { + range: 733..738, + name: Identifier { + id: "args", + range: 734..738, + }, + annotation: None, + }, + ), + kwonlyargs: [ + ParameterWithDefault { + range: 740..741, + parameter: Parameter { + range: 740..741, + name: Identifier { + id: "a", + range: 740..741, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 743..747, + parameter: Parameter { + range: 743..744, + name: Identifier { + id: "b", + range: 743..744, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 745..747, + value: Int( + 20, + ), + }, + ), + ), + }, + ParameterWithDefault { + range: 749..753, + parameter: Parameter { + range: 749..750, + name: Identifier { + id: "c", + range: 749..750, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 751..753, + value: Int( + 30, + ), + }, + ), + ), + }, + ], + kwarg: Some( + Parameter { + range: 755..763, + name: Identifier { + id: "kwargs", + range: 757..763, + }, + annotation: None, + }, + ), + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 770..774, + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 777..835, + is_async: false, + decorator_list: [], + name: Identifier { + id: "pos_and_kw_only_args", + range: 781..801, + }, + type_params: None, + parameters: Parameters { + range: 801..825, + posonlyargs: [ + ParameterWithDefault { + range: 802..803, + parameter: Parameter { + range: 802..803, + name: Identifier { + id: "a", + range: 802..803, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 805..806, + parameter: Parameter { + range: 805..806, + name: Identifier { + id: "b", + range: 805..806, + }, + annotation: None, + }, + default: None, + }, + ], + args: [ + ParameterWithDefault { + range: 811..812, + parameter: Parameter { + range: 811..812, + name: Identifier { + id: "c", + range: 811..812, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [ + ParameterWithDefault { + range: 817..818, + parameter: Parameter { + range: 817..818, + name: Identifier { + id: "d", + range: 817..818, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 820..821, + parameter: Parameter { + range: 820..821, + name: Identifier { + id: "e", + range: 820..821, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 823..824, + parameter: Parameter { + range: 823..824, + name: Identifier { + id: "f", + range: 823..824, + }, + annotation: None, + }, + default: None, + }, + ], + kwarg: None, + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 831..835, + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 838..916, + is_async: false, + decorator_list: [], + name: Identifier { + id: "pos_and_kw_only_args_with_defaults", + range: 842..876, + }, + type_params: None, + parameters: Parameters { + range: 876..906, + posonlyargs: [ + ParameterWithDefault { + range: 877..878, + parameter: Parameter { + range: 877..878, + name: Identifier { + id: "a", + range: 877..878, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 880..881, + parameter: Parameter { + range: 880..881, + name: Identifier { + id: "b", + range: 880..881, + }, + annotation: None, + }, + default: None, + }, + ], + args: [ + ParameterWithDefault { + range: 886..887, + parameter: Parameter { + range: 886..887, + name: Identifier { + id: "c", + range: 886..887, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [ + ParameterWithDefault { + range: 892..893, + parameter: Parameter { + range: 892..893, + name: Identifier { + id: "d", + range: 892..893, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 895..899, + parameter: Parameter { + range: 895..896, + name: Identifier { + id: "e", + range: 895..896, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 897..899, + value: Int( + 20, + ), + }, + ), + ), + }, + ParameterWithDefault { + range: 901..905, + parameter: Parameter { + range: 901..902, + name: Identifier { + id: "f", + range: 901..902, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 903..905, + value: Int( + 30, + ), + }, + ), + ), + }, + ], + kwarg: None, + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 912..916, + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 919..1013, + is_async: false, + decorator_list: [], + name: Identifier { + id: "pos_and_kw_only_args_with_defaults_and_varargs", + range: 923..969, + }, + type_params: None, + parameters: Parameters { + range: 969..1003, + posonlyargs: [ + ParameterWithDefault { + range: 970..971, + parameter: Parameter { + range: 970..971, + name: Identifier { + id: "a", + range: 970..971, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 973..974, + parameter: Parameter { + range: 973..974, + name: Identifier { + id: "b", + range: 973..974, + }, + annotation: None, + }, + default: None, + }, + ], + args: [ + ParameterWithDefault { + range: 979..980, + parameter: Parameter { + range: 979..980, + name: Identifier { + id: "c", + range: 979..980, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: Some( + Parameter { + range: 982..987, + name: Identifier { + id: "args", + range: 983..987, + }, + annotation: None, + }, + ), + kwonlyargs: [ + ParameterWithDefault { + range: 989..990, + parameter: Parameter { + range: 989..990, + name: Identifier { + id: "d", + range: 989..990, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 992..996, + parameter: Parameter { + range: 992..993, + name: Identifier { + id: "e", + range: 992..993, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 994..996, + value: Int( + 20, + ), + }, + ), + ), + }, + ParameterWithDefault { + range: 998..1002, + parameter: Parameter { + range: 998..999, + name: Identifier { + id: "f", + range: 998..999, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 1000..1002, + value: Int( + 30, + ), + }, + ), + ), + }, + ], + kwarg: None, + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 1009..1013, + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 1016..1121, + is_async: false, + decorator_list: [], + name: Identifier { + id: "pos_and_kw_only_args_with_defaults_and_kwargs", + range: 1020..1065, + }, + type_params: None, + parameters: Parameters { + range: 1065..1111, + posonlyargs: [ + ParameterWithDefault { + range: 1071..1072, + parameter: Parameter { + range: 1071..1072, + name: Identifier { + id: "a", + range: 1071..1072, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 1074..1075, + parameter: Parameter { + range: 1074..1075, + name: Identifier { + id: "b", + range: 1074..1075, + }, + annotation: None, + }, + default: None, + }, + ], + args: [ + ParameterWithDefault { + range: 1080..1081, + parameter: Parameter { + range: 1080..1081, + name: Identifier { + id: "c", + range: 1080..1081, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [ + ParameterWithDefault { + range: 1086..1087, + parameter: Parameter { + range: 1086..1087, + name: Identifier { + id: "d", + range: 1086..1087, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 1089..1093, + parameter: Parameter { + range: 1089..1090, + name: Identifier { + id: "e", + range: 1089..1090, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 1091..1093, + value: Int( + 20, + ), + }, + ), + ), + }, + ParameterWithDefault { + range: 1095..1099, + parameter: Parameter { + range: 1095..1096, + name: Identifier { + id: "f", + range: 1095..1096, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 1097..1099, + value: Int( + 30, + ), + }, + ), + ), + }, + ], + kwarg: Some( + Parameter { + range: 1101..1109, + name: Identifier { + id: "kwargs", + range: 1103..1109, + }, + annotation: None, + }, + ), + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 1117..1121, + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 1124..1245, + is_async: false, + decorator_list: [], + name: Identifier { + id: "pos_and_kw_only_args_with_defaults_and_varargs_and_kwargs", + range: 1128..1185, + }, + type_params: None, + parameters: Parameters { + range: 1185..1235, + posonlyargs: [ + ParameterWithDefault { + range: 1191..1192, + parameter: Parameter { + range: 1191..1192, + name: Identifier { + id: "a", + range: 1191..1192, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 1194..1195, + parameter: Parameter { + range: 1194..1195, + name: Identifier { + id: "b", + range: 1194..1195, + }, + annotation: None, + }, + default: None, + }, + ], + args: [ + ParameterWithDefault { + range: 1200..1201, + parameter: Parameter { + range: 1200..1201, + name: Identifier { + id: "c", + range: 1200..1201, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: Some( + Parameter { + range: 1203..1208, + name: Identifier { + id: "args", + range: 1204..1208, + }, + annotation: None, + }, + ), + kwonlyargs: [ + ParameterWithDefault { + range: 1210..1211, + parameter: Parameter { + range: 1210..1211, + name: Identifier { + id: "d", + range: 1210..1211, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 1213..1217, + parameter: Parameter { + range: 1213..1214, + name: Identifier { + id: "e", + range: 1213..1214, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 1215..1217, + value: Int( + 20, + ), + }, + ), + ), + }, + ParameterWithDefault { + range: 1219..1223, + parameter: Parameter { + range: 1219..1220, + name: Identifier { + id: "f", + range: 1219..1220, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 1221..1223, + value: Int( + 30, + ), + }, + ), + ), + }, + ], + kwarg: Some( + Parameter { + range: 1225..1233, + name: Identifier { + id: "kwargs", + range: 1227..1233, + }, + annotation: None, + }, + ), + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 1241..1245, + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 1248..1316, + is_async: false, + decorator_list: [], + name: Identifier { + id: "positional_and_keyword_parameters", + range: 1252..1285, + }, + type_params: None, + parameters: Parameters { + range: 1285..1306, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 1286..1287, + parameter: Parameter { + range: 1286..1287, + name: Identifier { + id: "a", + range: 1286..1287, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 1289..1290, + parameter: Parameter { + range: 1289..1290, + name: Identifier { + id: "b", + range: 1289..1290, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 1292..1293, + parameter: Parameter { + range: 1292..1293, + name: Identifier { + id: "c", + range: 1292..1293, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [ + ParameterWithDefault { + range: 1298..1299, + parameter: Parameter { + range: 1298..1299, + name: Identifier { + id: "d", + range: 1298..1299, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 1301..1302, + parameter: Parameter { + range: 1301..1302, + name: Identifier { + id: "e", + range: 1301..1302, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 1304..1305, + parameter: Parameter { + range: 1304..1305, + name: Identifier { + id: "f", + range: 1304..1305, + }, + annotation: None, + }, + default: None, + }, + ], + kwarg: None, + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 1312..1316, + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 1319..1407, + is_async: false, + decorator_list: [], + name: Identifier { + id: "positional_and_keyword_parameters_with_defaults", + range: 1323..1370, + }, + type_params: None, + parameters: Parameters { + range: 1370..1397, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 1371..1372, + parameter: Parameter { + range: 1371..1372, + name: Identifier { + id: "a", + range: 1371..1372, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 1374..1375, + parameter: Parameter { + range: 1374..1375, + name: Identifier { + id: "b", + range: 1374..1375, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 1377..1378, + parameter: Parameter { + range: 1377..1378, + name: Identifier { + id: "c", + range: 1377..1378, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [ + ParameterWithDefault { + range: 1383..1384, + parameter: Parameter { + range: 1383..1384, + name: Identifier { + id: "d", + range: 1383..1384, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 1386..1390, + parameter: Parameter { + range: 1386..1387, + name: Identifier { + id: "e", + range: 1386..1387, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 1388..1390, + value: Int( + 20, + ), + }, + ), + ), + }, + ParameterWithDefault { + range: 1392..1396, + parameter: Parameter { + range: 1392..1393, + name: Identifier { + id: "f", + range: 1392..1393, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 1394..1396, + value: Int( + 30, + ), + }, + ), + ), + }, + ], + kwarg: None, + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 1403..1407, + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 1410..1520, + is_async: false, + decorator_list: [], + name: Identifier { + id: "positional_and_keyword_parameters_with_defaults_and_varargs", + range: 1414..1473, + }, + type_params: None, + parameters: Parameters { + range: 1473..1510, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 1479..1480, + parameter: Parameter { + range: 1479..1480, + name: Identifier { + id: "a", + range: 1479..1480, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 1482..1483, + parameter: Parameter { + range: 1482..1483, + name: Identifier { + id: "b", + range: 1482..1483, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 1485..1486, + parameter: Parameter { + range: 1485..1486, + name: Identifier { + id: "c", + range: 1485..1486, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: Some( + Parameter { + range: 1488..1493, + name: Identifier { + id: "args", + range: 1489..1493, + }, + annotation: None, + }, + ), + kwonlyargs: [ + ParameterWithDefault { + range: 1495..1496, + parameter: Parameter { + range: 1495..1496, + name: Identifier { + id: "d", + range: 1495..1496, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 1498..1502, + parameter: Parameter { + range: 1498..1499, + name: Identifier { + id: "e", + range: 1498..1499, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 1500..1502, + value: Int( + 20, + ), + }, + ), + ), + }, + ParameterWithDefault { + range: 1504..1508, + parameter: Parameter { + range: 1504..1505, + name: Identifier { + id: "f", + range: 1504..1505, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 1506..1508, + value: Int( + 30, + ), + }, + ), + ), + }, + ], + kwarg: None, + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 1516..1520, + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 1523..1654, + is_async: false, + decorator_list: [], + name: Identifier { + id: "positional_and_keyword_parameters_with_defaults_and_varargs_and_kwargs", + range: 1527..1597, + }, + type_params: None, + parameters: Parameters { + range: 1597..1644, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 1603..1604, + parameter: Parameter { + range: 1603..1604, + name: Identifier { + id: "a", + range: 1603..1604, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 1606..1607, + parameter: Parameter { + range: 1606..1607, + name: Identifier { + id: "b", + range: 1606..1607, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 1609..1610, + parameter: Parameter { + range: 1609..1610, + name: Identifier { + id: "c", + range: 1609..1610, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: Some( + Parameter { + range: 1612..1617, + name: Identifier { + id: "args", + range: 1613..1617, + }, + annotation: None, + }, + ), + kwonlyargs: [ + ParameterWithDefault { + range: 1619..1620, + parameter: Parameter { + range: 1619..1620, + name: Identifier { + id: "d", + range: 1619..1620, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 1622..1626, + parameter: Parameter { + range: 1622..1623, + name: Identifier { + id: "e", + range: 1622..1623, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 1624..1626, + value: Int( + 20, + ), + }, + ), + ), + }, + ParameterWithDefault { + range: 1628..1632, + parameter: Parameter { + range: 1628..1629, + name: Identifier { + id: "f", + range: 1628..1629, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 1630..1632, + value: Int( + 30, + ), + }, + ), + ), + }, + ], + kwarg: Some( + Parameter { + range: 1634..1642, + name: Identifier { + id: "kwargs", + range: 1636..1642, + }, + annotation: None, + }, + ), + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 1650..1654, + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 1703..1735, + is_async: false, + decorator_list: [], + name: Identifier { + id: "func", + range: 1707..1711, + }, + type_params: Some( + TypeParams { + range: 1711..1714, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 1712..1713, + name: Identifier { + id: "T", + range: 1712..1713, + }, + bound: None, + default: None, + }, + ), + ], + }, + ), + parameters: Parameters { + range: 1714..1720, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 1715..1719, + parameter: Parameter { + range: 1715..1719, + name: Identifier { + id: "a", + range: 1715..1716, + }, + annotation: Some( + Name( + ExprName { + range: 1718..1719, + id: "T", + ctx: Load, + }, + ), + ), + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: Some( + Name( + ExprName { + range: 1724..1725, + id: "T", + ctx: Load, + }, + ), + ), + body: [ + Pass( + StmtPass { + range: 1731..1735, + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 1738..1775, + is_async: false, + decorator_list: [], + name: Identifier { + id: "func", + range: 1742..1746, + }, + type_params: Some( + TypeParams { + range: 1746..1754, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 1747..1753, + name: Identifier { + id: "T", + range: 1747..1748, + }, + bound: Some( + Name( + ExprName { + range: 1750..1753, + id: "str", + ctx: Load, + }, + ), + ), + default: None, + }, + ), + ], + }, + ), + parameters: Parameters { + range: 1754..1760, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 1755..1759, + parameter: Parameter { + range: 1755..1759, + name: Identifier { + id: "a", + range: 1755..1756, + }, + annotation: Some( + Name( + ExprName { + range: 1758..1759, + id: "T", + ctx: Load, + }, + ), + ), + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: Some( + Name( + ExprName { + range: 1764..1765, + id: "T", + ctx: Load, + }, + ), + ), + body: [ + Pass( + StmtPass { + range: 1771..1775, + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 1778..1824, + is_async: false, + decorator_list: [], + name: Identifier { + id: "func", + range: 1782..1786, + }, + type_params: Some( + TypeParams { + range: 1786..1803, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 1787..1802, + name: Identifier { + id: "T", + range: 1787..1788, + }, + bound: Some( + Tuple( + ExprTuple { + range: 1790..1802, + elts: [ + Name( + ExprName { + range: 1791..1794, + id: "str", + ctx: Load, + }, + ), + Name( + ExprName { + range: 1796..1801, + id: "bytes", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + ), + default: None, + }, + ), + ], + }, + ), + parameters: Parameters { + range: 1803..1809, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 1804..1808, + parameter: Parameter { + range: 1804..1808, + name: Identifier { + id: "a", + range: 1804..1805, + }, + annotation: Some( + Name( + ExprName { + range: 1807..1808, + id: "T", + ctx: Load, + }, + ), + ), + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: Some( + Name( + ExprName { + range: 1813..1814, + id: "T", + ctx: Load, + }, + ), + ), + body: [ + Pass( + StmtPass { + range: 1820..1824, + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 1827..1873, + is_async: false, + decorator_list: [], + name: Identifier { + id: "func", + range: 1831..1835, + }, + type_params: Some( + TypeParams { + range: 1835..1840, + type_params: [ + TypeVarTuple( + TypeParamTypeVarTuple { + range: 1836..1839, + name: Identifier { + id: "Ts", + range: 1837..1839, + }, + default: None, + }, + ), + ], + }, + ), + parameters: Parameters { + range: 1840..1849, + posonlyargs: [], + args: [], + vararg: Some( + Parameter { + range: 1841..1848, + name: Identifier { + id: "a", + range: 1842..1843, + }, + annotation: Some( + Starred( + ExprStarred { + range: 1845..1848, + value: Name( + ExprName { + range: 1846..1848, + id: "Ts", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ), + }, + ), + kwonlyargs: [], + kwarg: None, + }, + returns: Some( + Subscript( + ExprSubscript { + range: 1853..1863, + value: Name( + ExprName { + range: 1853..1858, + id: "Tuple", + ctx: Load, + }, + ), + slice: Tuple( + ExprTuple { + range: 1859..1862, + elts: [ + Starred( + ExprStarred { + range: 1859..1862, + value: Name( + ExprName { + range: 1860..1862, + id: "Ts", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + ctx: Load, + }, + ), + ), + body: [ + Pass( + StmtPass { + range: 1869..1873, + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 1876..1934, + is_async: false, + decorator_list: [], + name: Identifier { + id: "func", + range: 1880..1884, + }, + type_params: Some( + TypeParams { + range: 1884..1889, + type_params: [ + ParamSpec( + TypeParamParamSpec { + range: 1885..1888, + name: Identifier { + id: "P", + range: 1887..1888, + }, + default: None, + }, + ), + ], + }, + ), + parameters: Parameters { + range: 1889..1924, + posonlyargs: [], + args: [], + vararg: Some( + Parameter { + range: 1890..1903, + name: Identifier { + id: "args", + range: 1891..1895, + }, + annotation: Some( + Attribute( + ExprAttribute { + range: 1897..1903, + value: Name( + ExprName { + range: 1897..1898, + id: "P", + ctx: Load, + }, + ), + attr: Identifier { + id: "args", + range: 1899..1903, + }, + ctx: Load, + }, + ), + ), + }, + ), + kwonlyargs: [], + kwarg: Some( + Parameter { + range: 1905..1923, + name: Identifier { + id: "kwargs", + range: 1907..1913, + }, + annotation: Some( + Attribute( + ExprAttribute { + range: 1915..1923, + value: Name( + ExprName { + range: 1915..1916, + id: "P", + ctx: Load, + }, + ), + attr: Identifier { + id: "kwargs", + range: 1917..1923, + }, + ctx: Load, + }, + ), + ), + }, + ), + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 1930..1934, + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 1937..1978, + is_async: false, + decorator_list: [], + name: Identifier { + id: "func", + range: 1941..1945, + }, + type_params: Some( + TypeParams { + range: 1945..1966, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 1946..1947, + name: Identifier { + id: "T", + range: 1946..1947, + }, + bound: None, + default: None, + }, + ), + TypeVar( + TypeParamTypeVar { + range: 1949..1955, + name: Identifier { + id: "U", + range: 1949..1950, + }, + bound: Some( + Name( + ExprName { + range: 1952..1955, + id: "str", + ctx: Load, + }, + ), + ), + default: None, + }, + ), + TypeVarTuple( + TypeParamTypeVarTuple { + range: 1957..1960, + name: Identifier { + id: "Ts", + range: 1958..1960, + }, + default: None, + }, + ), + ParamSpec( + TypeParamParamSpec { + range: 1962..1965, + name: Identifier { + id: "P", + range: 1964..1965, + }, + default: None, + }, + ), + ], + }, + ), + parameters: Parameters { + range: 1966..1968, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 1974..1978, + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 1981..2000, + is_async: false, + decorator_list: [], + name: Identifier { + id: "ellipsis", + range: 1985..1993, + }, + type_params: None, + parameters: Parameters { + range: 1993..1995, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Expr( + StmtExpr { + range: 1997..2000, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 1997..2000, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 2003..2064, + is_async: false, + decorator_list: [], + name: Identifier { + id: "multiple_statements", + range: 2007..2026, + }, + type_params: None, + parameters: Parameters { + range: 2026..2028, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: Some( + Name( + ExprName { + range: 2032..2035, + id: "int", + ctx: Load, + }, + ), + ), + body: [ + Expr( + StmtExpr { + range: 2041..2047, + value: Call( + ExprCall { + range: 2041..2047, + func: Name( + ExprName { + range: 2041..2045, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 2045..2047, + args: [], + keywords: [], + }, + }, + ), + }, + ), + Pass( + StmtPass { + range: 2052..2056, + }, + ), + Expr( + StmtExpr { + range: 2061..2064, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 2061..2064, + }, + ), + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 2067..2091, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 2071..2074, + }, + type_params: None, + parameters: Parameters { + range: 2074..2081, + posonlyargs: [], + args: [], + vararg: Some( + Parameter { + range: 2075..2080, + name: Identifier { + id: "args", + range: 2076..2080, + }, + annotation: None, + }, + ), + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 2087..2091, + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 2094..2121, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 2098..2101, + }, + type_params: None, + parameters: Parameters { + range: 2101..2111, + posonlyargs: [], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: Some( + Parameter { + range: 2102..2110, + name: Identifier { + id: "kwargs", + range: 2104..2110, + }, + annotation: None, + }, + ), + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 2117..2121, + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 2124..2158, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 2128..2131, + }, + type_params: None, + parameters: Parameters { + range: 2131..2148, + posonlyargs: [], + args: [], + vararg: Some( + Parameter { + range: 2132..2137, + name: Identifier { + id: "args", + range: 2133..2137, + }, + annotation: None, + }, + ), + kwonlyargs: [], + kwarg: Some( + Parameter { + range: 2139..2147, + name: Identifier { + id: "kwargs", + range: 2141..2147, + }, + annotation: None, + }, + ), + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 2154..2158, + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 2161..2184, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 2165..2168, + }, + type_params: None, + parameters: Parameters { + range: 2168..2174, + posonlyargs: [ + ParameterWithDefault { + range: 2169..2170, + parameter: Parameter { + range: 2169..2170, + name: Identifier { + id: "a", + range: 2169..2170, + }, + annotation: None, + }, + default: None, + }, + ], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 2180..2184, + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 2187..2213, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 2191..2194, + }, + type_params: None, + parameters: Parameters { + range: 2194..2203, + posonlyargs: [ + ParameterWithDefault { + range: 2195..2196, + parameter: Parameter { + range: 2195..2196, + name: Identifier { + id: "a", + range: 2195..2196, + }, + annotation: None, + }, + default: None, + }, + ], + args: [ + ParameterWithDefault { + range: 2201..2202, + parameter: Parameter { + range: 2201..2202, + name: Identifier { + id: "b", + range: 2201..2202, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 2209..2213, + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 2216..2242, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 2220..2223, + }, + type_params: None, + parameters: Parameters { + range: 2223..2232, + posonlyargs: [ + ParameterWithDefault { + range: 2224..2227, + parameter: Parameter { + range: 2224..2225, + name: Identifier { + id: "a", + range: 2224..2225, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 2226..2227, + value: Int( + 1, + ), + }, + ), + ), + }, + ], + args: [], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 2238..2242, + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 2245..2277, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 2249..2252, + }, + type_params: None, + parameters: Parameters { + range: 2252..2267, + posonlyargs: [ + ParameterWithDefault { + range: 2253..2254, + parameter: Parameter { + range: 2253..2254, + name: Identifier { + id: "a", + range: 2253..2254, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 2256..2257, + parameter: Parameter { + range: 2256..2257, + name: Identifier { + id: "b", + range: 2256..2257, + }, + annotation: None, + }, + default: None, + }, + ], + args: [], + vararg: None, + kwonlyargs: [ + ParameterWithDefault { + range: 2265..2266, + parameter: Parameter { + range: 2265..2266, + name: Identifier { + id: "c", + range: 2265..2266, + }, + annotation: None, + }, + default: None, + }, + ], + kwarg: None, + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 2273..2277, + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 2280..2309, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 2284..2287, + }, + type_params: None, + parameters: Parameters { + range: 2287..2299, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 2288..2292, + parameter: Parameter { + range: 2288..2290, + name: Identifier { + id: "kw", + range: 2288..2290, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 2291..2292, + value: Int( + 1, + ), + }, + ), + ), + }, + ], + vararg: None, + kwonlyargs: [ + ParameterWithDefault { + range: 2297..2298, + parameter: Parameter { + range: 2297..2298, + name: Identifier { + id: "a", + range: 2297..2298, + }, + annotation: None, + }, + default: None, + }, + ], + kwarg: None, + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 2305..2309, + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 2312..2357, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 2316..2319, + }, + type_params: None, + parameters: Parameters { + range: 2319..2347, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 2320..2326, + parameter: Parameter { + range: 2320..2326, + name: Identifier { + id: "x", + range: 2320..2321, + }, + annotation: Some( + Name( + ExprName { + range: 2323..2326, + id: "int", + ctx: Load, + }, + ), + ), + }, + default: None, + }, + ParameterWithDefault { + range: 2328..2336, + parameter: Parameter { + range: 2328..2336, + name: Identifier { + id: "y", + range: 2328..2329, + }, + annotation: Some( + StringLiteral( + ExprStringLiteral { + range: 2331..2336, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 2331..2336, + value: "str", + flags: StringLiteralFlags { + quote_style: Double, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + ), + }, + default: None, + }, + ParameterWithDefault { + range: 2338..2346, + parameter: Parameter { + range: 2338..2346, + name: Identifier { + id: "z", + range: 2338..2339, + }, + annotation: Some( + BinOp( + ExprBinOp { + range: 2341..2346, + left: NumberLiteral( + ExprNumberLiteral { + range: 2341..2342, + value: Int( + 1, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 2345..2346, + value: Int( + 2, + ), + }, + ), + }, + ), + ), + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 2353..2357, + }, + ), + ], + }, + ), + FunctionDef( + StmtFunctionDef { + range: 2360..2398, + is_async: false, + decorator_list: [], + name: Identifier { + id: "foo", + range: 2364..2367, + }, + type_params: None, + parameters: Parameters { + range: 2367..2388, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 2368..2372, + parameter: Parameter { + range: 2368..2372, + name: Identifier { + id: "self", + range: 2368..2372, + }, + annotation: None, + }, + default: None, + }, + ParameterWithDefault { + range: 2374..2377, + parameter: Parameter { + range: 2374..2375, + name: Identifier { + id: "a", + range: 2374..2375, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 2376..2377, + value: Int( + 1, + ), + }, + ), + ), + }, + ParameterWithDefault { + range: 2379..2382, + parameter: Parameter { + range: 2379..2380, + name: Identifier { + id: "b", + range: 2379..2380, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 2381..2382, + value: Int( + 2, + ), + }, + ), + ), + }, + ParameterWithDefault { + range: 2384..2387, + parameter: Parameter { + range: 2384..2385, + name: Identifier { + id: "c", + range: 2384..2385, + }, + annotation: None, + }, + default: Some( + NumberLiteral( + ExprNumberLiteral { + range: 2386..2387, + value: Int( + 3, + ), + }, + ), + ), + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + returns: None, + body: [ + Pass( + StmtPass { + range: 2394..2398, + }, + ), + ], + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__if.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__if.py.snap.new new file mode 100644 index 0000000000..61665feb7b --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__if.py.snap.new @@ -0,0 +1,674 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/valid/statement/if.py +--- +## AST + +``` +Module( + ModModule { + range: 0..375, + body: [ + If( + StmtIf { + range: 0..28, + test: NumberLiteral( + ExprNumberLiteral { + range: 3..4, + value: Int( + 1, + ), + }, + ), + body: [ + Expr( + StmtExpr { + range: 6..8, + value: NumberLiteral( + ExprNumberLiteral { + range: 6..8, + value: Int( + 10, + ), + }, + ), + }, + ), + ], + elif_else_clauses: [ + ElifElseClause { + range: 9..19, + test: Some( + NumberLiteral( + ExprNumberLiteral { + range: 14..15, + value: Int( + 2, + ), + }, + ), + ), + body: [ + Expr( + StmtExpr { + range: 17..19, + value: NumberLiteral( + ExprNumberLiteral { + range: 17..19, + value: Int( + 20, + ), + }, + ), + }, + ), + ], + }, + ElifElseClause { + range: 20..28, + test: None, + body: [ + Expr( + StmtExpr { + range: 26..28, + value: NumberLiteral( + ExprNumberLiteral { + range: 26..28, + value: Int( + 30, + ), + }, + ), + }, + ), + ], + }, + ], + }, + ), + If( + StmtIf { + range: 30..52, + test: BooleanLiteral( + ExprBooleanLiteral { + range: 33..37, + value: true, + }, + ), + body: [ + Expr( + StmtExpr { + range: 43..44, + value: NumberLiteral( + ExprNumberLiteral { + range: 43..44, + value: Int( + 1, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 49..52, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 49..52, + }, + ), + }, + ), + ], + elif_else_clauses: [], + }, + ), + If( + StmtIf { + range: 53..85, + test: Compare( + ExprCompare { + range: 56..61, + left: Name( + ExprName { + range: 56..57, + id: "x", + ctx: Load, + }, + ), + ops: [ + Lt, + ], + comparators: [ + NumberLiteral( + ExprNumberLiteral { + range: 60..61, + value: Int( + 1, + ), + }, + ), + ], + }, + ), + body: [ + Expr( + StmtExpr { + range: 67..70, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 67..70, + }, + ), + }, + ), + ], + elif_else_clauses: [ + ElifElseClause { + range: 71..85, + test: None, + body: [ + Pass( + StmtPass { + range: 81..85, + }, + ), + ], + }, + ], + }, + ), + If( + StmtIf { + range: 87..117, + test: Name( + ExprName { + range: 90..91, + id: "a", + ctx: Load, + }, + ), + body: [ + Pass( + StmtPass { + range: 97..101, + }, + ), + ], + elif_else_clauses: [ + ElifElseClause { + range: 102..117, + test: Some( + Name( + ExprName { + range: 107..108, + id: "b", + ctx: Load, + }, + ), + ), + body: [ + Expr( + StmtExpr { + range: 114..117, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 114..117, + }, + ), + }, + ), + ], + }, + ], + }, + ), + If( + StmtIf { + range: 119..203, + test: BoolOp( + ExprBoolOp { + range: 122..129, + op: And, + values: [ + Name( + ExprName { + range: 122..123, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 128..129, + id: "b", + ctx: Load, + }, + ), + ], + }, + ), + body: [ + Expr( + StmtExpr { + range: 135..138, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 135..138, + }, + ), + }, + ), + ], + elif_else_clauses: [ + ElifElseClause { + range: 139..157, + test: Some( + BooleanLiteral( + ExprBooleanLiteral { + range: 144..148, + value: true, + }, + ), + ), + body: [ + Expr( + StmtExpr { + range: 154..157, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 154..157, + }, + ), + }, + ), + ], + }, + ElifElseClause { + range: 158..173, + test: Some( + Name( + ExprName { + range: 163..164, + id: "c", + ctx: Load, + }, + ), + ), + body: [ + Expr( + StmtExpr { + range: 170..173, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 170..173, + }, + ), + }, + ), + ], + }, + ElifElseClause { + range: 174..189, + test: Some( + Name( + ExprName { + range: 179..180, + id: "d", + ctx: Load, + }, + ), + ), + body: [ + Expr( + StmtExpr { + range: 186..189, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 186..189, + }, + ), + }, + ), + ], + }, + ElifElseClause { + range: 190..203, + test: None, + body: [ + Expr( + StmtExpr { + range: 200..203, + value: Call( + ExprCall { + range: 200..203, + func: Name( + ExprName { + range: 200..201, + id: "f", + ctx: Load, + }, + ), + arguments: Arguments { + range: 201..203, + args: [], + keywords: [], + }, + }, + ), + }, + ), + ], + }, + ], + }, + ), + If( + StmtIf { + range: 229..260, + test: Named( + ExprNamed { + range: 232..238, + target: Name( + ExprName { + range: 232..233, + id: "a", + ctx: Store, + }, + ), + value: Name( + ExprName { + range: 237..238, + id: "b", + ctx: Load, + }, + ), + }, + ), + body: [ + Expr( + StmtExpr { + range: 240..243, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 240..243, + }, + ), + }, + ), + ], + elif_else_clauses: [ + ElifElseClause { + range: 244..260, + test: Some( + Named( + ExprNamed { + range: 249..255, + target: Name( + ExprName { + range: 249..250, + id: "a", + ctx: Store, + }, + ), + value: Name( + ExprName { + range: 254..255, + id: "b", + ctx: Load, + }, + ), + }, + ), + ), + body: [ + Expr( + StmtExpr { + range: 257..260, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 257..260, + }, + ), + }, + ), + ], + }, + ], + }, + ), + If( + StmtIf { + range: 261..302, + test: Lambda( + ExprLambda { + range: 264..275, + parameters: Some( + Parameters { + range: 271..272, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 271..272, + parameter: Parameter { + range: 271..272, + name: Identifier { + id: "x", + range: 271..272, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: Name( + ExprName { + range: 274..275, + id: "x", + ctx: Load, + }, + ), + }, + ), + body: [ + Expr( + StmtExpr { + range: 277..280, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 277..280, + }, + ), + }, + ), + ], + elif_else_clauses: [ + ElifElseClause { + range: 281..302, + test: Some( + Lambda( + ExprLambda { + range: 286..297, + parameters: Some( + Parameters { + range: 293..294, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 293..294, + parameter: Parameter { + range: 293..294, + name: Identifier { + id: "x", + range: 293..294, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: Name( + ExprName { + range: 296..297, + id: "x", + ctx: Load, + }, + ), + }, + ), + ), + body: [ + Expr( + StmtExpr { + range: 299..302, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 299..302, + }, + ), + }, + ), + ], + }, + ], + }, + ), + If( + StmtIf { + range: 303..336, + test: Await( + ExprAwait { + range: 306..313, + value: Name( + ExprName { + range: 312..313, + id: "x", + ctx: Load, + }, + ), + }, + ), + body: [ + Expr( + StmtExpr { + range: 315..318, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 315..318, + }, + ), + }, + ), + ], + elif_else_clauses: [ + ElifElseClause { + range: 319..336, + test: Some( + Await( + ExprAwait { + range: 324..331, + value: Name( + ExprName { + range: 330..331, + id: "x", + ctx: Load, + }, + ), + }, + ), + ), + body: [ + Expr( + StmtExpr { + range: 333..336, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 333..336, + }, + ), + }, + ), + ], + }, + ], + }, + ), + If( + StmtIf { + range: 337..374, + test: Yield( + ExprYield { + range: 341..348, + value: Some( + Name( + ExprName { + range: 347..348, + id: "x", + ctx: Load, + }, + ), + ), + }, + ), + body: [ + Expr( + StmtExpr { + range: 351..354, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 351..354, + }, + ), + }, + ), + ], + elif_else_clauses: [ + ElifElseClause { + range: 355..374, + test: Some( + Yield( + ExprYield { + range: 361..368, + value: Some( + Name( + ExprName { + range: 367..368, + id: "x", + ctx: Load, + }, + ), + ), + }, + ), + ), + body: [ + Expr( + StmtExpr { + range: 371..374, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 371..374, + }, + ), + }, + ), + ], + }, + ], + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__import.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__import.py.snap.new new file mode 100644 index 0000000000..e1d3248def --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__import.py.snap.new @@ -0,0 +1,130 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/valid/statement/import.py +--- +## AST + +``` +Module( + ModModule { + range: 0..92, + body: [ + Import( + StmtImport { + range: 0..8, + names: [ + Alias { + range: 7..8, + name: Identifier { + id: "a", + range: 7..8, + }, + asname: None, + }, + ], + }, + ), + Import( + StmtImport { + range: 9..21, + names: [ + Alias { + range: 16..21, + name: Identifier { + id: "a.b.c", + range: 16..21, + }, + asname: None, + }, + ], + }, + ), + Import( + StmtImport { + range: 22..39, + names: [ + Alias { + range: 29..39, + name: Identifier { + id: "a.b.c", + range: 29..34, + }, + asname: Some( + Identifier { + id: "d", + range: 38..39, + }, + ), + }, + ], + }, + ), + Import( + StmtImport { + range: 40..54, + names: [ + Alias { + range: 47..48, + name: Identifier { + id: "a", + range: 47..48, + }, + asname: None, + }, + Alias { + range: 50..51, + name: Identifier { + id: "b", + range: 50..51, + }, + asname: None, + }, + Alias { + range: 53..54, + name: Identifier { + id: "c", + range: 53..54, + }, + asname: None, + }, + ], + }, + ), + Import( + StmtImport { + range: 55..91, + names: [ + Alias { + range: 62..74, + name: Identifier { + id: "foo.bar", + range: 62..69, + }, + asname: Some( + Identifier { + id: "a", + range: 73..74, + }, + ), + }, + Alias { + range: 76..91, + name: Identifier { + id: "a.b.c.d", + range: 76..83, + }, + asname: Some( + Identifier { + id: "abcd", + range: 87..91, + }, + ), + }, + ], + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__match.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__match.py.snap.new new file mode 100644 index 0000000000..da1222e23e --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__match.py.snap.new @@ -0,0 +1,7717 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/valid/statement/match.py +--- +## AST + +``` +Module( + ModModule { + range: 0..5743, + body: [ + Match( + StmtMatch { + range: 67..103, + subject: Name( + ExprName { + range: 73..74, + id: "x", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 80..103, + pattern: MatchValue( + PatternMatchValue { + range: 85..88, + value: UnaryOp( + ExprUnaryOp { + range: 85..88, + op: USub, + operand: NumberLiteral( + ExprNumberLiteral { + range: 86..88, + value: Complex { + real: 0.0, + imag: 0.0, + }, + }, + ), + }, + ), + }, + ), + guard: None, + body: [ + Assign( + StmtAssign { + range: 98..103, + targets: [ + Name( + ExprName { + range: 98..99, + id: "y", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 102..103, + value: Int( + 0, + ), + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 126..167, + subject: Name( + ExprName { + range: 132..133, + id: "x", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 139..167, + pattern: MatchClass( + PatternMatchClass { + range: 144..152, + cls: Name( + ExprName { + range: 144..149, + id: "bytes", + ctx: Load, + }, + ), + arguments: PatternArguments { + range: 149..152, + patterns: [ + MatchAs( + PatternMatchAs { + range: 150..151, + pattern: None, + name: Some( + Identifier { + id: "z", + range: 150..151, + }, + ), + }, + ), + ], + keywords: [], + }, + }, + ), + guard: None, + body: [ + Assign( + StmtAssign { + range: 162..167, + targets: [ + Name( + ExprName { + range: 162..163, + id: "y", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 166..167, + value: Int( + 0, + ), + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 190..260, + subject: Name( + ExprName { + range: 196..197, + id: "x", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 203..229, + pattern: MatchValue( + PatternMatchValue { + range: 208..209, + value: NumberLiteral( + ExprNumberLiteral { + range: 208..209, + value: Int( + 0, + ), + }, + ), + }, + ), + guard: Some( + NumberLiteral( + ExprNumberLiteral { + range: 213..214, + value: Int( + 0, + ), + }, + ), + ), + body: [ + Assign( + StmtAssign { + range: 224..229, + targets: [ + Name( + ExprName { + range: 224..225, + id: "y", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 228..229, + value: Int( + 0, + ), + }, + ), + }, + ), + ], + }, + MatchCase { + range: 234..260, + pattern: MatchValue( + PatternMatchValue { + range: 239..240, + value: NumberLiteral( + ExprNumberLiteral { + range: 239..240, + value: Int( + 0, + ), + }, + ), + }, + ), + guard: Some( + NumberLiteral( + ExprNumberLiteral { + range: 244..245, + value: Int( + 1, + ), + }, + ), + ), + body: [ + Assign( + StmtAssign { + range: 255..260, + targets: [ + Name( + ExprName { + range: 255..256, + id: "y", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 259..260, + value: Int( + 1, + ), + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 283..332, + subject: NumberLiteral( + ExprNumberLiteral { + range: 289..290, + value: Int( + 3, + ), + }, + ), + cases: [ + MatchCase { + range: 296..332, + pattern: MatchOr( + PatternMatchOr { + range: 301..314, + patterns: [ + MatchValue( + PatternMatchValue { + range: 301..302, + value: NumberLiteral( + ExprNumberLiteral { + range: 301..302, + value: Int( + 0, + ), + }, + ), + }, + ), + MatchValue( + PatternMatchValue { + range: 305..306, + value: NumberLiteral( + ExprNumberLiteral { + range: 305..306, + value: Int( + 1, + ), + }, + ), + }, + ), + MatchValue( + PatternMatchValue { + range: 309..310, + value: NumberLiteral( + ExprNumberLiteral { + range: 309..310, + value: Int( + 2, + ), + }, + ), + }, + ), + MatchValue( + PatternMatchValue { + range: 313..314, + value: NumberLiteral( + ExprNumberLiteral { + range: 313..314, + value: Int( + 3, + ), + }, + ), + }, + ), + ], + }, + ), + guard: None, + body: [ + Assign( + StmtAssign { + range: 324..332, + targets: [ + Name( + ExprName { + range: 324..325, + id: "x", + ctx: Store, + }, + ), + ], + value: BooleanLiteral( + ExprBooleanLiteral { + range: 328..332, + value: true, + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 355..403, + subject: Name( + ExprName { + range: 361..362, + id: "x", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 368..403, + pattern: MatchOr( + PatternMatchOr { + range: 373..388, + patterns: [ + MatchSequence( + PatternMatchSequence { + range: 373..379, + patterns: [ + MatchValue( + PatternMatchValue { + range: 374..375, + value: NumberLiteral( + ExprNumberLiteral { + range: 374..375, + value: Int( + 0, + ), + }, + ), + }, + ), + MatchValue( + PatternMatchValue { + range: 377..378, + value: NumberLiteral( + ExprNumberLiteral { + range: 377..378, + value: Int( + 1, + ), + }, + ), + }, + ), + ], + }, + ), + MatchSequence( + PatternMatchSequence { + range: 382..388, + patterns: [ + MatchValue( + PatternMatchValue { + range: 383..384, + value: NumberLiteral( + ExprNumberLiteral { + range: 383..384, + value: Int( + 1, + ), + }, + ), + }, + ), + MatchValue( + PatternMatchValue { + range: 386..387, + value: NumberLiteral( + ExprNumberLiteral { + range: 386..387, + value: Int( + 0, + ), + }, + ), + }, + ), + ], + }, + ), + ], + }, + ), + guard: None, + body: [ + Assign( + StmtAssign { + range: 398..403, + targets: [ + Name( + ExprName { + range: 398..399, + id: "y", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 402..403, + value: Int( + 0, + ), + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 445..523, + subject: Name( + ExprName { + range: 451..452, + id: "x", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 458..489, + pattern: MatchSequence( + PatternMatchSequence { + range: 463..467, + patterns: [ + MatchStar( + PatternMatchStar { + range: 464..466, + name: None, + }, + ), + ], + }, + ), + guard: None, + body: [ + Return( + StmtReturn { + range: 477..489, + value: Some( + StringLiteral( + ExprStringLiteral { + range: 484..489, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 484..489, + value: "seq", + flags: StringLiteralFlags { + quote_style: Double, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + ), + }, + ), + ], + }, + MatchCase { + range: 494..523, + pattern: MatchMapping( + PatternMatchMapping { + range: 499..501, + keys: [], + patterns: [], + rest: None, + }, + ), + guard: None, + body: [ + Return( + StmtReturn { + range: 511..523, + value: Some( + StringLiteral( + ExprStringLiteral { + range: 518..523, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 518..523, + value: "map", + flags: StringLiteralFlags { + quote_style: Double, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 546..714, + subject: Name( + ExprName { + range: 552..553, + id: "x", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 559..594, + pattern: MatchMapping( + PatternMatchMapping { + range: 564..579, + keys: [ + NumberLiteral( + ExprNumberLiteral { + range: 565..566, + value: Int( + 0, + ), + }, + ), + ], + patterns: [ + MatchSequence( + PatternMatchSequence { + range: 568..578, + patterns: [ + MatchValue( + PatternMatchValue { + range: 569..570, + value: NumberLiteral( + ExprNumberLiteral { + range: 569..570, + value: Int( + 1, + ), + }, + ), + }, + ), + MatchValue( + PatternMatchValue { + range: 572..573, + value: NumberLiteral( + ExprNumberLiteral { + range: 572..573, + value: Int( + 2, + ), + }, + ), + }, + ), + MatchMapping( + PatternMatchMapping { + range: 575..577, + keys: [], + patterns: [], + rest: None, + }, + ), + ], + }, + ), + ], + rest: None, + }, + ), + guard: None, + body: [ + Assign( + StmtAssign { + range: 589..594, + targets: [ + Name( + ExprName { + range: 589..590, + id: "y", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 593..594, + value: Int( + 0, + ), + }, + ), + }, + ), + ], + }, + MatchCase { + range: 599..687, + pattern: MatchOr( + PatternMatchOr { + range: 604..672, + patterns: [ + MatchMapping( + PatternMatchMapping { + range: 604..626, + keys: [ + NumberLiteral( + ExprNumberLiteral { + range: 605..606, + value: Int( + 0, + ), + }, + ), + ], + patterns: [ + MatchOr( + PatternMatchOr { + range: 608..625, + patterns: [ + MatchSequence( + PatternMatchSequence { + range: 608..618, + patterns: [ + MatchValue( + PatternMatchValue { + range: 609..610, + value: NumberLiteral( + ExprNumberLiteral { + range: 609..610, + value: Int( + 1, + ), + }, + ), + }, + ), + MatchValue( + PatternMatchValue { + range: 612..613, + value: NumberLiteral( + ExprNumberLiteral { + range: 612..613, + value: Int( + 2, + ), + }, + ), + }, + ), + MatchMapping( + PatternMatchMapping { + range: 615..617, + keys: [], + patterns: [], + rest: None, + }, + ), + ], + }, + ), + MatchSingleton( + PatternMatchSingleton { + range: 621..625, + value: True, + }, + ), + ], + }, + ), + ], + rest: None, + }, + ), + MatchMapping( + PatternMatchMapping { + range: 629..638, + keys: [ + NumberLiteral( + ExprNumberLiteral { + range: 630..631, + value: Int( + 1, + ), + }, + ), + ], + patterns: [ + MatchSequence( + PatternMatchSequence { + range: 633..637, + patterns: [ + MatchSequence( + PatternMatchSequence { + range: 634..636, + patterns: [], + }, + ), + ], + }, + ), + ], + rest: None, + }, + ), + MatchMapping( + PatternMatchMapping { + range: 641..656, + keys: [ + NumberLiteral( + ExprNumberLiteral { + range: 642..643, + value: Int( + 0, + ), + }, + ), + ], + patterns: [ + MatchSequence( + PatternMatchSequence { + range: 645..655, + patterns: [ + MatchValue( + PatternMatchValue { + range: 646..647, + value: NumberLiteral( + ExprNumberLiteral { + range: 646..647, + value: Int( + 1, + ), + }, + ), + }, + ), + MatchValue( + PatternMatchValue { + range: 649..650, + value: NumberLiteral( + ExprNumberLiteral { + range: 649..650, + value: Int( + 2, + ), + }, + ), + }, + ), + MatchMapping( + PatternMatchMapping { + range: 652..654, + keys: [], + patterns: [], + rest: None, + }, + ), + ], + }, + ), + ], + rest: None, + }, + ), + MatchSequence( + PatternMatchSequence { + range: 659..661, + patterns: [], + }, + ), + MatchValue( + PatternMatchValue { + range: 664..667, + value: StringLiteral( + ExprStringLiteral { + range: 664..667, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 664..667, + value: "X", + flags: StringLiteralFlags { + quote_style: Double, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + }, + ), + MatchMapping( + PatternMatchMapping { + range: 670..672, + keys: [], + patterns: [], + rest: None, + }, + ), + ], + }, + ), + guard: None, + body: [ + Assign( + StmtAssign { + range: 682..687, + targets: [ + Name( + ExprName { + range: 682..683, + id: "y", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 686..687, + value: Int( + 1, + ), + }, + ), + }, + ), + ], + }, + MatchCase { + range: 692..714, + pattern: MatchSequence( + PatternMatchSequence { + range: 697..699, + patterns: [], + }, + ), + guard: None, + body: [ + Assign( + StmtAssign { + range: 709..714, + targets: [ + Name( + ExprName { + range: 709..710, + id: "y", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 713..714, + value: Int( + 2, + ), + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 737..782, + subject: Name( + ExprName { + range: 743..744, + id: "x", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 750..782, + pattern: MatchValue( + PatternMatchValue { + range: 755..767, + value: BinOp( + ExprBinOp { + range: 755..767, + left: NumberLiteral( + ExprNumberLiteral { + range: 755..759, + value: Float( + 0.25, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 762..767, + value: Complex { + real: 0.0, + imag: 1.75, + }, + }, + ), + }, + ), + }, + ), + guard: None, + body: [ + Assign( + StmtAssign { + range: 777..782, + targets: [ + Name( + ExprName { + range: 777..778, + id: "y", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 781..782, + value: Int( + 0, + ), + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 805..841, + subject: Name( + ExprName { + range: 811..812, + id: "x", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 818..841, + pattern: MatchValue( + PatternMatchValue { + range: 823..826, + value: UnaryOp( + ExprUnaryOp { + range: 823..826, + op: USub, + operand: NumberLiteral( + ExprNumberLiteral { + range: 824..826, + value: Complex { + real: 0.0, + imag: 0.0, + }, + }, + ), + }, + ), + }, + ), + guard: None, + body: [ + Assign( + StmtAssign { + range: 836..841, + targets: [ + Name( + ExprName { + range: 836..837, + id: "y", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 840..841, + value: Int( + 0, + ), + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 864..913, + subject: NumberLiteral( + ExprNumberLiteral { + range: 870..871, + value: Int( + 4, + ), + }, + ), + cases: [ + MatchCase { + range: 877..913, + pattern: MatchOr( + PatternMatchOr { + range: 882..895, + patterns: [ + MatchValue( + PatternMatchValue { + range: 882..883, + value: NumberLiteral( + ExprNumberLiteral { + range: 882..883, + value: Int( + 0, + ), + }, + ), + }, + ), + MatchValue( + PatternMatchValue { + range: 886..887, + value: NumberLiteral( + ExprNumberLiteral { + range: 886..887, + value: Int( + 1, + ), + }, + ), + }, + ), + MatchValue( + PatternMatchValue { + range: 890..891, + value: NumberLiteral( + ExprNumberLiteral { + range: 890..891, + value: Int( + 2, + ), + }, + ), + }, + ), + MatchValue( + PatternMatchValue { + range: 894..895, + value: NumberLiteral( + ExprNumberLiteral { + range: 894..895, + value: Int( + 3, + ), + }, + ), + }, + ), + ], + }, + ), + guard: None, + body: [ + Assign( + StmtAssign { + range: 905..913, + targets: [ + Name( + ExprName { + range: 905..906, + id: "x", + ctx: Store, + }, + ), + ], + value: BooleanLiteral( + ExprBooleanLiteral { + range: 909..913, + value: true, + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 936..975, + subject: Name( + ExprName { + range: 942..943, + id: "x", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 949..975, + pattern: MatchValue( + PatternMatchValue { + range: 954..955, + value: NumberLiteral( + ExprNumberLiteral { + range: 954..955, + value: Int( + 0, + ), + }, + ), + }, + ), + guard: Some( + Name( + ExprName { + range: 959..960, + id: "x", + ctx: Load, + }, + ), + ), + body: [ + Assign( + StmtAssign { + range: 970..975, + targets: [ + Name( + ExprName { + range: 970..971, + id: "y", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 974..975, + value: Int( + 0, + ), + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 998..1098, + subject: Name( + ExprName { + range: 1004..1005, + id: "x", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 1011..1037, + pattern: MatchMapping( + PatternMatchMapping { + range: 1016..1022, + keys: [ + NumberLiteral( + ExprNumberLiteral { + range: 1017..1018, + value: Int( + 1, + ), + }, + ), + ], + patterns: [ + MatchValue( + PatternMatchValue { + range: 1020..1021, + value: NumberLiteral( + ExprNumberLiteral { + range: 1020..1021, + value: Int( + 0, + ), + }, + ), + }, + ), + ], + rest: None, + }, + ), + guard: None, + body: [ + Assign( + StmtAssign { + range: 1032..1037, + targets: [ + Name( + ExprName { + range: 1032..1033, + id: "y", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 1036..1037, + value: Int( + 0, + ), + }, + ), + }, + ), + ], + }, + MatchCase { + range: 1042..1068, + pattern: MatchMapping( + PatternMatchMapping { + range: 1047..1053, + keys: [ + NumberLiteral( + ExprNumberLiteral { + range: 1048..1049, + value: Int( + 0, + ), + }, + ), + ], + patterns: [ + MatchValue( + PatternMatchValue { + range: 1051..1052, + value: NumberLiteral( + ExprNumberLiteral { + range: 1051..1052, + value: Int( + 0, + ), + }, + ), + }, + ), + ], + rest: None, + }, + ), + guard: None, + body: [ + Assign( + StmtAssign { + range: 1063..1068, + targets: [ + Name( + ExprName { + range: 1063..1064, + id: "y", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 1067..1068, + value: Int( + 1, + ), + }, + ), + }, + ), + ], + }, + MatchCase { + range: 1073..1098, + pattern: MatchMapping( + PatternMatchMapping { + range: 1078..1083, + keys: [], + patterns: [], + rest: Some( + Identifier { + id: "z", + range: 1081..1082, + }, + ), + }, + ), + guard: None, + body: [ + Assign( + StmtAssign { + range: 1093..1098, + targets: [ + Name( + ExprName { + range: 1093..1094, + id: "y", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 1097..1098, + value: Int( + 2, + ), + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 1121..1162, + subject: Call( + ExprCall { + range: 1127..1132, + func: Name( + ExprName { + range: 1127..1130, + id: "Seq", + ctx: Load, + }, + ), + arguments: Arguments { + range: 1130..1132, + args: [], + keywords: [], + }, + }, + ), + cases: [ + MatchCase { + range: 1138..1162, + pattern: MatchSequence( + PatternMatchSequence { + range: 1143..1147, + patterns: [ + MatchStar( + PatternMatchStar { + range: 1144..1146, + name: None, + }, + ), + ], + }, + ), + guard: None, + body: [ + Assign( + StmtAssign { + range: 1157..1162, + targets: [ + Name( + ExprName { + range: 1157..1158, + id: "y", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 1161..1162, + value: Int( + 0, + ), + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 1185..1245, + subject: Name( + ExprName { + range: 1191..1192, + id: "x", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 1198..1219, + pattern: MatchValue( + PatternMatchValue { + range: 1203..1204, + value: NumberLiteral( + ExprNumberLiteral { + range: 1203..1204, + value: Int( + 1, + ), + }, + ), + }, + ), + guard: None, + body: [ + Assign( + StmtAssign { + range: 1214..1219, + targets: [ + Name( + ExprName { + range: 1214..1215, + id: "y", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 1218..1219, + value: Int( + 0, + ), + }, + ), + }, + ), + ], + }, + MatchCase { + range: 1224..1245, + pattern: MatchValue( + PatternMatchValue { + range: 1229..1230, + value: NumberLiteral( + ExprNumberLiteral { + range: 1229..1230, + value: Int( + 1, + ), + }, + ), + }, + ), + guard: None, + body: [ + Assign( + StmtAssign { + range: 1240..1245, + targets: [ + Name( + ExprName { + range: 1240..1241, + id: "y", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 1244..1245, + value: Int( + 1, + ), + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 1268..1315, + subject: Name( + ExprName { + range: 1274..1275, + id: "x", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 1281..1315, + pattern: MatchMapping( + PatternMatchMapping { + range: 1286..1298, + keys: [ + StringLiteral( + ExprStringLiteral { + range: 1287..1292, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 1287..1292, + value: "foo", + flags: StringLiteralFlags { + quote_style: Double, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + ], + patterns: [ + MatchAs( + PatternMatchAs { + range: 1294..1297, + pattern: None, + name: Some( + Identifier { + id: "bar", + range: 1294..1297, + }, + ), + }, + ), + ], + rest: None, + }, + ), + guard: None, + body: [ + Assign( + StmtAssign { + range: 1308..1315, + targets: [ + Name( + ExprName { + range: 1308..1309, + id: "y", + ctx: Store, + }, + ), + ], + value: Name( + ExprName { + range: 1312..1315, + id: "bar", + ctx: Load, + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 1338..1392, + subject: Tuple( + ExprTuple { + range: 1344..1353, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 1345..1346, + value: Int( + 0, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 1348..1349, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 1351..1352, + value: Int( + 2, + ), + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + cases: [ + MatchCase { + range: 1359..1392, + pattern: MatchSequence( + PatternMatchSequence { + range: 1364..1377, + patterns: [ + MatchValue( + PatternMatchValue { + range: 1365..1366, + value: NumberLiteral( + ExprNumberLiteral { + range: 1365..1366, + value: Int( + 0, + ), + }, + ), + }, + ), + MatchValue( + PatternMatchValue { + range: 1368..1369, + value: NumberLiteral( + ExprNumberLiteral { + range: 1368..1369, + value: Int( + 1, + ), + }, + ), + }, + ), + MatchStar( + PatternMatchStar { + range: 1371..1373, + name: Some( + Identifier { + id: "x", + range: 1372..1373, + }, + ), + }, + ), + MatchValue( + PatternMatchValue { + range: 1375..1376, + value: NumberLiteral( + ExprNumberLiteral { + range: 1375..1376, + value: Int( + 2, + ), + }, + ), + }, + ), + ], + }, + ), + guard: None, + body: [ + Assign( + StmtAssign { + range: 1387..1392, + targets: [ + Name( + ExprName { + range: 1387..1388, + id: "y", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 1391..1392, + value: Int( + 0, + ), + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 1415..1529, + subject: Name( + ExprName { + range: 1421..1422, + id: "x", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 1428..1451, + pattern: MatchSequence( + PatternMatchSequence { + range: 1433..1436, + patterns: [ + MatchValue( + PatternMatchValue { + range: 1434..1435, + value: NumberLiteral( + ExprNumberLiteral { + range: 1434..1435, + value: Int( + 0, + ), + }, + ), + }, + ), + ], + }, + ), + guard: None, + body: [ + Assign( + StmtAssign { + range: 1446..1451, + targets: [ + Name( + ExprName { + range: 1446..1447, + id: "y", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 1450..1451, + value: Int( + 0, + ), + }, + ), + }, + ), + ], + }, + MatchCase { + range: 1456..1498, + pattern: MatchSequence( + PatternMatchSequence { + range: 1461..1467, + patterns: [ + MatchValue( + PatternMatchValue { + range: 1462..1463, + value: NumberLiteral( + ExprNumberLiteral { + range: 1462..1463, + value: Int( + 1, + ), + }, + ), + }, + ), + MatchValue( + PatternMatchValue { + range: 1465..1466, + value: NumberLiteral( + ExprNumberLiteral { + range: 1465..1466, + value: Int( + 0, + ), + }, + ), + }, + ), + ], + }, + ), + guard: Some( + Named( + ExprNamed { + range: 1472..1482, + target: Name( + ExprName { + range: 1472..1473, + id: "x", + ctx: Store, + }, + ), + value: Subscript( + ExprSubscript { + range: 1477..1482, + value: Name( + ExprName { + range: 1477..1478, + id: "x", + ctx: Load, + }, + ), + slice: Slice( + ExprSlice { + range: 1479..1481, + lower: None, + upper: Some( + NumberLiteral( + ExprNumberLiteral { + range: 1480..1481, + value: Int( + 0, + ), + }, + ), + ), + step: None, + }, + ), + ctx: Load, + }, + ), + }, + ), + ), + body: [ + Assign( + StmtAssign { + range: 1493..1498, + targets: [ + Name( + ExprName { + range: 1493..1494, + id: "y", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 1497..1498, + value: Int( + 1, + ), + }, + ), + }, + ), + ], + }, + MatchCase { + range: 1503..1529, + pattern: MatchSequence( + PatternMatchSequence { + range: 1508..1514, + patterns: [ + MatchValue( + PatternMatchValue { + range: 1509..1510, + value: NumberLiteral( + ExprNumberLiteral { + range: 1509..1510, + value: Int( + 1, + ), + }, + ), + }, + ), + MatchValue( + PatternMatchValue { + range: 1512..1513, + value: NumberLiteral( + ExprNumberLiteral { + range: 1512..1513, + value: Int( + 0, + ), + }, + ), + }, + ), + ], + }, + ), + guard: None, + body: [ + Assign( + StmtAssign { + range: 1524..1529, + targets: [ + Name( + ExprName { + range: 1524..1525, + id: "y", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 1528..1529, + value: Int( + 2, + ), + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 1552..1595, + subject: Name( + ExprName { + range: 1558..1559, + id: "w", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 1565..1595, + pattern: MatchSequence( + PatternMatchSequence { + range: 1570..1580, + patterns: [ + MatchAs( + PatternMatchAs { + range: 1571..1572, + pattern: None, + name: Some( + Identifier { + id: "x", + range: 1571..1572, + }, + ), + }, + ), + MatchAs( + PatternMatchAs { + range: 1574..1575, + pattern: None, + name: Some( + Identifier { + id: "y", + range: 1574..1575, + }, + ), + }, + ), + MatchStar( + PatternMatchStar { + range: 1577..1579, + name: None, + }, + ), + ], + }, + ), + guard: None, + body: [ + Assign( + StmtAssign { + range: 1590..1595, + targets: [ + Name( + ExprName { + range: 1590..1591, + id: "z", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 1594..1595, + value: Int( + 0, + ), + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 1618..1664, + subject: Name( + ExprName { + range: 1624..1625, + id: "x", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 1631..1664, + pattern: MatchValue( + PatternMatchValue { + range: 1636..1649, + value: BinOp( + ExprBinOp { + range: 1636..1649, + left: UnaryOp( + ExprUnaryOp { + range: 1636..1641, + op: USub, + operand: NumberLiteral( + ExprNumberLiteral { + range: 1637..1641, + value: Float( + 0.25, + ), + }, + ), + }, + ), + op: Sub, + right: NumberLiteral( + ExprNumberLiteral { + range: 1644..1649, + value: Complex { + real: 0.0, + imag: 1.75, + }, + }, + ), + }, + ), + }, + ), + guard: None, + body: [ + Assign( + StmtAssign { + range: 1659..1664, + targets: [ + Name( + ExprName { + range: 1659..1660, + id: "y", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 1663..1664, + value: Int( + 0, + ), + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 1687..1726, + subject: Tuple( + ExprTuple { + range: 1693..1697, + elts: [ + Name( + ExprName { + range: 1694..1695, + id: "x", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + cases: [ + MatchCase { + range: 1703..1726, + pattern: MatchSequence( + PatternMatchSequence { + range: 1708..1711, + patterns: [ + MatchAs( + PatternMatchAs { + range: 1709..1710, + pattern: None, + name: Some( + Identifier { + id: "y", + range: 1709..1710, + }, + ), + }, + ), + ], + }, + ), + guard: None, + body: [ + Assign( + StmtAssign { + range: 1721..1726, + targets: [ + Name( + ExprName { + range: 1721..1722, + id: "z", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 1725..1726, + value: Int( + 0, + ), + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 1749..1789, + subject: Name( + ExprName { + range: 1755..1756, + id: "x", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 1762..1789, + pattern: MatchValue( + PatternMatchValue { + range: 1767..1774, + value: Attribute( + ExprAttribute { + range: 1767..1774, + value: Attribute( + ExprAttribute { + range: 1767..1772, + value: Attribute( + ExprAttribute { + range: 1767..1770, + value: Name( + ExprName { + range: 1767..1768, + id: "A", + ctx: Load, + }, + ), + attr: Identifier { + id: "B", + range: 1769..1770, + }, + ctx: Load, + }, + ), + attr: Identifier { + id: "C", + range: 1771..1772, + }, + ctx: Load, + }, + ), + attr: Identifier { + id: "D", + range: 1773..1774, + }, + ctx: Load, + }, + ), + }, + ), + guard: None, + body: [ + Assign( + StmtAssign { + range: 1784..1789, + targets: [ + Name( + ExprName { + range: 1784..1785, + id: "y", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 1788..1789, + value: Int( + 0, + ), + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 1812..1849, + subject: Name( + ExprName { + range: 1818..1819, + id: "x", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 1825..1849, + pattern: MatchSingleton( + PatternMatchSingleton { + range: 1830..1834, + value: None, + }, + ), + guard: None, + body: [ + Assign( + StmtAssign { + range: 1844..1849, + targets: [ + Name( + ExprName { + range: 1844..1845, + id: "y", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 1848..1849, + value: Int( + 0, + ), + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 1872..1906, + subject: Name( + ExprName { + range: 1878..1879, + id: "x", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 1885..1906, + pattern: MatchValue( + PatternMatchValue { + range: 1890..1891, + value: NumberLiteral( + ExprNumberLiteral { + range: 1890..1891, + value: Int( + 0, + ), + }, + ), + }, + ), + guard: None, + body: [ + Assign( + StmtAssign { + range: 1901..1906, + targets: [ + Name( + ExprName { + range: 1901..1902, + id: "y", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 1905..1906, + value: Int( + 0, + ), + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 1929..1967, + subject: Name( + ExprName { + range: 1935..1936, + id: "x", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 1942..1967, + pattern: MatchSingleton( + PatternMatchSingleton { + range: 1947..1952, + value: False, + }, + ), + guard: None, + body: [ + Assign( + StmtAssign { + range: 1962..1967, + targets: [ + Name( + ExprName { + range: 1962..1963, + id: "y", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 1966..1967, + value: Int( + 0, + ), + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 1990..2081, + subject: Name( + ExprName { + range: 1996..1997, + id: "x", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 2003..2025, + pattern: MatchSequence( + PatternMatchSequence { + range: 2008..2010, + patterns: [], + }, + ), + guard: None, + body: [ + Assign( + StmtAssign { + range: 2020..2025, + targets: [ + Name( + ExprName { + range: 2020..2021, + id: "y", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 2024..2025, + value: Int( + 0, + ), + }, + ), + }, + ), + ], + }, + MatchCase { + range: 2030..2054, + pattern: MatchSequence( + PatternMatchSequence { + range: 2035..2039, + patterns: [ + MatchValue( + PatternMatchValue { + range: 2036..2038, + value: StringLiteral( + ExprStringLiteral { + range: 2036..2038, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 2036..2038, + value: "", + flags: StringLiteralFlags { + quote_style: Double, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + }, + ), + ], + }, + ), + guard: None, + body: [ + Assign( + StmtAssign { + range: 2049..2054, + targets: [ + Name( + ExprName { + range: 2049..2050, + id: "y", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 2053..2054, + value: Int( + 1, + ), + }, + ), + }, + ), + ], + }, + MatchCase { + range: 2059..2081, + pattern: MatchValue( + PatternMatchValue { + range: 2064..2066, + value: StringLiteral( + ExprStringLiteral { + range: 2064..2066, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 2064..2066, + value: "", + flags: StringLiteralFlags { + quote_style: Double, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + }, + ), + guard: None, + body: [ + Assign( + StmtAssign { + range: 2076..2081, + targets: [ + Name( + ExprName { + range: 2076..2077, + id: "y", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 2080..2081, + value: Int( + 2, + ), + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 2104..2138, + subject: Name( + ExprName { + range: 2110..2111, + id: "x", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 2117..2138, + pattern: MatchAs( + PatternMatchAs { + range: 2122..2123, + pattern: None, + name: Some( + Identifier { + id: "z", + range: 2122..2123, + }, + ), + }, + ), + guard: None, + body: [ + Assign( + StmtAssign { + range: 2133..2138, + targets: [ + Name( + ExprName { + range: 2133..2134, + id: "y", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 2137..2138, + value: Int( + 0, + ), + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 2161..2207, + subject: Name( + ExprName { + range: 2167..2168, + id: "w", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 2174..2207, + pattern: MatchSequence( + PatternMatchSequence { + range: 2179..2192, + patterns: [ + MatchAs( + PatternMatchAs { + range: 2180..2181, + pattern: None, + name: Some( + Identifier { + id: "x", + range: 2180..2181, + }, + ), + }, + ), + MatchAs( + PatternMatchAs { + range: 2183..2184, + pattern: None, + name: Some( + Identifier { + id: "y", + range: 2183..2184, + }, + ), + }, + ), + MatchStar( + PatternMatchStar { + range: 2186..2191, + name: Some( + Identifier { + id: "rest", + range: 2187..2191, + }, + ), + }, + ), + ], + }, + ), + guard: None, + body: [ + Assign( + StmtAssign { + range: 2202..2207, + targets: [ + Name( + ExprName { + range: 2202..2203, + id: "z", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 2206..2207, + value: Int( + 0, + ), + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 2230..2307, + subject: Name( + ExprName { + range: 2236..2237, + id: "x", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 2243..2307, + pattern: MatchOr( + PatternMatchOr { + range: 2248..2278, + patterns: [ + MatchAs( + PatternMatchAs { + range: 2249..2255, + pattern: Some( + MatchValue( + PatternMatchValue { + range: 2249..2250, + value: NumberLiteral( + ExprNumberLiteral { + range: 2249..2250, + value: Int( + 0, + ), + }, + ), + }, + ), + ), + name: Some( + Identifier { + id: "z", + range: 2254..2255, + }, + ), + }, + ), + MatchAs( + PatternMatchAs { + range: 2260..2266, + pattern: Some( + MatchValue( + PatternMatchValue { + range: 2260..2261, + value: NumberLiteral( + ExprNumberLiteral { + range: 2260..2261, + value: Int( + 1, + ), + }, + ), + }, + ), + ), + name: Some( + Identifier { + id: "z", + range: 2265..2266, + }, + ), + }, + ), + MatchAs( + PatternMatchAs { + range: 2271..2277, + pattern: Some( + MatchValue( + PatternMatchValue { + range: 2271..2272, + value: NumberLiteral( + ExprNumberLiteral { + range: 2271..2272, + value: Int( + 2, + ), + }, + ), + }, + ), + ), + name: Some( + Identifier { + id: "z", + range: 2276..2277, + }, + ), + }, + ), + ], + }, + ), + guard: Some( + Compare( + ExprCompare { + range: 2282..2292, + left: Name( + ExprName { + range: 2282..2283, + id: "z", + ctx: Load, + }, + ), + ops: [ + Eq, + ], + comparators: [ + BinOp( + ExprBinOp { + range: 2287..2292, + left: Name( + ExprName { + range: 2287..2288, + id: "x", + ctx: Load, + }, + ), + op: Mod, + right: NumberLiteral( + ExprNumberLiteral { + range: 2291..2292, + value: Int( + 2, + ), + }, + ), + }, + ), + ], + }, + ), + ), + body: [ + Assign( + StmtAssign { + range: 2302..2307, + targets: [ + Name( + ExprName { + range: 2302..2303, + id: "y", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 2306..2307, + value: Int( + 0, + ), + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 2330..2499, + subject: Name( + ExprName { + range: 2336..2337, + id: "x", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 2343..2378, + pattern: MatchMapping( + PatternMatchMapping { + range: 2348..2363, + keys: [ + NumberLiteral( + ExprNumberLiteral { + range: 2349..2350, + value: Int( + 0, + ), + }, + ), + ], + patterns: [ + MatchSequence( + PatternMatchSequence { + range: 2352..2362, + patterns: [ + MatchValue( + PatternMatchValue { + range: 2353..2354, + value: NumberLiteral( + ExprNumberLiteral { + range: 2353..2354, + value: Int( + 1, + ), + }, + ), + }, + ), + MatchValue( + PatternMatchValue { + range: 2356..2357, + value: NumberLiteral( + ExprNumberLiteral { + range: 2356..2357, + value: Int( + 2, + ), + }, + ), + }, + ), + MatchMapping( + PatternMatchMapping { + range: 2359..2361, + keys: [], + patterns: [], + rest: None, + }, + ), + ], + }, + ), + ], + rest: None, + }, + ), + guard: None, + body: [ + Assign( + StmtAssign { + range: 2373..2378, + targets: [ + Name( + ExprName { + range: 2373..2374, + id: "y", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 2377..2378, + value: Int( + 0, + ), + }, + ), + }, + ), + ], + }, + MatchCase { + range: 2383..2472, + pattern: MatchOr( + PatternMatchOr { + range: 2388..2457, + patterns: [ + MatchMapping( + PatternMatchMapping { + range: 2388..2411, + keys: [ + NumberLiteral( + ExprNumberLiteral { + range: 2389..2390, + value: Int( + 0, + ), + }, + ), + ], + patterns: [ + MatchOr( + PatternMatchOr { + range: 2392..2410, + patterns: [ + MatchSequence( + PatternMatchSequence { + range: 2392..2402, + patterns: [ + MatchValue( + PatternMatchValue { + range: 2393..2394, + value: NumberLiteral( + ExprNumberLiteral { + range: 2393..2394, + value: Int( + 1, + ), + }, + ), + }, + ), + MatchValue( + PatternMatchValue { + range: 2396..2397, + value: NumberLiteral( + ExprNumberLiteral { + range: 2396..2397, + value: Int( + 2, + ), + }, + ), + }, + ), + MatchMapping( + PatternMatchMapping { + range: 2399..2401, + keys: [], + patterns: [], + rest: None, + }, + ), + ], + }, + ), + MatchSingleton( + PatternMatchSingleton { + range: 2405..2410, + value: False, + }, + ), + ], + }, + ), + ], + rest: None, + }, + ), + MatchMapping( + PatternMatchMapping { + range: 2414..2423, + keys: [ + NumberLiteral( + ExprNumberLiteral { + range: 2415..2416, + value: Int( + 1, + ), + }, + ), + ], + patterns: [ + MatchSequence( + PatternMatchSequence { + range: 2418..2422, + patterns: [ + MatchSequence( + PatternMatchSequence { + range: 2419..2421, + patterns: [], + }, + ), + ], + }, + ), + ], + rest: None, + }, + ), + MatchMapping( + PatternMatchMapping { + range: 2426..2441, + keys: [ + NumberLiteral( + ExprNumberLiteral { + range: 2427..2428, + value: Int( + 0, + ), + }, + ), + ], + patterns: [ + MatchSequence( + PatternMatchSequence { + range: 2430..2440, + patterns: [ + MatchValue( + PatternMatchValue { + range: 2431..2432, + value: NumberLiteral( + ExprNumberLiteral { + range: 2431..2432, + value: Int( + 1, + ), + }, + ), + }, + ), + MatchValue( + PatternMatchValue { + range: 2434..2435, + value: NumberLiteral( + ExprNumberLiteral { + range: 2434..2435, + value: Int( + 2, + ), + }, + ), + }, + ), + MatchMapping( + PatternMatchMapping { + range: 2437..2439, + keys: [], + patterns: [], + rest: None, + }, + ), + ], + }, + ), + ], + rest: None, + }, + ), + MatchSequence( + PatternMatchSequence { + range: 2444..2446, + patterns: [], + }, + ), + MatchValue( + PatternMatchValue { + range: 2449..2452, + value: StringLiteral( + ExprStringLiteral { + range: 2449..2452, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 2449..2452, + value: "X", + flags: StringLiteralFlags { + quote_style: Double, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + }, + ), + MatchMapping( + PatternMatchMapping { + range: 2455..2457, + keys: [], + patterns: [], + rest: None, + }, + ), + ], + }, + ), + guard: None, + body: [ + Assign( + StmtAssign { + range: 2467..2472, + targets: [ + Name( + ExprName { + range: 2467..2468, + id: "y", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 2471..2472, + value: Int( + 1, + ), + }, + ), + }, + ), + ], + }, + MatchCase { + range: 2477..2499, + pattern: MatchSequence( + PatternMatchSequence { + range: 2482..2484, + patterns: [], + }, + ), + guard: None, + body: [ + Assign( + StmtAssign { + range: 2494..2499, + targets: [ + Name( + ExprName { + range: 2494..2495, + id: "y", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 2498..2499, + value: Int( + 2, + ), + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 2522..2568, + subject: Tuple( + ExprTuple { + range: 2528..2537, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 2529..2530, + value: Int( + 0, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 2532..2533, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 2535..2536, + value: Int( + 2, + ), + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + cases: [ + MatchCase { + range: 2543..2568, + pattern: MatchSequence( + PatternMatchSequence { + range: 2548..2553, + patterns: [ + MatchValue( + PatternMatchValue { + range: 2548..2549, + value: NumberLiteral( + ExprNumberLiteral { + range: 2548..2549, + value: Int( + 0, + ), + }, + ), + }, + ), + MatchStar( + PatternMatchStar { + range: 2551..2553, + name: Some( + Identifier { + id: "x", + range: 2552..2553, + }, + ), + }, + ), + ], + }, + ), + guard: None, + body: [ + Assign( + StmtAssign { + range: 2563..2568, + targets: [ + Name( + ExprName { + range: 2563..2564, + id: "y", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 2567..2568, + value: Int( + 0, + ), + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 2591..2638, + subject: Tuple( + ExprTuple { + range: 2597..2606, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 2598..2599, + value: Int( + 0, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 2601..2602, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 2604..2605, + value: Int( + 2, + ), + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + cases: [ + MatchCase { + range: 2612..2638, + pattern: MatchSequence( + PatternMatchSequence { + range: 2617..2623, + patterns: [ + MatchStar( + PatternMatchStar { + range: 2617..2619, + name: Some( + Identifier { + id: "x", + range: 2618..2619, + }, + ), + }, + ), + MatchValue( + PatternMatchValue { + range: 2621..2622, + value: NumberLiteral( + ExprNumberLiteral { + range: 2621..2622, + value: Int( + 2, + ), + }, + ), + }, + ), + ], + }, + ), + guard: None, + body: [ + Assign( + StmtAssign { + range: 2633..2638, + targets: [ + Name( + ExprName { + range: 2633..2634, + id: "y", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 2637..2638, + value: Int( + 0, + ), + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 2661..2697, + subject: Tuple( + ExprTuple { + range: 2667..2669, + elts: [ + Name( + ExprName { + range: 2667..2668, + id: "x", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + cases: [ + MatchCase { + range: 2675..2697, + pattern: MatchSequence( + PatternMatchSequence { + range: 2680..2682, + patterns: [ + MatchAs( + PatternMatchAs { + range: 2680..2681, + pattern: None, + name: Some( + Identifier { + id: "y", + range: 2680..2681, + }, + ), + }, + ), + ], + }, + ), + guard: None, + body: [ + Assign( + StmtAssign { + range: 2692..2697, + targets: [ + Name( + ExprName { + range: 2692..2693, + id: "z", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 2696..2697, + value: Int( + 0, + ), + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 2720..2760, + subject: Tuple( + ExprTuple { + range: 2726..2730, + elts: [ + Name( + ExprName { + range: 2726..2727, + id: "w", + ctx: Load, + }, + ), + Name( + ExprName { + range: 2729..2730, + id: "x", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + cases: [ + MatchCase { + range: 2736..2760, + pattern: MatchSequence( + PatternMatchSequence { + range: 2741..2745, + patterns: [ + MatchAs( + PatternMatchAs { + range: 2741..2742, + pattern: None, + name: Some( + Identifier { + id: "y", + range: 2741..2742, + }, + ), + }, + ), + MatchAs( + PatternMatchAs { + range: 2744..2745, + pattern: None, + name: Some( + Identifier { + id: "z", + range: 2744..2745, + }, + ), + }, + ), + ], + }, + ), + guard: None, + body: [ + Assign( + StmtAssign { + range: 2755..2760, + targets: [ + Name( + ExprName { + range: 2755..2756, + id: "v", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 2759..2760, + value: Int( + 0, + ), + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 2783..2829, + subject: Tuple( + ExprTuple { + range: 2789..2796, + elts: [ + Named( + ExprNamed { + range: 2789..2795, + target: Name( + ExprName { + range: 2789..2790, + id: "w", + ctx: Store, + }, + ), + value: Name( + ExprName { + range: 2794..2795, + id: "x", + ctx: Load, + }, + ), + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + cases: [ + MatchCase { + range: 2802..2829, + pattern: MatchSequence( + PatternMatchSequence { + range: 2807..2814, + patterns: [ + MatchAs( + PatternMatchAs { + range: 2807..2813, + pattern: Some( + MatchAs( + PatternMatchAs { + range: 2807..2808, + pattern: None, + name: Some( + Identifier { + id: "y", + range: 2807..2808, + }, + ), + }, + ), + ), + name: Some( + Identifier { + id: "v", + range: 2812..2813, + }, + ), + }, + ), + ], + }, + ), + guard: None, + body: [ + Assign( + StmtAssign { + range: 2824..2829, + targets: [ + Name( + ExprName { + range: 2824..2825, + id: "z", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 2828..2829, + value: Int( + 0, + ), + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 2831..2952, + subject: Name( + ExprName { + range: 2837..2838, + id: "x", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 2927..2952, + pattern: MatchValue( + PatternMatchValue { + range: 2932..2938, + value: FString( + ExprFString { + range: 2932..2938, + value: FStringValue { + inner: Single( + FString( + FString { + range: 2932..2938, + elements: [ + Expression( + FStringExpressionElement { + range: 2934..2937, + expression: Name( + ExprName { + range: 2935..2936, + id: "y", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + }, + ), + guard: None, + body: [ + Pass( + StmtPass { + range: 2948..2952, + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 2953..3025, + subject: Dict( + ExprDict { + range: 2959..2970, + items: [ + DictItem { + key: Some( + StringLiteral( + ExprStringLiteral { + range: 2960..2966, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 2960..2966, + value: "test", + flags: StringLiteralFlags { + quote_style: Double, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 2968..2969, + value: Int( + 1, + ), + }, + ), + }, + ], + }, + ), + cases: [ + MatchCase { + range: 2976..3025, + pattern: MatchMapping( + PatternMatchMapping { + range: 2981..3004, + keys: [], + patterns: [], + rest: Some( + Identifier { + id: "rest", + range: 2993..2997, + }, + ), + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 3014..3025, + value: Call( + ExprCall { + range: 3014..3025, + func: Name( + ExprName { + range: 3014..3019, + id: "print", + ctx: Load, + }, + ), + arguments: Arguments { + range: 3019..3025, + args: [ + Name( + ExprName { + range: 3020..3024, + id: "rest", + ctx: Load, + }, + ), + ], + keywords: [], + }, + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 3026..3129, + subject: Dict( + ExprDict { + range: 3032..3049, + items: [ + DictItem { + key: Some( + StringLiteral( + ExprStringLiteral { + range: 3033..3040, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 3033..3040, + value: "label", + flags: StringLiteralFlags { + quote_style: Double, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + ), + value: StringLiteral( + ExprStringLiteral { + range: 3042..3048, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 3042..3048, + value: "test", + flags: StringLiteralFlags { + quote_style: Double, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + }, + ], + }, + ), + cases: [ + MatchCase { + range: 3055..3129, + pattern: MatchMapping( + PatternMatchMapping { + range: 3060..3107, + keys: [ + StringLiteral( + ExprStringLiteral { + range: 3070..3077, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 3070..3077, + value: "label", + flags: StringLiteralFlags { + quote_style: Double, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + ], + patterns: [ + MatchAs( + PatternMatchAs { + range: 3079..3100, + pattern: Some( + MatchOr( + PatternMatchOr { + range: 3079..3091, + patterns: [ + MatchClass( + PatternMatchClass { + range: 3079..3084, + cls: Name( + ExprName { + range: 3079..3082, + id: "str", + ctx: Load, + }, + ), + arguments: PatternArguments { + range: 3082..3084, + patterns: [], + keywords: [], + }, + }, + ), + MatchSingleton( + PatternMatchSingleton { + range: 3087..3091, + value: None, + }, + ), + ], + }, + ), + ), + name: Some( + Identifier { + id: "label", + range: 3095..3100, + }, + ), + }, + ), + ], + rest: None, + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 3117..3129, + value: Call( + ExprCall { + range: 3117..3129, + func: Name( + ExprName { + range: 3117..3122, + id: "print", + ctx: Load, + }, + ), + arguments: Arguments { + range: 3122..3129, + args: [ + Name( + ExprName { + range: 3123..3128, + id: "label", + ctx: Load, + }, + ), + ], + keywords: [], + }, + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 3130..3170, + subject: Name( + ExprName { + range: 3136..3137, + id: "x", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 3143..3170, + pattern: MatchSequence( + PatternMatchSequence { + range: 3148..3155, + patterns: [ + MatchValue( + PatternMatchValue { + range: 3149..3150, + value: NumberLiteral( + ExprNumberLiteral { + range: 3149..3150, + value: Int( + 0, + ), + }, + ), + }, + ), + MatchValue( + PatternMatchValue { + range: 3152..3153, + value: NumberLiteral( + ExprNumberLiteral { + range: 3152..3153, + value: Int( + 1, + ), + }, + ), + }, + ), + ], + }, + ), + guard: None, + body: [ + Assign( + StmtAssign { + range: 3165..3170, + targets: [ + Name( + ExprName { + range: 3165..3166, + id: "y", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 3169..3170, + value: Int( + 0, + ), + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 3171..3211, + subject: Name( + ExprName { + range: 3177..3178, + id: "x", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 3184..3211, + pattern: MatchSequence( + PatternMatchSequence { + range: 3189..3196, + patterns: [ + MatchValue( + PatternMatchValue { + range: 3190..3191, + value: NumberLiteral( + ExprNumberLiteral { + range: 3190..3191, + value: Int( + 0, + ), + }, + ), + }, + ), + MatchValue( + PatternMatchValue { + range: 3193..3194, + value: NumberLiteral( + ExprNumberLiteral { + range: 3193..3194, + value: Int( + 1, + ), + }, + ), + }, + ), + ], + }, + ), + guard: None, + body: [ + Assign( + StmtAssign { + range: 3206..3211, + targets: [ + Name( + ExprName { + range: 3206..3207, + id: "y", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 3210..3211, + value: Int( + 0, + ), + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 3212..3249, + subject: Name( + ExprName { + range: 3218..3219, + id: "x", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 3225..3249, + pattern: MatchSequence( + PatternMatchSequence { + range: 3230..3234, + patterns: [ + MatchValue( + PatternMatchValue { + range: 3231..3232, + value: NumberLiteral( + ExprNumberLiteral { + range: 3231..3232, + value: Int( + 0, + ), + }, + ), + }, + ), + ], + }, + ), + guard: None, + body: [ + Assign( + StmtAssign { + range: 3244..3249, + targets: [ + Name( + ExprName { + range: 3244..3245, + id: "y", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 3248..3249, + value: Int( + 0, + ), + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 3250..3284, + subject: Tuple( + ExprTuple { + range: 3256..3258, + elts: [ + Name( + ExprName { + range: 3256..3257, + id: "x", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + cases: [ + MatchCase { + range: 3264..3284, + pattern: MatchAs( + PatternMatchAs { + range: 3269..3270, + pattern: None, + name: Some( + Identifier { + id: "z", + range: 3269..3270, + }, + ), + }, + ), + guard: None, + body: [ + Pass( + StmtPass { + range: 3280..3284, + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 3285..3321, + subject: Tuple( + ExprTuple { + range: 3291..3295, + elts: [ + Name( + ExprName { + range: 3291..3292, + id: "x", + ctx: Load, + }, + ), + Name( + ExprName { + range: 3294..3295, + id: "y", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + cases: [ + MatchCase { + range: 3301..3321, + pattern: MatchAs( + PatternMatchAs { + range: 3306..3307, + pattern: None, + name: Some( + Identifier { + id: "z", + range: 3306..3307, + }, + ), + }, + ), + guard: None, + body: [ + Pass( + StmtPass { + range: 3317..3321, + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 3322..3359, + subject: Tuple( + ExprTuple { + range: 3328..3333, + elts: [ + Name( + ExprName { + range: 3328..3329, + id: "x", + ctx: Load, + }, + ), + Name( + ExprName { + range: 3331..3332, + id: "y", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + cases: [ + MatchCase { + range: 3339..3359, + pattern: MatchAs( + PatternMatchAs { + range: 3344..3345, + pattern: None, + name: Some( + Identifier { + id: "z", + range: 3344..3345, + }, + ), + }, + ), + guard: None, + body: [ + Pass( + StmtPass { + range: 3355..3359, + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 3385..3475, + subject: Name( + ExprName { + range: 3391..3392, + id: "x", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 3398..3420, + pattern: MatchSingleton( + PatternMatchSingleton { + range: 3403..3407, + value: None, + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 3417..3420, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 3417..3420, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 3425..3447, + pattern: MatchSingleton( + PatternMatchSingleton { + range: 3430..3434, + value: True, + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 3444..3447, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 3444..3447, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 3452..3475, + pattern: MatchSingleton( + PatternMatchSingleton { + range: 3457..3462, + value: False, + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 3472..3475, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 3472..3475, + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 3497..3821, + subject: Name( + ExprName { + range: 3503..3504, + id: "x", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 3510..3531, + pattern: MatchValue( + PatternMatchValue { + range: 3515..3518, + value: Attribute( + ExprAttribute { + range: 3515..3518, + value: Name( + ExprName { + range: 3515..3516, + id: "a", + ctx: Load, + }, + ), + attr: Identifier { + id: "b", + range: 3517..3518, + }, + ctx: Load, + }, + ), + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 3528..3531, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 3528..3531, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 3536..3559, + pattern: MatchValue( + PatternMatchValue { + range: 3541..3546, + value: Attribute( + ExprAttribute { + range: 3541..3546, + value: Attribute( + ExprAttribute { + range: 3541..3544, + value: Name( + ExprName { + range: 3541..3542, + id: "a", + ctx: Load, + }, + ), + attr: Identifier { + id: "b", + range: 3543..3544, + }, + ctx: Load, + }, + ), + attr: Identifier { + id: "c", + range: 3545..3546, + }, + ctx: Load, + }, + ), + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 3556..3559, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 3556..3559, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 3564..3584, + pattern: MatchValue( + PatternMatchValue { + range: 3569..3571, + value: StringLiteral( + ExprStringLiteral { + range: 3569..3571, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 3569..3571, + value: "", + flags: StringLiteralFlags { + quote_style: Single, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 3581..3584, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 3581..3584, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 3589..3610, + pattern: MatchValue( + PatternMatchValue { + range: 3594..3597, + value: BytesLiteral( + ExprBytesLiteral { + range: 3594..3597, + value: BytesLiteralValue { + inner: Single( + BytesLiteral { + range: 3594..3597, + value: [], + flags: BytesLiteralFlags { + quote_style: Single, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 3607..3610, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 3607..3610, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 3615..3634, + pattern: MatchValue( + PatternMatchValue { + range: 3620..3621, + value: NumberLiteral( + ExprNumberLiteral { + range: 3620..3621, + value: Int( + 1, + ), + }, + ), + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 3631..3634, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 3631..3634, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 3639..3660, + pattern: MatchValue( + PatternMatchValue { + range: 3644..3647, + value: NumberLiteral( + ExprNumberLiteral { + range: 3644..3647, + value: Float( + 1.0, + ), + }, + ), + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 3657..3660, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 3657..3660, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 3665..3687, + pattern: MatchValue( + PatternMatchValue { + range: 3670..3674, + value: NumberLiteral( + ExprNumberLiteral { + range: 3670..3674, + value: Complex { + real: 0.0, + imag: 1.0, + }, + }, + ), + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 3684..3687, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 3684..3687, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 3692..3716, + pattern: MatchValue( + PatternMatchValue { + range: 3697..3703, + value: BinOp( + ExprBinOp { + range: 3697..3703, + left: NumberLiteral( + ExprNumberLiteral { + range: 3697..3698, + value: Int( + 1, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 3701..3703, + value: Complex { + real: 0.0, + imag: 1.0, + }, + }, + ), + }, + ), + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 3713..3716, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 3713..3716, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 3721..3741, + pattern: MatchValue( + PatternMatchValue { + range: 3726..3728, + value: UnaryOp( + ExprUnaryOp { + range: 3726..3728, + op: USub, + operand: NumberLiteral( + ExprNumberLiteral { + range: 3727..3728, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 3738..3741, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 3738..3741, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 3746..3767, + pattern: MatchValue( + PatternMatchValue { + range: 3751..3754, + value: UnaryOp( + ExprUnaryOp { + range: 3751..3754, + op: USub, + operand: NumberLiteral( + ExprNumberLiteral { + range: 3752..3754, + value: Float( + 1.0, + ), + }, + ), + }, + ), + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 3764..3767, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 3764..3767, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 3772..3795, + pattern: MatchValue( + PatternMatchValue { + range: 3777..3782, + value: UnaryOp( + ExprUnaryOp { + range: 3777..3782, + op: USub, + operand: NumberLiteral( + ExprNumberLiteral { + range: 3778..3782, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 3792..3795, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 3792..3795, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 3800..3821, + pattern: MatchValue( + PatternMatchValue { + range: 3806..3807, + value: NumberLiteral( + ExprNumberLiteral { + range: 3806..3807, + value: Int( + 1, + ), + }, + ), + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 3818..3821, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 3818..3821, + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 3840..3927, + subject: Name( + ExprName { + range: 3846..3847, + id: "x", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 3853..3876, + pattern: MatchOr( + PatternMatchOr { + range: 3858..3863, + patterns: [ + MatchValue( + PatternMatchValue { + range: 3858..3859, + value: NumberLiteral( + ExprNumberLiteral { + range: 3858..3859, + value: Int( + 1, + ), + }, + ), + }, + ), + MatchValue( + PatternMatchValue { + range: 3862..3863, + value: NumberLiteral( + ExprNumberLiteral { + range: 3862..3863, + value: Int( + 2, + ), + }, + ), + }, + ), + ], + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 3873..3876, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 3873..3876, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 3881..3927, + pattern: MatchOr( + PatternMatchOr { + range: 3886..3914, + patterns: [ + MatchValue( + PatternMatchValue { + range: 3886..3888, + value: StringLiteral( + ExprStringLiteral { + range: 3886..3888, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 3886..3888, + value: "", + flags: StringLiteralFlags { + quote_style: Single, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + }, + ), + MatchValue( + PatternMatchValue { + range: 3891..3894, + value: NumberLiteral( + ExprNumberLiteral { + range: 3891..3894, + value: Float( + 1.1, + ), + }, + ), + }, + ), + MatchValue( + PatternMatchValue { + range: 3897..3899, + value: UnaryOp( + ExprUnaryOp { + range: 3897..3899, + op: USub, + operand: NumberLiteral( + ExprNumberLiteral { + range: 3898..3899, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ), + MatchValue( + PatternMatchValue { + range: 3902..3908, + value: BinOp( + ExprBinOp { + range: 3902..3908, + left: NumberLiteral( + ExprNumberLiteral { + range: 3902..3903, + value: Int( + 1, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 3906..3908, + value: Complex { + real: 0.0, + imag: 1.0, + }, + }, + ), + }, + ), + }, + ), + MatchValue( + PatternMatchValue { + range: 3911..3914, + value: Attribute( + ExprAttribute { + range: 3911..3914, + value: Name( + ExprName { + range: 3911..3912, + id: "a", + ctx: Load, + }, + ), + attr: Identifier { + id: "b", + range: 3913..3914, + }, + ctx: Load, + }, + ), + }, + ), + ], + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 3924..3927, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 3924..3927, + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 3946..4163, + subject: Name( + ExprName { + range: 3952..3953, + id: "x", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 3959..3978, + pattern: MatchAs( + PatternMatchAs { + range: 3964..3965, + pattern: None, + name: Some( + Identifier { + id: "a", + range: 3964..3965, + }, + ), + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 3975..3978, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 3975..3978, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 3983..4007, + pattern: MatchAs( + PatternMatchAs { + range: 3988..3994, + pattern: Some( + MatchAs( + PatternMatchAs { + range: 3988..3989, + pattern: None, + name: Some( + Identifier { + id: "a", + range: 3988..3989, + }, + ), + }, + ), + ), + name: Some( + Identifier { + id: "b", + range: 3993..3994, + }, + ), + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 4004..4007, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 4004..4007, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 4012..4042, + pattern: MatchAs( + PatternMatchAs { + range: 4017..4029, + pattern: Some( + MatchOr( + PatternMatchOr { + range: 4017..4022, + patterns: [ + MatchValue( + PatternMatchValue { + range: 4017..4018, + value: NumberLiteral( + ExprNumberLiteral { + range: 4017..4018, + value: Int( + 1, + ), + }, + ), + }, + ), + MatchValue( + PatternMatchValue { + range: 4021..4022, + value: NumberLiteral( + ExprNumberLiteral { + range: 4021..4022, + value: Int( + 2, + ), + }, + ), + }, + ), + ], + }, + ), + ), + name: Some( + Identifier { + id: "two", + range: 4026..4029, + }, + ), + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 4039..4042, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 4039..4042, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 4047..4078, + pattern: MatchAs( + PatternMatchAs { + range: 4052..4065, + pattern: Some( + MatchValue( + PatternMatchValue { + range: 4052..4058, + value: BinOp( + ExprBinOp { + range: 4052..4058, + left: NumberLiteral( + ExprNumberLiteral { + range: 4052..4053, + value: Int( + 1, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 4056..4058, + value: Complex { + real: 0.0, + imag: 3.0, + }, + }, + ), + }, + ), + }, + ), + ), + name: Some( + Identifier { + id: "sum", + range: 4062..4065, + }, + ), + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 4075..4078, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 4075..4078, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 4083..4110, + pattern: MatchAs( + PatternMatchAs { + range: 4088..4097, + pattern: Some( + MatchValue( + PatternMatchValue { + range: 4088..4091, + value: Attribute( + ExprAttribute { + range: 4088..4091, + value: Name( + ExprName { + range: 4088..4089, + id: "a", + ctx: Load, + }, + ), + attr: Identifier { + id: "b", + range: 4090..4091, + }, + ctx: Load, + }, + ), + }, + ), + ), + name: Some( + Identifier { + id: "ab", + range: 4095..4097, + }, + ), + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 4107..4110, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 4107..4110, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 4115..4134, + pattern: MatchAs( + PatternMatchAs { + range: 4120..4121, + pattern: None, + name: None, + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 4131..4134, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 4131..4134, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 4139..4163, + pattern: MatchAs( + PatternMatchAs { + range: 4144..4150, + pattern: Some( + MatchAs( + PatternMatchAs { + range: 4144..4145, + pattern: None, + name: None, + }, + ), + ), + name: Some( + Identifier { + id: "x", + range: 4149..4150, + }, + ), + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 4160..4163, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 4160..4163, + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 4188..4439, + subject: Name( + ExprName { + range: 4194..4195, + id: "x", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 4201..4226, + pattern: MatchSequence( + PatternMatchSequence { + range: 4206..4213, + patterns: [ + MatchValue( + PatternMatchValue { + range: 4206..4207, + value: NumberLiteral( + ExprNumberLiteral { + range: 4206..4207, + value: Int( + 1, + ), + }, + ), + }, + ), + MatchValue( + PatternMatchValue { + range: 4209..4210, + value: NumberLiteral( + ExprNumberLiteral { + range: 4209..4210, + value: Int( + 2, + ), + }, + ), + }, + ), + MatchValue( + PatternMatchValue { + range: 4212..4213, + value: NumberLiteral( + ExprNumberLiteral { + range: 4212..4213, + value: Int( + 3, + ), + }, + ), + }, + ), + ], + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 4223..4226, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 4223..4226, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 4231..4259, + pattern: MatchSequence( + PatternMatchSequence { + range: 4236..4246, + patterns: [ + MatchValue( + PatternMatchValue { + range: 4237..4238, + value: NumberLiteral( + ExprNumberLiteral { + range: 4237..4238, + value: Int( + 1, + ), + }, + ), + }, + ), + MatchValue( + PatternMatchValue { + range: 4240..4241, + value: NumberLiteral( + ExprNumberLiteral { + range: 4240..4241, + value: Int( + 2, + ), + }, + ), + }, + ), + MatchValue( + PatternMatchValue { + range: 4243..4244, + value: NumberLiteral( + ExprNumberLiteral { + range: 4243..4244, + value: Int( + 3, + ), + }, + ), + }, + ), + ], + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 4256..4259, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 4256..4259, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 4264..4304, + pattern: MatchSequence( + PatternMatchSequence { + range: 4269..4291, + patterns: [ + MatchValue( + PatternMatchValue { + range: 4270..4276, + value: BinOp( + ExprBinOp { + range: 4270..4276, + left: NumberLiteral( + ExprNumberLiteral { + range: 4270..4271, + value: Int( + 1, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 4274..4276, + value: Complex { + real: 0.0, + imag: 2.0, + }, + }, + ), + }, + ), + }, + ), + MatchAs( + PatternMatchAs { + range: 4278..4279, + pattern: None, + name: Some( + Identifier { + id: "a", + range: 4278..4279, + }, + ), + }, + ), + MatchSingleton( + PatternMatchSingleton { + range: 4281..4285, + value: None, + }, + ), + MatchValue( + PatternMatchValue { + range: 4287..4290, + value: Attribute( + ExprAttribute { + range: 4287..4290, + value: Name( + ExprName { + range: 4287..4288, + id: "a", + ctx: Load, + }, + ), + attr: Identifier { + id: "b", + range: 4289..4290, + }, + ctx: Load, + }, + ), + }, + ), + ], + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 4301..4304, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 4301..4304, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 4309..4343, + pattern: MatchAs( + PatternMatchAs { + range: 4314..4330, + pattern: Some( + MatchSequence( + PatternMatchSequence { + range: 4314..4325, + patterns: [ + MatchAs( + PatternMatchAs { + range: 4315..4321, + pattern: Some( + MatchValue( + PatternMatchValue { + range: 4315..4316, + value: NumberLiteral( + ExprNumberLiteral { + range: 4315..4316, + value: Int( + 1, + ), + }, + ), + }, + ), + ), + name: Some( + Identifier { + id: "X", + range: 4320..4321, + }, + ), + }, + ), + MatchAs( + PatternMatchAs { + range: 4323..4324, + pattern: None, + name: Some( + Identifier { + id: "b", + range: 4323..4324, + }, + ), + }, + ), + ], + }, + ), + ), + name: Some( + Identifier { + id: "S", + range: 4329..4330, + }, + ), + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 4340..4343, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 4340..4343, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 4348..4380, + pattern: MatchSequence( + PatternMatchSequence { + range: 4353..4367, + patterns: [ + MatchValue( + PatternMatchValue { + range: 4354..4355, + value: NumberLiteral( + ExprNumberLiteral { + range: 4354..4355, + value: Int( + 1, + ), + }, + ), + }, + ), + MatchValue( + PatternMatchValue { + range: 4357..4358, + value: NumberLiteral( + ExprNumberLiteral { + range: 4357..4358, + value: Int( + 2, + ), + }, + ), + }, + ), + MatchValue( + PatternMatchValue { + range: 4360..4366, + value: BinOp( + ExprBinOp { + range: 4360..4366, + left: NumberLiteral( + ExprNumberLiteral { + range: 4360..4361, + value: Int( + 3, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 4364..4366, + value: Complex { + real: 0.0, + imag: 1.0, + }, + }, + ), + }, + ), + }, + ), + ], + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 4377..4380, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 4377..4380, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 4385..4413, + pattern: MatchSequence( + PatternMatchSequence { + range: 4390..4400, + patterns: [ + MatchSequence( + PatternMatchSequence { + range: 4391..4396, + patterns: [ + MatchValue( + PatternMatchValue { + range: 4392..4393, + value: NumberLiteral( + ExprNumberLiteral { + range: 4392..4393, + value: Int( + 1, + ), + }, + ), + }, + ), + MatchValue( + PatternMatchValue { + range: 4394..4395, + value: NumberLiteral( + ExprNumberLiteral { + range: 4394..4395, + value: Int( + 2, + ), + }, + ), + }, + ), + ], + }, + ), + MatchValue( + PatternMatchValue { + range: 4398..4399, + value: NumberLiteral( + ExprNumberLiteral { + range: 4398..4399, + value: Int( + 3, + ), + }, + ), + }, + ), + ], + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 4410..4413, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 4410..4413, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 4418..4439, + pattern: MatchSequence( + PatternMatchSequence { + range: 4423..4426, + patterns: [ + MatchValue( + PatternMatchValue { + range: 4424..4425, + value: NumberLiteral( + ExprNumberLiteral { + range: 4424..4425, + value: Int( + 1, + ), + }, + ), + }, + ), + ], + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 4436..4439, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 4436..4439, + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 4460..4589, + subject: Name( + ExprName { + range: 4466..4467, + id: "x", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 4473..4494, + pattern: MatchSequence( + PatternMatchSequence { + range: 4478..4481, + patterns: [ + MatchStar( + PatternMatchStar { + range: 4478..4480, + name: Some( + Identifier { + id: "a", + range: 4479..4480, + }, + ), + }, + ), + ], + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 4491..4494, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 4491..4494, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 4499..4520, + pattern: MatchSequence( + PatternMatchSequence { + range: 4504..4507, + patterns: [ + MatchStar( + PatternMatchStar { + range: 4504..4506, + name: None, + }, + ), + ], + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 4517..4520, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 4517..4520, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 4525..4556, + pattern: MatchSequence( + PatternMatchSequence { + range: 4530..4543, + patterns: [ + MatchValue( + PatternMatchValue { + range: 4531..4532, + value: NumberLiteral( + ExprNumberLiteral { + range: 4531..4532, + value: Int( + 1, + ), + }, + ), + }, + ), + MatchValue( + PatternMatchValue { + range: 4534..4535, + value: NumberLiteral( + ExprNumberLiteral { + range: 4534..4535, + value: Int( + 2, + ), + }, + ), + }, + ), + MatchStar( + PatternMatchStar { + range: 4537..4542, + name: Some( + Identifier { + id: "rest", + range: 4538..4542, + }, + ), + }, + ), + ], + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 4553..4556, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 4553..4556, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 4561..4589, + pattern: MatchSequence( + PatternMatchSequence { + range: 4566..4576, + patterns: [ + MatchStar( + PatternMatchStar { + range: 4567..4569, + name: None, + }, + ), + MatchValue( + PatternMatchValue { + range: 4571..4572, + value: NumberLiteral( + ExprNumberLiteral { + range: 4571..4572, + value: Int( + 1, + ), + }, + ), + }, + ), + MatchValue( + PatternMatchValue { + range: 4574..4575, + value: NumberLiteral( + ExprNumberLiteral { + range: 4574..4575, + value: Int( + 2, + ), + }, + ), + }, + ), + ], + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 4586..4589, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 4586..4589, + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 4611..4883, + subject: Name( + ExprName { + range: 4617..4618, + id: "x", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 4624..4649, + pattern: MatchClass( + PatternMatchClass { + range: 4629..4636, + cls: Name( + ExprName { + range: 4629..4634, + id: "Point", + ctx: Load, + }, + ), + arguments: PatternArguments { + range: 4634..4636, + patterns: [], + keywords: [], + }, + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 4646..4649, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 4646..4649, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 4654..4683, + pattern: MatchClass( + PatternMatchClass { + range: 4659..4670, + cls: Attribute( + ExprAttribute { + range: 4659..4668, + value: Attribute( + ExprAttribute { + range: 4659..4662, + value: Name( + ExprName { + range: 4659..4660, + id: "a", + ctx: Load, + }, + ), + attr: Identifier { + id: "b", + range: 4661..4662, + }, + ctx: Load, + }, + ), + attr: Identifier { + id: "Point", + range: 4663..4668, + }, + ctx: Load, + }, + ), + arguments: PatternArguments { + range: 4668..4670, + patterns: [], + keywords: [], + }, + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 4680..4683, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 4680..4683, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 4688..4718, + pattern: MatchClass( + PatternMatchClass { + range: 4693..4705, + cls: Name( + ExprName { + range: 4693..4700, + id: "Point2D", + ctx: Load, + }, + ), + arguments: PatternArguments { + range: 4700..4705, + patterns: [], + keywords: [ + PatternKeyword { + range: 4701..4704, + attr: Identifier { + id: "x", + range: 4701..4702, + }, + pattern: MatchValue( + PatternMatchValue { + range: 4703..4704, + value: NumberLiteral( + ExprNumberLiteral { + range: 4703..4704, + value: Int( + 0, + ), + }, + ), + }, + ), + }, + ], + }, + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 4715..4718, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 4715..4718, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 4723..4759, + pattern: MatchClass( + PatternMatchClass { + range: 4728..4746, + cls: Name( + ExprName { + range: 4728..4735, + id: "Point2D", + ctx: Load, + }, + ), + arguments: PatternArguments { + range: 4735..4746, + patterns: [], + keywords: [ + PatternKeyword { + range: 4736..4739, + attr: Identifier { + id: "x", + range: 4736..4737, + }, + pattern: MatchValue( + PatternMatchValue { + range: 4738..4739, + value: NumberLiteral( + ExprNumberLiteral { + range: 4738..4739, + value: Int( + 0, + ), + }, + ), + }, + ), + }, + PatternKeyword { + range: 4741..4744, + attr: Identifier { + id: "y", + range: 4741..4742, + }, + pattern: MatchValue( + PatternMatchValue { + range: 4743..4744, + value: NumberLiteral( + ExprNumberLiteral { + range: 4743..4744, + value: Int( + 0, + ), + }, + ), + }, + ), + }, + ], + }, + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 4756..4759, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 4756..4759, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 4764..4795, + pattern: MatchClass( + PatternMatchClass { + range: 4769..4782, + cls: Name( + ExprName { + range: 4769..4776, + id: "Point2D", + ctx: Load, + }, + ), + arguments: PatternArguments { + range: 4776..4782, + patterns: [ + MatchValue( + PatternMatchValue { + range: 4777..4778, + value: NumberLiteral( + ExprNumberLiteral { + range: 4777..4778, + value: Int( + 0, + ), + }, + ), + }, + ), + MatchValue( + PatternMatchValue { + range: 4780..4781, + value: NumberLiteral( + ExprNumberLiteral { + range: 4780..4781, + value: Int( + 1, + ), + }, + ), + }, + ), + ], + keywords: [], + }, + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 4792..4795, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 4792..4795, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 4800..4838, + pattern: MatchClass( + PatternMatchClass { + range: 4805..4825, + cls: Name( + ExprName { + range: 4805..4812, + id: "Point2D", + ctx: Load, + }, + ), + arguments: PatternArguments { + range: 4812..4825, + patterns: [ + MatchSequence( + PatternMatchSequence { + range: 4813..4819, + patterns: [ + MatchValue( + PatternMatchValue { + range: 4814..4815, + value: NumberLiteral( + ExprNumberLiteral { + range: 4814..4815, + value: Int( + 0, + ), + }, + ), + }, + ), + MatchValue( + PatternMatchValue { + range: 4817..4818, + value: NumberLiteral( + ExprNumberLiteral { + range: 4817..4818, + value: Int( + 1, + ), + }, + ), + }, + ), + ], + }, + ), + ], + keywords: [ + PatternKeyword { + range: 4821..4824, + attr: Identifier { + id: "y", + range: 4821..4822, + }, + pattern: MatchValue( + PatternMatchValue { + range: 4823..4824, + value: NumberLiteral( + ExprNumberLiteral { + range: 4823..4824, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ], + }, + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 4835..4838, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 4835..4838, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 4843..4883, + pattern: MatchClass( + PatternMatchClass { + range: 4848..4870, + cls: Name( + ExprName { + range: 4848..4855, + id: "Point2D", + ctx: Load, + }, + ), + arguments: PatternArguments { + range: 4855..4870, + patterns: [], + keywords: [ + PatternKeyword { + range: 4856..4864, + attr: Identifier { + id: "x", + range: 4856..4857, + }, + pattern: MatchSequence( + PatternMatchSequence { + range: 4858..4864, + patterns: [ + MatchValue( + PatternMatchValue { + range: 4859..4860, + value: NumberLiteral( + ExprNumberLiteral { + range: 4859..4860, + value: Int( + 0, + ), + }, + ), + }, + ), + MatchValue( + PatternMatchValue { + range: 4862..4863, + value: NumberLiteral( + ExprNumberLiteral { + range: 4862..4863, + value: Int( + 1, + ), + }, + ), + }, + ), + ], + }, + ), + }, + PatternKeyword { + range: 4866..4869, + attr: Identifier { + id: "y", + range: 4866..4867, + }, + pattern: MatchValue( + PatternMatchValue { + range: 4868..4869, + value: NumberLiteral( + ExprNumberLiteral { + range: 4868..4869, + value: Int( + 1, + ), + }, + ), + }, + ), + }, + ], + }, + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 4880..4883, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 4880..4883, + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 4907..5001, + subject: Named( + ExprNamed { + range: 4913..4919, + target: Name( + ExprName { + range: 4913..4914, + id: "x", + ctx: Store, + }, + ), + value: Name( + ExprName { + range: 4918..4919, + id: "b", + ctx: Load, + }, + ), + }, + ), + cases: [ + MatchCase { + range: 4925..4949, + pattern: MatchMapping( + PatternMatchMapping { + range: 4930..4936, + keys: [ + NumberLiteral( + ExprNumberLiteral { + range: 4931..4932, + value: Int( + 1, + ), + }, + ), + ], + patterns: [ + MatchAs( + PatternMatchAs { + range: 4934..4935, + pattern: None, + name: None, + }, + ), + ], + rest: None, + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 4946..4949, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 4946..4949, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 4954..5001, + pattern: MatchMapping( + PatternMatchMapping { + range: 4959..4988, + keys: [ + StringLiteral( + ExprStringLiteral { + range: 4960..4962, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 4960..4962, + value: "", + flags: StringLiteralFlags { + quote_style: Single, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + NoneLiteral( + ExprNoneLiteral { + range: 4967..4971, + }, + ), + ], + patterns: [ + MatchAs( + PatternMatchAs { + range: 4964..4965, + pattern: None, + name: Some( + Identifier { + id: "a", + range: 4964..4965, + }, + ), + }, + ), + MatchSequence( + PatternMatchSequence { + range: 4973..4979, + patterns: [ + MatchValue( + PatternMatchValue { + range: 4974..4975, + value: NumberLiteral( + ExprNumberLiteral { + range: 4974..4975, + value: Int( + 1, + ), + }, + ), + }, + ), + MatchValue( + PatternMatchValue { + range: 4977..4978, + value: NumberLiteral( + ExprNumberLiteral { + range: 4977..4978, + value: Int( + 2, + ), + }, + ), + }, + ), + ], + }, + ), + ], + rest: Some( + Identifier { + id: "rest", + range: 4983..4987, + }, + ), + }, + ), + guard: None, + body: [ + Expr( + StmtExpr { + range: 4998..5001, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 4998..5001, + }, + ), + }, + ), + ], + }, + ], + }, + ), + Match( + StmtMatch { + range: 5019..5079, + subject: Name( + ExprName { + range: 5025..5026, + id: "y", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 5032..5053, + pattern: MatchAs( + PatternMatchAs { + range: 5037..5038, + pattern: None, + name: Some( + Identifier { + id: "a", + range: 5037..5038, + }, + ), + }, + ), + guard: Some( + Named( + ExprNamed { + range: 5042..5048, + target: Name( + ExprName { + range: 5042..5043, + id: "b", + ctx: Store, + }, + ), + value: Name( + ExprName { + range: 5047..5048, + id: "c", + ctx: Load, + }, + ), + }, + ), + ), + body: [ + Expr( + StmtExpr { + range: 5050..5053, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 5050..5053, + }, + ), + }, + ), + ], + }, + MatchCase { + range: 5058..5079, + pattern: MatchAs( + PatternMatchAs { + range: 5063..5064, + pattern: None, + name: Some( + Identifier { + id: "e", + range: 5063..5064, + }, + ), + }, + ), + guard: Some( + Compare( + ExprCompare { + range: 5069..5074, + left: NumberLiteral( + ExprNumberLiteral { + range: 5069..5070, + value: Int( + 1, + ), + }, + ), + ops: [ + Lt, + ], + comparators: [ + NumberLiteral( + ExprNumberLiteral { + range: 5073..5074, + value: Int( + 2, + ), + }, + ), + ], + }, + ), + ), + body: [ + Expr( + StmtExpr { + range: 5076..5079, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 5076..5079, + }, + ), + }, + ), + ], + }, + ], + }, + ), + Expr( + StmtExpr { + range: 5108..5123, + value: Tuple( + ExprTuple { + range: 5108..5123, + elts: [ + BinOp( + ExprBinOp { + range: 5108..5120, + left: BinOp( + ExprBinOp { + range: 5108..5116, + left: Name( + ExprName { + range: 5108..5113, + id: "match", + ctx: Load, + }, + ), + op: Mult, + right: Name( + ExprName { + range: 5115..5116, + id: "a", + ctx: Load, + }, + ), + }, + ), + op: Add, + right: Name( + ExprName { + range: 5119..5120, + id: "b", + ctx: Load, + }, + ), + }, + ), + Name( + ExprName { + range: 5122..5123, + id: "c", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 5149..5166, + value: Tuple( + ExprTuple { + range: 5149..5166, + elts: [ + BinOp( + ExprBinOp { + range: 5149..5163, + left: Name( + ExprName { + range: 5149..5154, + id: "match", + ctx: Load, + }, + ), + op: Mult, + right: BinOp( + ExprBinOp { + range: 5157..5162, + left: Name( + ExprName { + range: 5157..5158, + id: "a", + ctx: Load, + }, + ), + op: Add, + right: Name( + ExprName { + range: 5161..5162, + id: "b", + ctx: Load, + }, + ), + }, + ), + }, + ), + Name( + ExprName { + range: 5165..5166, + id: "c", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 5192..5209, + value: Call( + ExprCall { + range: 5192..5209, + func: Name( + ExprName { + range: 5192..5197, + id: "match", + ctx: Load, + }, + ), + arguments: Arguments { + range: 5198..5209, + args: [ + Starred( + ExprStarred { + range: 5199..5205, + value: BinOp( + ExprBinOp { + range: 5200..5205, + left: Name( + ExprName { + range: 5200..5201, + id: "a", + ctx: Load, + }, + ), + op: Add, + right: Name( + ExprName { + range: 5204..5205, + id: "b", + ctx: Load, + }, + ), + }, + ), + ctx: Load, + }, + ), + Name( + ExprName { + range: 5207..5208, + id: "c", + ctx: Load, + }, + ), + ], + keywords: [], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 5236..5252, + value: BinOp( + ExprBinOp { + range: 5236..5252, + left: BinOp( + ExprBinOp { + range: 5236..5248, + left: Name( + ExprName { + range: 5236..5241, + id: "match", + ctx: Load, + }, + ), + op: Sub, + right: BinOp( + ExprBinOp { + range: 5243..5248, + left: Name( + ExprName { + range: 5243..5244, + id: "a", + ctx: Load, + }, + ), + op: Mult, + right: Name( + ExprName { + range: 5247..5248, + id: "b", + ctx: Load, + }, + ), + }, + ), + }, + ), + op: Add, + right: Name( + ExprName { + range: 5251..5252, + id: "c", + ctx: Load, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 5279..5297, + value: BinOp( + ExprBinOp { + range: 5279..5297, + left: BinOp( + ExprBinOp { + range: 5279..5293, + left: Name( + ExprName { + range: 5279..5284, + id: "match", + ctx: Load, + }, + ), + op: Sub, + right: BinOp( + ExprBinOp { + range: 5287..5292, + left: Name( + ExprName { + range: 5287..5288, + id: "a", + ctx: Load, + }, + ), + op: Mult, + right: Name( + ExprName { + range: 5291..5292, + id: "b", + ctx: Load, + }, + ), + }, + ), + }, + ), + op: Add, + right: Name( + ExprName { + range: 5296..5297, + id: "c", + ctx: Load, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 5324..5342, + value: BinOp( + ExprBinOp { + range: 5324..5342, + left: BinOp( + ExprBinOp { + range: 5324..5338, + left: Call( + ExprCall { + range: 5324..5334, + func: Name( + ExprName { + range: 5324..5329, + id: "match", + ctx: Load, + }, + ), + arguments: Arguments { + range: 5330..5334, + args: [ + UnaryOp( + ExprUnaryOp { + range: 5331..5333, + op: USub, + operand: Name( + ExprName { + range: 5332..5333, + id: "a", + ctx: Load, + }, + ), + }, + ), + ], + keywords: [], + }, + }, + ), + op: Mult, + right: Name( + ExprName { + range: 5337..5338, + id: "b", + ctx: Load, + }, + ), + }, + ), + op: Add, + right: Name( + ExprName { + range: 5341..5342, + id: "c", + ctx: Load, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 5370..5380, + value: Attribute( + ExprAttribute { + range: 5370..5380, + value: Call( + ExprCall { + range: 5370..5378, + func: Name( + ExprName { + range: 5370..5375, + id: "match", + ctx: Load, + }, + ), + arguments: Arguments { + range: 5376..5378, + args: [], + keywords: [], + }, + }, + ), + attr: Identifier { + id: "a", + range: 5379..5380, + }, + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 5397..5409, + value: Attribute( + ExprAttribute { + range: 5397..5409, + value: Call( + ExprCall { + range: 5397..5407, + func: Name( + ExprName { + range: 5397..5402, + id: "match", + ctx: Load, + }, + ), + arguments: Arguments { + range: 5403..5407, + args: [ + Tuple( + ExprTuple { + range: 5404..5406, + elts: [], + ctx: Load, + parenthesized: true, + }, + ), + ], + keywords: [], + }, + }, + ), + attr: Identifier { + id: "a", + range: 5408..5409, + }, + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 5428..5441, + value: Attribute( + ExprAttribute { + range: 5428..5441, + value: Call( + ExprCall { + range: 5428..5439, + func: Name( + ExprName { + range: 5428..5433, + id: "match", + ctx: Load, + }, + ), + arguments: Arguments { + range: 5434..5439, + args: [ + Tuple( + ExprTuple { + range: 5435..5437, + elts: [], + ctx: Load, + parenthesized: true, + }, + ), + ], + keywords: [], + }, + }, + ), + attr: Identifier { + id: "a", + range: 5440..5441, + }, + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 5460..5471, + value: Attribute( + ExprAttribute { + range: 5460..5471, + value: Subscript( + ExprSubscript { + range: 5460..5469, + value: Name( + ExprName { + range: 5460..5465, + id: "match", + ctx: Load, + }, + ), + slice: Name( + ExprName { + range: 5467..5468, + id: "a", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + attr: Identifier { + id: "b", + range: 5470..5471, + }, + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 5489..5501, + value: Attribute( + ExprAttribute { + range: 5489..5501, + value: Subscript( + ExprSubscript { + range: 5489..5499, + value: Name( + ExprName { + range: 5489..5494, + id: "match", + ctx: Load, + }, + ), + slice: Tuple( + ExprTuple { + range: 5496..5498, + elts: [ + Name( + ExprName { + range: 5496..5497, + id: "a", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + ctx: Load, + }, + ), + attr: Identifier { + id: "b", + range: 5500..5501, + }, + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 5542..5556, + value: Attribute( + ExprAttribute { + range: 5542..5556, + value: Subscript( + ExprSubscript { + range: 5542..5554, + value: Name( + ExprName { + range: 5542..5547, + id: "match", + ctx: Load, + }, + ), + slice: Tuple( + ExprTuple { + range: 5549..5553, + elts: [ + Name( + ExprName { + range: 5550..5551, + id: "a", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + ctx: Load, + }, + ), + attr: Identifier { + id: "b", + range: 5555..5556, + }, + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 5577..5594, + value: Subscript( + ExprSubscript { + range: 5577..5594, + value: Call( + ExprCall { + range: 5577..5584, + func: Name( + ExprName { + range: 5577..5582, + id: "match", + ctx: Load, + }, + ), + arguments: Arguments { + range: 5582..5584, + args: [], + keywords: [], + }, + }, + ), + slice: Slice( + ExprSlice { + range: 5585..5593, + lower: Some( + Name( + ExprName { + range: 5585..5586, + id: "a", + ctx: Load, + }, + ), + ), + upper: Some( + Name( + ExprName { + range: 5592..5593, + id: "b", + ctx: Load, + }, + ), + ), + step: None, + }, + ), + ctx: Load, + }, + ), + }, + ), + If( + StmtIf { + range: 5614..5633, + test: Named( + ExprNamed { + range: 5617..5627, + target: Name( + ExprName { + range: 5617..5622, + id: "match", + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 5626..5627, + value: Int( + 1, + ), + }, + ), + }, + ), + body: [ + Pass( + StmtPass { + range: 5629..5633, + }, + ), + ], + elif_else_clauses: [], + }, + ), + Match( + StmtMatch { + range: 5634..5688, + subject: Name( + ExprName { + range: 5640..5645, + id: "match", + ctx: Load, + }, + ), + cases: [ + MatchCase { + range: 5651..5663, + pattern: MatchValue( + PatternMatchValue { + range: 5656..5657, + value: NumberLiteral( + ExprNumberLiteral { + range: 5656..5657, + value: Int( + 1, + ), + }, + ), + }, + ), + guard: None, + body: [ + Pass( + StmtPass { + range: 5659..5663, + }, + ), + ], + }, + MatchCase { + range: 5668..5688, + pattern: MatchValue( + PatternMatchValue { + range: 5673..5674, + value: NumberLiteral( + ExprNumberLiteral { + range: 5673..5674, + value: Int( + 2, + ), + }, + ), + }, + ), + guard: None, + body: [ + Pass( + StmtPass { + range: 5684..5688, + }, + ), + ], + }, + ], + }, + ), + Assign( + StmtAssign { + range: 5689..5725, + targets: [ + Name( + ExprName { + range: 5689..5694, + id: "match", + ctx: Store, + }, + ), + ], + value: Lambda( + ExprLambda { + range: 5697..5725, + parameters: Some( + Parameters { + range: 5704..5709, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 5704..5709, + parameter: Parameter { + range: 5704..5709, + name: Identifier { + id: "query", + range: 5704..5709, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: Compare( + ExprCompare { + range: 5711..5725, + left: Name( + ExprName { + range: 5711..5716, + id: "query", + ctx: Load, + }, + ), + ops: [ + Eq, + ], + comparators: [ + Name( + ExprName { + range: 5720..5725, + id: "event", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 5726..5742, + value: Call( + ExprCall { + range: 5726..5742, + func: Name( + ExprName { + range: 5726..5731, + id: "print", + ctx: Load, + }, + ), + arguments: Arguments { + range: 5731..5742, + args: [ + Call( + ExprCall { + range: 5732..5741, + func: Name( + ExprName { + range: 5732..5737, + id: "match", + ctx: Load, + }, + ), + arguments: Arguments { + range: 5737..5741, + args: [ + NumberLiteral( + ExprNumberLiteral { + range: 5738..5740, + value: Int( + 12, + ), + }, + ), + ], + keywords: [], + }, + }, + ), + ], + keywords: [], + }, + }, + ), + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__raise.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__raise.py.snap.new new file mode 100644 index 0000000000..a639504e78 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__raise.py.snap.new @@ -0,0 +1,491 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/valid/statement/raise.py +--- +## AST + +``` +Module( + ModModule { + range: 0..289, + body: [ + Raise( + StmtRaise { + range: 8..13, + exc: None, + cause: None, + }, + ), + Raise( + StmtRaise { + range: 14..21, + exc: Some( + Name( + ExprName { + range: 20..21, + id: "a", + ctx: Load, + }, + ), + ), + cause: None, + }, + ), + Raise( + StmtRaise { + range: 22..34, + exc: Some( + Tuple( + ExprTuple { + range: 28..34, + elts: [ + Name( + ExprName { + range: 29..30, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 32..33, + id: "b", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + ), + cause: None, + }, + ), + Raise( + StmtRaise { + range: 35..46, + exc: Some( + Compare( + ExprCompare { + range: 41..46, + left: NumberLiteral( + ExprNumberLiteral { + range: 41..42, + value: Int( + 1, + ), + }, + ), + ops: [ + Lt, + ], + comparators: [ + NumberLiteral( + ExprNumberLiteral { + range: 45..46, + value: Int( + 2, + ), + }, + ), + ], + }, + ), + ), + cause: None, + }, + ), + Raise( + StmtRaise { + range: 47..60, + exc: Some( + BoolOp( + ExprBoolOp { + range: 53..60, + op: And, + values: [ + Name( + ExprName { + range: 53..54, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 59..60, + id: "b", + ctx: Load, + }, + ), + ], + }, + ), + ), + cause: None, + }, + ), + Raise( + StmtRaise { + range: 61..78, + exc: Some( + Lambda( + ExprLambda { + range: 67..78, + parameters: Some( + Parameters { + range: 74..75, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 74..75, + parameter: Parameter { + range: 74..75, + name: Identifier { + id: "x", + range: 74..75, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: Name( + ExprName { + range: 77..78, + id: "y", + ctx: Load, + }, + ), + }, + ), + ), + cause: None, + }, + ), + Raise( + StmtRaise { + range: 79..92, + exc: Some( + Await( + ExprAwait { + range: 85..92, + value: Name( + ExprName { + range: 91..92, + id: "x", + ctx: Load, + }, + ), + }, + ), + ), + cause: None, + }, + ), + Raise( + StmtRaise { + range: 93..115, + exc: Some( + If( + ExprIf { + range: 99..115, + test: BooleanLiteral( + ExprBooleanLiteral { + range: 104..108, + value: true, + }, + ), + body: Name( + ExprName { + range: 99..100, + id: "x", + ctx: Load, + }, + ), + orelse: Name( + ExprName { + range: 114..115, + id: "y", + ctx: Load, + }, + ), + }, + ), + ), + cause: None, + }, + ), + Raise( + StmtRaise { + range: 138..152, + exc: Some( + Name( + ExprName { + range: 144..145, + id: "x", + ctx: Load, + }, + ), + ), + cause: Some( + Name( + ExprName { + range: 151..152, + id: "a", + ctx: Load, + }, + ), + ), + }, + ), + Raise( + StmtRaise { + range: 153..172, + exc: Some( + Name( + ExprName { + range: 159..160, + id: "x", + ctx: Load, + }, + ), + ), + cause: Some( + Tuple( + ExprTuple { + range: 166..172, + elts: [ + Name( + ExprName { + range: 167..168, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 170..171, + id: "b", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + ), + }, + ), + Raise( + StmtRaise { + range: 173..191, + exc: Some( + Name( + ExprName { + range: 179..180, + id: "x", + ctx: Load, + }, + ), + ), + cause: Some( + Compare( + ExprCompare { + range: 186..191, + left: NumberLiteral( + ExprNumberLiteral { + range: 186..187, + value: Int( + 1, + ), + }, + ), + ops: [ + Lt, + ], + comparators: [ + NumberLiteral( + ExprNumberLiteral { + range: 190..191, + value: Int( + 2, + ), + }, + ), + ], + }, + ), + ), + }, + ), + Raise( + StmtRaise { + range: 192..212, + exc: Some( + Name( + ExprName { + range: 198..199, + id: "x", + ctx: Load, + }, + ), + ), + cause: Some( + BoolOp( + ExprBoolOp { + range: 205..212, + op: And, + values: [ + Name( + ExprName { + range: 205..206, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 211..212, + id: "b", + ctx: Load, + }, + ), + ], + }, + ), + ), + }, + ), + Raise( + StmtRaise { + range: 213..237, + exc: Some( + Name( + ExprName { + range: 219..220, + id: "x", + ctx: Load, + }, + ), + ), + cause: Some( + Lambda( + ExprLambda { + range: 226..237, + parameters: Some( + Parameters { + range: 233..234, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 233..234, + parameter: Parameter { + range: 233..234, + name: Identifier { + id: "x", + range: 233..234, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: Name( + ExprName { + range: 236..237, + id: "y", + ctx: Load, + }, + ), + }, + ), + ), + }, + ), + Raise( + StmtRaise { + range: 238..258, + exc: Some( + Name( + ExprName { + range: 244..245, + id: "x", + ctx: Load, + }, + ), + ), + cause: Some( + Await( + ExprAwait { + range: 251..258, + value: Name( + ExprName { + range: 257..258, + id: "x", + ctx: Load, + }, + ), + }, + ), + ), + }, + ), + Raise( + StmtRaise { + range: 259..288, + exc: Some( + Name( + ExprName { + range: 265..266, + id: "x", + ctx: Load, + }, + ), + ), + cause: Some( + If( + ExprIf { + range: 272..288, + test: BooleanLiteral( + ExprBooleanLiteral { + range: 277..281, + value: true, + }, + ), + body: Name( + ExprName { + range: 272..273, + id: "x", + ctx: Load, + }, + ), + orelse: Name( + ExprName { + range: 287..288, + id: "y", + ctx: Load, + }, + ), + }, + ), + ), + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__return.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__return.py.snap.new new file mode 100644 index 0000000000..f6a9a4e2a9 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__return.py.snap.new @@ -0,0 +1,383 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/valid/statement/return.py +--- +## AST + +``` +Module( + ModModule { + range: 0..191, + body: [ + Return( + StmtReturn { + range: 0..6, + value: None, + }, + ), + Return( + StmtReturn { + range: 7..15, + value: Some( + Name( + ExprName { + range: 14..15, + id: "x", + ctx: Load, + }, + ), + ), + }, + ), + Return( + StmtReturn { + range: 16..25, + value: Some( + Starred( + ExprStarred { + range: 23..25, + value: Name( + ExprName { + range: 24..25, + id: "x", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ), + }, + ), + Return( + StmtReturn { + range: 26..39, + value: Some( + Starred( + ExprStarred { + range: 33..39, + value: BinOp( + ExprBinOp { + range: 34..39, + left: Name( + ExprName { + range: 34..35, + id: "x", + ctx: Load, + }, + ), + op: BitOr, + right: Name( + ExprName { + range: 38..39, + id: "y", + ctx: Load, + }, + ), + }, + ), + ctx: Load, + }, + ), + ), + }, + ), + Return( + StmtReturn { + range: 40..53, + value: Some( + Tuple( + ExprTuple { + range: 47..53, + elts: [ + Starred( + ExprStarred { + range: 47..49, + value: Name( + ExprName { + range: 48..49, + id: "x", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + Starred( + ExprStarred { + range: 51..53, + value: Name( + ExprName { + range: 52..53, + id: "y", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + ), + }, + ), + Return( + StmtReturn { + range: 54..69, + value: Some( + Named( + ExprNamed { + range: 62..68, + target: Name( + ExprName { + range: 62..63, + id: "x", + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 67..68, + value: Int( + 1, + ), + }, + ), + }, + ), + ), + }, + ), + Return( + StmtReturn { + range: 70..81, + value: Some( + NoneLiteral( + ExprNoneLiteral { + range: 77..81, + }, + ), + ), + }, + ), + Return( + StmtReturn { + range: 82..96, + value: Some( + BoolOp( + ExprBoolOp { + range: 89..96, + op: And, + values: [ + Name( + ExprName { + range: 89..90, + id: "x", + ctx: Load, + }, + ), + Name( + ExprName { + range: 95..96, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + ), + }, + ), + Return( + StmtReturn { + range: 97..109, + value: Some( + Compare( + ExprCompare { + range: 104..109, + left: NumberLiteral( + ExprNumberLiteral { + range: 104..105, + value: Int( + 1, + ), + }, + ), + ops: [ + Lt, + ], + comparators: [ + NumberLiteral( + ExprNumberLiteral { + range: 108..109, + value: Int( + 2, + ), + }, + ), + ], + }, + ), + ), + }, + ), + Return( + StmtReturn { + range: 110..122, + value: Some( + Tuple( + ExprTuple { + range: 117..122, + elts: [ + NumberLiteral( + ExprNumberLiteral { + range: 117..118, + value: Int( + 1, + ), + }, + ), + NumberLiteral( + ExprNumberLiteral { + range: 120..121, + value: Int( + 2, + ), + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + ), + }, + ), + Return( + StmtReturn { + range: 123..136, + value: Some( + Call( + ExprCall { + range: 130..136, + func: Name( + ExprName { + range: 130..134, + id: "call", + ctx: Load, + }, + ), + arguments: Arguments { + range: 134..136, + args: [], + keywords: [], + }, + }, + ), + ), + }, + ), + Return( + StmtReturn { + range: 137..156, + value: Some( + Call( + ExprCall { + range: 144..156, + func: Attribute( + ExprAttribute { + range: 144..154, + value: Name( + ExprName { + range: 144..148, + id: "attr", + ctx: Load, + }, + ), + attr: Identifier { + id: "value", + range: 149..154, + }, + ctx: Load, + }, + ), + arguments: Arguments { + range: 154..156, + args: [], + keywords: [], + }, + }, + ), + ), + }, + ), + Return( + StmtReturn { + range: 157..171, + value: Some( + Await( + ExprAwait { + range: 164..171, + value: Name( + ExprName { + range: 170..171, + id: "x", + ctx: Load, + }, + ), + }, + ), + ), + }, + ), + Return( + StmtReturn { + range: 172..190, + value: Some( + Lambda( + ExprLambda { + range: 179..190, + parameters: Some( + Parameters { + range: 186..187, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 186..187, + parameter: Parameter { + range: 186..187, + name: Identifier { + id: "x", + range: 186..187, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: Name( + ExprName { + range: 189..190, + id: "y", + ctx: Load, + }, + ), + }, + ), + ), + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__simple.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__simple.py.snap.new new file mode 100644 index 0000000000..83d3944114 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__simple.py.snap.new @@ -0,0 +1,249 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/valid/statement/simple.py +--- +## AST + +``` +Module( + ModModule { + range: 0..172, + body: [ + Continue( + StmtContinue { + range: 61..69, + }, + ), + Break( + StmtBreak { + range: 70..75, + }, + ), + If( + StmtIf { + range: 77..86, + test: Name( + ExprName { + range: 80..81, + id: "x", + ctx: Load, + }, + ), + body: [ + Expr( + StmtExpr { + range: 83..86, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 83..86, + }, + ), + }, + ), + ], + elif_else_clauses: [], + }, + ), + If( + StmtIf { + range: 87..100, + test: BooleanLiteral( + ExprBooleanLiteral { + range: 90..94, + value: true, + }, + ), + body: [ + Pass( + StmtPass { + range: 96..100, + }, + ), + ], + elif_else_clauses: [], + }, + ), + Expr( + StmtExpr { + range: 101..102, + value: NumberLiteral( + ExprNumberLiteral { + range: 101..102, + value: Int( + 1, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 104..105, + value: NumberLiteral( + ExprNumberLiteral { + range: 104..105, + value: Int( + 2, + ), + }, + ), + }, + ), + Pass( + StmtPass { + range: 107..111, + }, + ), + Expr( + StmtExpr { + range: 112..113, + value: NumberLiteral( + ExprNumberLiteral { + range: 112..113, + value: Int( + 1, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 115..118, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 115..118, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 120..133, + value: If( + ExprIf { + range: 120..133, + test: Name( + ExprName { + range: 125..126, + id: "b", + ctx: Load, + }, + ), + body: Name( + ExprName { + range: 120..121, + id: "a", + ctx: Load, + }, + ), + orelse: Name( + ExprName { + range: 132..133, + id: "c", + ctx: Load, + }, + ), + }, + ), + }, + ), + If( + StmtIf { + range: 135..157, + test: Name( + ExprName { + range: 138..139, + id: "c", + ctx: Load, + }, + ), + body: [ + Expr( + StmtExpr { + range: 141..142, + value: Name( + ExprName { + range: 141..142, + id: "B", + ctx: Load, + }, + ), + }, + ), + Delete( + StmtDelete { + range: 144..149, + targets: [ + Name( + ExprName { + range: 148..149, + id: "A", + ctx: Del, + }, + ), + ], + }, + ), + ], + elif_else_clauses: [ + ElifElseClause { + range: 150..157, + test: None, + body: [ + Expr( + StmtExpr { + range: 156..157, + value: Name( + ExprName { + range: 156..157, + id: "C", + ctx: Load, + }, + ), + }, + ), + ], + }, + ], + }, + ), + If( + StmtIf { + range: 158..171, + test: Name( + ExprName { + range: 161..162, + id: "x", + ctx: Load, + }, + ), + body: [ + Expr( + StmtExpr { + range: 164..171, + value: Yield( + ExprYield { + range: 164..171, + value: Some( + Name( + ExprName { + range: 170..171, + id: "x", + ctx: Load, + }, + ), + ), + }, + ), + }, + ), + ], + elif_else_clauses: [], + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__try.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__try.py.snap.new new file mode 100644 index 0000000000..ea9cce6376 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__try.py.snap.new @@ -0,0 +1,1562 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/valid/statement/try.py +--- +## AST + +``` +Module( + ModModule { + range: 0..1223, + body: [ + Try( + StmtTry { + range: 0..28, + body: [ + Expr( + StmtExpr { + range: 9..12, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 9..12, + }, + ), + }, + ), + ], + handlers: [ + ExceptHandler( + ExceptHandlerExceptHandler { + range: 13..28, + type_: None, + name: None, + body: [ + Expr( + StmtExpr { + range: 25..28, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 25..28, + }, + ), + }, + ), + ], + }, + ), + ], + orelse: [], + finalbody: [], + is_star: false, + }, + ), + Try( + StmtTry { + range: 30..106, + body: [ + Expr( + StmtExpr { + range: 39..42, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 39..42, + }, + ), + }, + ), + ], + handlers: [ + ExceptHandler( + ExceptHandlerExceptHandler { + range: 43..74, + type_: Some( + Name( + ExprName { + range: 50..60, + id: "Exception1", + ctx: Load, + }, + ), + ), + name: Some( + Identifier { + id: "e", + range: 64..65, + }, + ), + body: [ + Expr( + StmtExpr { + range: 71..74, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 71..74, + }, + ), + }, + ), + ], + }, + ), + ExceptHandler( + ExceptHandlerExceptHandler { + range: 75..106, + type_: Some( + Name( + ExprName { + range: 82..92, + id: "Exception2", + ctx: Load, + }, + ), + ), + name: Some( + Identifier { + id: "e", + range: 96..97, + }, + ), + body: [ + Expr( + StmtExpr { + range: 103..106, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 103..106, + }, + ), + }, + ), + ], + }, + ), + ], + orelse: [], + finalbody: [], + is_star: false, + }, + ), + Try( + StmtTry { + range: 108..184, + body: [ + Expr( + StmtExpr { + range: 117..120, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 117..120, + }, + ), + }, + ), + ], + handlers: [ + ExceptHandler( + ExceptHandlerExceptHandler { + range: 121..151, + type_: Some( + Name( + ExprName { + range: 128..137, + id: "Exception", + ctx: Load, + }, + ), + ), + name: Some( + Identifier { + id: "e", + range: 141..142, + }, + ), + body: [ + Expr( + StmtExpr { + range: 148..151, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 148..151, + }, + ), + }, + ), + ], + }, + ), + ExceptHandler( + ExceptHandlerExceptHandler { + range: 152..167, + type_: None, + name: None, + body: [ + Expr( + StmtExpr { + range: 164..167, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 164..167, + }, + ), + }, + ), + ], + }, + ), + ], + orelse: [], + finalbody: [ + Expr( + StmtExpr { + range: 181..184, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 181..184, + }, + ), + }, + ), + ], + is_star: false, + }, + ), + Try( + StmtTry { + range: 186..228, + body: [ + Expr( + StmtExpr { + range: 195..198, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 195..198, + }, + ), + }, + ), + ], + handlers: [ + ExceptHandler( + ExceptHandlerExceptHandler { + range: 199..214, + type_: None, + name: None, + body: [ + Expr( + StmtExpr { + range: 211..214, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 211..214, + }, + ), + }, + ), + ], + }, + ), + ], + orelse: [ + Expr( + StmtExpr { + range: 225..228, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 225..228, + }, + ), + }, + ), + ], + finalbody: [], + is_star: false, + }, + ), + Try( + StmtTry { + range: 230..289, + body: [ + Expr( + StmtExpr { + range: 239..242, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 239..242, + }, + ), + }, + ), + ], + handlers: [ + ExceptHandler( + ExceptHandlerExceptHandler { + range: 243..258, + type_: None, + name: None, + body: [ + Expr( + StmtExpr { + range: 255..258, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 255..258, + }, + ), + }, + ), + ], + }, + ), + ], + orelse: [ + Expr( + StmtExpr { + range: 269..272, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 269..272, + }, + ), + }, + ), + ], + finalbody: [ + Expr( + StmtExpr { + range: 286..289, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 286..289, + }, + ), + }, + ), + ], + is_star: false, + }, + ), + Try( + StmtTry { + range: 291..320, + body: [ + Expr( + StmtExpr { + range: 300..303, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 300..303, + }, + ), + }, + ), + ], + handlers: [], + orelse: [], + finalbody: [ + Expr( + StmtExpr { + range: 317..320, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 317..320, + }, + ), + }, + ), + ], + is_star: false, + }, + ), + Try( + StmtTry { + range: 322..365, + body: [ + Expr( + StmtExpr { + range: 331..334, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 331..334, + }, + ), + }, + ), + ], + handlers: [], + orelse: [ + Expr( + StmtExpr { + range: 345..348, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 345..348, + }, + ), + }, + ), + ], + finalbody: [ + Expr( + StmtExpr { + range: 362..365, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 362..365, + }, + ), + }, + ), + ], + is_star: false, + }, + ), + Try( + StmtTry { + range: 367..441, + body: [ + Expr( + StmtExpr { + range: 376..379, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 376..379, + }, + ), + }, + ), + ], + handlers: [ + ExceptHandler( + ExceptHandlerExceptHandler { + range: 380..409, + type_: Some( + Name( + ExprName { + range: 388..394, + id: "GroupA", + ctx: Load, + }, + ), + ), + name: Some( + Identifier { + id: "eg", + range: 398..400, + }, + ), + body: [ + Expr( + StmtExpr { + range: 406..409, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 406..409, + }, + ), + }, + ), + ], + }, + ), + ExceptHandler( + ExceptHandlerExceptHandler { + range: 410..441, + type_: Some( + Name( + ExprName { + range: 418..432, + id: "ExceptionGroup", + ctx: Load, + }, + ), + ), + name: None, + body: [ + Expr( + StmtExpr { + range: 438..441, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 438..441, + }, + ), + }, + ), + ], + }, + ), + ], + orelse: [], + finalbody: [], + is_star: true, + }, + ), + Try( + StmtTry { + range: 443..577, + body: [ + Raise( + StmtRaise { + range: 452..471, + exc: Some( + Call( + ExprCall { + range: 458..471, + func: Name( + ExprName { + range: 458..468, + id: "ValueError", + ctx: Load, + }, + ), + arguments: Arguments { + range: 468..471, + args: [ + NumberLiteral( + ExprNumberLiteral { + range: 469..470, + value: Int( + 1, + ), + }, + ), + ], + keywords: [], + }, + }, + ), + ), + cause: None, + }, + ), + ], + handlers: [ + ExceptHandler( + ExceptHandlerExceptHandler { + range: 472..525, + type_: Some( + Name( + ExprName { + range: 479..488, + id: "TypeError", + ctx: Load, + }, + ), + ), + name: Some( + Identifier { + id: "e", + range: 492..493, + }, + ), + body: [ + Expr( + StmtExpr { + range: 499..525, + value: Call( + ExprCall { + range: 499..525, + func: Name( + ExprName { + range: 499..504, + id: "print", + ctx: Load, + }, + ), + arguments: Arguments { + range: 504..525, + args: [ + FString( + ExprFString { + range: 505..524, + value: FStringValue { + inner: Single( + FString( + FString { + range: 505..524, + elements: [ + Literal( + FStringLiteralElement { + range: 507..514, + value: "caught ", + }, + ), + Expression( + FStringExpressionElement { + range: 514..523, + expression: Call( + ExprCall { + range: 515..522, + func: Name( + ExprName { + range: 515..519, + id: "type", + ctx: Load, + }, + ), + arguments: Arguments { + range: 519..522, + args: [ + Name( + ExprName { + range: 520..521, + id: "e", + ctx: Load, + }, + ), + ], + keywords: [], + }, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + ], + keywords: [], + }, + }, + ), + }, + ), + ], + }, + ), + ExceptHandler( + ExceptHandlerExceptHandler { + range: 526..577, + type_: Some( + Name( + ExprName { + range: 533..540, + id: "OSError", + ctx: Load, + }, + ), + ), + name: Some( + Identifier { + id: "e", + range: 544..545, + }, + ), + body: [ + Expr( + StmtExpr { + range: 551..577, + value: Call( + ExprCall { + range: 551..577, + func: Name( + ExprName { + range: 551..556, + id: "print", + ctx: Load, + }, + ), + arguments: Arguments { + range: 556..577, + args: [ + FString( + ExprFString { + range: 557..576, + value: FStringValue { + inner: Single( + FString( + FString { + range: 557..576, + elements: [ + Literal( + FStringLiteralElement { + range: 559..566, + value: "caught ", + }, + ), + Expression( + FStringExpressionElement { + range: 566..575, + expression: Call( + ExprCall { + range: 567..574, + func: Name( + ExprName { + range: 567..571, + id: "type", + ctx: Load, + }, + ), + arguments: Arguments { + range: 571..574, + args: [ + Name( + ExprName { + range: 572..573, + id: "e", + ctx: Load, + }, + ), + ], + keywords: [], + }, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + ], + keywords: [], + }, + }, + ), + }, + ), + ], + }, + ), + ], + orelse: [], + finalbody: [], + is_star: false, + }, + ), + Try( + StmtTry { + range: 579..831, + body: [ + Raise( + StmtRaise { + range: 588..669, + exc: Some( + Call( + ExprCall { + range: 594..669, + func: Name( + ExprName { + range: 594..608, + id: "ExceptionGroup", + ctx: Load, + }, + ), + arguments: Arguments { + range: 608..669, + args: [ + StringLiteral( + ExprStringLiteral { + range: 609..613, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 609..613, + value: "eg", + flags: StringLiteralFlags { + quote_style: Double, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + List( + ExprList { + range: 615..668, + elts: [ + Call( + ExprCall { + range: 616..629, + func: Name( + ExprName { + range: 616..626, + id: "ValueError", + ctx: Load, + }, + ), + arguments: Arguments { + range: 626..629, + args: [ + NumberLiteral( + ExprNumberLiteral { + range: 627..628, + value: Int( + 1, + ), + }, + ), + ], + keywords: [], + }, + }, + ), + Call( + ExprCall { + range: 631..643, + func: Name( + ExprName { + range: 631..640, + id: "TypeError", + ctx: Load, + }, + ), + arguments: Arguments { + range: 640..643, + args: [ + NumberLiteral( + ExprNumberLiteral { + range: 641..642, + value: Int( + 2, + ), + }, + ), + ], + keywords: [], + }, + }, + ), + Call( + ExprCall { + range: 645..655, + func: Name( + ExprName { + range: 645..652, + id: "OSError", + ctx: Load, + }, + ), + arguments: Arguments { + range: 652..655, + args: [ + NumberLiteral( + ExprNumberLiteral { + range: 653..654, + value: Int( + 3, + ), + }, + ), + ], + keywords: [], + }, + }, + ), + Call( + ExprCall { + range: 657..667, + func: Name( + ExprName { + range: 657..664, + id: "OSError", + ctx: Load, + }, + ), + arguments: Arguments { + range: 664..667, + args: [ + NumberLiteral( + ExprNumberLiteral { + range: 665..666, + value: Int( + 4, + ), + }, + ), + ], + keywords: [], + }, + }, + ), + ], + ctx: Load, + }, + ), + ], + keywords: [], + }, + }, + ), + ), + cause: None, + }, + ), + ], + handlers: [ + ExceptHandler( + ExceptHandlerExceptHandler { + range: 670..751, + type_: Some( + Name( + ExprName { + range: 678..687, + id: "TypeError", + ctx: Load, + }, + ), + ), + name: Some( + Identifier { + id: "e", + range: 691..692, + }, + ), + body: [ + Expr( + StmtExpr { + range: 698..751, + value: Call( + ExprCall { + range: 698..751, + func: Name( + ExprName { + range: 698..703, + id: "print", + ctx: Load, + }, + ), + arguments: Arguments { + range: 703..751, + args: [ + FString( + ExprFString { + range: 704..750, + value: FStringValue { + inner: Single( + FString( + FString { + range: 704..750, + elements: [ + Literal( + FStringLiteralElement { + range: 706..713, + value: "caught ", + }, + ), + Expression( + FStringExpressionElement { + range: 713..722, + expression: Call( + ExprCall { + range: 714..721, + func: Name( + ExprName { + range: 714..718, + id: "type", + ctx: Load, + }, + ), + arguments: Arguments { + range: 718..721, + args: [ + Name( + ExprName { + range: 719..720, + id: "e", + ctx: Load, + }, + ), + ], + keywords: [], + }, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + Literal( + FStringLiteralElement { + range: 722..735, + value: " with nested ", + }, + ), + Expression( + FStringExpressionElement { + range: 735..749, + expression: Attribute( + ExprAttribute { + range: 736..748, + value: Name( + ExprName { + range: 736..737, + id: "e", + ctx: Load, + }, + ), + attr: Identifier { + id: "exceptions", + range: 738..748, + }, + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + ], + keywords: [], + }, + }, + ), + }, + ), + ], + }, + ), + ExceptHandler( + ExceptHandlerExceptHandler { + range: 752..831, + type_: Some( + Name( + ExprName { + range: 760..767, + id: "OSError", + ctx: Load, + }, + ), + ), + name: Some( + Identifier { + id: "e", + range: 771..772, + }, + ), + body: [ + Expr( + StmtExpr { + range: 778..831, + value: Call( + ExprCall { + range: 778..831, + func: Name( + ExprName { + range: 778..783, + id: "print", + ctx: Load, + }, + ), + arguments: Arguments { + range: 783..831, + args: [ + FString( + ExprFString { + range: 784..830, + value: FStringValue { + inner: Single( + FString( + FString { + range: 784..830, + elements: [ + Literal( + FStringLiteralElement { + range: 786..793, + value: "caught ", + }, + ), + Expression( + FStringExpressionElement { + range: 793..802, + expression: Call( + ExprCall { + range: 794..801, + func: Name( + ExprName { + range: 794..798, + id: "type", + ctx: Load, + }, + ), + arguments: Arguments { + range: 798..801, + args: [ + Name( + ExprName { + range: 799..800, + id: "e", + ctx: Load, + }, + ), + ], + keywords: [], + }, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + Literal( + FStringLiteralElement { + range: 802..815, + value: " with nested ", + }, + ), + Expression( + FStringExpressionElement { + range: 815..829, + expression: Attribute( + ExprAttribute { + range: 816..828, + value: Name( + ExprName { + range: 816..817, + id: "e", + ctx: Load, + }, + ), + attr: Identifier { + id: "exceptions", + range: 818..828, + }, + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + ], + flags: FStringFlags { + quote_style: Double, + prefix: Regular, + triple_quoted: false, + }, + }, + ), + ), + }, + }, + ), + ], + keywords: [], + }, + }, + ), + }, + ), + ], + }, + ), + ], + orelse: [], + finalbody: [], + is_star: true, + }, + ), + Try( + StmtTry { + range: 833..1075, + body: [ + Pass( + StmtPass { + range: 842..846, + }, + ), + ], + handlers: [ + ExceptHandler( + ExceptHandlerExceptHandler { + range: 847..875, + type_: Some( + StringLiteral( + ExprStringLiteral { + range: 854..865, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 854..865, + value: "exception", + flags: StringLiteralFlags { + quote_style: Double, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + ), + name: None, + body: [ + Pass( + StmtPass { + range: 871..875, + }, + ), + ], + }, + ), + ExceptHandler( + ExceptHandlerExceptHandler { + range: 876..894, + type_: Some( + NumberLiteral( + ExprNumberLiteral { + range: 883..884, + value: Int( + 1, + ), + }, + ), + ), + name: None, + body: [ + Pass( + StmtPass { + range: 890..894, + }, + ), + ], + }, + ), + ExceptHandler( + ExceptHandlerExceptHandler { + range: 895..916, + type_: Some( + BooleanLiteral( + ExprBooleanLiteral { + range: 902..906, + value: true, + }, + ), + ), + name: None, + body: [ + Pass( + StmtPass { + range: 912..916, + }, + ), + ], + }, + ), + ExceptHandler( + ExceptHandlerExceptHandler { + range: 917..939, + type_: Some( + BinOp( + ExprBinOp { + range: 924..929, + left: NumberLiteral( + ExprNumberLiteral { + range: 924..925, + value: Int( + 1, + ), + }, + ), + op: Add, + right: NumberLiteral( + ExprNumberLiteral { + range: 928..929, + value: Int( + 1, + ), + }, + ), + }, + ), + ), + name: None, + body: [ + Pass( + StmtPass { + range: 935..939, + }, + ), + ], + }, + ), + ExceptHandler( + ExceptHandlerExceptHandler { + range: 940..962, + type_: Some( + BinOp( + ExprBinOp { + range: 947..952, + left: Name( + ExprName { + range: 947..948, + id: "a", + ctx: Load, + }, + ), + op: BitOr, + right: Name( + ExprName { + range: 951..952, + id: "b", + ctx: Load, + }, + ), + }, + ), + ), + name: None, + body: [ + Pass( + StmtPass { + range: 958..962, + }, + ), + ], + }, + ), + ExceptHandler( + ExceptHandlerExceptHandler { + range: 963..987, + type_: Some( + BoolOp( + ExprBoolOp { + range: 970..977, + op: And, + values: [ + Name( + ExprName { + range: 970..971, + id: "x", + ctx: Load, + }, + ), + Name( + ExprName { + range: 976..977, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + ), + name: None, + body: [ + Pass( + StmtPass { + range: 983..987, + }, + ), + ], + }, + ), + ExceptHandler( + ExceptHandlerExceptHandler { + range: 988..1012, + type_: Some( + Await( + ExprAwait { + range: 995..1002, + value: Name( + ExprName { + range: 1001..1002, + id: "x", + ctx: Load, + }, + ), + }, + ), + ), + name: None, + body: [ + Pass( + StmtPass { + range: 1008..1012, + }, + ), + ], + }, + ), + ExceptHandler( + ExceptHandlerExceptHandler { + range: 1013..1041, + type_: Some( + Lambda( + ExprLambda { + range: 1020..1031, + parameters: Some( + Parameters { + range: 1027..1028, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 1027..1028, + parameter: Parameter { + range: 1027..1028, + name: Identifier { + id: "x", + range: 1027..1028, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: Name( + ExprName { + range: 1030..1031, + id: "x", + ctx: Load, + }, + ), + }, + ), + ), + name: None, + body: [ + Pass( + StmtPass { + range: 1037..1041, + }, + ), + ], + }, + ), + ExceptHandler( + ExceptHandlerExceptHandler { + range: 1042..1075, + type_: Some( + If( + ExprIf { + range: 1049..1065, + test: BooleanLiteral( + ExprBooleanLiteral { + range: 1054..1058, + value: true, + }, + ), + body: Name( + ExprName { + range: 1049..1050, + id: "x", + ctx: Load, + }, + ), + orelse: Name( + ExprName { + range: 1064..1065, + id: "y", + ctx: Load, + }, + ), + }, + ), + ), + name: None, + body: [ + Pass( + StmtPass { + range: 1071..1075, + }, + ), + ], + }, + ), + ], + orelse: [], + finalbody: [], + is_star: false, + }, + ), + If( + StmtIf { + range: 1077..1222, + test: BooleanLiteral( + ExprBooleanLiteral { + range: 1080..1084, + value: true, + }, + ), + body: [ + Try( + StmtTry { + range: 1090..1133, + body: [ + Pass( + StmtPass { + range: 1103..1107, + }, + ), + ], + handlers: [], + orelse: [], + finalbody: [ + Pass( + StmtPass { + range: 1129..1133, + }, + ), + ], + is_star: false, + }, + ), + ], + elif_else_clauses: [ + ElifElseClause { + range: 1208..1222, + test: None, + body: [ + Pass( + StmtPass { + range: 1218..1222, + }, + ), + ], + }, + ], + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__type.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__type.py.snap.new new file mode 100644 index 0000000000..be8d995ca1 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__type.py.snap.new @@ -0,0 +1,2752 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/valid/statement/type.py +--- +## AST + +``` +Module( + ModModule { + range: 0..1828, + body: [ + TypeAlias( + StmtTypeAlias { + range: 0..12, + name: Name( + ExprName { + range: 5..6, + id: "X", + ctx: Store, + }, + ), + type_params: None, + value: Name( + ExprName { + range: 9..12, + id: "int", + ctx: Load, + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 13..31, + name: Name( + ExprName { + range: 18..19, + id: "X", + ctx: Store, + }, + ), + type_params: None, + value: BinOp( + ExprBinOp { + range: 22..31, + left: Name( + ExprName { + range: 22..25, + id: "int", + ctx: Load, + }, + ), + op: BitOr, + right: Name( + ExprName { + range: 28..31, + id: "str", + ctx: Load, + }, + ), + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 32..60, + name: Name( + ExprName { + range: 37..38, + id: "X", + ctx: Store, + }, + ), + type_params: None, + value: BinOp( + ExprBinOp { + range: 41..60, + left: Name( + ExprName { + range: 41..44, + id: "int", + ctx: Load, + }, + ), + op: BitOr, + right: StringLiteral( + ExprStringLiteral { + range: 47..60, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 47..60, + value: "ForwardRefY", + flags: StringLiteralFlags { + quote_style: Double, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 61..87, + name: Name( + ExprName { + range: 66..67, + id: "X", + ctx: Store, + }, + ), + type_params: Some( + TypeParams { + range: 67..70, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 68..69, + name: Identifier { + id: "T", + range: 68..69, + }, + bound: None, + default: None, + }, + ), + ], + }, + ), + value: BinOp( + ExprBinOp { + range: 73..87, + left: Name( + ExprName { + range: 73..74, + id: "T", + ctx: Load, + }, + ), + op: BitOr, + right: Subscript( + ExprSubscript { + range: 77..87, + value: Name( + ExprName { + range: 77..81, + id: "list", + ctx: Load, + }, + ), + slice: Subscript( + ExprSubscript { + range: 82..86, + value: Name( + ExprName { + range: 82..83, + id: "X", + ctx: Load, + }, + ), + slice: Name( + ExprName { + range: 84..85, + id: "T", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ctx: Load, + }, + ), + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 101..116, + name: Name( + ExprName { + range: 106..107, + id: "X", + ctx: Store, + }, + ), + type_params: Some( + TypeParams { + range: 107..110, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 108..109, + name: Identifier { + id: "T", + range: 108..109, + }, + bound: None, + default: None, + }, + ), + ], + }, + ), + value: Name( + ExprName { + range: 113..116, + id: "int", + ctx: Load, + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 117..145, + name: Name( + ExprName { + range: 122..123, + id: "X", + ctx: Store, + }, + ), + type_params: Some( + TypeParams { + range: 123..126, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 124..125, + name: Identifier { + id: "T", + range: 124..125, + }, + bound: None, + default: None, + }, + ), + ], + }, + ), + value: BinOp( + ExprBinOp { + range: 129..145, + left: Subscript( + ExprSubscript { + range: 129..136, + value: Name( + ExprName { + range: 129..133, + id: "list", + ctx: Load, + }, + ), + slice: Name( + ExprName { + range: 134..135, + id: "T", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + op: BitOr, + right: Subscript( + ExprSubscript { + range: 139..145, + value: Name( + ExprName { + range: 139..142, + id: "set", + ctx: Load, + }, + ), + slice: Name( + ExprName { + range: 143..144, + id: "T", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 146..178, + name: Name( + ExprName { + range: 151..152, + id: "X", + ctx: Store, + }, + ), + type_params: Some( + TypeParams { + range: 152..165, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 153..154, + name: Identifier { + id: "T", + range: 153..154, + }, + bound: None, + default: None, + }, + ), + TypeVarTuple( + TypeParamTypeVarTuple { + range: 156..159, + name: Identifier { + id: "Ts", + range: 157..159, + }, + default: None, + }, + ), + ParamSpec( + TypeParamParamSpec { + range: 161..164, + name: Identifier { + id: "P", + range: 163..164, + }, + default: None, + }, + ), + ], + }, + ), + value: Tuple( + ExprTuple { + range: 168..178, + elts: [ + Name( + ExprName { + range: 169..170, + id: "T", + ctx: Load, + }, + ), + Name( + ExprName { + range: 172..174, + id: "Ts", + ctx: Load, + }, + ), + Name( + ExprName { + range: 176..177, + id: "P", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 179..216, + name: Name( + ExprName { + range: 184..185, + id: "X", + ctx: Store, + }, + ), + type_params: Some( + TypeParams { + range: 185..203, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 186..192, + name: Identifier { + id: "T", + range: 186..187, + }, + bound: Some( + Name( + ExprName { + range: 189..192, + id: "int", + ctx: Load, + }, + ), + ), + default: None, + }, + ), + TypeVarTuple( + TypeParamTypeVarTuple { + range: 194..197, + name: Identifier { + id: "Ts", + range: 195..197, + }, + default: None, + }, + ), + ParamSpec( + TypeParamParamSpec { + range: 199..202, + name: Identifier { + id: "P", + range: 201..202, + }, + default: None, + }, + ), + ], + }, + ), + value: Tuple( + ExprTuple { + range: 206..216, + elts: [ + Name( + ExprName { + range: 207..208, + id: "T", + ctx: Load, + }, + ), + Name( + ExprName { + range: 210..212, + id: "Ts", + ctx: Load, + }, + ), + Name( + ExprName { + range: 214..215, + id: "P", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 217..261, + name: Name( + ExprName { + range: 222..223, + id: "X", + ctx: Store, + }, + ), + type_params: Some( + TypeParams { + range: 223..248, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 224..237, + name: Identifier { + id: "T", + range: 224..225, + }, + bound: Some( + Tuple( + ExprTuple { + range: 227..237, + elts: [ + Name( + ExprName { + range: 228..231, + id: "int", + ctx: Load, + }, + ), + Name( + ExprName { + range: 233..236, + id: "str", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + ), + default: None, + }, + ), + TypeVarTuple( + TypeParamTypeVarTuple { + range: 239..242, + name: Identifier { + id: "Ts", + range: 240..242, + }, + default: None, + }, + ), + ParamSpec( + TypeParamParamSpec { + range: 244..247, + name: Identifier { + id: "P", + range: 246..247, + }, + default: None, + }, + ), + ], + }, + ), + value: Tuple( + ExprTuple { + range: 251..261, + elts: [ + Name( + ExprName { + range: 252..253, + id: "T", + ctx: Load, + }, + ), + Name( + ExprName { + range: 255..257, + id: "Ts", + ctx: Load, + }, + ), + Name( + ExprName { + range: 259..260, + id: "P", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 262..287, + name: Name( + ExprName { + range: 267..268, + id: "X", + ctx: Store, + }, + ), + type_params: Some( + TypeParams { + range: 268..277, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 269..276, + name: Identifier { + id: "T", + range: 269..270, + }, + bound: None, + default: Some( + Name( + ExprName { + range: 273..276, + id: "int", + ctx: Load, + }, + ), + ), + }, + ), + ], + }, + ), + value: BinOp( + ExprBinOp { + range: 280..287, + left: Name( + ExprName { + range: 280..281, + id: "T", + ctx: Load, + }, + ), + op: BitOr, + right: Name( + ExprName { + range: 284..287, + id: "str", + ctx: Load, + }, + ), + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 288..330, + name: Name( + ExprName { + range: 293..294, + id: "X", + ctx: Store, + }, + ), + type_params: Some( + TypeParams { + range: 294..314, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 295..313, + name: Identifier { + id: "T", + range: 295..296, + }, + bound: Some( + BinOp( + ExprBinOp { + range: 298..307, + left: Name( + ExprName { + range: 298..301, + id: "int", + ctx: Load, + }, + ), + op: BitOr, + right: Name( + ExprName { + range: 304..307, + id: "str", + ctx: Load, + }, + ), + }, + ), + ), + default: Some( + Name( + ExprName { + range: 310..313, + id: "int", + ctx: Load, + }, + ), + ), + }, + ), + ], + }, + ), + value: BinOp( + ExprBinOp { + range: 317..330, + left: BinOp( + ExprBinOp { + range: 317..324, + left: Name( + ExprName { + range: 317..318, + id: "T", + ctx: Load, + }, + ), + op: BitOr, + right: Name( + ExprName { + range: 321..324, + id: "int", + ctx: Load, + }, + ), + }, + ), + op: BitOr, + right: Name( + ExprName { + range: 327..330, + id: "str", + ctx: Load, + }, + ), + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 331..384, + name: Name( + ExprName { + range: 336..337, + id: "X", + ctx: Store, + }, + ), + type_params: Some( + TypeParams { + range: 337..361, + type_params: [ + TypeVarTuple( + TypeParamTypeVarTuple { + range: 338..360, + name: Identifier { + id: "Ts", + range: 339..341, + }, + default: Some( + Starred( + ExprStarred { + range: 344..360, + value: Subscript( + ExprSubscript { + range: 345..360, + value: Name( + ExprName { + range: 345..350, + id: "tuple", + ctx: Load, + }, + ), + slice: Tuple( + ExprTuple { + range: 351..359, + elts: [ + Name( + ExprName { + range: 351..354, + id: "int", + ctx: Load, + }, + ), + Name( + ExprName { + range: 356..359, + id: "str", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ), + }, + ), + ], + }, + ), + value: Subscript( + ExprSubscript { + range: 364..384, + value: Name( + ExprName { + range: 364..369, + id: "tuple", + ctx: Load, + }, + ), + slice: Tuple( + ExprTuple { + range: 370..383, + elts: [ + Name( + ExprName { + range: 370..373, + id: "int", + ctx: Load, + }, + ), + Starred( + ExprStarred { + range: 375..378, + value: Name( + ExprName { + range: 376..378, + id: "Ts", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + Name( + ExprName { + range: 380..383, + id: "str", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + ctx: Load, + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 385..428, + name: Name( + ExprName { + range: 390..391, + id: "X", + ctx: Store, + }, + ), + type_params: Some( + TypeParams { + range: 391..409, + type_params: [ + ParamSpec( + TypeParamParamSpec { + range: 392..408, + name: Identifier { + id: "P", + range: 394..395, + }, + default: Some( + List( + ExprList { + range: 398..408, + elts: [ + Name( + ExprName { + range: 399..402, + id: "int", + ctx: Load, + }, + ), + Name( + ExprName { + range: 404..407, + id: "str", + ctx: Load, + }, + ), + ], + ctx: Load, + }, + ), + ), + }, + ), + ], + }, + ), + value: Subscript( + ExprSubscript { + range: 412..428, + value: Name( + ExprName { + range: 412..420, + id: "Callable", + ctx: Load, + }, + ), + slice: Tuple( + ExprTuple { + range: 421..427, + elts: [ + Name( + ExprName { + range: 421..422, + id: "P", + ctx: Load, + }, + ), + Name( + ExprName { + range: 424..427, + id: "str", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + ctx: Load, + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 459..474, + name: Name( + ExprName { + range: 464..468, + id: "type", + ctx: Store, + }, + ), + type_params: None, + value: Name( + ExprName { + range: 471..474, + id: "int", + ctx: Load, + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 475..491, + name: Name( + ExprName { + range: 480..485, + id: "match", + ctx: Store, + }, + ), + type_params: None, + value: Name( + ExprName { + range: 488..491, + id: "int", + ctx: Load, + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 492..507, + name: Name( + ExprName { + range: 497..501, + id: "case", + ctx: Store, + }, + ), + type_params: None, + value: Name( + ExprName { + range: 504..507, + id: "int", + ctx: Load, + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 533..548, + name: Name( + ExprName { + range: 538..541, + id: "foo", + ctx: Store, + }, + ), + type_params: None, + value: Name( + ExprName { + range: 544..548, + id: "type", + ctx: Load, + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 549..565, + name: Name( + ExprName { + range: 554..557, + id: "foo", + ctx: Store, + }, + ), + type_params: None, + value: Name( + ExprName { + range: 560..565, + id: "match", + ctx: Load, + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 566..581, + name: Name( + ExprName { + range: 571..574, + id: "foo", + ctx: Store, + }, + ), + type_params: None, + value: Name( + ExprName { + range: 577..581, + id: "case", + ctx: Load, + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 605..620, + name: Name( + ExprName { + range: 613..614, + id: "X", + ctx: Store, + }, + ), + type_params: None, + value: Name( + ExprName { + range: 617..620, + id: "int", + ctx: Load, + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 621..636, + name: Name( + ExprName { + range: 626..627, + id: "X", + ctx: Store, + }, + ), + type_params: None, + value: Name( + ExprName { + range: 633..636, + id: "int", + ctx: Load, + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 637..652, + name: Name( + ExprName { + range: 642..643, + id: "X", + ctx: Store, + }, + ), + type_params: None, + value: Name( + ExprName { + range: 649..652, + id: "int", + ctx: Load, + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 653..673, + name: Name( + ExprName { + range: 658..659, + id: "X", + ctx: Store, + }, + ), + type_params: None, + value: Name( + ExprName { + range: 668..671, + id: "int", + ctx: Load, + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 674..693, + name: Name( + ExprName { + range: 685..686, + id: "X", + ctx: Store, + }, + ), + type_params: Some( + TypeParams { + range: 686..689, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 687..688, + name: Identifier { + id: "T", + range: 687..688, + }, + bound: None, + default: None, + }, + ), + ], + }, + ), + value: Name( + ExprName { + range: 692..693, + id: "T", + ctx: Load, + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 694..714, + name: Name( + ExprName { + range: 699..700, + id: "X", + ctx: Store, + }, + ), + type_params: Some( + TypeParams { + range: 707..710, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 708..709, + name: Identifier { + id: "T", + range: 708..709, + }, + bound: None, + default: None, + }, + ), + ], + }, + ), + value: Name( + ExprName { + range: 713..714, + id: "T", + ctx: Load, + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 715..734, + name: Name( + ExprName { + range: 720..721, + id: "X", + ctx: Store, + }, + ), + type_params: Some( + TypeParams { + range: 721..724, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 722..723, + name: Identifier { + id: "T", + range: 722..723, + }, + bound: None, + default: None, + }, + ), + ], + }, + ), + value: Name( + ExprName { + range: 733..734, + id: "T", + ctx: Load, + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 756..768, + name: Name( + ExprName { + range: 761..762, + id: "X", + ctx: Store, + }, + ), + type_params: None, + value: Name( + ExprName { + range: 765..768, + id: "int", + ctx: Load, + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 770..782, + name: Name( + ExprName { + range: 775..776, + id: "X", + ctx: Store, + }, + ), + type_params: None, + value: Name( + ExprName { + range: 779..782, + id: "str", + ctx: Load, + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 784..797, + name: Name( + ExprName { + range: 789..790, + id: "X", + ctx: Store, + }, + ), + type_params: None, + value: Name( + ExprName { + range: 793..797, + id: "type", + ctx: Load, + }, + ), + }, + ), + ClassDef( + StmtClassDef { + range: 798..819, + decorator_list: [], + name: Identifier { + id: "X", + range: 804..805, + }, + type_params: None, + arguments: None, + body: [ + TypeAlias( + StmtTypeAlias { + range: 807..819, + name: Name( + ExprName { + range: 812..813, + id: "X", + ctx: Store, + }, + ), + type_params: None, + value: Name( + ExprName { + range: 816..819, + id: "int", + ctx: Load, + }, + ), + }, + ), + ], + }, + ), + TypeAlias( + StmtTypeAlias { + range: 821..853, + name: Name( + ExprName { + range: 826..831, + id: "Point", + ctx: Store, + }, + ), + type_params: None, + value: Subscript( + ExprSubscript { + range: 834..853, + value: Name( + ExprName { + range: 834..839, + id: "tuple", + ctx: Load, + }, + ), + slice: Tuple( + ExprTuple { + range: 840..852, + elts: [ + Name( + ExprName { + range: 840..845, + id: "float", + ctx: Load, + }, + ), + Name( + ExprName { + range: 847..852, + id: "float", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + ctx: Load, + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 854..881, + name: Name( + ExprName { + range: 859..864, + id: "Point", + ctx: Store, + }, + ), + type_params: Some( + TypeParams { + range: 864..867, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 865..866, + name: Identifier { + id: "T", + range: 865..866, + }, + bound: None, + default: None, + }, + ), + ], + }, + ), + value: Subscript( + ExprSubscript { + range: 870..881, + value: Name( + ExprName { + range: 870..875, + id: "tuple", + ctx: Load, + }, + ), + slice: Tuple( + ExprTuple { + range: 876..880, + elts: [ + Name( + ExprName { + range: 876..877, + id: "T", + ctx: Load, + }, + ), + Name( + ExprName { + range: 879..880, + id: "T", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + ctx: Load, + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 882..918, + name: Name( + ExprName { + range: 887..894, + id: "IntFunc", + ctx: Store, + }, + ), + type_params: Some( + TypeParams { + range: 894..899, + type_params: [ + ParamSpec( + TypeParamParamSpec { + range: 895..898, + name: Identifier { + id: "P", + range: 897..898, + }, + default: None, + }, + ), + ], + }, + ), + value: Subscript( + ExprSubscript { + range: 902..918, + value: Name( + ExprName { + range: 902..910, + id: "Callable", + ctx: Load, + }, + ), + slice: Tuple( + ExprTuple { + range: 911..917, + elts: [ + Name( + ExprName { + range: 911..912, + id: "P", + ctx: Load, + }, + ), + Name( + ExprName { + range: 914..917, + id: "int", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + ctx: Load, + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 932..972, + name: Name( + ExprName { + range: 937..949, + id: "LabeledTuple", + ctx: Store, + }, + ), + type_params: Some( + TypeParams { + range: 949..954, + type_params: [ + TypeVarTuple( + TypeParamTypeVarTuple { + range: 950..953, + name: Identifier { + id: "Ts", + range: 951..953, + }, + default: None, + }, + ), + ], + }, + ), + value: Subscript( + ExprSubscript { + range: 957..972, + value: Name( + ExprName { + range: 957..962, + id: "tuple", + ctx: Load, + }, + ), + slice: Tuple( + ExprTuple { + range: 963..971, + elts: [ + Name( + ExprName { + range: 963..966, + id: "str", + ctx: Load, + }, + ), + Starred( + ExprStarred { + range: 968..971, + value: Name( + ExprName { + range: 969..971, + id: "Ts", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + ctx: Load, + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 989..1037, + name: Name( + ExprName { + range: 994..1010, + id: "HashableSequence", + ctx: Store, + }, + ), + type_params: Some( + TypeParams { + range: 1010..1023, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 1011..1022, + name: Identifier { + id: "T", + range: 1011..1012, + }, + bound: Some( + Name( + ExprName { + range: 1014..1022, + id: "Hashable", + ctx: Load, + }, + ), + ), + default: None, + }, + ), + ], + }, + ), + value: Subscript( + ExprSubscript { + range: 1026..1037, + value: Name( + ExprName { + range: 1026..1034, + id: "Sequence", + ctx: Load, + }, + ), + slice: Name( + ExprName { + range: 1035..1036, + id: "T", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 1060..1110, + name: Name( + ExprName { + range: 1065..1081, + id: "IntOrStrSequence", + ctx: Store, + }, + ), + type_params: Some( + TypeParams { + range: 1081..1096, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 1082..1095, + name: Identifier { + id: "T", + range: 1082..1083, + }, + bound: Some( + Tuple( + ExprTuple { + range: 1085..1095, + elts: [ + Name( + ExprName { + range: 1086..1089, + id: "int", + ctx: Load, + }, + ), + Name( + ExprName { + range: 1091..1094, + id: "str", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + ), + default: None, + }, + ), + ], + }, + ), + value: Subscript( + ExprSubscript { + range: 1099..1110, + value: Name( + ExprName { + range: 1099..1107, + id: "Sequence", + ctx: Load, + }, + ), + slice: Name( + ExprName { + range: 1108..1109, + id: "T", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 1164..1178, + value: Tuple( + ExprTuple { + range: 1164..1178, + elts: [ + BinOp( + ExprBinOp { + range: 1164..1175, + left: BinOp( + ExprBinOp { + range: 1164..1171, + left: Name( + ExprName { + range: 1164..1168, + id: "type", + ctx: Load, + }, + ), + op: Mult, + right: Name( + ExprName { + range: 1170..1171, + id: "a", + ctx: Load, + }, + ), + }, + ), + op: Add, + right: Name( + ExprName { + range: 1174..1175, + id: "b", + ctx: Load, + }, + ), + }, + ), + Name( + ExprName { + range: 1177..1178, + id: "c", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 1203..1219, + value: Tuple( + ExprTuple { + range: 1203..1219, + elts: [ + BinOp( + ExprBinOp { + range: 1203..1216, + left: Name( + ExprName { + range: 1203..1207, + id: "type", + ctx: Load, + }, + ), + op: Mult, + right: BinOp( + ExprBinOp { + range: 1210..1215, + left: Name( + ExprName { + range: 1210..1211, + id: "a", + ctx: Load, + }, + ), + op: Add, + right: Name( + ExprName { + range: 1214..1215, + id: "b", + ctx: Load, + }, + ), + }, + ), + }, + ), + Name( + ExprName { + range: 1218..1219, + id: "c", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 1244..1260, + value: Call( + ExprCall { + range: 1244..1260, + func: Name( + ExprName { + range: 1244..1248, + id: "type", + ctx: Load, + }, + ), + arguments: Arguments { + range: 1249..1260, + args: [ + Starred( + ExprStarred { + range: 1250..1256, + value: BinOp( + ExprBinOp { + range: 1251..1256, + left: Name( + ExprName { + range: 1251..1252, + id: "a", + ctx: Load, + }, + ), + op: Add, + right: Name( + ExprName { + range: 1255..1256, + id: "b", + ctx: Load, + }, + ), + }, + ), + ctx: Load, + }, + ), + Name( + ExprName { + range: 1258..1259, + id: "c", + ctx: Load, + }, + ), + ], + keywords: [], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 1286..1301, + value: BinOp( + ExprBinOp { + range: 1286..1301, + left: BinOp( + ExprBinOp { + range: 1286..1297, + left: Name( + ExprName { + range: 1286..1290, + id: "type", + ctx: Load, + }, + ), + op: Sub, + right: BinOp( + ExprBinOp { + range: 1292..1297, + left: Name( + ExprName { + range: 1292..1293, + id: "a", + ctx: Load, + }, + ), + op: Mult, + right: Name( + ExprName { + range: 1296..1297, + id: "b", + ctx: Load, + }, + ), + }, + ), + }, + ), + op: Add, + right: Name( + ExprName { + range: 1300..1301, + id: "c", + ctx: Load, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 1327..1344, + value: BinOp( + ExprBinOp { + range: 1327..1344, + left: BinOp( + ExprBinOp { + range: 1327..1340, + left: Name( + ExprName { + range: 1327..1331, + id: "type", + ctx: Load, + }, + ), + op: Sub, + right: BinOp( + ExprBinOp { + range: 1334..1339, + left: Name( + ExprName { + range: 1334..1335, + id: "a", + ctx: Load, + }, + ), + op: Mult, + right: Name( + ExprName { + range: 1338..1339, + id: "b", + ctx: Load, + }, + ), + }, + ), + }, + ), + op: Add, + right: Name( + ExprName { + range: 1343..1344, + id: "c", + ctx: Load, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 1370..1387, + value: BinOp( + ExprBinOp { + range: 1370..1387, + left: BinOp( + ExprBinOp { + range: 1370..1383, + left: Call( + ExprCall { + range: 1370..1379, + func: Name( + ExprName { + range: 1370..1374, + id: "type", + ctx: Load, + }, + ), + arguments: Arguments { + range: 1375..1379, + args: [ + UnaryOp( + ExprUnaryOp { + range: 1376..1378, + op: USub, + operand: Name( + ExprName { + range: 1377..1378, + id: "a", + ctx: Load, + }, + ), + }, + ), + ], + keywords: [], + }, + }, + ), + op: Mult, + right: Name( + ExprName { + range: 1382..1383, + id: "b", + ctx: Load, + }, + ), + }, + ), + op: Add, + right: Name( + ExprName { + range: 1386..1387, + id: "c", + ctx: Load, + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 1414..1423, + value: Attribute( + ExprAttribute { + range: 1414..1423, + value: Call( + ExprCall { + range: 1414..1421, + func: Name( + ExprName { + range: 1414..1418, + id: "type", + ctx: Load, + }, + ), + arguments: Arguments { + range: 1419..1421, + args: [], + keywords: [], + }, + }, + ), + attr: Identifier { + id: "a", + range: 1422..1423, + }, + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 1439..1450, + value: Attribute( + ExprAttribute { + range: 1439..1450, + value: Call( + ExprCall { + range: 1439..1448, + func: Name( + ExprName { + range: 1439..1443, + id: "type", + ctx: Load, + }, + ), + arguments: Arguments { + range: 1444..1448, + args: [ + Tuple( + ExprTuple { + range: 1445..1447, + elts: [], + ctx: Load, + parenthesized: true, + }, + ), + ], + keywords: [], + }, + }, + ), + attr: Identifier { + id: "a", + range: 1449..1450, + }, + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 1468..1480, + value: Attribute( + ExprAttribute { + range: 1468..1480, + value: Call( + ExprCall { + range: 1468..1478, + func: Name( + ExprName { + range: 1468..1472, + id: "type", + ctx: Load, + }, + ), + arguments: Arguments { + range: 1473..1478, + args: [ + Tuple( + ExprTuple { + range: 1474..1476, + elts: [], + ctx: Load, + parenthesized: true, + }, + ), + ], + keywords: [], + }, + }, + ), + attr: Identifier { + id: "a", + range: 1479..1480, + }, + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 1498..1508, + value: Attribute( + ExprAttribute { + range: 1498..1508, + value: Subscript( + ExprSubscript { + range: 1498..1506, + value: Name( + ExprName { + range: 1498..1502, + id: "type", + ctx: Load, + }, + ), + slice: Name( + ExprName { + range: 1504..1505, + id: "a", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + attr: Identifier { + id: "b", + range: 1507..1508, + }, + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 1525..1536, + value: Attribute( + ExprAttribute { + range: 1525..1536, + value: Subscript( + ExprSubscript { + range: 1525..1534, + value: Name( + ExprName { + range: 1525..1529, + id: "type", + ctx: Load, + }, + ), + slice: Tuple( + ExprTuple { + range: 1531..1533, + elts: [ + Name( + ExprName { + range: 1531..1532, + id: "a", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: false, + }, + ), + ctx: Load, + }, + ), + attr: Identifier { + id: "b", + range: 1535..1536, + }, + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 1575..1588, + value: Attribute( + ExprAttribute { + range: 1575..1588, + value: Subscript( + ExprSubscript { + range: 1575..1586, + value: Name( + ExprName { + range: 1575..1579, + id: "type", + ctx: Load, + }, + ), + slice: Tuple( + ExprTuple { + range: 1581..1585, + elts: [ + Name( + ExprName { + range: 1582..1583, + id: "a", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + ctx: Load, + }, + ), + attr: Identifier { + id: "b", + range: 1587..1588, + }, + ctx: Load, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 1608..1624, + value: Subscript( + ExprSubscript { + range: 1608..1624, + value: Call( + ExprCall { + range: 1608..1614, + func: Name( + ExprName { + range: 1608..1612, + id: "type", + ctx: Load, + }, + ), + arguments: Arguments { + range: 1612..1614, + args: [], + keywords: [], + }, + }, + ), + slice: Slice( + ExprSlice { + range: 1615..1623, + lower: Some( + Name( + ExprName { + range: 1615..1616, + id: "a", + ctx: Load, + }, + ), + ), + upper: Some( + Name( + ExprName { + range: 1622..1623, + id: "b", + ctx: Load, + }, + ), + ), + step: None, + }, + ), + ctx: Load, + }, + ), + }, + ), + If( + StmtIf { + range: 1643..1661, + test: Named( + ExprNamed { + range: 1646..1655, + target: Name( + ExprName { + range: 1646..1650, + id: "type", + ctx: Store, + }, + ), + value: NumberLiteral( + ExprNumberLiteral { + range: 1654..1655, + value: Int( + 1, + ), + }, + ), + }, + ), + body: [ + Pass( + StmtPass { + range: 1657..1661, + }, + ), + ], + elif_else_clauses: [], + }, + ), + Assign( + StmtAssign { + range: 1662..1697, + targets: [ + Name( + ExprName { + range: 1662..1666, + id: "type", + ctx: Store, + }, + ), + ], + value: Lambda( + ExprLambda { + range: 1669..1697, + parameters: Some( + Parameters { + range: 1676..1681, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 1676..1681, + parameter: Parameter { + range: 1676..1681, + name: Identifier { + id: "query", + range: 1676..1681, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: Compare( + ExprCompare { + range: 1683..1697, + left: Name( + ExprName { + range: 1683..1688, + id: "query", + ctx: Load, + }, + ), + ops: [ + Eq, + ], + comparators: [ + Name( + ExprName { + range: 1692..1697, + id: "event", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 1698..1713, + value: Call( + ExprCall { + range: 1698..1713, + func: Name( + ExprName { + range: 1698..1703, + id: "print", + ctx: Load, + }, + ), + arguments: Arguments { + range: 1703..1713, + args: [ + Call( + ExprCall { + range: 1704..1712, + func: Name( + ExprName { + range: 1704..1708, + id: "type", + ctx: Load, + }, + ), + arguments: Arguments { + range: 1708..1712, + args: [ + NumberLiteral( + ExprNumberLiteral { + range: 1709..1711, + value: Int( + 12, + ), + }, + ), + ], + keywords: [], + }, + }, + ), + ], + keywords: [], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 1714..1724, + value: Call( + ExprCall { + range: 1714..1724, + func: Name( + ExprName { + range: 1714..1718, + id: "type", + ctx: Load, + }, + ), + arguments: Arguments { + range: 1718..1724, + args: [ + Name( + ExprName { + range: 1719..1723, + id: "type", + ctx: Load, + }, + ), + ], + keywords: [], + }, + }, + ), + }, + ), + Assign( + StmtAssign { + range: 1725..1743, + targets: [ + Name( + ExprName { + range: 1725..1726, + id: "a", + ctx: Store, + }, + ), + ], + value: Compare( + ExprCompare { + range: 1732..1741, + left: Name( + ExprName { + range: 1732..1736, + id: "type", + ctx: Load, + }, + ), + ops: [ + In, + ], + comparators: [ + Name( + ExprName { + range: 1740..1741, + id: "C", + ctx: Load, + }, + ), + ], + }, + ), + }, + ), + Assign( + StmtAssign { + range: 1744..1760, + targets: [ + Name( + ExprName { + range: 1744..1745, + id: "a", + ctx: Store, + }, + ), + ], + value: Call( + ExprCall { + range: 1751..1758, + func: Name( + ExprName { + range: 1751..1755, + id: "type", + ctx: Load, + }, + ), + arguments: Arguments { + range: 1755..1758, + args: [ + Name( + ExprName { + range: 1756..1757, + id: "b", + ctx: Load, + }, + ), + ], + keywords: [], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 1761..1778, + value: Call( + ExprCall { + range: 1761..1778, + func: Name( + ExprName { + range: 1761..1765, + id: "type", + ctx: Load, + }, + ), + arguments: Arguments { + range: 1766..1778, + args: [], + keywords: [ + Keyword { + range: 1769..1776, + arg: Some( + Identifier { + id: "X", + range: 1769..1770, + }, + ), + value: Name( + ExprName { + range: 1773..1776, + id: "int", + ctx: Load, + }, + ), + }, + ], + }, + }, + ), + }, + ), + Assign( + StmtAssign { + range: 1779..1787, + targets: [ + Name( + ExprName { + range: 1779..1783, + id: "type", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 1786..1787, + value: Int( + 1, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 1788..1800, + targets: [ + Name( + ExprName { + range: 1788..1792, + id: "type", + ctx: Store, + }, + ), + Name( + ExprName { + range: 1795..1796, + id: "x", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 1799..1800, + value: Int( + 1, + ), + }, + ), + }, + ), + Assign( + StmtAssign { + range: 1801..1813, + targets: [ + Name( + ExprName { + range: 1801..1802, + id: "x", + ctx: Store, + }, + ), + Name( + ExprName { + range: 1805..1809, + id: "type", + ctx: Store, + }, + ), + ], + value: NumberLiteral( + ExprNumberLiteral { + range: 1812..1813, + value: Int( + 1, + ), + }, + ), + }, + ), + Expr( + StmtExpr { + range: 1814..1828, + value: Lambda( + ExprLambda { + range: 1814..1828, + parameters: Some( + Parameters { + range: 1821..1822, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 1821..1822, + parameter: Parameter { + range: 1821..1822, + name: Identifier { + id: "x", + range: 1821..1822, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: Name( + ExprName { + range: 1824..1828, + id: "type", + ctx: Load, + }, + ), + }, + ), + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__while.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__while.py.snap.new new file mode 100644 index 0000000000..65db231ca5 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__while.py.snap.new @@ -0,0 +1,462 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/valid/statement/while.py +--- +## AST + +``` +Module( + ModModule { + range: 0..314, + body: [ + While( + StmtWhile { + range: 0..16, + test: Name( + ExprName { + range: 6..7, + id: "x", + ctx: Load, + }, + ), + body: [ + Expr( + StmtExpr { + range: 13..16, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 13..16, + }, + ), + }, + ), + ], + orelse: [], + }, + ), + While( + StmtWhile { + range: 18..61, + test: BoolOp( + ExprBoolOp { + range: 24..37, + op: And, + values: [ + Compare( + ExprCompare { + range: 25..30, + left: Name( + ExprName { + range: 25..26, + id: "x", + ctx: Load, + }, + ), + ops: [ + Gt, + ], + comparators: [ + NumberLiteral( + ExprNumberLiteral { + range: 29..30, + value: Int( + 1, + ), + }, + ), + ], + }, + ), + Name( + ExprName { + range: 36..37, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + body: [ + Pass( + StmtPass { + range: 43..47, + }, + ), + ], + orelse: [ + Expr( + StmtExpr { + range: 58..61, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 58..61, + }, + ), + }, + ), + ], + }, + ), + While( + StmtWhile { + range: 63..152, + test: BoolOp( + ExprBoolOp { + range: 69..76, + op: And, + values: [ + Name( + ExprName { + range: 69..70, + id: "x", + ctx: Load, + }, + ), + Name( + ExprName { + range: 75..76, + id: "y", + ctx: Load, + }, + ), + ], + }, + ), + body: [ + Expr( + StmtExpr { + range: 82..85, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 82..85, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 90..111, + value: Call( + ExprCall { + range: 90..111, + func: Name( + ExprName { + range: 90..95, + id: "print", + ctx: Load, + }, + ), + arguments: Arguments { + range: 95..111, + args: [ + StringLiteral( + ExprStringLiteral { + range: 96..110, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 96..110, + value: "Hello World!", + flags: StringLiteralFlags { + quote_style: Single, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + ], + keywords: [], + }, + }, + ), + }, + ), + ], + orelse: [ + Expr( + StmtExpr { + range: 123..144, + value: Call( + ExprCall { + range: 123..144, + func: Name( + ExprName { + range: 123..128, + id: "print", + ctx: Load, + }, + ), + arguments: Arguments { + range: 128..144, + args: [ + StringLiteral( + ExprStringLiteral { + range: 129..143, + value: StringLiteralValue { + inner: Single( + StringLiteral { + range: 129..143, + value: "Olá, Mundo!", + flags: StringLiteralFlags { + quote_style: Single, + prefix: Empty, + triple_quoted: false, + }, + }, + ), + }, + }, + ), + ], + keywords: [], + }, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 149..152, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 149..152, + }, + ), + }, + ), + ], + }, + ), + While( + StmtWhile { + range: 154..171, + test: Named( + ExprNamed { + range: 160..166, + target: Name( + ExprName { + range: 160..161, + id: "a", + ctx: Store, + }, + ), + value: Name( + ExprName { + range: 165..166, + id: "b", + ctx: Load, + }, + ), + }, + ), + body: [ + Expr( + StmtExpr { + range: 168..171, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 168..171, + }, + ), + }, + ), + ], + orelse: [], + }, + ), + While( + StmtWhile { + range: 172..197, + test: BoolOp( + ExprBoolOp { + range: 178..192, + op: And, + values: [ + Named( + ExprNamed { + range: 179..185, + target: Name( + ExprName { + range: 179..180, + id: "a", + ctx: Store, + }, + ), + value: Name( + ExprName { + range: 184..185, + id: "b", + ctx: Load, + }, + ), + }, + ), + Name( + ExprName { + range: 191..192, + id: "c", + ctx: Load, + }, + ), + ], + }, + ), + body: [ + Expr( + StmtExpr { + range: 194..197, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 194..197, + }, + ), + }, + ), + ], + orelse: [], + }, + ), + While( + StmtWhile { + range: 198..220, + test: Lambda( + ExprLambda { + range: 204..215, + parameters: Some( + Parameters { + range: 211..212, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 211..212, + parameter: Parameter { + range: 211..212, + name: Identifier { + id: "x", + range: 211..212, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: Name( + ExprName { + range: 214..215, + id: "x", + ctx: Load, + }, + ), + }, + ), + body: [ + Expr( + StmtExpr { + range: 217..220, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 217..220, + }, + ), + }, + ), + ], + orelse: [], + }, + ), + While( + StmtWhile { + range: 221..239, + test: Await( + ExprAwait { + range: 227..234, + value: Name( + ExprName { + range: 233..234, + id: "x", + ctx: Load, + }, + ), + }, + ), + body: [ + Expr( + StmtExpr { + range: 236..239, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 236..239, + }, + ), + }, + ), + ], + orelse: [], + }, + ), + If( + StmtIf { + range: 241..313, + test: BooleanLiteral( + ExprBooleanLiteral { + range: 244..248, + value: true, + }, + ), + body: [ + While( + StmtWhile { + range: 254..298, + test: Name( + ExprName { + range: 260..261, + id: "x", + ctx: Load, + }, + ), + body: [ + Pass( + StmtPass { + range: 271..275, + }, + ), + ], + orelse: [ + Pass( + StmtPass { + range: 294..298, + }, + ), + ], + }, + ), + ], + elif_else_clauses: [ + ElifElseClause { + range: 299..313, + test: None, + body: [ + Pass( + StmtPass { + range: 309..313, + }, + ), + ], + }, + ], + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__with.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__with.py.snap.new new file mode 100644 index 0000000000..161d2e1591 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@statement__with.py.snap.new @@ -0,0 +1,400 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/valid/statement/with.py +--- +## AST + +``` +Module( + ModModule { + range: 0..361, + body: [ + With( + StmtWith { + range: 137..151, + is_async: false, + items: [ + WithItem { + range: 142..146, + context_expr: Name( + ExprName { + range: 142..146, + id: "item", + ctx: Load, + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 148..151, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 148..151, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 152..171, + is_async: false, + items: [ + WithItem { + range: 157..166, + context_expr: Name( + ExprName { + range: 157..161, + id: "item", + ctx: Load, + }, + ), + optional_vars: Some( + Name( + ExprName { + range: 165..166, + id: "f", + ctx: Store, + }, + ), + ), + }, + ], + body: [ + Expr( + StmtExpr { + range: 168..171, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 168..171, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 172..194, + is_async: false, + items: [ + WithItem { + range: 177..182, + context_expr: Name( + ExprName { + range: 177..182, + id: "item1", + ctx: Load, + }, + ), + optional_vars: None, + }, + WithItem { + range: 184..189, + context_expr: Name( + ExprName { + range: 184..189, + id: "item2", + ctx: Load, + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 191..194, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 191..194, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 195..229, + is_async: false, + items: [ + WithItem { + range: 200..211, + context_expr: Name( + ExprName { + range: 200..205, + id: "item1", + ctx: Load, + }, + ), + optional_vars: Some( + Name( + ExprName { + range: 209..211, + id: "f1", + ctx: Store, + }, + ), + ), + }, + WithItem { + range: 213..224, + context_expr: Name( + ExprName { + range: 213..218, + id: "item2", + ctx: Load, + }, + ), + optional_vars: Some( + Name( + ExprName { + range: 222..224, + id: "f2", + ctx: Store, + }, + ), + ), + }, + ], + body: [ + Expr( + StmtExpr { + range: 226..229, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 226..229, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 231..257, + is_async: false, + items: [ + WithItem { + range: 236..252, + context_expr: If( + ExprIf { + range: 236..252, + test: BooleanLiteral( + ExprBooleanLiteral { + range: 241..245, + value: true, + }, + ), + body: Name( + ExprName { + range: 236..237, + id: "x", + ctx: Load, + }, + ), + orelse: Name( + ExprName { + range: 251..252, + id: "y", + ctx: Load, + }, + ), + }, + ), + optional_vars: None, + }, + ], + body: [ + Expr( + StmtExpr { + range: 254..257, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 254..257, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 258..289, + is_async: false, + items: [ + WithItem { + range: 263..284, + context_expr: If( + ExprIf { + range: 263..279, + test: BooleanLiteral( + ExprBooleanLiteral { + range: 268..272, + value: true, + }, + ), + body: Name( + ExprName { + range: 263..264, + id: "x", + ctx: Load, + }, + ), + orelse: Name( + ExprName { + range: 278..279, + id: "y", + ctx: Load, + }, + ), + }, + ), + optional_vars: Some( + Name( + ExprName { + range: 283..284, + id: "f", + ctx: Store, + }, + ), + ), + }, + ], + body: [ + Expr( + StmtExpr { + range: 286..289, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 286..289, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 313..334, + is_async: false, + items: [ + WithItem { + range: 318..329, + context_expr: Call( + ExprCall { + range: 318..324, + func: Name( + ExprName { + range: 318..322, + id: "open", + ctx: Load, + }, + ), + arguments: Arguments { + range: 322..324, + args: [], + keywords: [], + }, + }, + ), + optional_vars: Some( + Name( + ExprName { + range: 328..329, + id: "f", + ctx: Store, + }, + ), + ), + }, + ], + body: [ + Expr( + StmtExpr { + range: 331..334, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 331..334, + }, + ), + }, + ), + ], + }, + ), + With( + StmtWith { + range: 335..361, + is_async: false, + items: [ + WithItem { + range: 340..356, + context_expr: Call( + ExprCall { + range: 340..346, + func: Name( + ExprName { + range: 340..344, + id: "open", + ctx: Load, + }, + ), + arguments: Arguments { + range: 344..346, + args: [], + keywords: [], + }, + }, + ), + optional_vars: Some( + Attribute( + ExprAttribute { + range: 350..356, + value: Name( + ExprName { + range: 350..351, + id: "f", + ctx: Load, + }, + ), + attr: Identifier { + id: "attr", + range: 352..356, + }, + ctx: Store, + }, + ), + ), + }, + ], + body: [ + Expr( + StmtExpr { + range: 358..361, + value: EllipsisLiteral( + ExprEllipsisLiteral { + range: 358..361, + }, + ), + }, + ), + ], + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@type_param_param_spec.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@type_param_param_spec.py.snap.new new file mode 100644 index 0000000000..7d36dd55b5 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@type_param_param_spec.py.snap.new @@ -0,0 +1,198 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/inline/ok/type_param_param_spec.py +--- +## AST + +``` +Module( + ModModule { + range: 0..90, + body: [ + TypeAlias( + StmtTypeAlias { + range: 0..17, + name: Name( + ExprName { + range: 5..6, + id: "X", + ctx: Store, + }, + ), + type_params: Some( + TypeParams { + range: 6..11, + type_params: [ + ParamSpec( + TypeParamParamSpec { + range: 7..10, + name: Identifier { + id: "P", + range: 9..10, + }, + default: None, + }, + ), + ], + }, + ), + value: Name( + ExprName { + range: 14..17, + id: "int", + ctx: Load, + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 18..41, + name: Name( + ExprName { + range: 23..24, + id: "X", + ctx: Store, + }, + ), + type_params: Some( + TypeParams { + range: 24..35, + type_params: [ + ParamSpec( + TypeParamParamSpec { + range: 25..34, + name: Identifier { + id: "P", + range: 27..28, + }, + default: Some( + Name( + ExprName { + range: 31..34, + id: "int", + ctx: Load, + }, + ), + ), + }, + ), + ], + }, + ), + value: Name( + ExprName { + range: 38..41, + id: "int", + ctx: Load, + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 42..62, + name: Name( + ExprName { + range: 47..48, + id: "X", + ctx: Store, + }, + ), + type_params: Some( + TypeParams { + range: 48..56, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 49..50, + name: Identifier { + id: "T", + range: 49..50, + }, + bound: None, + default: None, + }, + ), + ParamSpec( + TypeParamParamSpec { + range: 52..55, + name: Identifier { + id: "P", + range: 54..55, + }, + default: None, + }, + ), + ], + }, + ), + value: Name( + ExprName { + range: 59..62, + id: "int", + ctx: Load, + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 63..89, + name: Name( + ExprName { + range: 68..69, + id: "X", + ctx: Store, + }, + ), + type_params: Some( + TypeParams { + range: 69..83, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 70..71, + name: Identifier { + id: "T", + range: 70..71, + }, + bound: None, + default: None, + }, + ), + ParamSpec( + TypeParamParamSpec { + range: 73..82, + name: Identifier { + id: "P", + range: 75..76, + }, + default: Some( + Name( + ExprName { + range: 79..82, + id: "int", + ctx: Load, + }, + ), + ), + }, + ), + ], + }, + ), + value: Name( + ExprName { + range: 86..89, + id: "int", + ctx: Load, + }, + ), + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@type_param_type_var.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@type_param_type_var.py.snap.new new file mode 100644 index 0000000000..cf9dacb179 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@type_param_type_var.py.snap.new @@ -0,0 +1,316 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/inline/ok/type_param_type_var.py +--- +## AST + +``` +Module( + ModModule { + range: 0..147, + body: [ + TypeAlias( + StmtTypeAlias { + range: 0..15, + name: Name( + ExprName { + range: 5..6, + id: "X", + ctx: Store, + }, + ), + type_params: Some( + TypeParams { + range: 6..9, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 7..8, + name: Identifier { + id: "T", + range: 7..8, + }, + bound: None, + default: None, + }, + ), + ], + }, + ), + value: Name( + ExprName { + range: 12..15, + id: "int", + ctx: Load, + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 16..37, + name: Name( + ExprName { + range: 21..22, + id: "X", + ctx: Store, + }, + ), + type_params: Some( + TypeParams { + range: 22..31, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 23..30, + name: Identifier { + id: "T", + range: 23..24, + }, + bound: None, + default: Some( + Name( + ExprName { + range: 27..30, + id: "int", + ctx: Load, + }, + ), + ), + }, + ), + ], + }, + ), + value: Name( + ExprName { + range: 34..37, + id: "int", + ctx: Load, + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 38..64, + name: Name( + ExprName { + range: 43..44, + id: "X", + ctx: Store, + }, + ), + type_params: Some( + TypeParams { + range: 44..58, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 45..57, + name: Identifier { + id: "T", + range: 45..46, + }, + bound: Some( + Name( + ExprName { + range: 48..51, + id: "int", + ctx: Load, + }, + ), + ), + default: Some( + Name( + ExprName { + range: 54..57, + id: "int", + ctx: Load, + }, + ), + ), + }, + ), + ], + }, + ), + value: Name( + ExprName { + range: 61..64, + id: "int", + ctx: Load, + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 65..98, + name: Name( + ExprName { + range: 70..71, + id: "X", + ctx: Store, + }, + ), + type_params: Some( + TypeParams { + range: 71..92, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 72..91, + name: Identifier { + id: "T", + range: 72..73, + }, + bound: Some( + Tuple( + ExprTuple { + range: 75..85, + elts: [ + Name( + ExprName { + range: 76..79, + id: "int", + ctx: Load, + }, + ), + Name( + ExprName { + range: 81..84, + id: "int", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + ), + default: Some( + Name( + ExprName { + range: 88..91, + id: "int", + ctx: Load, + }, + ), + ), + }, + ), + ], + }, + ), + value: Name( + ExprName { + range: 95..98, + id: "int", + ctx: Load, + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 99..146, + name: Name( + ExprName { + range: 104..105, + id: "X", + ctx: Store, + }, + ), + type_params: Some( + TypeParams { + range: 105..140, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 106..118, + name: Identifier { + id: "T", + range: 106..107, + }, + bound: Some( + Name( + ExprName { + range: 109..112, + id: "int", + ctx: Load, + }, + ), + ), + default: Some( + Name( + ExprName { + range: 115..118, + id: "int", + ctx: Load, + }, + ), + ), + }, + ), + TypeVar( + TypeParamTypeVar { + range: 120..139, + name: Identifier { + id: "U", + range: 120..121, + }, + bound: Some( + Tuple( + ExprTuple { + range: 123..133, + elts: [ + Name( + ExprName { + range: 124..127, + id: "int", + ctx: Load, + }, + ), + Name( + ExprName { + range: 129..132, + id: "int", + ctx: Load, + }, + ), + ], + ctx: Load, + parenthesized: true, + }, + ), + ), + default: Some( + Name( + ExprName { + range: 136..139, + id: "int", + ctx: Load, + }, + ), + ), + }, + ), + ], + }, + ), + value: Name( + ExprName { + range: 143..146, + id: "int", + ctx: Load, + }, + ), + }, + ), + ], + }, +) +``` diff --git a/crates/ruff_python_parser/tests/snapshots/valid_syntax@type_param_type_var_tuple.py.snap.new b/crates/ruff_python_parser/tests/snapshots/valid_syntax@type_param_type_var_tuple.py.snap.new new file mode 100644 index 0000000000..59375c4149 --- /dev/null +++ b/crates/ruff_python_parser/tests/snapshots/valid_syntax@type_param_type_var_tuple.py.snap.new @@ -0,0 +1,248 @@ +--- +source: crates/ruff_python_parser/tests/fixtures.rs +assertion_line: 76 +input_file: crates/ruff_python_parser/resources/inline/ok/type_param_type_var_tuple.py +--- +## AST + +``` +Module( + ModModule { + range: 0..115, + body: [ + TypeAlias( + StmtTypeAlias { + range: 0..17, + name: Name( + ExprName { + range: 5..6, + id: "X", + ctx: Store, + }, + ), + type_params: Some( + TypeParams { + range: 6..11, + type_params: [ + TypeVarTuple( + TypeParamTypeVarTuple { + range: 7..10, + name: Identifier { + id: "Ts", + range: 8..10, + }, + default: None, + }, + ), + ], + }, + ), + value: Name( + ExprName { + range: 14..17, + id: "int", + ctx: Load, + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 18..41, + name: Name( + ExprName { + range: 23..24, + id: "X", + ctx: Store, + }, + ), + type_params: Some( + TypeParams { + range: 24..35, + type_params: [ + TypeVarTuple( + TypeParamTypeVarTuple { + range: 25..34, + name: Identifier { + id: "Ts", + range: 26..28, + }, + default: Some( + Name( + ExprName { + range: 31..34, + id: "int", + ctx: Load, + }, + ), + ), + }, + ), + ], + }, + ), + value: Name( + ExprName { + range: 38..41, + id: "int", + ctx: Load, + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 42..66, + name: Name( + ExprName { + range: 47..48, + id: "X", + ctx: Store, + }, + ), + type_params: Some( + TypeParams { + range: 48..60, + type_params: [ + TypeVarTuple( + TypeParamTypeVarTuple { + range: 49..59, + name: Identifier { + id: "Ts", + range: 50..52, + }, + default: Some( + Starred( + ExprStarred { + range: 55..59, + value: Name( + ExprName { + range: 56..59, + id: "int", + ctx: Load, + }, + ), + ctx: Load, + }, + ), + ), + }, + ), + ], + }, + ), + value: Name( + ExprName { + range: 63..66, + id: "int", + ctx: Load, + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 67..87, + name: Name( + ExprName { + range: 72..73, + id: "X", + ctx: Store, + }, + ), + type_params: Some( + TypeParams { + range: 73..81, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 74..75, + name: Identifier { + id: "T", + range: 74..75, + }, + bound: None, + default: None, + }, + ), + TypeVarTuple( + TypeParamTypeVarTuple { + range: 77..80, + name: Identifier { + id: "Ts", + range: 78..80, + }, + default: None, + }, + ), + ], + }, + ), + value: Name( + ExprName { + range: 84..87, + id: "int", + ctx: Load, + }, + ), + }, + ), + TypeAlias( + StmtTypeAlias { + range: 88..114, + name: Name( + ExprName { + range: 93..94, + id: "X", + ctx: Store, + }, + ), + type_params: Some( + TypeParams { + range: 94..108, + type_params: [ + TypeVar( + TypeParamTypeVar { + range: 95..96, + name: Identifier { + id: "T", + range: 95..96, + }, + bound: None, + default: None, + }, + ), + TypeVarTuple( + TypeParamTypeVarTuple { + range: 98..107, + name: Identifier { + id: "Ts", + range: 99..101, + }, + default: Some( + Name( + ExprName { + range: 104..107, + id: "int", + ctx: Load, + }, + ), + ), + }, + ), + ], + }, + ), + value: Name( + ExprName { + range: 111..114, + id: "int", + ctx: Load, + }, + ), + }, + ), + ], + }, +) +```