[英]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=[""])
由於 PEP560和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=[''])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.