Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
baca675
Working draft without _source
rhettinger Sep 8, 2017
3a2aa41
Re-use itemgetter() instances
rhettinger Sep 8, 2017
e24ef4b
Speed-up calls to __new__() with a pre-bound tuple.__new__()
rhettinger Sep 8, 2017
49aa967
Add note regarding string interning
rhettinger Sep 8, 2017
c34b444
Remove unnecessary create function wrappers
rhettinger Sep 8, 2017
0c7f163
Minor sync-ups with PR-2736. Mostly formatting and f-strings
rhettinger Sep 8, 2017
5de0b7f
Bring-in qualname/__module fix-ups from PR-2736
rhettinger Sep 8, 2017
ca643f4
Formally remove the verbose flag and _source attribute
rhettinger Sep 8, 2017
e18b92e
Restore a test of potentially problematic field names
rhettinger Sep 8, 2017
0851fc7
Restore kwonly_args test but without the verbose option
rhettinger Sep 8, 2017
3ac1151
Adopt Inada's idea to reuse the docstrings for the itemgetters
rhettinger Sep 8, 2017
b31f063
Neaten-up a bit
rhettinger Sep 8, 2017
deb30a2
Add news blurb
rhettinger Sep 8, 2017
f402cb3
Merge branch 'master' of github.com:python/cpython into optimize-name…
rhettinger Sep 9, 2017
86cef9e
Serhiy pointed-out the need for interning
rhettinger Sep 9, 2017
83b5e93
Jelle noticed as missing f on an f-string
rhettinger Sep 9, 2017
0168317
Add whatsnew entry for feature removal
rhettinger Sep 9, 2017
dadafc0
Accede to request for dict literals instead keyword arguments
rhettinger Sep 9, 2017
483d08c
Leave the method.__module__ attribute pointing the actual location of…
rhettinger Sep 9, 2017
bc6852c
Improve variable names and add a micro-optimization for an non-public…
rhettinger Sep 10, 2017
c897674
Simplify by in-lining reuse_itemgetter()
rhettinger Sep 10, 2017
bd4ea4e
Arrange steps in more logical order
rhettinger Sep 10, 2017
ffb78c9
Save docstring in local cache instead of interning
rhettinger Sep 10, 2017
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
Formally remove the verbose flag and _source attribute
  • Loading branch information
rhettinger committed Sep 8, 2017
commit ca643f4acb542c64cb6aae58ce06b5d4e11f6e7c
18 changes: 4 additions & 14 deletions Doc/library/collections.rst
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,7 @@ Named tuples assign meaning to each position in a tuple and allow for more reada
self-documenting code. They can be used wherever regular tuples are used, and
they add the ability to access fields by name instead of position index.

.. function:: namedtuple(typename, field_names, *, verbose=False, rename=False, module=None)
.. function:: namedtuple(typename, field_names, *, rename=False, module=None)

Returns a new tuple subclass named *typename*. The new subclass is used to
create tuple-like objects that have fields accessible by attribute lookup as
Expand All @@ -786,10 +786,6 @@ they add the ability to access fields by name instead of position index.
converted to ``['abc', '_1', 'ghi', '_3']``, eliminating the keyword
``def`` and the duplicate fieldname ``abc``.

If *verbose* is true, the class definition is printed after it is
built. This option is outdated; instead, it is simpler to print the
:attr:`_source` attribute.

If *module* is defined, the ``__module__`` attribute of the named tuple is
set to that value.

Expand All @@ -806,6 +802,9 @@ they add the ability to access fields by name instead of position index.
.. versionchanged:: 3.6
Added the *module* parameter.

.. versionchanged:: 3.7
Remove the *verbose* parameter and the :attr:`_source` attribute.

.. doctest::
:options: +NORMALIZE_WHITESPACE

Expand Down Expand Up @@ -878,15 +877,6 @@ field names, the method and attribute names start with an underscore.
>>> for partnum, record in inventory.items():
... inventory[partnum] = record._replace(price=newprices[partnum], timestamp=time.now())

.. attribute:: somenamedtuple._source

A string with the pure Python source code used to create the named
tuple class. The source makes the named tuple self-documenting.
It can be printed, executed using :func:`exec`, or saved to a file
and imported.

.. versionadded:: 3.3

.. attribute:: somenamedtuple._fields

Tuple of strings listing the field names. Useful for introspection
Expand Down
5 changes: 3 additions & 2 deletions Lib/collections/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ def __eq__(self, other):

_nt_itemgetters = {}

def namedtuple(typename, field_names, *, verbose=False, rename=False, module=None):
def namedtuple(typename, field_names, *, rename=False, module=None):
"""Returns a new subclass of tuple with named fields.

>>> Point = namedtuple('Point', ['x', 'y'])
Expand Down Expand Up @@ -385,7 +385,8 @@ def _make(cls, iterable):
raise TypeError(f'Expected {num_fields} arguments, got {len(result)}')
return result

_make.__doc__ = f'Make a new {typename} object from a sequence or iterable'
_make.__func__.__doc__ = (f'Make a new {typename} object from a sequence '
'or iterable')

def _replace(_self, **kwds):
result = _self._make(map(kwds.pop, field_names, _self))
Expand Down
4 changes: 1 addition & 3 deletions Lib/test/test_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,6 @@ def test_factory(self):
self.assertEqual(Point.__module__, __name__)
self.assertEqual(Point.__getitem__, tuple.__getitem__)
self.assertEqual(Point._fields, ('x', 'y'))
#self.assertIn('class Point(tuple)', Point._source)

self.assertRaises(ValueError, namedtuple, 'abc%', 'efg ghi') # type has non-alpha char
self.assertRaises(ValueError, namedtuple, 'class', 'efg ghi') # type has keyword
Expand Down Expand Up @@ -367,8 +366,7 @@ def test_name_conflicts(self):
self.assertEqual(newt, (10,20,30,40,50))

def test_repr(self):
with support.captured_stdout() as template:
A = namedtuple('A', 'x', verbose=True)
A = namedtuple('A', 'x')
self.assertEqual(repr(A(1)), 'A(x=1)')
# repr should show the name of the subclass
class B(A):
Expand Down