Widen check_multiprocess_shutdown_event type hint via Protocol (closes #247)#356
Open
jbbqqf wants to merge 1 commit into
Open
Widen check_multiprocess_shutdown_event type hint via Protocol (closes #247)#356jbbqqf wants to merge 1 commit into
jbbqqf wants to merge 1 commit into
Conversation
The function only calls ``shutdown_event.is_set()``, but it was annotated as ``multiprocessing.synchronize.Event``. mypy therefore rejected the two equally-valid alternatives that users routinely reach for: * ``multiprocessing.Manager().Event()`` — a proxy that wraps ``threading.Event`` (per `the docs <https://docs.python.org/3/library/multiprocessing.html#multiprocessing.managers.SyncManager.Event>`_). * Plain ``threading.Event`` for in-process testing. Introduce a minimal ``IsSetEvent`` ``Protocol`` so any object exposing ``is_set()`` is accepted. Existing call sites using ``multiprocessing.Event()`` continue to type-check unchanged. Closes pgjones#247.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
check_multiprocess_shutdown_eventonly readsshutdown_event.is_set(), but its parameter is typed asmultiprocessing.synchronize.Event. That rejects the two equally-valid alternatives users routinely reach for:multiprocessing.Manager().Event()— a proxy that wrapsthreading.Event(Python docs).threading.Eventfor in-process tests.mypy reports
error: Argument 1 ... has incompatible type "threading.Event"; expected "multiprocessing.synchronize.Event" [arg-type]for both, even though the call is semantically correct.Fix: introduce a minimal
IsSetEventProtocolexposingis_set() -> booland annotate the parameter with it. Existing call sites that passmultiprocessing.Event()continue to type-check unchanged (they structurally satisfy the protocol).Fixes #247.
Changes
src/hypercorn/utils.py— addIsSetEvent(Protocol); widencheck_multiprocess_shutdown_eventsignature; drop the now-unusedEventTypeimport.tests/test_utils.py—test_check_multiprocess_shutdown_event_accepts_is_set_protocolparametrised over three event-like types (multiprocessing.Event,multiprocessing.Manager().Event(),threading.Event).Reproduce BEFORE/AFTER yourself (copy-paste)
What I ran locally
Edge cases
mainmultiprocessing.Event()multiprocessing.Event()Manager().Event()(issue #247)threading.Eventthreading.Eventthreading.Event()is_set()object()AttributeErrorat runtime (unchanged)Risk / blast radius
The change is purely an annotation widening. Runtime behaviour is identical — the call site only ever invokes
.is_set(). No source file's mypy output regressed (pre-existing unused-ignore comments insrc/hypercorn/logging.pyare unaffected). TheProtocolfollows the same pattern already used insrc/hypercorn/typing.pyforH2SyncStream,H2AsyncStream,Event, etc.PR drafted with assistance from Claude Code (Anthropic). The change was reviewed manually against hypercorn's source. The reproducer block above is the one I used during development; reviewers can paste it verbatim.