Skip to content

Add NamedTuple __match_args__ attribute#15906

Merged
AlexWaygood merged 1 commit into
python:mainfrom
charliermarsh:charlie/add-namedtuple-match-args
Jun 13, 2026
Merged

Add NamedTuple __match_args__ attribute#15906
AlexWaygood merged 1 commit into
python:mainfrom
charliermarsh:charlie/add-namedtuple-match-args

Conversation

@charliermarsh

Copy link
Copy Markdown
Contributor

Summary

Python 3.10 adds __match_args__ to classes created by NamedTuple.

This adds the class variable to _typeshed._type_checker_internals.NamedTupleFallback and the obsolete typing.NamedTuple compatibility copy.

This was found while implementing __match_args__ support for named tuples in ty: astral-sh/ruff#25934

@github-actions

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@srittau srittau left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM, we can merge this when you undraft it.

@charliermarsh charliermarsh marked this pull request as ready for review June 13, 2026 11:28
@charliermarsh

Copy link
Copy Markdown
Contributor Author

Done, thanks!

@AlexWaygood AlexWaygood merged commit 95aa599 into python:main Jun 13, 2026
58 checks passed
charliermarsh added a commit to astral-sh/ruff that referenced this pull request Jun 13, 2026
## Summary

Python 3.10 generates `__match_args__` on named tuples so their fields
can be used in positional class patterns. We already synthesize other
generated named-tuple attributes, but did not expose `__match_args__`.

```python
from typing import NamedTuple

class Point(NamedTuple):
    x: int
    y: str

reveal_type(Point.__match_args__)  # tuple[Literal["x"], Literal["y"]]
```

The gradual fallback for named tuples with unknown fields comes from
`NamedTupleFallback`, as proposed in
python/typeshed#15906. (I've included a
temporary copy of that typeshed change until approved.)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants