I'm using NamedTuple
s to hold data, and I want to add a method that can be inherited by multiple NamedTuple
based classes. But when I try using multiple inheritance or subclassing NamedTuple
based classes, it doesn't work. Specifically, I'm trying to automatically give all of my data classes a method that can look at the classes annotations and then call some serializing code based on that. Here are some examples of what I've tried:
from typing import NamedTuple
class Base1:
def foo(self):
print(self.__annotations__)
class Test1(NamedTuple, Base1):
x: int
y: int
x = Test1(1, 2)
x.foo() # raises AttributeError
class Base2(NamedTuple):
def foo(self):
print(self.__annotations__)
class Test2(Base2):
x: int
y: int
x = Test2(1, 2) # TypeError: __new__() takes 1 positional argument but 3 were given
is there a way for me to use the NamedTuple
class like this?
At issue is the metaclass used by typing.NamedTuple
; this metaclass ignores all base classes and just generates a collections.namedtuple()
class with added annotation information (copying across any additional attributes directly defined on the class).
You can define your own metaclass (which must be a subclass of typing.NamedTupleMeta
), that adds your additional base classes after generating the named tuple class:
import typing
class MultipleInheritanceNamedTupleMeta(typing.NamedTupleMeta):
def __new__(mcls, typename, bases, ns):
if typing.NamedTuple in bases:
base = super().__new__(mcls, '_base_' + typename, bases, ns)
bases = (base, *(b for b in bases if not isinstance(b, typing.NamedTuple)))
return super(typing.NamedTupleMeta, mcls).__new__(mcls, typename, bases, ns)
class Base1(metaclass=MultipleInheritanceNamedTupleMeta):
def foo(self):
print(self.__annotations__)
class Test1(NamedTuple, Base1):
x: int
y: int
Note that this won't let you inherit fields! That's because you must generate a new namedtuple
class for any combination of fields. The above produces the following structure:
Test1
, inherits from
_base_Test1
- the actual typing.NamedTuple
generated namedtuple
tuple
Base1
and this works as required:
>>> x = Test1(1, 2)
>>> x.foo()
{'x': <class 'int'>, 'y': <class 'int'>}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With