Skip to content

[ruff] Recognize re.prefixmatch (RUF039, RUF055) - #28311

Merged
ntBre merged 1 commit into
astral-sh:mainfrom
gorewilliams:ruf039-ruf055-prefixmatch
Sep 4, 2026
Merged

ntBre merged 1 commit into
astral-sh:mainfrom
gorewilliams:ruf039-ruf055-prefixmatch

Conversation

@gorewilliams

@gorewilliams gorewilliams commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

Summary

Adds support for Python 3.15’s re.prefixmatch to:

  • unraw-re-pattern (RUF039)
  • unnecessary-regular-expression (RUF055)

RUF055 treats re.prefixmatch like re.match, replacing plain string patterns with str.startswith when the result is used in a truth-value context.

Recognition is independent of the configured target version, while the mdtests use Python 3.15 where the API is available.

Closes part of #28206.

Test Plan

  • cargo nextest run -p ruff_mdtest --test mdtest
  • cargo nextest run -p ruff_linter rules::ruff::tests
  • cargo dev generate-all
  • Ran prek on all changed files

@astral-sh-bot
astral-sh-bot Bot requested a review from ntBre September 4, 2026 12:04
@astral-sh-bot

astral-sh-bot Bot commented Sep 4, 2026

Copy link
Copy Markdown

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@gorewilliams
gorewilliams force-pushed the ruf039-ruf055-prefixmatch branch from 33f7635 to 7926d73 Compare September 4, 2026 12:13

@ntBre ntBre left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could you please split off the changes around re.Pattern? That's a much larger change than the 3.15-related changes I had planned, and I also don't think this has been requested previously. Such a large expansion of a rule should go through an issue first.

Comment thread crates/ruff_linter/src/rules/ruff/rules/unnecessary_regular_expression.rs Outdated
Comment thread crates/ruff_linter/resources/test/fixtures/ruff/RUF055_py315.py Outdated
@ntBre ntBre mentioned this pull request Sep 4, 2026
30 tasks
@ntBre ntBre changed the title Add Python 3.15 prefixmatch support to RUF039 and RUF055 [ruff] Recognize re.prefixmatch (RUF039, RUF055) Sep 4, 2026
@ntBre ntBre added rule Implementing or modifying a lint rule preview Related to preview mode features python315 labels Sep 4, 2026
@gorewilliams
gorewilliams force-pushed the ruf039-ruf055-prefixmatch branch from 7926d73 to 18c73f7 Compare September 4, 2026 19:23
@gorewilliams
gorewilliams requested a review from ntBre September 4, 2026 19:30
@gorewilliams

Copy link
Copy Markdown
Contributor Author

Done, back to you for review. Regarding the re.Pattern / re.compile support: there are a few open threads on this (#14680 #14691 #25533). Dropped for now.

Comment thread crates/ruff_linter/src/rules/ruff/rules/unraw_re_pattern.rs Outdated
@gorewilliams
gorewilliams force-pushed the ruf039-ruf055-prefixmatch branch from 18c73f7 to 04a904a Compare September 4, 2026 19:45

@ntBre ntBre left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thank you!

@ntBre
ntBre enabled auto-merge (squash) September 4, 2026 19:51
@ntBre
ntBre merged commit a4ea5a3 into astral-sh:main Sep 4, 2026
48 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

preview Related to preview mode features python315 rule Implementing or modifying a lint rule

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants