Skip to content

Instantly share code, notes, and snippets.

@ties
Last active February 29, 2016 11:10
Show Gist options
  • Save ties/abb27bca6f82b792a07a to your computer and use it in GitHub Desktop.
Save ties/abb27bca6f82b792a07a to your computer and use it in GitHub Desktop.

I ran into cpython issue #24931.

The _asdict() function of my namedtuple subclasses returned empty
dictionaries. My subclass implicitly created an instance dictionary (c.f. empty __slots__). This broke the Python 3.4 version of _asdict.

When you see the source of the namedtuple that is implemented it becomes clear why this happens.

# Python 3.4
In [2]: collections.namedtuple('A', ['a', 'b', 'c'], verbose=True) [1/23]
from builtins import property as _property, tuple as _tuple
from operator import itemgetter as _itemgetter
from collections import OrderedDict
class A(tuple):
'A(a, b, c)'
__slots__ = ()
_fields = ('a', 'b', 'c')
def __new__(_cls, a, b, c):
'Create new instance of A(a, b, c)'
return _tuple.__new__(_cls, (a, b, c))
@classmethod
def _make(cls, iterable, new=tuple.__new__, len=len):
'Make a new A object from a sequence or iterable'
result = new(cls, iterable)
if len(result) != 3:
raise TypeError('Expected 3 arguments, got %d' % len(result))
return result
def _replace(_self, **kwds):
'Return a new A object replacing specified fields with new values'
result = _self._make(map(kwds.pop, ('a', 'b', 'c'), _self))
if kwds:
raise ValueError('Got unexpected field names: %r' % list(kwds))
return result
def __repr__(self):
'Return a nicely formatted representation string'
return self.__class__.__name__ + '(a=%r, b=%r, c=%r)' % self
@property
def __dict__(self):
'A new OrderedDict mapping field names to their values'
return OrderedDict(zip(self._fields, self))
def _asdict(self):
'Return a new OrderedDict which maps field names to their values.'
return self.__dict__
def __getnewargs__(self):
'Return self as a plain tuple. Used by copy and pickle.'
return tuple(self)
def __getstate__(self):
'Exclude the OrderedDict from pickling'
return None
a = _property(_itemgetter(0), doc='Alias for field number 0')
b = _property(_itemgetter(1), doc='Alias for field number 1')
c = _property(_itemgetter(2), doc='Alias for field number 2')
Out[2]: __main__.A
# Python 3.5
In [2]: collections.namedtuple('A', ['a', 'b', 'c'], verbose=True)
from builtins import property as _property, tuple as _tuple
from operator import itemgetter as _itemgetter
from collections import OrderedDict
class A(tuple):
'A(a, b, c)'
__slots__ = ()
_fields = ('a', 'b', 'c')
def __new__(_cls, a, b, c):
'Create new instance of A(a, b, c)'
return _tuple.__new__(_cls, (a, b, c))
@classmethod
def _make(cls, iterable, new=tuple.__new__, len=len):
'Make a new A object from a sequence or iterable'
result = new(cls, iterable)
if len(result) != 3:
raise TypeError('Expected 3 arguments, got %d' % len(result))
return result
def _replace(_self, **kwds):
'Return a new A object replacing specified fields with new values'
result = _self._make(map(kwds.pop, ('a', 'b', 'c'), _self))
if kwds:
raise ValueError('Got unexpected field names: %r' % list(kwds))
return result
def __repr__(self):
'Return a nicely formatted representation string'
return self.__class__.__name__ + '(a=%r, b=%r, c=%r)' % self
def _asdict(self):
'Return a new OrderedDict which maps field names to their values.'
return OrderedDict(zip(self._fields, self))
def __getnewargs__(self):
'Return self as a plain tuple. Used by copy and pickle.'
return tuple(self)
a = _property(_itemgetter(0), doc='Alias for field number 0')
b = _property(_itemgetter(1), doc='Alias for field number 1')
c = _property(_itemgetter(2), doc='Alias for field number 2')
Out[2]: __main__.A
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment