Skip to content

Start improving detection of syntax errors #16034

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

Closed
wants to merge 18 commits into from
Closed

Conversation

ntBre
Copy link
Contributor

@ntBre ntBre commented Feb 8, 2025

Summary

This is a first prototype of syntax error detection meant to accompany an internal design document (coming soon!).

This PR introduces the ruff_python_syntax_errors crate, which contains a check_syntax function for traversing the parsed AST and detecting SyntaxErrors, which can be converted into Diagnostics in both ruff and red-knot.

Only one new syntax error is currently detected, MatchBeforePy310, the use of match statements in Python 3.9 or earlier. However, check_syntax is wired into ruff and red-knot, so all future syntax errors should only require additions to ruff_python_syntax_errors, with a caveat I'll note as a review comment below.

Test Plan

There are two new, very simple test cases in ruff_python_syntax_errors itself, as well as a couple of new diagnostics in existing ruff tests. I think it would be nice to add at least one red-knot test that triggers the new diagnostic before merging.

@ntBre ntBre added the rule Implementing or modifying a lint rule label Feb 8, 2025
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I initially tried just implementing From<SyntaxError> for ruff_diagnostics::Diagnostic like in red-knot, but this caused problems in code_to_rule, which maps code names like match-before-python-310 back to rule codes. As it stands, every new SyntaxErrorKind will also need an entry in the syntax_errors macro and an arm in the match here.

This macro should pass along the documentation, though, so I think this will integrate well with the way other lint rules are documented, at least.

Copy link
Contributor

github-actions bot commented Feb 8, 2025

ruff-ecosystem results

Linter (stable)

ℹ️ ecosystem check detected linter changes. (+6 -0 violations, +0 -0 fixes in 1 projects; 54 projects unchanged)

facebookresearch/chameleon (+6 -0 violations, +0 -0 fixes)

+ chameleon/viewer/backend/models/chameleon_distributed.py:405:13: SYN001 Cannot use `match` statement on Python 3.9 (syntax was new in Python 3.10)
+ chameleon/viewer/backend/models/chameleon_distributed.py:818:17: SYN001 Cannot use `match` statement on Python 3.9 (syntax was new in Python 3.10)
+ chameleon/viewer/backend/models/chameleon_local.py:633:13: SYN001 Cannot use `match` statement on Python 3.9 (syntax was new in Python 3.10)
+ chameleon/viewer/backend/models/service.py:131:21: SYN001 Cannot use `match` statement on Python 3.9 (syntax was new in Python 3.10)
+ chameleon/viewer/backend/models/service.py:154:17: SYN001 Cannot use `match` statement on Python 3.9 (syntax was new in Python 3.10)
+ chameleon/viewer/backend/models/service.py:226:25: SYN001 Cannot use `match` statement on Python 3.9 (syntax was new in Python 3.10)

Changes by rule (1 rules affected)

code total + violation - violation + fix - fix
SYN001 6 6 0 0 0

Linter (preview)

ℹ️ ecosystem check detected linter changes. (+6 -0 violations, +0 -0 fixes in 1 projects; 54 projects unchanged)

facebookresearch/chameleon (+6 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --no-fix --output-format concise --preview

+ chameleon/viewer/backend/models/chameleon_distributed.py:405:13: SYN001 Cannot use `match` statement on Python 3.9 (syntax was new in Python 3.10)
+ chameleon/viewer/backend/models/chameleon_distributed.py:818:17: SYN001 Cannot use `match` statement on Python 3.9 (syntax was new in Python 3.10)
+ chameleon/viewer/backend/models/chameleon_local.py:633:13: SYN001 Cannot use `match` statement on Python 3.9 (syntax was new in Python 3.10)
+ chameleon/viewer/backend/models/service.py:131:21: SYN001 Cannot use `match` statement on Python 3.9 (syntax was new in Python 3.10)
+ chameleon/viewer/backend/models/service.py:154:17: SYN001 Cannot use `match` statement on Python 3.9 (syntax was new in Python 3.10)
+ chameleon/viewer/backend/models/service.py:226:25: SYN001 Cannot use `match` statement on Python 3.9 (syntax was new in Python 3.10)

Changes by rule (1 rules affected)

code total + violation - violation + fix - fix
SYN001 6 6 0 0 0

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

@MichaReiser
Copy link
Member

It would be great to have a design discussion first on how we plan on integrating this into red know and ruff from a ux perspective because this approach already implies some important decisions

@AlexWaygood
Copy link
Member

AlexWaygood commented Feb 8, 2025

It would be great to have a design discussion first on how we plan on integrating this into red know and ruff from a ux perspective because this approach already implies some important decisions

Brent's been writing up a design doc and he's shared an early draft with me. He will be sharing it with the rest of the team as well. I believe the idea of this (draft) PR is for it to be a prototype to share alongside the design doc.

@ntBre
Copy link
Contributor Author

ntBre commented Feb 8, 2025

Yes, sorry about that. The intention is definitely to have a design discussion first.

@ntBre
Copy link
Contributor Author

ntBre commented Feb 24, 2025

Closing in favor #16090 and #16106.

@ntBre ntBre closed this Feb 24, 2025
@ntBre ntBre deleted the brent/syntax-errors branch May 1, 2025 15:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rule Implementing or modifying a lint rule
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants