Skip to content

Fix single-member enum literal subset checks - #5264

Closed
swaminimore wants to merge 2 commits into
meta:mainfrom
swaminimore:fix/5191-single-member-enum-literal
Closed

swaminimore wants to merge 2 commits into
meta:mainfrom
swaminimore:fix/5191-single-member-enum-literal

Conversation

@swaminimore

Copy link
Copy Markdown
Contributor

Summary

Fixes #5191

When a union containing a literal of a single-member enum is normalized to the enum class, Pyrefly can incorrectly reject the resulting type against a generic type alias containing that enum literal.

This change updates subset checking to accept the enum class as compatible with its literal member when both refer to the same enum and that enum has exactly one member. Multi-member enums remain subject to the existing compatibility checks.

Regression tests cover the generic type-alias case, direct function arguments, inline enum-literal unions, and compatibility boundaries for multi-member enums.

  • I am a human, and I fully understand the code I am submitting.

Test Plan

  • cargo test -p pyrefly - passed (9,109 tests passed, 3 ignored).
  • cargo fmt --check - passed, with warnings for nightly-only formatting options.
  • git diff --check - passed.
  • python3 test.py --no-test --no-tensor-shapes --no-conformance --no-jsonschema - passed.
  • cargo clippy -p pyrefly --all-targets - completed with one warning in the existing, unrelated pyrefly/benches/micro.rs file.

AI assistance was used during investigation, implementation, and testing. The changes were reviewed and tested locally.

@meta-cla

meta-cla Bot commented Oct 18, 2026

Copy link
Copy Markdown

Hi @swaminimore!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@meta-cla

meta-cla Bot commented Oct 18, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@swaminimore

swaminimore commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor Author

Hi @stroxler, just following up on this PR. The CLA is signed, and the Meta Import Checks have passed. The fix includes regression tests for the single-member enum literal case and the multi-member enum compatibility boundary.

Could you please take a look when you get a chance? I’m happy to address any feedback or make changes if needed. Thank you!

@stroxler stroxler 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.

Thanks, this looks pretty good to me!

There's one issue: the typing spec specifically excludes Flag classes from this behavior:
https://typing.python.org/en/latest/spec/enums.html#enum-literal-expansion

We should probably cover this in a test, and make sure we exclude it in Pyrefly

Also, the next time you submit it should auto-import, which will help me merge this (unfortunately auto-import doesn't run for first-time contributions because the CLA isn't signed, so only the next update triggers it)

@stroxler stroxler 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.

Sorry, meant to request changes for the Flag modification

@github-actions

This comment has been minimized.

@swaminimore

Copy link
Copy Markdown
Contributor Author

The Shape stubs CI check is still failing at Verify shape stubs artifacts. The built JAX wheel includes pyrefly-numpy-stubs==0.0.0, while the verifier expects only jax and pyrefly-shape-extensions==1.4.0.

The upstream JAX pyproject.toml declares the NumPy stubs dependency, and my PR does not modify the packaging or CI files. Could you confirm whether this is a known CI/configuration mismatch?

@stroxler

stroxler commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

I think if you rebase (and fix the concerns above) that problem should go away. There was a bug in my initial setup for jax-pyrefly-stubs but it is fixed on trunk. I'm pretty sure the current PR is just not rebased yet

@rchen152 rchen152 added size/xs and removed size/m labels Nov 2, 2026
@swaminimore
swaminimore force-pushed the fix/5191-single-member-enum-literal branch from dc8517b to e95aa62 Compare November 2, 2026 19:50
@github-actions github-actions Bot added size/xs and removed size/xs labels Nov 2, 2026
@meta-codesync

meta-codesync Bot commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

This pull request has been imported. If you are a Meta employee, you can view this in D129147679. (Because this pull request was imported automatically, there will not be any future comments.)

@swaminimore

Copy link
Copy Markdown
Contributor Author

Hi @stroxler ,

I've updated the PR based on your feedback and rebased it onto the latest upstream/main.

The changes now explicitly exclude Flag and IntFlag subclasses from the single-member enum literal compatibility behavior, with regression tests covering both cases.

The full local validation has passed, including cargo test -p pyrefly, cargo fmt --check, git diff --check, and python3 test.py.

The updated PR has also been successfully imported. Could you please take another look and approve the pending workflows when convenient?

Thank you!

@github-actions

github-actions Bot commented Nov 3, 2026

Copy link
Copy Markdown

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@stroxler stroxler 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.

Review automatically exported from Phabricator review in Meta.

@meta-codesync meta-codesync Bot closed this in e37124c Nov 3, 2026
@meta-codesync meta-codesync Bot added the Merged label Nov 3, 2026
@meta-codesync

meta-codesync Bot commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

This pull request has been merged in e37124c.

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.

Literal[Enum.MEMBER] in a union is normalized to the enum class, then rejected against a generic TypeAlias containing the same literal

3 participants