自定义typing.NamedTuple [英] customizing typing.NamedTuple
问题描述
我使用 NamedTuple
来保存数据,并且我想添加一个可以被多个基于 NamedTuple
的类继承的方法.但是当我尝试使用多重继承或子类化基于 NamedTuple
的类时,它不起作用.具体来说,我试图自动为我的所有数据类提供一个方法,该方法可以查看类注释,然后基于该方法调用一些序列化代码.以下是我尝试过的一些示例:
from 输入 import NamedTuple类 Base1:def foo(self):打印(自我.__注释__)类 Test1(NamedTuple,Base1):x: 整数y:整数x = 测试 1(1, 2)x.foo() # 引发 AttributeError类 Base2(NamedTuple):def foo(self):打印(自我.__注释__)类 Test2(Base2):x: 整数y:整数x = Test2(1, 2) # TypeError: __new__() 需要 1 个位置参数,但给出了 3 个
有没有办法像这样使用 NamedTuple
类?
问题在于 typing.NamedTuple
使用的元类;这个元类忽略所有基类,只生成一个带有添加注释信息的collections.namedtuple()
类(复制直接定义在类上的任何附加属性).
您可以定义自己的元类(它必须是 typing.NamedTupleMeta
的子类),在生成命名元组类之后添加额外的基类:>
导入输入类 MultipleInheritanceNamedTupleMeta(typing.NamedTupleMeta):def __new__(mcls, typename, bases, ns):如果在基数中键入.NamedTuple: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)类 Base1(元类=MultipleInheritanceNamedTupleMeta):def foo(self):打印(自我.__注释__)类 Test1(NamedTuple,Base1):x: 整数y:整数
请注意,这不会让您继承字段!那是因为您必须为任何字段组合生成一个新的 namedtuple
类.以上产生了以下结构:
Test1
,继承自_base_Test1
- 实际的typing.NamedTuple
生成的namedtuple
元组
Base1
这按要求工作:
<预><代码>>>>x = 测试 1(1, 2)>>>x.foo(){'x':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 actualtyping.NamedTuple
generatednamedtuple
tuple
Base1
and this works as required:
>>> x = Test1(1, 2)
>>> x.foo()
{'x': <class 'int'>, 'y': <class 'int'>}
这篇关于自定义typing.NamedTuple的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!