mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-28 15:34:19 +00:00
[syntax-errors] Assignment expressions before Python 3.8 (#16383)
## Summary This PR is the first in a series derived from https://github.com/astral-sh/ruff/pull/16308, each of which add support for detecting one version-related syntax error from https://github.com/astral-sh/ruff/issues/6591. This one should be the largest because it also includes the addition of the `Parser::add_unsupported_syntax_error` method Otherwise I think the general structure will be the same for each syntax error: * Detecting the error in the parser * Inline parser tests for the new error * New ruff CLI tests for the new error ## Test Plan As noted above, there are new inline parser tests, as well as new ruff CLI tests. Once https://github.com/astral-sh/ruff/pull/16379 is resolved, there should also be new mdtests for red-knot, but this PR does not currently include those.
This commit is contained in:
parent
ba44e9de13
commit
4431978262
9 changed files with 182 additions and 28 deletions
|
@ -444,31 +444,35 @@ pub struct UnsupportedSyntaxError {
|
|||
pub target_version: PythonVersion,
|
||||
}
|
||||
|
||||
impl UnsupportedSyntaxError {
|
||||
/// The earliest allowed version for the syntax associated with this error.
|
||||
pub const fn minimum_version(&self) -> PythonVersion {
|
||||
match self.kind {
|
||||
UnsupportedSyntaxErrorKind::MatchBeforePy310 => PythonVersion::PY310,
|
||||
}
|
||||
}
|
||||
#[derive(Debug, PartialEq, Clone, Copy)]
|
||||
pub enum UnsupportedSyntaxErrorKind {
|
||||
Match,
|
||||
Walrus,
|
||||
}
|
||||
|
||||
impl Display for UnsupportedSyntaxError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self.kind {
|
||||
UnsupportedSyntaxErrorKind::MatchBeforePy310 => write!(
|
||||
f,
|
||||
"Cannot use `match` statement on Python {} (syntax was added in Python {})",
|
||||
self.target_version,
|
||||
self.minimum_version(),
|
||||
),
|
||||
}
|
||||
let kind = match self.kind {
|
||||
UnsupportedSyntaxErrorKind::Match => "`match` statement",
|
||||
UnsupportedSyntaxErrorKind::Walrus => "named assignment expression (`:=`)",
|
||||
};
|
||||
write!(
|
||||
f,
|
||||
"Cannot use {kind} on Python {} (syntax was added in Python {})",
|
||||
self.target_version,
|
||||
self.minimum_version(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone, Copy)]
|
||||
pub enum UnsupportedSyntaxErrorKind {
|
||||
MatchBeforePy310,
|
||||
impl UnsupportedSyntaxError {
|
||||
/// The earliest allowed version for the syntax associated with this error.
|
||||
pub const fn minimum_version(&self) -> PythonVersion {
|
||||
match self.kind {
|
||||
UnsupportedSyntaxErrorKind::Match => PythonVersion::PY310,
|
||||
UnsupportedSyntaxErrorKind::Walrus => PythonVersion::PY38,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(target_pointer_width = "64")]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue