fix Overriding methods on NamedTuple does not work #934#2681
fix Overriding methods on NamedTuple does not work #934#2681asukaminato0721 wants to merge 1 commit intofacebook:mainfrom
Conversation
There was a problem hiding this comment.
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 forfor-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.
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
|
@yangdanny97 has imported this pull request. If you are a Meta employee, you can view this in D95486526. |
fangyi-zhou
left a comment
There was a problem hiding this comment.
Review automatically exported from Phabricator review in Meta.
|
@yangdanny97 merged this pull request in e1f2ed7. |
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.