From 5f5a1cebfd51adc9e9f70b929a727f02bf0cdd61 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Thu, 30 Nov 2023 14:03:00 -0500 Subject: [PATCH] Expand tests --- crates/ruff_python_parser/src/parser.rs | 7 ++++ ...er__parser__tests__type_as_identifier.snap | 40 +++++++++++++++++++ .../ruff_python_parser/src/soft_keywords.rs | 7 ++-- 3 files changed, 50 insertions(+), 4 deletions(-) diff --git a/crates/ruff_python_parser/src/parser.rs b/crates/ruff_python_parser/src/parser.rs index 557270babb1a9..963d3a33064eb 100644 --- a/crates/ruff_python_parser/src/parser.rs +++ b/crates/ruff_python_parser/src/parser.rs @@ -863,10 +863,17 @@ type ( type = 1 type = x = 1 x = type = 1 +lambda x: type "; insta::assert_debug_snapshot!(parse_suite(source, "").unwrap()); } + #[test] + fn test_invalid_type() { + assert!(parse_suite("a: type X = int", "").is_err()); + assert!(parse_suite("lambda: type X = int", "").is_err()); + } + #[test] fn numeric_literals() { let source = r"x = 123456789 diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__type_as_identifier.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__type_as_identifier.snap index ad394b4042ac6..0296e7ad0511e 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__type_as_identifier.snap +++ b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__type_as_identifier.snap @@ -988,4 +988,44 @@ expression: "parse_suite(source, \"\").unwrap()" ), }, ), + Expr( + StmtExpr { + range: 652..666, + value: Lambda( + ExprLambda { + range: 652..666, + parameters: Some( + Parameters { + range: 659..660, + posonlyargs: [], + args: [ + ParameterWithDefault { + range: 659..660, + parameter: Parameter { + range: 659..660, + name: Identifier { + id: "x", + range: 659..660, + }, + annotation: None, + }, + default: None, + }, + ], + vararg: None, + kwonlyargs: [], + kwarg: None, + }, + ), + body: Name( + ExprName { + range: 662..666, + id: "type", + ctx: Load, + }, + ), + }, + ), + }, + ), ] diff --git a/crates/ruff_python_parser/src/soft_keywords.rs b/crates/ruff_python_parser/src/soft_keywords.rs index 274b3182c396b..379ae1c08db38 100644 --- a/crates/ruff_python_parser/src/soft_keywords.rs +++ b/crates/ruff_python_parser/src/soft_keywords.rs @@ -32,7 +32,7 @@ where pub fn new(lexer: I, mode: Mode) -> Self { Self { underlying: lexer.multipeek(), // spell-checker:ignore multipeek - position: if matches!(mode, Mode::Expression) { + position: if mode == Mode::Expression { Position::Other } else { Position::Statement @@ -54,7 +54,6 @@ where // If the token is a soft keyword e.g. `type`, `match`, or `case`, check if it's // used as an identifier. We assume every soft keyword use is an identifier unless // a heuristic is met. - match tok { // For `match` and `case`, all of the following conditions must be met: // 1. The token is at the start of a logical line. @@ -62,7 +61,7 @@ where // inside a parenthesized expression, list, or dictionary). // 3. The top-level colon is not the immediate sibling of a `match` or `case` token. // (This is to avoid treating `match` or `case` as identifiers when annotated with - // type hints.) type hints.) + // type hints.) Tok::Match | Tok::Case => { if matches!(self.position, Position::Statement) { let mut nesting = 0; @@ -162,7 +161,7 @@ where // ```python // class Class: type X = int // ``` - Tok::Colon if matches!(self.position, Position::Other) => { + Tok::Colon if self.position == Position::Other => { self.position = Position::SimpleStatement; } Tok::Lpar | Tok::Lsqb | Tok::Lbrace => {