8000 gh-85795: Add support for `super()` in `NamedTuple` subclasses by bswck · Pull Request #129352 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-85795: Add support for super() in NamedTuple subclasses #129352

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use a sentinel for missing __classcell__
  • Loading branch information
bswck committed Jan 29, 2025
commit d9cdb27ee580d9a65e246b3af2e20bada84ce7a9
6 changes: 4 additions & 2 deletions Lib/collections/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,9 @@ def __ror__(self, other):
except ImportError:
_tuplegetter = lambda index, doc: property(_itemgetter(index), doc=doc)

def _namedtuple(typename, field_names, *, rename=False, defaults=None, module=None, classcell=None):
_nmtuple_classcell_sentinel = object()

def _namedtuple(typename, field_names, *, rename=False, defaults=None, module=None, classcell=_nmtuple_classcell_sentinel):
# Validate the field names. At the user's option, either generate an error
# message or automatically replace the field name with a valid name.
if isinstance(field_names, str):
Expand Down Expand Up @@ -486,7 +488,7 @@ def __getnewargs__(self):
'__match_args__': field_names,
}

if classcell is not None:
if classcell is not _nmtuple_classcell_sentinel:
class_namespace["__classcell__"] = classcell

for index, name in enumerate(field_names):
Expand Down
4 changes: 2 additions & 2 deletions Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2929,7 +2929,7 @@ def __round__(self, ndigits: int = 0) -> T:
pass


def _make_nmtuple(name, fields, annotate_func, module, defaults = (), classcell=None):
def _make_nmtuple(name, fields, annotate_func, module, defaults = (), classcell=collections._nmtuple_classcell_sentinel):
nm_tpl = collections._namedtuple(name, fields, defaults=defaults,
module=module, classcell=classcell)
nm_tpl.__annotate__ = nm_tpl.__new__.__annotate__ = annotate_func
Expand Down Expand Up @@ -3000,7 +3000,7 @@ def annotate(format):
f"{', '.join(default_names)}")
nm_tpl = _make_nmtuple(typename, field_names, annotate,
defaults=[ns[n] for n in default_names], module=ns['__module__'],
classcell=ns.pop('__classcell__', None))
classcell=ns.pop('__classcell__', collections._nmtuple_classcell_sentinel))
nm_tpl.__bases__ = bases
if Generic in bases:
class_getitem = _generic_class_getitem
Expand Down
0