Skip to content
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

[syntax-errors] Fix star annotation before Python 3.11 #16878

Merged
merged 1 commit into from
Mar 20, 2025

Conversation

ntBre
Copy link
Contributor

@ntBre ntBre commented Mar 20, 2025

Summary

Fixes #16874. I previously emitted a syntax error when starred annotations were allowed rather than when they were actually used. This caused false positives for any starred parameter name because these are allowed to have starred annotations but not required to. The fix is to check if the annotation is actually starred after parsing it.

Test Plan

New inline parser tests derived from the initial report and more examples from the comments, although I think the first case should cover them all.

Summary
--

Fixes #16874. I previously emitted a syntax error when starred annotations were
_allowed_ rather than when they were actually used. This caused false positives
for any starred parameter name because these are allowed to have starred
annotations but not required to. The fix is to check if the annotation is
actually starred after parsing it.

Test Plan
--

New inline parser tests derived from the initial report and more examples from
the comments, although I think the first case should cover them all.
@ntBre ntBre added bug Something isn't working preview Related to preview mode features labels Mar 20, 2025
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.

Do we get an ecosystem report for the syntax-error checks? That could have helped us catch this before it landed

@ntBre
Copy link
Contributor Author

ntBre commented Mar 20, 2025

We do , but it was clean: #16545 (comment)

@ntBre
Copy link
Contributor Author

ntBre commented Mar 20, 2025

Maybe it's not actually checking for them, I guess that's what you reallly meant 🤦

Copy link
Contributor

github-actions bot commented Mar 20, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

ℹ️ ecosystem check detected linter changes. (+0 -2751 violations, +0 -0 fixes in 39 projects; 16 projects unchanged)

DisnakeDev/disnake (+0 -98 violations, +0 -0 fixes)

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

- disnake/client.py:1260:26: SyntaxError: Cannot use star annotation on Python 3.8 (syntax was added in Python 3.11)
- disnake/client.py:732:16: SyntaxError: Cannot use star annotation on Python 3.8 (syntax was added in Python 3.11)
- disnake/client.py:749:16: SyntaxError: Cannot use star annotation on Python 3.8 (syntax was added in Python 3.11)
- disnake/client.py:756:43: SyntaxError: Cannot use star annotation on Python 3.8 (syntax was added in Python 3.11)
- disnake/client.py:949:56: SyntaxError: Cannot use star annotation on Python 3.8 (syntax was added in Python 3.11)
- disnake/ext/commands/base_core.py:134:29: SyntaxError: Cannot use star annotation on Python 3.8 (syntax was added in Python 3.11)
- disnake/ext/commands/base_core.py:348:66: SyntaxError: Cannot use star annotation on Python 3.8 (syntax was added in Python 3.11)
- disnake/ext/commands/base_core.py:449:73: SyntaxError: Cannot use star annotation on Python 3.8 (syntax was added in Python 3.11)
- disnake/ext/commands/bot_base.py:58:34: SyntaxError: Cannot use star annotation on Python 3.8 (syntax was added in Python 3.11)
- disnake/ext/commands/cog.py:153:44: SyntaxError: Cannot use star annotation on Python 3.8 (syntax was added in Python 3.11)
... 88 additional changes omitted for project

RasaHQ/rasa (+0 -48 violations, +0 -0 fixes)

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

- rasa/core/actions/forms.py:719:39: SyntaxError: Cannot use star annotation on Python 3.9 (syntax was added in Python 3.11)
- rasa/core/agent.py:269:39: SyntaxError: Cannot use star annotation on Python 3.9 (syntax was added in Python 3.11)
- rasa/core/brokers/pika.py:225:49: SyntaxError: Cannot use star annotation on Python 3.9 (syntax was added in Python 3.11)
- rasa/core/channels/socketio.py:19:61: SyntaxError: Cannot use star annotation on Python 3.9 (syntax was added in Python 3.11)
- rasa/core/training/interactive.py:607:47: SyntaxError: Cannot use star annotation on Python 3.9 (syntax was added in Python 3.11)
- rasa/engine/graph.py:441:44: SyntaxError: Cannot use star annotation on Python 3.9 (syntax was added in Python 3.11)
- rasa/server.py:154:30: SyntaxError: Cannot use star annotation on Python 3.9 (syntax was added in Python 3.11)
- rasa/server.py:178:38: SyntaxError: Cannot use star annotation on Python 3.9 (syntax was added in Python 3.11)
- rasa/server.py:213:38: SyntaxError: Cannot use star annotation on Python 3.9 (syntax was added in Python 3.11)
- rasa/server.py:235:38: SyntaxError: Cannot use star annotation on Python 3.9 (syntax was added in Python 3.11)
... 38 additional changes omitted for project

