Skip to content

Conversation

@ntBre
Copy link
Contributor

@ntBre ntBre commented Nov 10, 2025

Summary

Fixes #21360 by using the union of names instead of overwriting them, as Micha suggested originally on #21104.

This avoids overwriting the n name in the Subscript by the empty set of names visited in the nested OR pattern before visiting the other arm of the outer OR pattern.

Test Plan

A new inline test case taken from the issue

Summary
--

Fixes #21360 by using the union of names instead of overwriting them, as Micha
suggested originally on #21104.

This avoids overwriting the `n` name in the `Subscript` by the empty set of
names visited in the nested OR pattern before visiting the other arm of the
outer OR pattern.

Test Plan
--

A new inline test case taken from the issue
@ntBre ntBre added the bug Something isn't working label Nov 10, 2025
@astral-sh-bot
Copy link

astral-sh-bot bot commented Nov 10, 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 November 10, 2025 13:39
break;
}
self.names = visitor.names;
self.names.extend(visitor.names);
Copy link
Member

Choose a reason for hiding this comment

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

Can you say a bit more about the fix. My understanding (and why I recommended it) was that using union or intersection would only matter for the error recovery case. But it seems that using union over intersection is important for valid cases too or are we only fixing a symptom here?

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 think the union is correct because there can be existing patterns in self.names before visiting nested alternative patterns. We need to compare all of the names we visit in that arm to all of the names we visit in the next arm, not just the set of names visited in the last, innermost alternative pattern.

That's what's happening in this case because we first visit the outer class pattern, which captures n, then we were overwriting that n with the empty set when visiting the Constant | Slice part of the pattern. Instead we need the union of n and the empty set to compare to the Attribute(n) branch.

match 42:
    case ast.Subscript(n, ast.Constant() | ast.Slice()) 
       | ast.Attribute(n): ...

@ntBre ntBre merged commit 835e31b into main Nov 10, 2025
39 checks passed
@ntBre ntBre deleted the brent/alt-patterns-2 branch November 10, 2025 15:51
dcreager added a commit that referenced this pull request Nov 11, 2025
* origin/main: (38 commits)
  [ty] Make implicit submodule imports only occur in global scope (#21370)
  [ty] introduce local variables for `from` imports of submodules in `__init__.py(i)` (#21173)
  [`ruff`] Ignore `str()` when not used for simple conversion (`RUF065`) (#21330)
  [ty] implement `typing.NewType` by adding `Type::NewTypeInstance`
  [ty] supress inlay hints for `+1` and `-1` (#21368)
  [ty] Use type context for inference of generic constructors (#20933)
  [ty] Improve generic call expression inference (#21210)
  [ty] supress some trivial expr inlay hints (#21367)
  [`configuration`] Fix unclear error messages for line-length values exceeding `u16::MAX` (#21329)
  [ty] Fix incorrect inference of `enum.auto()` for enums with non-`int` mixins, and imprecise inference of `enum.auto()` for single-member enums (#20541)
  [`refurb`] Detect empty f-strings (`FURB105`) (#21348)
  [ty] provide `import` completion when in `from <name> <name>` statement (#21291)
  [ty] elide redundant inlay hints for function args (#21365)
  Fix syntax error false positive on alternative `match` patterns (#21362)
  Add a new "Opening a PR" section to the contribution guide (#21298)
  [`flake8-simplify`] Fix SIM222 false positive for `tuple(generator) or None` (`SIM222`) (#21187)
  Rebuild ruff binary instead of sharing it across jobs (#21361)
  [ty] Fix `--exclude` and `src.exclude` merging (#21341)
  [ty] Add support for properties that return `Self` (#21335)
  Add upstream linter URL to `ruff linter --output-format=json` (#21316)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

False positive: invalid-syntax alternative patterns bind different names

3 participants