ruff/crates/ruff_python_parser/resources/inline/ok
Brent Westbrook 144484d46c
Refactor semantic syntax error scope handling (#17314)
## Summary

Based on the discussion in
https://github.com/astral-sh/ruff/pull/17298#discussion_r2033975460, we
decided to move the scope handling out of the `SemanticSyntaxChecker`
and into the `SemanticSyntaxContext` trait. This PR implements that
refactor by:

- Reverting all of the `Checkpoint` and `in_async_context` code in the
`SemanticSyntaxChecker`
- Adding four new methods to the `SemanticSyntaxContext` trait
- `in_async_context`: matches `SemanticModel::in_async_context` and only
detects the nearest enclosing function
- `in_sync_comprehension`: uses the new `is_async` tracking on
`Generator` scopes to detect any enclosing sync comprehension
  - `in_module_scope`: reports whether we're at the top-level scope
  - `in_notebook`: reports whether we're in a Jupyter notebook
- In-lining the `TestContext` directly into the
`SemanticSyntaxCheckerVisitor`
- This allows modifying the context as the visitor traverses the AST,
which wasn't possible before

One potential question here is "why not add a single method returning a
`Scope` or `Scopes` to the context?" The main reason is that the `Scope`
type is defined in the `ruff_python_semantic` crate, which is not
currently a dependency of the parser. It also doesn't appear to be used
in red-knot. So it seemed best to use these more granular methods
instead of trying to access `Scope` in `ruff_python_parser` (and
red-knot).

## Test Plan

Existing parser and linter tests.
2025-04-09 14:23:29 -04:00
..
all_async_comprehension_py310.py [syntax-errors] Async comprehension in sync comprehension (#17177) 2025-04-08 12:50:52 -04:00
ambiguous_lpar_with_items_binary_expr.py Consider binary expr for parenthesized with items parsing (#11012) 2024-04-18 21:39:30 +05:30
ambiguous_lpar_with_items_if_expr.py Consider if expression for parenthesized with items parsing (#11010) 2024-04-18 14:30:15 +00:00
ann_assign_stmt_simple_target.py Replace LALRPOP parser with hand-written parser (#10036) 2024-04-18 17:57:39 +05:30
args_unparenthesized_generator.py Raise syntax error for unparenthesized generator expr in multi-argument call (#12445) 2024-07-22 14:44:20 +05:30
assign_targets_terminator.py Replace LALRPOP parser with hand-written parser (#10036) 2024-04-18 17:57:39 +05:30
async_for_statement.py Replace LALRPOP parser with hand-written parser (#10036) 2024-04-18 17:57:39 +05:30
async_function_definition.py Replace LALRPOP parser with hand-written parser (#10036) 2024-04-18 17:57:39 +05:30
async_with_statement.py Replace LALRPOP parser with hand-written parser (#10036) 2024-04-18 17:57:39 +05:30
class_def_arguments.py Replace LALRPOP parser with hand-written parser (#10036) 2024-04-18 17:57:39 +05:30
class_keyword_in_case_pattern.py [syntax-errors] Fix multiple assignment for class keyword argument (#17184) 2025-04-03 17:32:39 -04:00
class_type_params_py312.py [syntax-errors] Type parameter lists before Python 3.12 (#16479) 2025-03-05 13:19:09 +00:00
comma_separated_regular_list_terminator.py Implement re-lexing logic for better error recovery (#11845) 2024-06-17 06:47:00 +00:00
debug_rename_import.py [syntax-errors] Store to or delete __debug__ (#16984) 2025-03-29 12:07:20 -04:00
decorator_async_function.py Replace LALRPOP parser with hand-written parser (#10036) 2024-04-18 17:57:39 +05:30
decorator_await_expression_py39.py [syntax-errors] Improve error message and range for pre-PEP-614 decorator syntax errors (#16581) 2025-03-17 11:17:27 +00:00
decorator_expression_dotted_ident_py38.py [syntax-errors] Named expressions in decorators before Python 3.9 (#16386) 2025-03-05 17:08:18 +00:00
decorator_expression_eval_hack_py38.py [syntax-errors] Named expressions in decorators before Python 3.9 (#16386) 2025-03-05 17:08:18 +00:00
decorator_expression_identity_hack_py38.py [syntax-errors] Named expressions in decorators before Python 3.9 (#16386) 2025-03-05 17:08:18 +00:00
decorator_expression_py39.py [syntax-errors] Named expressions in decorators before Python 3.9 (#16386) 2025-03-05 17:08:18 +00:00
del_debug_py38.py [syntax-errors] Store to or delete __debug__ (#16984) 2025-03-29 12:07:20 -04:00
del_targets_terminator.py Replace LALRPOP parser with hand-written parser (#10036) 2024-04-18 17:57:39 +05:30
dotted_name_normalized_spaces.py Replace LALRPOP parser with hand-written parser (#10036) 2024-04-18 17:57:39 +05:30
duplicate_match_key_attr.py [syntax-errors] Detect duplicate keys in match mapping patterns (#17129) 2025-04-03 10:22:37 -04:00
except_star_py311.py [syntax-errors] except* before Python 3.11 (#16446) 2025-03-02 18:20:18 +00:00
except_stmt_as_name_soft_keyword.py Maintain synchronicity between the lexer and the parser (#11457) 2024-06-03 18:23:50 +05:30
for_in_target_valid_expr.py Add ExpressionContext for expression parsing (#11055) 2024-04-23 04:19:05 +00:00
for_iter_unpack_py38.py [syntax-errors] Tuple unpacking in for statement iterator clause before Python 3.9 (#16558) 2025-03-13 15:55:17 -04:00
for_iter_unpack_py39.py [syntax-errors] Tuple unpacking in for statement iterator clause before Python 3.9 (#16558) 2025-03-13 15:55:17 -04:00
from_import_no_space.py Replace LALRPOP parser with hand-written parser (#10036) 2024-04-18 17:57:39 +05:30
from_import_soft_keyword_module_name.py Maintain synchronicity between the lexer and the parser (#11457) 2024-06-03 18:23:50 +05:30
from_import_stmt_terminator.py Replace LALRPOP parser with hand-written parser (#10036) 2024-04-18 17:57:39 +05:30
fstring_format_spec_terminator.py Replace LALRPOP parser with hand-written parser (#10036) 2024-04-18 17:57:39 +05:30
function_def_parameter_range.py Replace LALRPOP parser with hand-written parser (#10036) 2024-04-18 17:57:39 +05:30
function_def_parenthesized_return_types.py Replace LALRPOP parser with hand-written parser (#10036) 2024-04-18 17:57:39 +05:30
function_def_valid_return_expr.py [syntax-errors] Invalid syntax in annotations (#17101) 2025-04-03 17:56:55 -04:00
function_type_params_py312.py [syntax-errors] Type parameter lists before Python 3.12 (#16479) 2025-03-05 13:19:09 +00:00
global_stmt.py Replace LALRPOP parser with hand-written parser (#10036) 2024-04-18 17:57:39 +05:30
import_as_name_soft_keyword.py Maintain synchronicity between the lexer and the parser (#11457) 2024-06-03 18:23:50 +05:30
import_stmt_terminator.py Replace LALRPOP parser with hand-written parser (#10036) 2024-04-18 17:57:39 +05:30
irrefutable_case_pattern_at_end.py [syntax-errors] Irrefutable case pattern before final case (#16905) 2025-03-26 12:27:16 -04:00
iter_unpack_return_py37.py [syntax-errors] Tuple unpacking in return and yield before Python 3.8 (#16485) 2025-03-06 11:57:20 -05:00
iter_unpack_return_py38.py [syntax-errors] Tuple unpacking in return and yield before Python 3.8 (#16485) 2025-03-06 11:57:20 -05:00
iter_unpack_yield_py37.py [syntax-errors] Tuple unpacking in return and yield before Python 3.8 (#16485) 2025-03-06 11:57:20 -05:00
iter_unpack_yield_py38.py [syntax-errors] Tuple unpacking in return and yield before Python 3.8 (#16485) 2025-03-06 11:57:20 -05:00
lambda_with_no_parameters.py Replace LALRPOP parser with hand-written parser (#10036) 2024-04-18 17:57:39 +05:30
lambda_with_valid_body.py Replace LALRPOP parser with hand-written parser (#10036) 2024-04-18 17:57:39 +05:30
match_after_py310.py Allow passing ParseOptions to inline tests (#16357) 2025-02-27 10:23:15 -05:00
match_as_pattern.py [syntax-errors] Irrefutable case pattern before final case (#16905) 2025-03-26 12:27:16 -04:00
match_as_pattern_soft_keyword.py [syntax-errors] Irrefutable case pattern before final case (#16905) 2025-03-26 12:27:16 -04:00
match_attr_pattern_soft_keyword.py Maintain synchronicity between the lexer and the parser (#11457) 2024-06-03 18:23:50 +05:30
match_classify_as_identifier_1.py Maintain synchronicity between the lexer and the parser (#11457) 2024-06-03 18:23:50 +05:30
match_classify_as_identifier_2.py Maintain synchronicity between the lexer and the parser (#11457) 2024-06-03 18:23:50 +05:30
match_classify_as_keyword_1.py Maintain synchronicity between the lexer and the parser (#11457) 2024-06-03 18:23:50 +05:30
match_classify_as_keyword_2.py Maintain synchronicity between the lexer and the parser (#11457) 2024-06-03 18:23:50 +05:30
match_classify_as_keyword_or_identifier.py Maintain synchronicity between the lexer and the parser (#11457) 2024-06-03 18:23:50 +05:30
match_sequence_pattern_parentheses_terminator.py Replace LALRPOP parser with hand-written parser (#10036) 2024-04-18 17:57:39 +05:30
match_sequence_pattern_terminator.py [syntax-errors] Irrefutable case pattern before final case (#16905) 2025-03-26 12:27:16 -04:00
match_stmt_subject_expr.py Replace LALRPOP parser with hand-written parser (#10036) 2024-04-18 17:57:39 +05:30
match_stmt_valid_guard_expr.py Replace LALRPOP parser with hand-written parser (#10036) 2024-04-18 17:57:39 +05:30
multiple_assignment_in_case_pattern.py [syntax-errors] Multiple assignments in case pattern (#16957) 2025-03-26 13:02:42 -04:00
nested_async_comprehension_py310.py Refactor semantic syntax error scope handling (#17314) 2025-04-09 14:23:29 -04:00
nested_async_comprehension_py311.py [syntax-errors] Async comprehension in sync comprehension (#17177) 2025-04-08 12:50:52 -04:00
non_duplicate_type_parameter_names.py [syntax-errors] Duplicate type parameter names (#16858) 2025-03-21 15:06:22 -04:00
non_rebound_comprehension_variable.py [syntax-errors] Start detecting compile-time syntax errors (#16106) 2025-03-21 14:45:25 -04:00
nonlocal_stmt.py Replace LALRPOP parser with hand-written parser (#10036) 2024-04-18 17:57:39 +05:30
param_with_annotation.py [syntax-errors] Invalid syntax in annotations (#17101) 2025-04-03 17:56:55 -04:00
param_with_default.py Replace LALRPOP parser with hand-written parser (#10036) 2024-04-18 17:57:39 +05:30
param_with_star_annotation.py Replace LALRPOP parser with hand-written parser (#10036) 2024-04-18 17:57:39 +05:30
param_with_star_annotation_py310.py [syntax-errors] Fix star annotation before Python 3.11 (#16878) 2025-03-20 17:44:52 -04:00
param_with_star_annotation_py311.py [syntax-errors] Star annotations before Python 3.11 (#16545) 2025-03-14 15:20:44 +00:00
params_non_default_after_star.py Replace LALRPOP parser with hand-written parser (#10036) 2024-04-18 17:57:39 +05:30
params_seen_keyword_only_param_after_star.py Replace LALRPOP parser with hand-written parser (#10036) 2024-04-18 17:57:39 +05:30
parenthesized_context_manager_py39.py [syntax-errors] Parenthesized context managers before Python 3.9 (#16523) 2025-03-17 08:54:55 -04:00
parenthesized_kwarg_py37.py [syntax-errors] Parenthesized keyword argument names after Python 3.8 (#16482) 2025-03-06 12:18:13 -05:00
parenthesized_named_expr_index_py38.py [syntax-errors] Unparenthesized assignment expressions in sets and indexes (#16404) 2025-03-14 15:06:42 -04:00
parenthesized_named_expr_py38.py [syntax-errors] Unparenthesized assignment expressions in sets and indexes (#16404) 2025-03-14 15:06:42 -04:00
parenthesized_star_index_py310.py [syntax-errors] Fix false positive for parenthesized tuple index (#16948) 2025-03-24 10:34:38 -04:00
pep701_f_string_py311.py [syntax-errors] PEP 701 f-strings before Python 3.12 (#16543) 2025-03-18 11:12:15 -04:00
pep701_f_string_py312.py [syntax-errors] PEP 701 f-strings before Python 3.12 (#16543) 2025-03-18 11:12:15 -04:00
pos_only_py38.py [syntax-errors] Positional-only parameters before Python 3.8 (#16481) 2025-03-05 13:46:43 +00:00
read_from_debug.py [syntax-errors] Store to or delete __debug__ (#16984) 2025-03-29 12:07:20 -04:00
simple_stmts_in_block.py Replace LALRPOP parser with hand-written parser (#10036) 2024-04-18 17:57:39 +05:30
simple_stmts_with_semicolons.py Replace LALRPOP parser with hand-written parser (#10036) 2024-04-18 17:57:39 +05:30
single_star_in_tuple.py [syntax-errors] Starred expressions in return, yield, and for (#17134) 2025-04-02 08:38:25 -04:00
single_starred_assignment_target.py [syntax-errors] Single starred assignment target (#17024) 2025-03-29 12:35:47 -04:00
star_index_py311.py [syntax-errors] Star expression in index before Python 3.11 (#16544) 2025-03-14 14:51:34 +00:00
tuple_context_manager_py38.py [syntax-errors] Parenthesized context managers before Python 3.9 (#16523) 2025-03-17 08:54:55 -04:00
type_param_default_py313.py [syntax-errors] Type parameter defaults before Python 3.13 (#16447) 2025-03-04 16:53:38 +00:00
type_param_param_spec.py Add support for PEP 696 syntax (#11120) 2024-04-26 09:47:29 +02:00
type_param_type_var.py Add support for PEP 696 syntax (#11120) 2024-04-26 09:47:29 +02:00
type_param_type_var_tuple.py Add support for PEP 696 syntax (#11120) 2024-04-26 09:47:29 +02:00
type_stmt_py312.py [syntax-errors] type alias statements before Python 3.12 (#16478) 2025-03-04 17:20:10 +00:00
unparenthesized_named_expr_index_py39.py [syntax-errors] Unparenthesized assignment expressions in sets and indexes (#16404) 2025-03-14 15:06:42 -04:00
unparenthesized_named_expr_py39.py [syntax-errors] Unparenthesized assignment expressions in sets and indexes (#16404) 2025-03-14 15:06:42 -04:00
valid_annotation_class.py [syntax-errors] Extend annotation checks to await (#17282) 2025-04-08 08:55:43 -04:00
valid_annotation_function_py313.py [syntax-errors] Extend annotation checks to await (#17282) 2025-04-08 08:55:43 -04:00
valid_annotation_py313.py [syntax-errors] Check annotations in annotated assignments (#17283) 2025-04-08 08:56:25 -04:00
walrus_py38.py [syntax-errors] Assignment expressions before Python 3.8 (#16383) 2025-02-28 17:13:46 -05:00