[TEST] Support adjacent __new__.__defaults__ for functional namedtuples (#2882)#2882
[TEST] Support adjacent __new__.__defaults__ for functional namedtuples (#2882)#2882migeed-z wants to merge 1 commit intofacebook:mainfrom
Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
…ebook#2882) Summary: Support the canonical `P.__new__.__defaults__ = (val,)` idiom adjacent to functional namedtuple definitions, making trailing fields optional in the constructor. This is the standard pre-class-syntax way to add defaults to namedtuples: ```python Point = namedtuple("Point", ["x", "y", "z"]) Point.__new__.__defaults__ = (0,) # makes z optional p = Point(1, 2) # valid — z defaults to 0 ``` Works for both `collections.namedtuple` and `typing.NamedTuple` functional forms. Detection uses forward peek in `stmts()`, only the immediately adjacent statement is checked, matching Pyright's behavior. - Only tuple/None literal RHS is consumed; non-literal RHS flows through normal type checking - Right-aligned defaults following Python semantics (N defaults → last N fields optional) - `__new__.__defaults__` replaces any existing `defaults=` kwarg, matching runtime behavior Fixes facebook#2611 Differential Revision: D97825073
61cc85c to
a9928a1
Compare
|
Diff from mypy_primer, showing the effect of this PR on open source code: cloud-init (https://github.com/canonical/cloud-init)
- ERROR tests/unittests/sources/test_smartos.py:818:12-43: `in` is not supported between `Literal['payload']` and `None` [not-iterable]
- ERROR tests/unittests/sources/test_smartos.py:820:17-46: `None` is not subscriptable [unsupported-operation]
- ERROR tests/unittests/sources/test_smartos.py:824:45-827:14: No matching overload found for function `str.format` called with arguments: (payloadstr=Literal[''] | Unknown, **Unknown | None) [no-matching-overload]
- ERROR tests/unittests/sources/test_smartos.py:836:16-44: `None` is not subscriptable [unsupported-operation]
- ERROR tests/unittests/sources/test_smartos.py:842:5-20: Object of class `NoneType` has no attribute `read` [missing-attribute]
|
Primer Diff Classification✅ 1 improvement(s) | 1 project(s) total | -5 errors 1 improvement(s) across cloud-init.
Detailed analysis✅ Improvement (1)cloud-init (-5)
Was this helpful? React with 👍 or 👎 Classification by primer-classifier (1 LLM) |
Summary:
Support the canonical
P.__new__.__defaults__ = (val,)idiom adjacent to functional namedtuple definitions, making trailing fields optional in the constructor.This is the standard pre-class-syntax way to add defaults to namedtuples:
Works for both
collections.namedtupleandtyping.NamedTuplefunctional forms. Detection uses forward peek instmts(), only the immediately adjacent statement is checked, matching Pyright's behavior.__new__.__defaults__replaces any existingdefaults=kwarg, matching runtime behaviorFixes #2611
Differential Revision: D97825073