This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author rectalogic
Recipients rectalogic
Date 2019-04-03.13:07:29
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1554296850.04.0.71784053328.issue36517@roundup.psfhosted.org>
In-reply-to
Content
Subclassing typing.NamedTuple an inheriting from a mixin class does not work. It does work for collections.namedtuple, and can be worked around by modifying typing.NamedTupleMeta:

>>> import collections
>>> import typing
>>>
>>>
>>> class Mixin:
...     def mixin(self):
...         return "mixin"
...
>>>
>>> class CollectionsNamedTuple(Mixin, collections.namedtuple('CollectionsNamedTuple', [
...     "a",
...     "b",
... ])):
...     pass
...
>>>
>>> class TypingNamedTuple(Mixin, typing.NamedTuple):
...     a: str
...     b: str
...
>>>
>>> class NamedTupleMeta(typing.NamedTupleMeta):
...     def __new__(cls, typename, bases, ns):
...         cls_obj = super().__new__(cls, typename + '_nm_base', bases, ns)
...         bases = bases + (cls_obj,)
...         return type(typename, bases, {})
...
>>>
>>> class FixedTypingNamedTuple(Mixin, metaclass=NamedTupleMeta):
...     a: str
...     b: str
...
>>>
>>> cnt = CollectionsNamedTuple("av", "bv")
>>> tnt = TypingNamedTuple("av", "bv")
>>> ftnt = FixedTypingNamedTuple("av", "bv")
>>>
>>> cnt.mixin()
'mixin'
>>> ftnt.mixin()
'mixin'
>>> tnt.mixin()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'TypingNamedTuple' object has no attribute 'mixin'
History
Date User Action Args
2019-04-03 13:07:30rectalogicsetrecipients: + rectalogic
2019-04-03 13:07:30rectalogicsetmessageid: <1554296850.04.0.71784053328.issue36517@roundup.psfhosted.org>
2019-04-03 13:07:30rectalogiclinkissue36517 messages
2019-04-03 13:07:29rectalogiccreate