Skip to content

Conversation

@asukaminato0721
Copy link
Contributor

Summary

Fixes #2153

Added a pattern-specific class-pattern narrow op that validates the class target and emits the new invalid-pattern error when it isn’t a class object (e.g., Color.RED())

Test Plan

a regression test and docs update.

@meta-cla meta-cla bot added the cla signed label Jan 19, 2026
@asukaminato0721 asukaminato0721 marked this pull request as ready for review January 19, 2026 12:17
Copilot AI review requested due to automatic review settings January 19, 2026 12:17
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes issue #2153 by adding proper validation for class patterns in match statements. Enum members are values that should be matched using value patterns (without parentheses), not class patterns (with parentheses). The fix introduces a new error kind InvalidPattern and a pattern-specific narrow operation IsInstancePattern that validates the class target and emits appropriate errors.

Changes:

  • Added new InvalidPattern error kind to categorize invalid pattern usage
  • Introduced IsInstancePattern narrow operation to validate class patterns specifically
  • Parameterized check_type_is_class_object to accept different error kinds for different contexts

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated no comments.

Show a summary per file
File Description
crates/pyrefly_config/src/error_kind.rs Adds InvalidPattern error kind enum variant
pyrefly/lib/binding/narrow.rs Adds IsInstancePattern variant with display and negation support
pyrefly/lib/binding/pattern.rs Uses IsInstancePattern for class pattern validation instead of IsInstance
pyrefly/lib/alt/narrow.rs Implements validation logic for IsInstancePattern that checks if the pattern target is a valid class object
pyrefly/lib/alt/special_calls.rs Parameterizes check_type_is_class_object with an error_kind parameter to support different error contexts
pyrefly/lib/test/pattern_match.rs Adds regression test for enum class pattern validation
website/docs/error-kinds.mdx Documents the new invalid-pattern error kind with examples

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@github-actions
Copy link

Diff from mypy_primer, showing the effect of this PR on open source code:

kopf (https://github.com/nolar/kopf)
+ ERROR kopf/_cogs/structs/dicts.py:257:14-38: Runtime checkable protocol `Iterable` has an unsafe overlap with type `Never` [unsafe-overlap]
+ ::error file=kopf/_cogs/structs/dicts.py,line=257,col=14,endLine=257,endColumn=38,title=Pyrefly unsafe-overlap::Runtime checkable protocol `Iterable` has an unsafe overlap with type `Never`%0A  Attribute `__iter__` has incompatible types: `Never.__iter__` has type `Never`, which is not consistent with `BoundMethod[Never, (self: Never) -> Iterator[@_]]` in `Iterable.__iter__` (the type of read-write attributes cannot be changed)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error when class patterns are used as enums in pattern match

1 participant