![](/img/trans.png)
[英]How to make a custom annotation that the type checker will recognize as str?
[英]How do I fix type checker error of imported literal annotation?
我有一个 class
class Literals:
runtime_type = Literal["offline", "online"]
包含可重用的类型提示文字,可以在代码中的多个位置使用。
然后,我导入Literals
并像这样使用它:
class FooBar:
def __init__(runtime_type: Literals.runtime_type) -> None:
self.runtime_type = runtime_type
当我运行 pyre 时,出现以下错误:
Undefined or invalid type [11]: Annotation `Literals.runtime_type` is not defined as a type.
有没有办法解决它? 如何让 pyre 明白Literals.runtime_type
确实是Literal["offline", "online"]
?
我用mypy
进行了测试,这是可行的,我将self
添加到__init__
:
class Literals:
runtime_type = Literal["offline", "online"]
class FooBar:
def __init__(self, runtime_type: Literals.runtime_type) -> None:
self.runtime_type = runtime_type
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.