Skip to content

Commit 71b7bc0

Browse files
committed
Recognize zipimported stdlib when filtering explanations
Under Pyodide the stdlib is imported from a zipfile on sys.path, which sysconfig's stdlib paths don't cover. Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01AaHKFyetTLidd2R2AVi4d2
1 parent 9495e0d commit 71b7bc0

2 files changed

Lines changed: 20 additions & 4 deletions

File tree

‎hypothesis/src/hypothesis/internal/scrutineer.py‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,10 +246,17 @@ def get_explaining_locations(traces):
246246

247247
# see e.g. https://docs.python.org/3/library/sysconfig.html#posix-user
248248
# for examples of these path schemes
249-
STDLIB_DIRS = {
250-
Path(sysconfig.get_path("platstdlib")).resolve(),
251-
Path(sysconfig.get_path("stdlib")).resolve(),
252-
}
249+
def _stdlib_dirs():
250+
return {
251+
Path(sysconfig.get_path("platstdlib")).resolve(),
252+
Path(sysconfig.get_path("stdlib")).resolve(),
253+
# Under Pyodide and other embedded pythons, the stdlib is imported from
254+
# a zipfile on sys.path, which sysconfig doesn't report.
255+
*map(Path, filter(lambda p: p.endswith(".zip"), sys.path)),
256+
}
257+
258+
259+
STDLIB_DIRS = _stdlib_dirs()
253260
SITE_PACKAGES_DIRS = {
254261
Path(sysconfig.get_path("purelib")).resolve(),
255262
Path(sysconfig.get_path("platlib")).resolve(),

‎hypothesis/tests/nocover/test_scrutineer.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212
import sys
1313
import sysconfig
1414
from collections import defaultdict
15+
from pathlib import Path
1516

1617
import pytest
1718

1819
from hypothesis import Phase, given, note, settings, strategies as st
20+
from hypothesis.internal import scrutineer
1921
from hypothesis.internal.compat import PYPY
2022
from hypothesis.internal.scrutineer import (
2123
EXPLANATION_STUB,
@@ -166,6 +168,13 @@ def test_report_sort(random):
166168
assert report_lines == expected_lines
167169

168170

171+
def test_zipimported_stdlib_counts_as_stdlib(monkeypatch):
172+
# Under Pyodide the stdlib is imported from e.g. "/lib/python314.zip",
173+
# which sysconfig doesn't report - check we find it from sys.path.
174+
monkeypatch.setattr(sys, "path", [*sys.path, "/fake/python314.zip"])
175+
assert Path("/fake/python314.zip") in scrutineer._stdlib_dirs()
176+
177+
169178
def test_explanations_drop_stdlib_locations():
170179
# e.g. a dataclass-generated dunder which happens to run only for failing
171180
# inputs would otherwise be reported as the explanation - misleadingly,

0 commit comments

Comments
 (0)