I've got the following class defined:
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()
The code works exactly as expected. However, mypy complains that
"BackgroundThread" has no attribute "_target"
"BackgroundThread" has no attribute "_args"
"BackgroundThread" has no attribute "_kwargs"
I can get around this by inserting
class BackgroundThread(threading.Thread):
_target: typing.Callable
_args: typing.Tuple
_kwargs: typing.Dict[str, typing.Any]
However, this seems hacky. Why doesn't mypy recognize these variables which come from the parent class? Is it because they begin with underscores?