-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[red-knot] Detect semantic syntax errors #17463
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
crates/red_knot_python_semantic/resources/mdtest/comprehensions/basic.md
Outdated
Show resolved
Hide resolved
|
|
The |
Summary -- This PR extends semantic syntax error detection to red-knot. The main changes here are: 1. Adding `SemanticSyntaxChecker` and `Vec<SemanticSyntaxError>` fields to the `SemanticIndexBuilder` 2. Calling `SemanticSyntaxChecker::visit_stmt` and `visit_expr` in the `SemanticIndexBuilder`'s `visit_stmt` and `visit_expr` methods 3. Implementing `SemanticSyntaxContext` for `SemanticIndexBuilder` 4. Adding new mdtests to test the context implementation and show diagnostics (3) is definitely the trickiest and required (I think) some additions to the `SemanticIndexBuilder`. I tried to look around for existing code performing the necessary checks, but I definitely could have missed something or misused the existing code even when I found it. Test Plan -- New mdtests
This tracking looks a bit more complicated in the ruff `Checker`, but that's because it does some other checks at the same time. Every arm of its `match` where this flag is set ends up setting it.
bf6cc0c to
60acba6
Compare
|
AlexWaygood
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🥳
crates/red_knot_python_semantic/resources/mdtest/annotations/invalid.md
Outdated
Show resolved
Hide resolved
crates/red_knot_python_semantic/resources/mdtest/comprehensions/basic.md
Outdated
Show resolved
Hide resolved
crates/red_knot_python_semantic/resources/mdtest/comprehensions/basic.md
Outdated
Show resolved
Hide resolved
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
AlexWaygood
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is great, thank you!
crates/red_knot_python_semantic/resources/mdtest/annotations/invalid.md
Outdated
Show resolved
Hide resolved
crates/red_knot_python_semantic/resources/mdtest/diagnostics/semantic_syntax_errors.md
Show resolved
Hide resolved
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
in favor of tracking this state in the SemanticSyntaxChecker itself
CodSpeed Performance ReportMerging #17463 will not alter performanceComparing Summary
|
|
I'm guessing (hoping) the horrible performance degradation was from my naive use of |
|
One more guess and then I'll set up for local benchmarks 😅 I'm not getting much from the salsa flamegraphs on codspeed. |
| } | ||
|
|
||
| fn report_semantic_error(&self, error: SemanticSyntaxError) { | ||
| if self.db.is_file_open(self.file) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could consider changing the semantic_checker field on the SemanticIndexBuilder to be an Option<SemanticSyntaxChecker> and only initialize it with Some if the file is open. with_semantic_checker is a no-op if the checker is None.
| python_version: PythonVersion, | ||
| semantic_checker: SemanticSyntaxChecker, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should move those fields into their own group (or to the builder state group at the top). The fields at the bottom are reserved for fields that match the fields on SemanticIndex
This reverts commit e32c188.
…tructor * origin/main: [red-knot] Trust module-level undeclared symbols in stubs (#17577) [`airflow`] Apply auto fixes to cases where the names have changed in Airflow 3 (`AIR301`) (#17355) [`pycodestyle`] Auto-fix redundant boolean comparison (`E712`) (#17090) [red-knot] Detect semantic syntax errors (#17463) Fix stale diagnostics in Ruff playground (#17583) [red-knot] Early return from `project.is_file_open` for vendored files (#17580)
…var-instance * dcreager/generic-constructor: Revert FunctionLiteral type [red-knot] Trust module-level undeclared symbols in stubs (#17577) [`airflow`] Apply auto fixes to cases where the names have changed in Airflow 3 (`AIR301`) (#17355) [`pycodestyle`] Auto-fix redundant boolean comparison (`E712`) (#17090) Clean this up a bit clippy [red-knot] Detect semantic syntax errors (#17463) Fix stale diagnostics in Ruff playground (#17583) [red-knot] Early return from `project.is_file_open` for vendored files (#17580)
Status -- This is a pretty minor change, but it was breaking a red-knot mdtest until #17463 landed. Now this should close #11934 as the last syntax error being tracked there! Summary -- Moves `Parser::validate_parameters` to `SemanticSyntaxChecker::duplicate_parameter_name`. Test Plan -- Existing tests, with `## Errors` replaced with `## Semantic Syntax Errors`.
## Summary This is a first step toward `global` support in red-knot (#15385). I went through all the matches for `global` in the `mypy/test-data` directory, but I didn't find anything too interesting that wasn't already covered by @carljm's suggestions on Discord. I still pulled in a couple of cases for a little extra variety. I also included a section from the [PLE0118](https://docs.astral.sh/ruff/rules/load-before-global-declaration/) tests in ruff that will become syntax errors once #17463 is merged and we handle `global` statements. I don't think I figured out how to use `@Todo` properly, so please let me know if I need to fix that. I hope this is a good start to the test suite otherwise. --------- Co-authored-by: Carl Meyer <carl@astral.sh>
Summary
This PR extends semantic syntax error detection to red-knot. The main changes here are:
SemanticSyntaxCheckerandVec<SemanticSyntaxError>fields to theSemanticIndexBuilderSemanticSyntaxChecker::visit_stmtandvisit_exprin theSemanticIndexBuilder'svisit_stmtandvisit_exprmethodsSemanticSyntaxContextforSemanticIndexBuilder(3) is definitely the trickiest and required (I think) a minor addition to the
SemanticIndexBuilder. I tried to look around for existing code performing the necessary checks, but I definitely could have missed something or misused the existing code even when I found it.There's still one TODO around
globalstatement handling. I don't think there's an existing way to look this up, but I'm happy to work on that here or in a separate PR. This currently only affects detection of one error (LoadBeforeGlobalDeclarationor PLE0118 in ruff), so it's not too big of a problem even if we leave the TODO.Test Plan
New mdtests, as well as new errors for existing mdtests