来自 Python 类元信息的 __init__ 函数的类型提示

sea*_*ers 6 python sqlalchemy type-hinting pycharm

我想做的是复制SQLAlchemy它的DeclarativeMeta类。有了这个代码,

from sqlalchemy import Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()


class Person(Base):
    __tablename__ = 'person'
    id = Column(Integer, primary_key=True)

    name = Column(String)
    age = Column(Integer)
Run Code Online (Sandbox Code Playgroud)

当你去创造一个人PyCharmPerson(...你得打字提示有关id: int, name: str, age: int

使用 Python、PyCharm 和 SQLAlchemy 进行类型提示

它在运行时的工作方式是通过 SQLAlchemy 的_declarative_constructor函数,

def _declarative_constructor(self, **kwargs):
    cls_ = type(self)
    for k in kwargs:
        if not hasattr(cls_, k):
            raise TypeError(
                "%r is an invalid keyword argument for %s" %
                (k, cls_.__name__))
        setattr(self, k, kwargs[k])
_declarative_constructor.__name__ = '__init__'
Run Code Online (Sandbox Code Playgroud)

为了获得非常好的类型提示(如果你的类有一个 id 字段,Column(Integer)你的构造函数将它类型提示为id: int),PyCharm实际上是在做一些底层的魔法,特定于 SQLAlchemy,但我不需要它为了做到这一点,我只想能够从类的元信息中以编程方式添加类型提示。

所以,简而言之,如果我有一个类,

class Simple:
    id: int = 0

    name: str = ''
    age: int = 0
Run Code Online (Sandbox Code Playgroud)

我希望能够像上面一样初始化类Simple(id=1, name='asdf'),但同时也获得类型提示。我可以得到一半(功能),但不能得到类型提示。

如果我像 SQLAlchemy 那样进行设置,

class SimpleMeta(type):
    def __init__(cls, classname, bases, dict_):
        type.__init__(cls, classname, bases, dict_)


metaclass = SimpleMeta(
    'Meta', (object,), dict(__init__=_declarative_constructor))


class Simple(metaclass):
    id: int = 0

    name: str = ''
    age: int = 0


print('cls', typing.get_type_hints(Simple))
print('init before', typing.get_type_hints(Simple.__init__))
Simple.__init__.__annotations__.update(Simple.__annotations__)
print('init after ', typing.get_type_hints(Simple.__init__))
s = Simple(id=1, name='asdf')
print(s.id, s.name)
Run Code Online (Sandbox Code Playgroud)

有效,但我没有得到任何类型提示,

没有 __init__ 类型提示

如果我确实传递参数,我实际上会收到Unexpected Argument警告,

意外的争论

在代码中,我手动更新了__annotations__,这使得get_type_hints返回正确,

cls {'id': <class 'int'>, 'name': <class 'str'>, 'age': <class 'int'>}
init before {}
init after  {'id': <class 'int'>, 'name': <class 'str'>, 'age': <class 'int'>}
1 asdf
Run Code Online (Sandbox Code Playgroud)

Jac*_* Ng 1

从上面的 python 3.7 开始,您可以通过使用@dataclass并向实例字段添加适当的 typehint 来达到相同的效果。

https://docs.python.org/3/library/dataclasses.html

打字提示截图