Snowflake-Labs/snowcli (+0 -14 violations, +0 -0 fixes)

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

- src/snowflake/cli/_plugins/nativeapp/codegen/sandbox.py:302:34: SyntaxError: Cannot use star annotation on Python 3.10 (syntax was added in Python 3.11)
- src/snowflake/cli/_plugins/nativeapp/codegen/setup/native_app_setup_processor.py:53:30: SyntaxError: Cannot use star annotation on Python 3.10 (syntax was added in Python 3.11)
- src/snowflake/cli/api/commands/overrideable_parameter.py:132:49: SyntaxError: Cannot use star annotation on Python 3.10 (syntax was added in Python 3.11)
- src/snowflake/cli/api/commands/overrideable_parameter.py:141:49: SyntaxError: Cannot use star annotation on Python 3.10 (syntax was added in Python 3.11)
- src/snowflake/cli/api/commands/overrideable_parameter.py:41:23: SyntaxError: Cannot use star annotation on Python 3.10 (syntax was added in Python 3.11)
- src/snowflake/cli/api/commands/overrideable_parameter.py:74:50: SyntaxError: Cannot use star annotation on Python 3.10 (syntax was added in Python 3.11)
- src/snowflake/cli/api/project/definition_conversion.py:76:74: SyntaxError: Cannot use star annotation on Python 3.10 (syntax was added in Python 3.11)
- src/snowflake/cli/api/project/util.py:201:26: SyntaxError: Cannot use star annotation on Python 3.10 (syntax was added in Python 3.11)
- tests_integration/testing_utils/assertions/test_file_assertions.py:22:17: SyntaxError: Cannot use star annotation on Python 3.10 (syntax was added in Python 3.11)
- tests_integration/testing_utils/snowpark_utils.py:317:75: SyntaxError: Cannot use star annotation on Python 3.10 (syntax was added in Python 3.11)
... 4 additional changes omitted for project

aiven/aiven-client (+0 -7 violations, +0 -0 fixes)

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

- aiven/client/argx.py:155:31: SyntaxError: Cannot use star annotation on Python 3.8 (syntax was added in Python 3.11)
- aiven/client/argx.py:54:31: SyntaxError: Cannot use star annotation on Python 3.8 (syntax was added in Python 3.11)
- aiven/client/client.py:1496:89: SyntaxError: Cannot use star annotation on Python 3.8 (syntax was added in Python 3.11)
- aiven/client/client.py:1589:61: SyntaxError: Cannot use star annotation on Python 3.8 (syntax was added in Python 3.11)
- aiven/client/client.py:249:28: SyntaxError: Cannot use star annotation on Python 3.8 (syntax was added in Python 3.11)
- aiven/client/session.py:18:31: SyntaxError: Cannot use star annotation on Python 3.8 (syntax was added in Python 3.11)
- aiven/client/session.py:22:27: SyntaxError: Cannot use star annotation on Python 3.8 (syntax was added in Python 3.11)

apache/airflow (+0 -61 violations, +0 -0 fixes)

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

- airflow/api_fastapi/auth/tokens.py:120:38: SyntaxError: Cannot use star annotation on Python 3.9 (syntax was added in Python 3.11)
- airflow/api_fastapi/common/parameters.py:145:29: SyntaxError: Cannot use star annotation on Python 3.9 (syntax was added in Python 3.11)
- airflow/api_fastapi/common/parameters.py:221:29: SyntaxError: Cannot use star annotation on Python 3.9 (syntax was added in Python 3.11)
- airflow/api_fastapi/common/parameters.py:305:29: SyntaxError: Cannot use star annotation on Python 3.9 (syntax was added in Python 3.11)
- airflow/api_fastapi/common/parameters.py:471:29: SyntaxError: Cannot use star annotation on Python 3.9 (syntax was added in Python 3.11)
- airflow/api_fastapi/common/parameters.py:83:29: SyntaxError: Cannot use star annotation on Python 3.9 (syntax was added in Python 3.11)
- airflow/decorators/base.py:357:31: SyntaxError: Cannot use star annotation on Python 3.9 (syntax was added in Python 3.11)
- airflow/decorators/condition.py:109:27: SyntaxError: Cannot use star annotation on Python 3.9 (syntax was added in Python 3.11)
- airflow/decorators/task_group.py:86:31: SyntaxError: Cannot use star annotation on Python 3.9 (syntax was added in Python 3.11)
- airflow/decorators/task_group.py:96:79: SyntaxError: Cannot use star annotation on Python 3.9 (syntax was added in Python 3.11)
... 51 additional changes omitted for project

