Python 3.6 中的通用 NamedTuple
Generic NamedTuple in Python 3.6
我正在尝试创建 NamedTuple 的通用版本,如下所示:
T1 = TypeVar("T1")
T2 = TypeVar("T2")
class Group(NamedTuple, Generic[T1, T2]):
key: T1
group: List[T2]
g = Group(1, [""]) # expecting type to be Group[int, str]
但是,我收到以下错误:
TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases
我不确定还有什么方法可以实现我在这里尝试做的事情,或者这在某种程度上是否是输入机制中的错误。
所以这是一个元类冲突,因为在 python 3.6 中,输入 NamedTuple
和 Generic
使用不同的元类(typing.NamedTupleMeta
和 typing.GenericMeta
),这python 处理不了。恐怕没有解决办法,除了从 tuple
子类化并手动初始化值:
T1 = TypeVar("T1")
T2 = TypeVar("T2")
class Group(tuple, Generic[T1, T2]):
key: T1
group: List[T2]
def __new__(cls, key: T1, group: List[T2]):
self = tuple.__new__(cls, (key, group))
self.key = key
self.group = group
return self
def __repr__(self) -> str:
return f'Group(key={self.key}, group={self.group})'
Group(1, [""]) # --> Group(key=1, group=[""])
由于 PEP 560 and 563,此问题已在 python 3.7:
中修复
Python 3.7.0b2 (v3.7.0b2:b0ef5c979b, Feb 28 2018, 02:24:20) [MSC v.1912 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from __future__ import annotations
>>> from typing import *
>>> T1 = TypeVar("T1")
>>> T2 = TypeVar("T2")
>>> class Group(NamedTuple, Generic[T1, T2]):
... key: T1
... group: List[T2]
...
>>> g: Group[int, str] = Group(1, [""])
>>> g
Group(key=1, group=[''])
当然,在 python 3.7 中,您可以只使用一个数据类,它的轻量级较低(且可变)但具有类似的目的。
from dataclasses import dataclass, astuple
from typing import Generic, TypeVar, List
T1 = TypeVar('T1')
T2 = TypeVar('T2')
@dataclass
class Group(Generic[T1, T2]):
# this stores the data like a tuple, but isn't required
__slots__ = ("key", "group")
key: T1
group: List[T2]
# if you want to be able to unpack like a tuple...
def __iter__(self):
yield from astuple(self)
g: Group[int, str] = Group(1, ['hello', 'world'])
k, v = g
print(g)
类型检查器在 python 3.7 中如何处理我/你的解决方案,尽管我还没有检查过。我怀疑它可能不是无缝的。
编辑
我找到了另一个解决方案 -- 创建一个新的元类
import typing
from typing import *
class NamedTupleGenericMeta(typing.NamedTupleMeta, typing.GenericMeta):
pass
class Group(NamedTuple, Generic[T1,T2], metaclass=NamedTupleGenericMeta):
key: T1
group: List[T2]
Group(1, ['']) # --> Group(key=1, group=[''])
我正在尝试创建 NamedTuple 的通用版本,如下所示:
T1 = TypeVar("T1")
T2 = TypeVar("T2")
class Group(NamedTuple, Generic[T1, T2]):
key: T1
group: List[T2]
g = Group(1, [""]) # expecting type to be Group[int, str]
但是,我收到以下错误:
TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases
我不确定还有什么方法可以实现我在这里尝试做的事情,或者这在某种程度上是否是输入机制中的错误。
所以这是一个元类冲突,因为在 python 3.6 中,输入 NamedTuple
和 Generic
使用不同的元类(typing.NamedTupleMeta
和 typing.GenericMeta
),这python 处理不了。恐怕没有解决办法,除了从 tuple
子类化并手动初始化值:
T1 = TypeVar("T1")
T2 = TypeVar("T2")
class Group(tuple, Generic[T1, T2]):
key: T1
group: List[T2]
def __new__(cls, key: T1, group: List[T2]):
self = tuple.__new__(cls, (key, group))
self.key = key
self.group = group
return self
def __repr__(self) -> str:
return f'Group(key={self.key}, group={self.group})'
Group(1, [""]) # --> Group(key=1, group=[""])
由于 PEP 560 and 563,此问题已在 python 3.7:
中修复Python 3.7.0b2 (v3.7.0b2:b0ef5c979b, Feb 28 2018, 02:24:20) [MSC v.1912 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from __future__ import annotations
>>> from typing import *
>>> T1 = TypeVar("T1")
>>> T2 = TypeVar("T2")
>>> class Group(NamedTuple, Generic[T1, T2]):
... key: T1
... group: List[T2]
...
>>> g: Group[int, str] = Group(1, [""])
>>> g
Group(key=1, group=[''])
当然,在 python 3.7 中,您可以只使用一个数据类,它的轻量级较低(且可变)但具有类似的目的。
from dataclasses import dataclass, astuple
from typing import Generic, TypeVar, List
T1 = TypeVar('T1')
T2 = TypeVar('T2')
@dataclass
class Group(Generic[T1, T2]):
# this stores the data like a tuple, but isn't required
__slots__ = ("key", "group")
key: T1
group: List[T2]
# if you want to be able to unpack like a tuple...
def __iter__(self):
yield from astuple(self)
g: Group[int, str] = Group(1, ['hello', 'world'])
k, v = g
print(g)
类型检查器在 python 3.7 中如何处理我/你的解决方案,尽管我还没有检查过。我怀疑它可能不是无缝的。
编辑
我找到了另一个解决方案 -- 创建一个新的元类
import typing
from typing import *
class NamedTupleGenericMeta(typing.NamedTupleMeta, typing.GenericMeta):
pass
class Group(NamedTuple, Generic[T1,T2], metaclass=NamedTupleGenericMeta):
key: T1
group: List[T2]
Group(1, ['']) # --> Group(key=1, group=[''])