Fix test_windows_event_loop_policy for Python 3.14 compatibility#251
Merged
Conversation
asyncio.WindowsSelectorEventLoopPolicy is a lazily-resolved deprecated alias on non-Windows platforms. On Python 3.14, that resolver raises NameError instead of AttributeError when accessed off Windows, and mock.patch(..., create=True) snapshots the original value via getattr(target, name, DEFAULT) before applying the patch - a call that doesn't catch NameError, so it blew up before the patch even took effect. Set/restore the attribute directly instead, sidestepping that getattr call. Verified against both Python 3.12 and 3.14. Signed-off-by: chris hay <chris.hay@uk.ibm.com>
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
Prerequisite for #221 (Python 3.14 bump). `test_windows_event_loop_policy` fails on 3.14 with a `NameError` from deep inside `asyncio`'s own module `getattr`:
`asyncio.WindowsSelectorEventLoopPolicy` is a lazily-resolved deprecated alias on non-Windows platforms. On Python 3.14, that resolver raises `NameError` instead of `AttributeError` when accessed off Windows (the backing `windows_events` submodule is only imported on win32). `mock.patch(..., create=True)` snapshots the original value via `getattr(target, name, DEFAULT)` before applying the patch, and that call doesn't catch `NameError` — so it blows up before the patch is even applied. This isn't platform-specific to my machine; it reproduces the same way on Linux CI runners.
Fixed by setting/restoring the attribute directly instead of going through `mock.patch`, sidestepping the problematic `getattr` call entirely.
Test plan