创建类型化装饰器作为类方法并 importing/using 在另一个文件中
Creating a typed decorator as classmethod and importing/using it in another file
我正在尝试向我的项目中添加类型。我几乎复制了 decorator factories.
的模式
这是链接文档中提供的示例:
from typing import Any, Callable, TypeVar
F = TypeVar("F", bound=Callable[..., Any])
def route(url: str) -> Callable[[F], F]:
...
@route(url='/')
def index(request: Any) -> str:
return 'Hello world'
这是我的实际代码:
from json import loads
from typing import TypeVar, Callable, Any, Optional, Dict, Union
Handler = TypeVar("Handler", bound=Callable[[Any], None])
class Subscriber:
"""Decorate topic handlers. Must register the subscriber with register_subscriber."""
def __init__(self) -> None:
self.handlers: Dict[str, tuple] = dict()
def topic(
self, topic: str,
parse_func: Optional[Callable[[Union[str, bytes]], Any]] = loads
) -> Callable[[Handler], Handler]:
"""Subscribe to mqtt topic. by default the response is parsed with json.loads and passed into the handler.
Override this with a custom parser or set it to None to receive the raw response."""
def add_handler(handler: Handler) -> Handler:
self.handlers[topic] = (handler, parse_func)
return handler
return add_handler
我正在导入并在另一个文件中使用这个 class。
from typing import Union
from from organization.namespace.package.mqtt.client import Subscriber
paho: Subscriber = Subscriber()
@paho.topic("test")
def test(payload: Union[dict, list]) -> None:
print(payload)
但我无法摆脱这个错误。
Untyped decorator makes function "handler" untypedmypy(error)
正如我们在@AlexWaygood 的帮助下发现的那样,在同一文件中使用时不会抛出此错误。但出于某种原因,当导入另一个文件时,如上所示。
我的项目有以下目录结构:
organization
└── namespace
└── package
└── mqtt
├── client.py
└── handler.py
我的目录或子目录中目前没有 __init__.py
个文件。
这个问题与模块系统有关。此问题有 2 种不同的解决方案:
- 在每个文件夹中创建一个
__init__.py
。
- 当 运行 mypy.
时使用选项 --namespace-packages and --explicit-package-bases
我正在尝试向我的项目中添加类型。我几乎复制了 decorator factories.
的模式这是链接文档中提供的示例:
from typing import Any, Callable, TypeVar
F = TypeVar("F", bound=Callable[..., Any])
def route(url: str) -> Callable[[F], F]:
...
@route(url='/')
def index(request: Any) -> str:
return 'Hello world'
这是我的实际代码:
from json import loads
from typing import TypeVar, Callable, Any, Optional, Dict, Union
Handler = TypeVar("Handler", bound=Callable[[Any], None])
class Subscriber:
"""Decorate topic handlers. Must register the subscriber with register_subscriber."""
def __init__(self) -> None:
self.handlers: Dict[str, tuple] = dict()
def topic(
self, topic: str,
parse_func: Optional[Callable[[Union[str, bytes]], Any]] = loads
) -> Callable[[Handler], Handler]:
"""Subscribe to mqtt topic. by default the response is parsed with json.loads and passed into the handler.
Override this with a custom parser or set it to None to receive the raw response."""
def add_handler(handler: Handler) -> Handler:
self.handlers[topic] = (handler, parse_func)
return handler
return add_handler
我正在导入并在另一个文件中使用这个 class。
from typing import Union
from from organization.namespace.package.mqtt.client import Subscriber
paho: Subscriber = Subscriber()
@paho.topic("test")
def test(payload: Union[dict, list]) -> None:
print(payload)
但我无法摆脱这个错误。
Untyped decorator makes function "handler" untypedmypy(error)
正如我们在@AlexWaygood 的帮助下发现的那样,在同一文件中使用时不会抛出此错误。但出于某种原因,当导入另一个文件时,如上所示。
我的项目有以下目录结构:
organization
└── namespace
└── package
└── mqtt
├── client.py
└── handler.py
我的目录或子目录中目前没有 __init__.py
个文件。
这个问题与模块系统有关。此问题有 2 种不同的解决方案:
- 在每个文件夹中创建一个
__init__.py
。 - 当 运行 mypy. 时使用选项 --namespace-packages and --explicit-package-bases