Skip to content

Commit ce9ab64

Browse files
committed
WIP
1 parent e47316e commit ce9ab64

File tree

5 files changed

+78
-15
lines changed

5 files changed

+78
-15
lines changed

crates/ruff/tests/lint.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2568,6 +2568,46 @@ fn a005_module_shadowing_strict_default() -> Result<()> {
25682568
Ok(())
25692569
}
25702570

2571+
#[test]
2572+
fn walrus_before_py38() {
2573+
// ok
2574+
assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME))
2575+
.args(STDIN_BASE_OPTIONS)
2576+
.args(["--stdin-filename", "test.py"])
2577+
.arg("--target-version=py38")
2578+
.arg("-")
2579+
.pass_stdin(r#"if x := 1: pass"#),
2580+
@r"
2581+
success: false
2582+
exit_code: 1
2583+
----- stdout -----
2584+
test.py:1:10: E701 Multiple statements on one line (colon)
2585+
Found 1 error.
2586+
2587+
----- stderr -----
2588+
"
2589+
);
2590+
2591+
// not ok on 3.7
2592+
assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME))
2593+
.args(STDIN_BASE_OPTIONS)
2594+
.args(["--stdin-filename", "test.py"])
2595+
.arg("--target-version=py37")
2596+
.arg("-")
2597+
.pass_stdin(r#"if x := 1: pass"#),
2598+
@r"
2599+
success: false
2600+
exit_code: 1
2601+
----- stdout -----
2602+
test.py:1:4: SyntaxError: Cannot use named assignment expression (`:=`) on Python 3.7 (syntax was new in Python 3.8)
2603+
test.py:1:10: E701 Multiple statements on one line (colon)
2604+
Found 2 errors.
2605+
2606+
----- stderr -----
2607+
"
2608+
);
2609+
}
2610+
25712611
#[test]
25722612
fn match_before_py310() {
25732613
assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME))

