如何解决Python类型提示-具有类的提示返回类型而不是给定类的实例
在这种情况下,我有两种方便的方法可以从入口点查找和初始化类,并且想将它们的作用区分开来。
以下是我的问题的简化版本
class ToBeExtended:
""" Abstract base class that is to be extended """
pass
def find(classname: str) -> ToBeExtended:
""" Find the class definition of the class named `classname` from the entry points """
... # Get the class
return ClassDefinition
def connect(classname: str,*args,**kwargs) -> ToBeExtended:
""" Find and initialise the class with the passed arguments and return """
return find(classname)(*args,**kwargs)
在此示例中, find
返回一个类对象,而不是实例。
有没有办法将其包装起来,以便将上下文提供给linter / user? typing
中似乎没有任何内容,我想拥有该类可识别的,没什么像-> type:
解决方法
import typing
class ToBeExtended:
""" Abstract base class that is to be extended """
pass
def find(classname: str) -> typing.Type[ToBeExtended]:
""" Find the class definition of the class named `classname` from the entry points """
... # Get the class
return ClassDefinition
def connect(classname: str,*args,**kwargs) -> ToBeExtended:
""" Find and initialise the class with the passed arguments and return """
return find(classname)(*args,**kwargs)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。