Message339390
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' |
|
Date |
User |
Action |
Args |
2019-04-03 13:07:30 | rectalogic | set | recipients:
+ rectalogic |
2019-04-03 13:07:30 | rectalogic | set | messageid: <1554296850.04.0.71784053328.issue36517@roundup.psfhosted.org> |
2019-04-03 13:07:30 | rectalogic | link | issue36517 messages |
2019-04-03 13:07:29 | rectalogic | create | |
|