Repository navigation
feat: also block DNS resolution - #482
Merged
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
This PR strengthens pytest-socket’s “no network” guarantee by blocking DNS resolution APIs (socket.getaddrinfo, socket.gethostbyname) when sockets are disabled, and updates tests to cover the new behavior and messaging.
Changes:
- Guard
socket.getaddrinfoandsocket.gethostbynameunder--disable-socket(and marker/fixture), with clearer per-entrypoint error messages. - Restore the DNS functions only when the currently-installed function is the plugin’s guard (identity-checked restoration).
- Add a dedicated DNS test module and loosen a shared assertion helper to match the expanded error surface.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
src/pytest_socket/__init__.py |
Adds DNS guards/restoration and updates SocketBlockedError messaging support. |
tests/test_dns.py |
Introduces coverage for DNS blocking/restoration and error message clarity. |
tests/common.py |
Broadens the helper assertion to accept the new blocked-call messages. |
Comments suppressed due to low confidence (2)
src/pytest_socket/init.py:146
enable_socket()restoressocket.getaddrinfo/socket.gethostbynamevia assignment, but these lines may also trip strict mypy (CI runsuv run mypy src). Consider adding the same kind of typing workaround as other monkeypatch points in this file (e.g.,# type: ignore[...]/cast) so type checking remains green.
socket.socket = _true_socket # type: ignore[misc]
if socket.getaddrinfo is _guarded_getaddrinfo:
socket.getaddrinfo = _true_getaddrinfo
if socket.gethostbyname is _guarded_gethostbyname:
socket.gethostbyname = _true_gethostbyname
src/pytest_socket/init.py:368
_remove_restrictions()now conditionally restoressocket.getaddrinfo/socket.gethostbynamevia assignment. As with the other new monkeypatch assignments, this may need an explicit# type: ignore[...]orcastto satisfy strict mypy (CI runsuv run mypy src).
socket.socket.connect = _true_connect # type: ignore[method-assign]
if socket.getaddrinfo is _guarded_getaddrinfo:
socket.getaddrinfo = _true_getaddrinfo
if socket.gethostbyname is _guarded_gethostbyname:
socket.gethostbyname = _true_gethostbyname
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
`--disable-socket` (and the marker/fixture) now guards `socket.getaddrinfo` and `socket.gethostbyname` too. Before this, a test could quietly resolve a hostname with sockets blocked, so "no network" wasn't really no network. The error names the call that got caught (`socket.socket`, `socket.getaddrinfo`, `socket.gethostbyname`) so failures point at the right one. Restoration is identity-checked: we only swap a function back if our guard is still the one in place. The resolution-cache tests monkeypatch `socket.getaddrinfo` themselves, and without that check each inner pytester teardown would stomp the outer monkeypatch. Closes #43. Supersedes #64. Signed-off-by: Mike Fiedler <[email protected]>
miketheman
force-pushed
the
feat/disable-dns-resolution
branch
from
May 20, 2026 03:43
310bf32 to
57591e0
Compare
miketheman
added a commit
that referenced
this pull request
Oct 12, 2026
Since DNS blocking landed in #482, resolve_hostnames called the patched socket.getaddrinfo. With the README's conftest recipe (disable_socket() in pytest_runtest_setup, which runs before the plugin's hook) and a hostname in --allow-hosts, every test errored in setup with SocketBlockedError, and the resolution cache never filled. Call the saved _true_getaddrinfo instead, and point the test fixture that fakes DNS at it. Signed-off-by: Mike Fiedler <[email protected]>
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.
--disable-socket(and the marker/fixture) now guardssocket.getaddrinfoandsocket.gethostbynametoo. Before this, a test could quietly resolve a hostname with sockets blocked, so "no network" wasn't really no network.The error names the call that got caught (
socket.socket,socket.getaddrinfo,socket.gethostbyname) so failures point at the right one.Restoration is identity-checked: we only swap a function back if our guard is still the one in place. The resolution-cache tests monkeypatch
socket.getaddrinfothemselves, and without that check each inner pytester teardown would stomp the outer monkeypatch.Closes #43. Supersedes #64.