2 回答

TA貢獻1878條經驗 獲得超4個贊
由于您在類上定義字段,實用的方法是對字段進行類型提示。請注意,您必須告訴mypy不要檢查線路本身。
class Person(PersonBase):
age: int = IntField() # type: ignore
這是最小的變化,但相當不靈活。
您可以使用帶有假簽名的輔助函數來創建自動鍵入的通用提示:
from typing import Type, TypeVar
T = TypeVar('T')
class __Field__:
"""The actual field specification"""
def __init__(self, *args, **kwargs):
self.args, self.kwargs = args, kwargs
def Field(tp: Type[T], *args, **kwargs) -> T:
"""Helper to fake the correct return type"""
return __Field__(tp, *args, **kwargs) # type: ignore
class Person:
# Field takes arbitrary arguments
# You can @overload Fields to have them checked as well
age = Field(int, True, object())
這就是attrs庫提供其遺留提示的方式。這種風格允許隱藏注釋的所有魔法/技巧。
由于元類可以檢查注釋,因此無需在 Field 上存儲類型。您可以Field對元數據使用裸,對類型使用注釋:
from typing import Any
class Field(Any): # the (Any) part is only valid in a .pyi file!
"""Field description for Any type"""
class MetaPerson(type):
"""Metaclass that creates default class attributes based on fields"""
def __new__(mcs, name, bases, namespace, **kwds):
for name, value in namespace.copy().items():
if isinstance(value, Field):
# look up type from annotation
field_type = namespace['__annotations__'][name]
namespace[name] = field_type()
return super().__new__(mcs, name, bases, namespace, **kwds)
class Person(metaclass=MetaPerson):
age: int = Field()
這就是attrs提供其 Python 3.6+ 屬性的方式。它既通用又符合注釋風格。請注意,這也可以與常規基類而不是元類一起使用。
class BasePerson:
def __init__(self):
for name, value in type(self).__dict__.items():
if isinstance(value, Field):
field_type = self.__annotations__[name]
setattr(self, name, field_type())
class Person(BasePerson):
age: int = Field()

TA貢獻1111條經驗 獲得超0個贊
Patrick Haugh 是對的,我試圖以錯誤的方式解決這個問題。描述符是要走的路:
from typing import TypeVar, Generic, Optional, ClassVar, Any, Type
FieldValueType = TypeVar('FieldValueType')
class Field(Generic[FieldValueType]):
value_type: Type[FieldValueType]
def __init__(self) -> None:
self.value: FieldValueType = self.value_type()
def __get__(self, obj, objtype) -> 'Field':
print('Retrieving', self.__class__)
return self
def __set__(self, obj, value):
print('Updating', self.__class__)
self.value = value
def to_string(self):
return self.value
class StringField(Field[str]):
value_type = str
class IntField(Field[int]):
value_type = int
def to_string(self):
return str(self.value)
class Person:
age = IntField()
person = Person()
person.age = 25
print(person.age.to_string())
MyPy可以完全理解這一點。謝謝!
添加回答
舉報