I have a method which takes a multiprocessing.Manager().Event()
, for the purposes of gracefully stopping my app.
When I run mypy against it, it complains that it's not a valid type:
error: Module "multiprocessing.managers" has no attribute "EventProxy" [attr-defined]
Code:
from multiprocessing.managers import EventProxy
class App:
def run(self, stop_event: EventProxy):
...
with Manager() as manager:
stop_event = manager.Event()
a = App()
a.run(stop_event)
I used the type EventProxy
because when I checked the type()
of the multiprocessing.Manager().Event()
it said it was a multiprocessing.managers.EventProxy
.
How can I type hint this method so that mypy won't complain?
Versions:
Python 3.9.14
mypy 0.991
mypy-extensions 0.4.3
CodePudding user response:
From the docs:
multiprocessing.Manager()
Returns a started SyncManager
object.
If we check the docs for SyncManager.Event
we see that it returns threading.Event
.
So in summary:
from multiprocessing import Manager
from threading import Event
class App:
def run(self, stop_event: Event) -> None:
print('Hello')
with Manager() as manager:
stop_event = manager.Event()
a = App()
a.run(stop_event)