crates/ruff_linter/src/message/mod.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub use pylint::PylintEmitter;
1616
pub use rdjson::RdjsonEmitter;
1717
use ruff_diagnostics::{Diagnostic, DiagnosticKind, Fix};
1818
use ruff_notebook::NotebookIndex;
19-
use ruff_python_parser::{ParseError, SyntaxError, SyntaxErrorKind};
19+
use ruff_python_parser::{ParseError, SyntaxError};
2020
use ruff_source_file::{SourceFile, SourceLocation};
2121
use ruff_text_size::{Ranged, TextLen, TextRange, TextSize};
2222
pub use sarif::SarifEmitter;
@@ -123,13 +123,11 @@ impl Message {
123123

124124
/// Create a [`Message`] from the given [`SyntaxError`].
125125
pub fn from_syntax_error(syntax_error: &SyntaxError, file: SourceFile) -> Message {
126-
match syntax_error.kind {
127-
SyntaxErrorKind::MatchBeforePy310 => Message::SyntaxError(SyntaxErrorMessage {
128-
message: format!("SyntaxError: {}", syntax_error.message()),
129-
range: syntax_error.range,
130-
file,
131-
}),
132-
}
126+
Message::SyntaxError(SyntaxErrorMessage {
127+
message: format!("SyntaxError: {}", syntax_error.message()),
128+
range: syntax_error.range,
129+
file,
130+
})
133131
}
134132

135133
pub const fn as_diagnostic_message(&self) -> Option<&DiagnosticMessage> {

crates/ruff_python_parser/src/error.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -437,30 +437,36 @@ pub struct SyntaxError {
437437
impl SyntaxError {
438438
pub fn message(&self) -> String {
439439
match self.kind {
440+
SyntaxErrorKind::WalrusBeforePy38 => format!(
441+
"Cannot use named assignment expression (`:=`) on Python {} (syntax was new in Python 3.8)",
442+
self.target_version
443+
),
440444
SyntaxErrorKind::MatchBeforePy310 => format!(
441-
"Cannot use `match` statement on Python {major}.{minor} (syntax was new in Python 3.10)",
442-
major = self.target_version.major,
443-
minor = self.target_version.minor,
445+
"Cannot use `match` statement on Python {} (syntax was new in Python 3.10)",
446+
self.target_version,
444447
),
445448
}
446449
}
447450

448451
/// The earliest allowed version for the syntax associated with this error.
449452
pub const fn version(&self) -> PythonVersion {
450453
match self.kind {
454+
SyntaxErrorKind::WalrusBeforePy38 => PythonVersion::PY38,
451455
SyntaxErrorKind::MatchBeforePy310 => PythonVersion::PY310,
452456
}
453457
}
454458
}
455459

456460
#[derive(Debug, PartialEq, Clone, Copy)]
457461
pub enum SyntaxErrorKind {
462+
WalrusBeforePy38,
458463
MatchBeforePy310,
459464
}
460465

461466
impl SyntaxErrorKind {
462467
pub const fn as_str(self) -> &'static str {
463468
match self {
469+
SyntaxErrorKind::WalrusBeforePy38 => "walrus-before-python-38",
464470
SyntaxErrorKind::MatchBeforePy310 => "match-before-python-310",
465471
}
466472
}

crates/ruff_python_parser/src/parser/expression.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_hash::{FxBuildHasher, FxHashSet};
77
use ruff_python_ast::name::Name;
88
use ruff_python_ast::{
99
self as ast, BoolOp, CmpOp, ConversionFlag, Expr, ExprContext, FStringElement, FStringElements,
10-
IpyEscapeKind, Number, Operator, StringFlags, UnaryOp,
10+
IpyEscapeKind, Number, Operator, PythonVersion, StringFlags, UnaryOp,
1111
};
1212
use ruff_text_size::{Ranged, TextLen, TextRange, TextSize};
1313

@@ -16,7 +16,7 @@ use crate::parser::{helpers, FunctionKind, Parser};
1616
use crate::string::{parse_fstring_literal_element, parse_string_literal, StringType};
1717
use crate::token::{TokenKind, TokenValue};
1818
use crate::token_set::TokenSet;
19-
use crate::{FStringErrorType, Mode, ParseErrorType};
19+
use crate::{FStringErrorType, Mode, ParseErrorType, SyntaxError, SyntaxErrorKind};
2020

2121
use super::{FStringElementsKind, Parenthesized, RecoveryContextKind};
2222

@@ -2161,10 +2161,20 @@ impl<'src> Parser<'src> {
21612161

21622162
let value = self.parse_conditional_expression_or_higher();
21632163

2164+
let range = self.node_range(start);
2165+
2166+
if self.options.target_version < PythonVersion::PY38 {
2167+
self.syntax_errors.push(SyntaxError {
2168+
kind: SyntaxErrorKind::WalrusBeforePy38,
2169+
range,
2170+
target_version: self.options.target_version,
2171+
});
2172+
}
2173+
21642174
ast::ExprNamed {
21652175
target: Box::new(target),
21662176
value: Box::new(value.expr),
2167-
range: self.node_range(start),
2177+
range,
21682178
}
21692179
}
21702180

crates/ruff_python_parser/src/parser/statement.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1458,13 +1458,22 @@ impl<'src> Parser<'src> {
14581458
);
14591459
}
14601460

1461+
let range = self.node_range(try_start);
1462+
if is_star && self.options.target_version < PythonVersion::PY311 {
1463+
self.syntax_errors.push(SyntaxError {
1464+
kind: SyntaxErrorKind::ExceptStarBeforePy311,
1465+
range,
1466+
target_version: self.options.target_version,
1467+
});
1468+
}
1469+
14611470
ast::StmtTry {
14621471
body: try_body,
14631472
handlers,
14641473
orelse,
14651474
finalbody,
14661475
is_star,
1467-
range: self.node_range(try_start),
1476+
range,
14681477
}
14691478
}
14701479

0 commit comments

Comments
 (0)