apache/superset (+0 -61 violations, +0 -0 fixes)

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

- superset/cli/test_db.py:88:30: SyntaxError: Cannot use star annotation on Python 3.10 (syntax was added in Python 3.11)
- superset/commands/chart/importers/dispatcher.py:43:57: SyntaxError: Cannot use star annotation on Python 3.10 (syntax was added in Python 3.11)
- superset/commands/dashboard/importers/dispatcher.py:46:57: SyntaxError: Cannot use star annotation on Python 3.10 (syntax was added in Python 3.11)
- superset/commands/database/importers/dispatcher.py:41:57: SyntaxError: Cannot use star annotation on Python 3.10 (syntax was added in Python 3.11)
- superset/commands/dataset/importers/dispatcher.py:46:57: SyntaxError: Cannot use star annotation on Python 3.10 (syntax was added in Python 3.11)
- superset/commands/dataset/importers/v0.py:231:16: SyntaxError: Cannot use star annotation on Python 3.10 (syntax was added in Python 3.11)
- superset/commands/importers/v1/__init__.py:51:57: SyntaxError: Cannot use star annotation on Python 3.10 (syntax was added in Python 3.11)
- superset/commands/importers/v1/assets.py:73:57: SyntaxError: Cannot use star annotation on Python 3.10 (syntax was added in Python 3.11)
- superset/commands/importers/v1/examples.py:62:57: SyntaxError: Cannot use star annotation on Python 3.10 (syntax was added in Python 3.11)
- superset/commands/query/importers/dispatcher.py:43:57: SyntaxError: Cannot use star annotation on Python 3.10 (syntax was added in Python 3.11)
... 51 additional changes omitted for project

aws/aws-sam-cli (+0 -1 violations, +0 -0 fixes)

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

- samcli/commands/local/cli_common/invoke_context.py:303:31: SyntaxError: Cannot use star annotation on Python 3.9 (syntax was added in Python 3.11)

bloomberg/pytest-memray (+0 -3 violations, +0 -0 fixes)

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

- src/pytest_memray/plugin.py:212:28: SyntaxError: Cannot use star annotation on Python 3.8 (syntax was added in Python 3.11)
- src/pytest_memray/plugin.py:217:40: SyntaxError: Cannot use star annotation on Python 3.8 (syntax was added in Python 3.11)
- src/pytest_memray/plugin.py:54:16: SyntaxError: Cannot use star annotation on Python 3.8 (syntax was added in Python 3.11)

bokeh/bokeh (+0 -143 violations, +0 -0 fixes)

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

- release/logger.py:57:30: SyntaxError: Cannot use star annotation on Python 3.9 (syntax was added in Python 3.11)
- src/bokeh/application/application.py:88:35: SyntaxError: Cannot use star annotation on Python 3.9 (syntax was added in Python 3.11)
- src/bokeh/application/handlers/code.py:175:27: SyntaxError: Cannot use star annotation on Python 3.9 (syntax was added in Python 3.11)
- src/bokeh/core/enums.py:199:26: SyntaxError: Cannot use star annotation on Python 3.9 (syntax was added in Python 3.11)
- src/bokeh/core/property/bases.py:449:38: SyntaxError: Cannot use star annotation on Python 3.9 (syntax was added in Python 3.11)
- src/bokeh/core/property/container.py:287:38: SyntaxError: Cannot use star annotation on Python 3.9 (syntax was added in Python 3.11)
- src/bokeh/core/property/either.py:79:78: SyntaxError: Cannot use star annotation on Python 3.9 (syntax was added in Python 3.11)
- src/bokeh/core/property/enum.py:59:44: SyntaxError: Cannot use star annotation on Python 3.9 (syntax was added in Python 3.11)
- src/bokeh/core/property/enum.py:61:64: SyntaxError: Cannot use star annotation on Python 3.9 (syntax was added in Python 3.11)
- src/bokeh/core/property/wrappers.py:283:37: SyntaxError: Cannot use star annotation on Python 3.9 (syntax was added in Python 3.11)
... 133 additional changes omitted for project

... Truncated remaining completed project reports due to GitHub comment length restrictions

