我一直在尝试针对某些代码库运行pytype
。
该项目使用sqlalchemy,为简化起见,可以说:
/proj/common/model/MyModel.py
:Base = declarative_base()
class MyModel(Base):
__tablename__ = "my_table"
id = Column(Integer, primary_key=True, autoincrement=True)
my_column = Column(String(64), nullable=True)
/proj/services/my_service/MyService.py
:class MyService:
def create_my_model(self, some_value):
my_model = MyModel(my_column=some_value)
self._db_session.add(my_model)
self._db_session.flush()
return my_model.id
def get_by_id(self, id):
return self._db_session.query(MyModel).filter(MyModel.id == id).one()
现在,我正在尝试在该服务上运行pytype
。所以:
cd /proj
pytype services/my_service/
此操作成功完成:
Computing dependencies
WARNING generate default: System module services.my_service.MyService
Analyzing 1 sources with 0 local dependencies
ninja: Entering directory `/proj/.pytype'
[1/1] check services.my_service.MyService
Success: no errors found
太酷了
但是
通过cat /proj/.pytype/pyi/services/my_service/MyService.pyi
检查生成的存根,我看到:
# (generated with --quick)
import __future__
from typing import Any
ALERT_TYPES: Any
ALERT_TYPE_2_SERVICE_NAME: dict
absolute_import: __future__._Feature
orm_object_to_dictionary_recursive: Any
class MyService(Any):
create_my_model: Any
get_by_id: Any
所以我认为这与sqlalchemy
有关,因此我安装了sqlalchemy的存根:pip install -U sqlalchemy-stubs
。
我还删除了.pytype
目录(rm -rf /proj/.pytype),然后重新运行pytype
(pytype my_service/
),只是得到了相同的结果(一切都是Any
。
但是如何告诉pytype
使用这些存根,并为我的服务生成更正确的存根?也就是说,看起来像这样:
class MyService(Any):
create_my_model: int
get_by_id: MyModel