Skip to content

Fix descriptor __get__/__set__ access on named tuple attributes - #22213

Open
VedantBhayani wants to merge 1 commit into
python:masterfrom
VedantBhayani:fix/22193-namedtuple-descriptor-get
Open

VedantBhayani wants to merge 1 commit into
python:masterfrom
VedantBhayani:fix/22193-namedtuple-descriptor-get

Conversation

@VedantBhayani

Copy link
Copy Markdown

Named tuples are represented as TupleType, but analyze_descriptor_access() returned early for any non-Instance type. As a result, a __get__ (or __set__) override defined in a NamedTuple class body was ignored when the named tuple was used as a class attribute descriptor: reveal_type(SomeClass.tuple_descr) produced the raw tuple fallback instead of the __get__ return type.

Map TupleType to its fallback instance before the Instance check, matching how regular tuple member access already handles this via tuple_fallback().

Adds testDescriptorGetNamedTuple and testDescriptorSetNamedTuple regression tests covering both the class-attribute and instance-attribute access paths.

Fixes #22193.

Local test runs:

  • pytest mypy/test/testcheck.py — 8319 passed, 16 skipped, 7 xfailed
  • pytest mypy/test/testfinegrained.py — 754 passed, 28 skipped
  • python -m mypy --config-file mypy_self_check.ini -p mypy — Success: no issues found in 196 source files
  • ruff check, ruff format --check, black --check — clean

Note: this contribution was developed with LLM assistance under human review.

Named tuples are represented as TupleType, but analyze_descriptor_access()
returned early for any non-Instance type, so a __get__ or __set__ override
defined on a named tuple class was ignored when the named tuple was used
as a class attribute descriptor.

Map TupleType to its fallback instance before the Instance check, matching
what regular member access already does.

Fixes python#22193.
@github-actions

Copy link
Copy Markdown
Contributor

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

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.

__get__ override not recognized in NamedTuple

1 participant