Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

NamedTuple class body does not supportsuper() or__class__ #136765

Open
Labels
pendingThe issue will be closed if no feedback is providedstdlibPython modules in the Lib dirtype-bugAn unexpected behavior, bug, or error
@GugelRobin

Description

@GugelRobin

Dear typing community,

I tried to find if this bug was already reported, but didn't find any mentions.
The following code crashes unexpectedly.

Reproduction steps:

fromtypingimportNamedTupleclassFoo(NamedTuple):defbar(self):print(__class__)

Expected behaviour:

ClassFoo gets created successuflly.

Current behaviour:

RuntimeError: __class__ not set defining 'Foo' as <class '__main__.Foo'>. Was __classcell__ propagated to type.__new__?

Since the bug lives inside of thetyping module I submit this bug report here instead of the python github.

As far as I was able to dig into the code it happens because when_make_nmtuple is called (which eventually callstype.__new__(...)) inside ofNamedTupleMeta.__new__ the__classcell__ object isn't passed along.
Which leads to a runtime error as described inthe documentation

Suggested solution:

  • Add"__classcell__" to_special (else__classcell__ will be a attribute of the new NamedTuple).
  • Add the following code beforeif Generic in bases:
if"__classcell__"inns:ns["__classcell__"].cell_contents=nm_tpl### already existing code:ifGenericinbases:nm_tpl.__init_subclass__()returnnm_tpl

Version:

Output ofpython -VV:Python 3.13.3 (main, Apr 8 2025, 13:54:08) [Clang 16.0.0 (clang-1600.0.26.6)]

I suspect though that any version >=3.8 is affected.

I haven't yet ever contributed to open source, if appropriate I can open a PR for this fix though.

Best regards,
Robin

Metadata

Metadata

Assignees

No one assigned

    Labels

    pendingThe issue will be closed if no feedback is providedstdlibPython modules in the Lib dirtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp