Skip to content

fix Overriding methods on NamedTuple does not work #934#2681

Closed
asukaminato0721 wants to merge 1 commit intofacebook:mainfrom
asukaminato0721:934
Closed

fix Overriding methods on NamedTuple does not work #934#2681
asukaminato0721 wants to merge 1 commit intofacebook:mainfrom
asukaminato0721:934

Conversation

@asukaminato0721
Copy link
Contributor

Summary

Fixes #934

changing iteration on NamedTuple instances to honor an explicit __iter__ override instead of always taking the tuple-element fast path.

overridden __iter__ now gets called directly and its return type is unwrapped for for-loop element inference.

Test Plan

converted the existing regression into a passing test.

@meta-cla meta-cla bot added the cla signed label Mar 5, 2026
@asukaminato0721 asukaminato0721 marked this pull request as ready for review March 6, 2026 00:13
Copilot AI review requested due to automatic review settings March 6, 2026 00:13
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

Fixes pyrefly’s iteration type inference for typing.NamedTuple instances that explicitly override __iter__, ensuring the override is respected instead of always using the tuple-element fast path (fixes #934).

Changes:

  • Add a NamedTuple-specific check to detect non-synthesized __iter__ overrides and use the override’s return type for for-loop element inference.
  • Convert the existing regression testcase from “expected failure” to a passing test.

Reviewed changes

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

File Description
pyrefly/lib/alt/solve.rs Updates iterate() to honor a user-defined NamedTuple.__iter__ override and infer loop element type from it.
pyrefly/lib/test/named_tuple.rs Updates the regression test to assert the correct inferred element type without marking it as a known bug.

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

You can also share your feedback on Copilot code review. Take the survey.

@github-actions
Copy link

github-actions bot commented Mar 6, 2026

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

@yangdanny97 yangdanny97 self-assigned this Mar 6, 2026
@meta-codesync
Copy link

meta-codesync bot commented Mar 6, 2026

@yangdanny97 has imported this pull request. If you are a Meta employee, you can view this in D95486526.

Copy link
Contributor

@fangyi-zhou fangyi-zhou left a comment

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 e1f2ed7 Mar 6, 2026
@meta-codesync
Copy link

meta-codesync bot commented Mar 6, 2026

@yangdanny97 merged this pull request in e1f2ed7.

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.

Overriding methods on NamedTuple does not work

5 participants