[syntax-errors] except* before Python 3.11 (#16446)

Summary
--

One of the simpler ones, just detect the use of `except*` before 3.11.

Test Plan
--

New inline tests.
This commit is contained in:
Brent Westbrook
2025-03-02 13:20:18 -05:00
committed by GitHub
parent 0d615b8765
commit e924ecbdac
9 changed files with 180 additions and 23 deletions

View File

@@ -438,9 +438,9 @@ pub struct UnsupportedSyntaxError {
/// The target [`PythonVersion`] for which this error was detected.
///
/// This is different from the version reported by the
/// [`minimum_version`](UnsupportedSyntaxError::minimum_version) method, which is the earliest
/// allowed version for this piece of syntax. The `target_version` is primarily used for
/// user-facing error messages.
/// [`minimum_version`](UnsupportedSyntaxErrorKind::minimum_version) method, which is the
/// earliest allowed version for this piece of syntax. The `target_version` is primarily used
/// for user-facing error messages.
pub target_version: PythonVersion,
}
@@ -448,6 +448,7 @@ pub struct UnsupportedSyntaxError {
pub enum UnsupportedSyntaxErrorKind {
Match,
Walrus,
ExceptStar,
}
impl Display for UnsupportedSyntaxError {
@@ -455,22 +456,24 @@ impl Display for UnsupportedSyntaxError {
let kind = match self.kind {
UnsupportedSyntaxErrorKind::Match => "`match` statement",
UnsupportedSyntaxErrorKind::Walrus => "named assignment expression (`:=`)",
UnsupportedSyntaxErrorKind::ExceptStar => "`except*`",
};
write!(
f,
"Cannot use {kind} on Python {} (syntax was added in Python {})",
self.target_version,
self.minimum_version(),
self.kind.minimum_version(),
)
}
}
impl UnsupportedSyntaxError {
impl UnsupportedSyntaxErrorKind {
/// The earliest allowed version for the syntax associated with this error.
pub const fn minimum_version(&self) -> PythonVersion {
match self.kind {
match self {
UnsupportedSyntaxErrorKind::Match => PythonVersion::PY310,
UnsupportedSyntaxErrorKind::Walrus => PythonVersion::PY38,
UnsupportedSyntaxErrorKind::ExceptStar => PythonVersion::PY311,
}
}
}