Skip to content

Commit efb9ad2

Browse files
committed
Encapsulate the _classcell parameter
1 parent cc20a2d commit efb9ad2

File tree

2 files changed

+32
-31
lines changed

2 files changed

+32
-31
lines changed

Lib/collections/__init__.py

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -358,30 +358,7 @@ def __ror__(self, other):
358358
except ImportError:
359359
_tuplegetter = lambda index, doc: property(_itemgetter(index), doc=doc)
360360

361-
def namedtuple(typename, field_names, *, rename=False, defaults=None, module=None, _classcell=None):
362-
"""Returns a new subclass of tuple with named fields.
363-
364-
>>> Point = namedtuple('Point', ['x', 'y'])
365-
>>> Point.__doc__ # docstring for the new class
366-
'Point(x, y)'
367-
>>> p = Point(11, y=22) # instantiate with positional args or keywords
368-
>>> p[0] + p[1] # indexable like a plain tuple
369-
33
370-
>>> x, y = p # unpack like a regular tuple
371-
>>> x, y
372-
(11, 22)
373-
>>> p.x + p.y # fields also accessible by name
374-
33
375-
>>> d = p._asdict() # convert to a dictionary
376-
>>> d['x']
377-
11
378-
>>> Point(**d) # convert from a dictionary
379-
Point(x=11, y=22)
380-
>>> p._replace(x=100) # _replace() is like str.replace() but targets named fields
381-
Point(x=100, y=22)
382-
383-
"""
384-
361+
def _namedtuple(typename, field_names, *, rename=False, defaults=None, module=None, classcell=None):
385362
# Validate the field names. At the user's option, either generate an error
386363
# message or automatically replace the field name with a valid name.
387364
if isinstance(field_names, str):
@@ -509,8 +486,8 @@ def __getnewargs__(self):
509486
'__match_args__': field_names,
510487
}
511488

512-
if _classcell is not None:
513-
class_namespace["__classcell__"] = _classcell
489+
if classcell is not None:
490+
class_namespace["__classcell__"] = classcell
514491

515492
for index, name in enumerate(field_names):
516493
doc = _sys.intern(f'Alias for field number {index}')
@@ -536,6 +513,30 @@ def __getnewargs__(self):
536513

537514
return result
538515

516+
def namedtuple(typename, field_names, *, rename=False, defaults=None, module=None):
517+
"""Returns a new subclass of tuple with named fields.
518+
519+
>>> Point = namedtuple('Point', ['x', 'y'])
520+
>>> Point.__doc__ # docstring for the new class
521+
'Point(x, y)'
522+
>>> p = Point(11, y=22) # instantiate with positional args or keywords
523+
>>> p[0] + p[1] # indexable like a plain tuple
524+
33
525+
>>> x, y = p # unpack like a regular tuple
526+
>>> x, y
527+
(11, 22)
528+
>>> p.x + p.y # fields also accessible by name
529+
33
530+
>>> d = p._asdict() # convert to a dictionary
531+
>>> d['x']
532+
11
533+
>>> Point(**d) # convert from a dictionary
534+
Point(x=11, y=22)
535+
>>> p._replace(x=100) # _replace() is like str.replace() but targets named fields
536+
Point(x=100, y=22)
537+
538+
"""
539+
return _namedtuple(typename, field_names, rename=rename, defaults=defaults, module=module)
539540

540541
########################################################################
541542
### Counter

Lib/typing.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2929,9 +2929,9 @@ def __round__(self, ndigits: int = 0) -> T:
29292929
pass
29302930

29312931

2932-
def _make_nmtuple(name, fields, annotate_func, module, defaults = (), _classcell=None):
2933-
nm_tpl = collections.namedtuple(name, fields, defaults=defaults,
2934-
module=module, _classcell=_classcell)
2932+
def _make_nmtuple(name, fields, annotate_func, module, defaults = (), classcell=None):
2933+
nm_tpl = collections._namedtuple(name, fields, defaults=defaults,
2934+
module=module, classcell=classcell)
29352935
nm_tpl.__annotate__ = nm_tpl.__new__.__annotate__ = annotate_func
29362936
return nm_tpl
29372937

@@ -2999,8 +2999,8 @@ def annotate(format):
29992999
f"{'s' if len(default_names) > 1 else ''} "
30003000
f"{', '.join(default_names)}")
30013001
nm_tpl = _make_nmtuple(typename, field_names, annotate,
3002-
defaults=[ns[n] for n in default_names],
3003-
module=ns['__module__'], _classcell=ns.pop("__classcell__", None))
3002+
defaults=[ns[n] for n in default_names], module=ns['__module__'],
3003+
classcell=ns.pop('__classcell__', None))
30043004
nm_tpl.__bases__ = bases
30053005
if Generic in bases:
30063006
class_getitem = _generic_class_getitem

0 commit comments

Comments
 (0)