跳到内容

bad-exit-annotation (PYI036)

源自 flake8-pyi 代码检查器。

有时提供修复。

作用

检查 __exit____aexit__ 方法上不正确的函数签名。

为什么这不好?

错误注释的 __exit____aexit__ 方法在与类型检查器交互时可能会导致意外行为。

示例

from types import TracebackType

class Foo:
    def __exit__(
        self, typ: BaseException, exc: BaseException, tb: TracebackType
    ) -> None: ...

建议改为

from types import TracebackType

class Foo:
    def __exit__(
        self,
        typ: type[BaseException] | None,
        exc: BaseException | None,
        tb: TracebackType | None,
    ) -> None: ...