Changes by rule (1 rules affected)

code total + violation - violation + fix - fix
SyntaxError: 2751 0 2751 0 0

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

@ntBre
Copy link
Contributor Author

ntBre commented Mar 20, 2025

It looks like --output-format concise suppresses these errors, which I think prevents them from showing up in the ecosystem check.

@AlexWaygood
Copy link
Member

Huh, is that the correct behaviour? I'd want to know about syntax errors in my code even if I specified that option, I think 😄

@ntBre
Copy link
Contributor Author

ntBre commented Mar 20, 2025

I don't think so, I'm working on a separate fix for that! ParseErrors are still displayed, so I think it was just another oversight on my part.

ntBre added a commit that referenced this pull request Mar 20, 2025
Summary
--

This updates the regex in `ruff-ecosystem` to catch syntax errors in an effort
to prevent bugs like #16874. This should catch `ParseError`s,
`UnsupportedSyntaxError`s, and the upcoming `SemanticSyntaxError`s.

Test Plan
--

I ran the ecosystem check locally comparing v0.11.0 and v0.11.1 and saw a large
number (2757!) of new syntax errors. I also manually tested the regex on a few
lines before that.

If we merge this before #16878, I'd expect to see that number decrease
substantially in that PR too, as another test.
@ntBre
Copy link
Contributor Author

ntBre commented Mar 20, 2025

I was just in too much of a hurry before 😅 --output-format was not the problem, I had just forgotten --preview. Instead, the regex matching diagnostics was excluding syntax errors. I've opened a PR updating it: #16879

ntBre added a commit that referenced this pull request Mar 20, 2025
Summary
--

This updates the regex in `ruff-ecosystem` to catch syntax errors in an
effort to prevent bugs like #16874. This should catch `ParseError`s,
`UnsupportedSyntaxError`s, and the upcoming `SemanticSyntaxError`s.

Test Plan
--

I ran the ecosystem check locally comparing v0.11.0 and v0.11.1 and saw
a large number (2757!) of new syntax errors. I also manually tested the
regex on a few lines before that.

If we merge this before #16878, I'd expect to see that number decrease
substantially in that PR too, as another test.
@ntBre ntBre closed this Mar 20, 2025
@ntBre ntBre reopened this Mar 20, 2025
Copy link
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

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

We may want to consider doing a patch release for this change (doesn't have to be today). Especially considering that we probably won't do a release next week

@ntBre
Copy link
Contributor Author

ntBre commented Mar 20, 2025

That sounds like a good idea. I closed and reopened to update the ecosystem report, and it's now showing -2751 violations.

@ntBre ntBre merged commit 42cbce5 into main Mar 20, 2025
43 checks passed
@ntBre ntBre deleted the brent/fix-star-annotations branch March 20, 2025 21:44
dcreager added a commit that referenced this pull request Mar 21, 2025
* main: (26 commits)
  Use the common `OperatorPrecedence` for the parser (#16747)
  [red-knot] Check subtype relation between callable types (#16804)
  [red-knot] Check whether two callable types are equivalent (#16698)
  [red-knot] Ban most `Type::Instance` types in type expressions (#16872)
  Special-case value-expression inference of special form subscriptions (#16877)
  [syntax-errors] Fix star annotation before Python 3.11 (#16878)
  Recognize `SyntaxError:` as an error code for ecosystem checks (#16879)
  [red-knot] add test cases result in false positive errors (#16856)
  Bump 0.11.1 (#16871)
  Allow discovery of venv in VIRTUAL_ENV env variable (#16853)
  Split git pathspecs in change determination onto separate lines (#16869)
  Use the correct base commit for change determination (#16857)
  Separate `BitXorOr` into `BitXor` and `BitOr` precedence (#16844)
  Server: Allow `FixAll` action in presence of version-specific syntax errors (#16848)
  [`refurb`] Fix starred expressions fix (`FURB161`) (#16550)
  [`flake8-executable`] Add pytest and uv run to help message for `shebang-missing-python` (`EXE003`) (#16855)
  Show more precise messages in invalid type expressions (#16850)
  [`flake8-executables`] Allow `uv run` in shebang line for `shebang-missing-python` (`EXE003`) (#16849)
  Add `--exit-non-zero-on-format` (#16009)
  [red-knot] Ban list literals in most contexts in type expressions (#16847)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working preview Related to preview mode features
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Incorrect SyntaxError for * in strings in annotations (e.g. Annotated, Literal)
3 participants