mypy 不识别从超类继承的变量

mypy not recognizing variables inherited from superclass

我定义了以下 class:

class BackgroundThread(threading.Thread):
    def __init__(self, *args: typing.Any, **kwargs: typing.Any) -> None:
        super().__init__(*args, **kwargs)
        self._quit = threading.Event()

    def run(self) -> None:
        while not self._quit.wait(timeout=0.0):
            self._target(*self._args, **self._kwargs)

    def __enter__(self) -> None:
        self.start()
        return None

    def __exit__(self, *args: typing.Any) -> None:
        self._quit.set()
        self.join()

代码完全符合预期。然而,mypy 抱怨

"BackgroundThread" has no attribute "_target"
"BackgroundThread" has no attribute "_args"
"BackgroundThread" has no attribute "_kwargs"

我可以通过插入

来解决这个问题
class BackgroundThread(threading.Thread):
    _target: typing.Callable
    _args: typing.Tuple
    _kwargs: typing.Dict[str, typing.Any]

然而,这似乎很老套。为什么 mypy 不识别这些来自父 class 的变量?是因为下划线开头吗?

这是Thread在standard stub中的定义。未定义下划线属性,只有双下划线是。您可以 ask/contribute 在 Github 上打字以添加缺少的下划线属性。不禁止添加它们,但通常它们不会列在类型存根中。

class Thread:
    name: str
    ident: Optional[int]
    daemon: bool
    if sys.version_info >= (3,):
        def __init__(
            self,
            group: None = ...,
            target: Optional[Callable[..., Any]] = ...,
            name: Optional[str] = ...,
            args: Iterable[Any] = ...,
            kwargs: Optional[Mapping[str, Any]] = ...,
            *,
            daemon: Optional[bool] = ...,
        ) -> None: ...
    else:
        def __init__(
            self,
            group: None = ...,
            target: Optional[Callable[..., Any]] = ...,
            name: Optional[Text] = ...,
            args: Iterable[Any] = ...,
            kwargs: Optional[Mapping[Text, Any]] = ...,
        ) -> None: ...
    def start(self) -> None: ...
    def run(self) -> None: ...
    def join(self, timeout: Optional[float] = ...) -> None: ...
    def getName(self) -> str: ...
    def setName(self, name: Text) -> None: ...
    if sys.version_info >= (3, 8):
        @property
        def native_id(self) -> Optional[int]: ...  # only available on some platforms
    def is_alive(self) -> bool: ...
    if sys.version_info < (3, 9):
        def isAlive(self) -> bool: ...
    def isDaemon(self) -> bool: ...
    def setDaemon(self, daemonic: bool) -> None: ...