Skip to content

Conversation

@ntBre
Copy link
Contributor

@ntBre ntBre commented Apr 18, 2025

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) 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 global statement 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 (LoadBeforeGlobalDeclaration or 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

@ntBre ntBre added the ty Multi-file analysis & type inference label Apr 18, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Apr 18, 2025

mypy_primer results

No ecosystem changes detected ✅

@ntBre
Copy link
Contributor Author

ntBre commented Apr 18, 2025

The mypy_primer results look like true positives to me, although it's a bit unfortunate because it seems clear that this file is meant to be split up somehow:

https://github.com/laowantong/paroxython/blob/44e14b17965c6b02871db4c9ed6acc8716c2740d/examples/idioms/programs_with_labels.py#L690-L695

ntBre added 8 commits April 18, 2025 12:00
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.
@ntBre ntBre force-pushed the brent/semantic-errors-red-knot branch from bf6cc0c to 60acba6 Compare April 18, 2025 16:00
@github-actions
Copy link
Contributor

github-actions bot commented Apr 18, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

@ntBre ntBre marked this pull request as ready for review April 18, 2025 16:50
Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🥳

ntBre and others added 2 commits April 18, 2025 16:27
Copy link
Member

@AlexWaygood AlexWaygood left a 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!

ntBre and others added 2 commits April 21, 2025 09:18
in favor of tracking this state in the SemanticSyntaxChecker itself
@codspeed-hq
Copy link

codspeed-hq bot commented Apr 22, 2025

CodSpeed Performance Report

Merging #17463 will not alter performance

Comparing brent/semantic-errors-red-knot (931fcf1) with main (5407249)

Summary

✅ 33 untouched benchmarks

@ntBre
Copy link
Contributor Author

ntBre commented Apr 22, 2025

I'm guessing (hoping) the horrible performance degradation was from my naive use of is_file_open. I started caching that on the builder and also merged main in case it was something else.

@ntBre
Copy link
Contributor Author

ntBre commented Apr 22, 2025

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) {
Copy link
Member

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.

Comment on lines 107 to 108
python_version: PythonVersion,
semantic_checker: SemanticSyntaxChecker,
Copy link
Member

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

@ntBre ntBre merged commit e7f38fe into main Apr 23, 2025
34 checks passed
@ntBre ntBre deleted the brent/semantic-errors-red-knot branch April 23, 2025 13:53
dcreager added a commit that referenced this pull request Apr 23, 2025
…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)
dcreager added a commit that referenced this pull request Apr 23, 2025
…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)
ntBre added a commit that referenced this pull request Apr 23, 2025
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`.
ntBre added a commit that referenced this pull request Apr 23, 2025
## 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants