python rtype 文档字符串/类工厂/选择器的重组文本 [英] python rtype docstring/restructured text for class factories/selectors

查看:46
本文介绍了python rtype 文档字符串/类工厂/选择器的重组文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

:rtype: 指定这是返回对象的类型.

:rtype: specifies that this is the type of the returned object.

因此,当我在以下代码段中创建对象 obj 时,我收到来自 IDE 的警告,指出 cls 不可调用,因为 IDE 期望 clsSomeAbstractClass 类型的 object,我想要 SomeAbstractClass 本身.

Therefore, when I create the object obj in the following snippet I receive a warning from the IDE that cls is not callable, since the IDE expects that cls is object of type SomeAbstractClass, and I want SomeAbstractClass itself.

IDE 是正确的,因为这是默认行为.但是如何指定我返回的是一个类,而不是一个类的实例?

The IDE is right, since this is the default behaviour. But how can I specify that I am returning a class, not an instance of a class?

指定 type 而不是 SomeAbstractClass 有一点帮助,但这不是解决方案,因为没有进一步的内省可用.

Specifying type instead of SomeAbstractClass helps a bit, but that's not a solution, since no further introspection is available.

def class_selector(data):
    """
    :rtype: SomeAbstractClass
    :return: Return some class based on given parameters
    """
    
    return get_from.get(data.name)
cls = class_selector(data)
obj = cls(data.more_data)

与此同时,我通过在对象创建后添加 """:type: SomeAbstractClass""" 解决了这个问题,但这并没有取消警告,这是一个肮脏的解决方案.

Meanwhile I have solved this by adding """:type: SomeAbstractClass""" after object creating, but this does not cancel the warning and it's a dirty solution.

顺便说一下,我说的是 Python 2.x.

By the way, I am talking about Python 2.x.

推荐答案

至于2020,唯一能正常工作的解决方案——使用python3注解:

As for 2020, the only solution that is working properly - using python3 annotations:

一般来说,2020 年完全可以接受

Which in general is totally acceptable for 2020

附上屏幕截图以演示正确的行为.基本上,def factory(name) ->type[SomeAbstractClass]:def factory(name) ->type[SomeAbstractClass]":(对于无法导入名称的情况)完全正常.

Attaching a screenshot to demonstrate the correct behavior. Basically, def factory(name) -> type[SomeAbstractClass]: or def factory(name) -> "type[SomeAbstractClass]": (for cases when can't import the name) works totally fine.

仍然无法为较旧的 Python 版本提出解决方案(即避免使用注释).但这不是 2020 年的交易破坏者

Still could not come up with a solution for older Python versions (i.e avoiding the use of annotations). But it's not a deal-breaker in 2020

和文本相同的代码,如果有人想复制粘贴来测试替代方法

And same code as text, if anyone wants to copy-paste to test alternative approaches

class SomeAbstractClass:
    property_by_class = 1
    def __init__(self):
        self.property_by_object = 1


def factory(name) -> type[SomeAbstractClass]:
    hide_it = dict()
    class ActualClassImpl(SomeAbstractClass):
        pass
    hide_it.__setitem__(name, ActualClassImpl)
    return hide_it.__getitem__(name)

if __name__ == '__main__':
    a = factory('testclass')
    by_class_should_be_ok = a.property_by_class
    by_object_should_hint_error = a.property_by_object
    just_like_none_existing_property_hints = a.property_by_object_bad


这篇关于python rtype 文档字符串/类工厂/选择器的重组文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