python 类中输入继承
我真的很喜欢在 python 中使用类型提示,所以我正在努力增加我的理解。在下面提供的场景中,BASE_CLASS = BASE[ENV]
和 BASE[ENV]
应始终具有相同的类型。那么为什么当Extract
直接继承BASE[ENV]
时类型提示没有被注释。
#app/base.py
from typing import Literal
import request
class Base:
def get_yerp(self): # ...
return self._load_yerp()
def _load_yerp(self) -> Literal['YERP']: ...
class Production(Base):
def _load_yerp(self):
with request(...)
class Development(Base):
def _load_yerp(self):
with open(...)
#app/extract.py
import os
from typing import Dict, Type, Literal
from app import base
ENV: Literal['production', 'development'] = os.getenv('ENV', 'development')
BASE: Dict[str, Type[base.Base]] = {
'production': base.Production,
'development': base.Development
}
BASE_CLASS = BASE[ENV]
class Extract1(BASE_CLASS):
def __init__(self):
super().get_yerp()#type hint=(method) get_yerp: () -> Literal['YERP']
class Extract2(BASE[ENV]):
def __init__(self):
super().get_yerp()#type hint=Any
I really enjoy using the type hints in python, so I'm trying to increase my understanding. In the scenario provided below BASE_CLASS = BASE[ENV]
and BASE[ENV]
should always be of the same type. So why is it when Extract
directly inherits the BASE[ENV]
the type hints arent annotated.
#app/base.py
from typing import Literal
import request
class Base:
def get_yerp(self): # ...
return self._load_yerp()
def _load_yerp(self) -> Literal['YERP']: ...
class Production(Base):
def _load_yerp(self):
with request(...)
class Development(Base):
def _load_yerp(self):
with open(...)
#app/extract.py
import os
from typing import Dict, Type, Literal
from app import base
ENV: Literal['production', 'development'] = os.getenv('ENV', 'development')
BASE: Dict[str, Type[base.Base]] = {
'production': base.Production,
'development': base.Development
}
BASE_CLASS = BASE[ENV]
class Extract1(BASE_CLASS):
def __init__(self):
super().get_yerp()#type hint=(method) get_yerp: () -> Literal['YERP']
class Extract2(BASE[ENV]):
def __init__(self):
super().get_yerp()#type hint=Any
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论