Skip to content

Commit 0c45285

Browse files
committed
clean up tests, simplify configs, parse complex constants
1 parent f3b6dfe commit 0c45285

17 files changed

Lines changed: 220 additions & 267 deletions

File tree

‎.github/workflows/main.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ jobs:
138138
run: |
139139
export TASK=${{ matrix.task }}
140140
if [[ $TASK == check-py314-niche ]]; then
141-
# Collect (cheap, sys.monitoring-based) coverage from this job too.
141+
# Collect (sys.monitoring-based) coverage from this job too.
142142
export COVERAGE_PROCESS_START=${{ github.workspace }}/hypothesis/pyproject.toml
143143
export COVERAGE_FILE=${{ github.workspace }}/hypothesis/.coverage
144144
fi

‎hypothesis/RELEASE.rst‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@ files.
77

88
The ``explain`` phase no longer reports lines inside the standard library,
99
which were occasionally included in (and made for misleading) explanations.
10+
11+
The constants-collection feature introduced in :ref:`version 6.131.1 <v6.131.1>`
12+
now also collects the float components of complex literals like ``3.5j``.

‎hypothesis/pyproject.toml‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,12 @@ exclude = [
153153
[tool.coverage.run]
154154
parallel = true
155155
branch = true
156-
# Not every job imports every measured module; the combined report is what counts.
156+
# Some of our CI jobs don't import any modules measured under coverage.
157157
disable_warnings = ["module-not-imported"]
158-
source = [
158+
# note that we use source_pkgs here to avoid the ambiguity between
159+
# the "hypothesis" package and the "hypothesis" directory during
160+
# resolution by coverage.py. source_pkgs forces package-only resolution.
161+
source_pkgs = [
159162
"hypothesis",
160163
"_hypothesis_ftz_detector",
161164
"_hypothesis_globals",
@@ -167,10 +170,11 @@ source = [
167170
# site-packages directory; map them onto our source tree so we can combine them.
168171
#
169172
# The keys of `source-paths` and `top-level-modules` here are arbitrary and
170-
# ignored, but the rules are checked in order - keep the more specific
171-
# hypothesis-package rule first. See
173+
# ignored. See
172174
# https://coverage.readthedocs.io/en/latest/config.html#paths.
175+
# (Note however that order matters).
173176
source-paths = ["src/hypothesis", "*/site-packages/hypothesis"]
177+
# e.g. our _hypothesis_pytestplugin, _hypothesis_globals, etc modules.
174178
top-level-modules = ["src", "*/site-packages"]
175179

176180
[tool.coverage.report]
@@ -182,9 +186,6 @@ skip_covered = true
182186
omit = [
183187
# only reachable via an also-excluded branch in strategies/_internal/numbers.py
184188
"**/_hypothesis_ftz_detector.py",
185-
# Test files can be measured when a test runs pytest in a subprocess, but
186-
# they aren't targets of our coverage requirement.
187-
"**/tests/**",
188189
]
189190
exclude_lines = [
190191
"pragma: no cover",

‎hypothesis/src/_hypothesis_pytestplugin.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def __call__(self, msg):
8989
# need balanced increment/decrement in configure/sessionstart to support nested
9090
# pytest (e.g. runpytest_inprocess), so this early increment in effect replaces
9191
# the first one in pytest_configure.
92-
# (the else-branch can only run in a child process; pragma: no branch)
92+
# (the else-branch can only run in a child process)
9393
if not os.environ.get("HYPOTHESIS_EXTEND_INITIALIZATION"): # pragma: no branch
9494
_hypothesis_globals.in_initialization += 1
9595
if "hypothesis" in sys.modules:

‎hypothesis/src/hypothesis/extra/ghostwriter.py‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -936,12 +936,7 @@ def _parameters_to_annotation_name(
936936
imports.update(new_imports)
937937
return type_name
938938
joined = _join_generics(("typing.Union", {"typing"}), annotations)
939-
if joined is None: # pragma: no cover
940-
# `annotations` is always non-empty here (we returned above otherwise) and
941-
# `_join_generics` only returns None for a falsy `origin_type_data` (never
942-
# the case here) or when it filters every annotation out, which only
943-
# happens for the "typing.Optional" special-case handled elsewhere.
944-
return None
939+
assert joined is not None
945940
imports.update(joined.imports)
946941
return joined.type_name
947942

@@ -1163,8 +1158,11 @@ def _get_testable_functions(thing: object) -> dict[str, Callable]:
11631158
elif isinstance(thing, types.ModuleType):
11641159
if hasattr(thing, "__all__"):
11651160
funcs = [getattr(thing, name, None) for name in thing.__all__]
1166-
elif hasattr(thing, "__package__"): # pragma: no branch # never absent
1167-
pkg = thing.__package__
1161+
else:
1162+
# thing.__spec__ is None for e.g. __main__ in scripts and the REPL,
1163+
# and dynamically created modules
1164+
spec = thing.__spec__
1165+
pkg = spec.parent if spec is not None else None
11681166
funcs = [
11691167
v
11701168
for k, v in vars(thing).items()

‎hypothesis/src/hypothesis/internal/constants_ast.py‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,25 +130,31 @@ def _add_constant(self, value: object) -> None:
130130
return
131131
if isinstance(value, bool):
132132
return
133-
if isinstance(value, float) and math.isinf(value):
134-
# we already upweight inf.
133+
if isinstance(value, float) and (math.isinf(value) or value == 0.0):
134+
# we already upweight inf and ±0.
135135
return
136136
if isinstance(value, int) and -100 < value < 100:
137137
# we already upweight small integers.
138138
return
139-
140139
if isinstance(value, (int, float, bytes, str)):
141140
self.constants.add(value)
142141
return
143-
144-
return
142+
if isinstance(value, complex):
143+
self._add_constant(value.imag)
144+
self._add_constant(value.real)
145+
return
146+
if value in (None, ...):
147+
return
148+
# we currently cover all possible cases, but future python versions may introduce
149+
# additional cases
150+
return # pragma: no cover
145151

146152
def visit_UnaryOp(self, node: UnaryOp) -> None:
147153
# `a = -1` is actually a combination of a USub and the constant 1.
148154
if (
149155
isinstance(node.op, USub)
150156
and isinstance(node.operand, Constant)
151-
and isinstance(node.operand.value, (int, float))
157+
and isinstance(node.operand.value, (int, float, complex))
152158
and not isinstance(node.operand.value, bool)
153159
):
154160
self._add_constant(-node.operand.value)

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

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,10 @@
3636

3737

3838
@functools.cache
39-
def should_trace_file(fname: str) -> bool:
39+
def should_trace_file(fname: str) -> bool: # pragma: no cover
4040
# fname.startswith("<") indicates runtime code-generation via compile,
4141
# e.g. compile("def ...", "<string>", "exec") in e.g. attrs methods.
42-
# Only called from the trace/trace_line callbacks below, so this body only
43-
# ever executes inside another tool's tracing callback - which makes it
44-
# invisible to sys.monitoring-based coverage (see Tracer.trace_line).
45-
return not (is_hypothesis_file(fname) or fname.startswith("<")) # pragma: no cover
42+
return not (is_hypothesis_file(fname) or fname.startswith("<"))
4643

4744

4845
# where possible, we'll use 3.12's new sys.monitoring module for low-overhead
@@ -82,9 +79,7 @@ def can_trace() -> bool:
8279
def branches(self) -> Trace:
8380
return frozenset(self._branches)
8481

85-
def trace(self, frame, event, arg): # pragma: no cover
86-
# settrace-based tracing; only used as a fallback on Python < 3.12,
87-
# where sys.monitoring is unavailable.
82+
def trace(self, frame, event, arg): # pragma: no cover # only on Python < 3.12
8883
try:
8984
if event == "call":
9085
return self.trace
@@ -100,9 +95,8 @@ def trace(self, frame, event, arg): # pragma: no cover
10095
def trace_line(
10196
self, code: types.CodeType, line_number: int
10297
) -> None: # pragma: no cover
103-
# sys.monitoring callback: code executed inside another tool's monitoring
104-
# callback is invisible to sys.monitoring-based coverage, so this body
105-
# can never be measured even though it runs on every traced line.
98+
# sys.monitoring callbacks do not fire for other monitoring tools, so coverage.py
99+
# can't see this function.
106100
fname = code.co_filename
107101
if not should_trace_file(fname):
108102
# this function is only called on 3.12+, but we want to avoid an
@@ -155,11 +149,7 @@ def __exit__(self, *args, **kwargs):
155149

156150

157151
UNHELPFUL_LOCATIONS = (
158-
# Standard-library locations are dropped wholesale (see the ModuleLocation
159-
# check in get_explaining_locations), so this list only needs to cover
160-
# testing tools whose lines are usually run downstream of the actual fault.
161152
# Note: The list is post-processed, so use plain "/" for separator here.
162-
#
163153
# Quite rarely, the first always-failing line is in Pytest's internals.
164154
"/_pytest/**",
165155
"/pluggy/_*.py",
@@ -230,14 +220,13 @@ def get_explaining_locations(traces):
230220
# The last step is to filter out explanations that we know would be uninformative.
231221
# When this is the first AFNP location, we conclude that Scrutineer missed the
232222
# real divergence (earlier in the trace) and drop that unhelpful explanation.
233-
# Lines inside the standard library - e.g. dataclass-generated dunders which
234-
# happen to run only during failure handling - get the same treatment.
235223
filter_regex = re.compile(_glob_to_re(UNHELPFUL_LOCATIONS))
236224
return {
237225
origin: {
238226
loc
239227
for loc in afnp_locs
240228
if not filter_regex.search(loc[0])
229+
# In addition to UNHELPFUL_LOCATIONS, we drop all stdlib locations.
241230
and ModuleLocation.from_path(loc[0]) is not ModuleLocation.STDLIB
242231
}
243232
for origin, afnp_locs in explanations.items()
@@ -252,7 +241,7 @@ def _stdlib_dirs():
252241
Path(sysconfig.get_path("stdlib")).resolve(),
253242
# Under Pyodide and other embedded pythons, the stdlib is imported from
254243
# a zipfile on sys.path, which sysconfig doesn't report.
255-
*map(Path, filter(lambda p: p.endswith(".zip"), sys.path)),
244+
*(Path(p) for p in sys.path if p.endswith(".zip")),
256245
}
257246

258247

‎hypothesis/tests/array_api/test_arrays.py‎

Lines changed: 19 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -544,12 +544,22 @@ def test_subnormal_elements_validation(xp, xps):
544544
check_can_generate_examples(strat)
545545

546546

547-
def _array_strategy(xp, xps, dtype):
548-
"""An ArrayStrategy for the given dtype, for exercising check_set_value()
549-
directly with contrived (val, val_0d) pairs that real array modules are
550-
unlikely to produce (e.g. modern NumPy raises rather than silently
551-
mismatching on integer overflow)."""
552-
return ArrayStrategy(
547+
@pytest.mark.parametrize(
548+
"dtype_name, val, stored, match",
549+
[
550+
("int8", 5, 6, "cannot be represented"),
551+
("float32", 5.0, 6.0, "cannot be represented"),
552+
# a non-numeric element makes the subnormal check itself error, which
553+
# still reports the generic message rather than propagating
554+
("float32", "not a float", 6.0, "cannot be represented"),
555+
("float32", width_smallest_normals(32) / 2, 0.0, "Generated subnormal"),
556+
],
557+
)
558+
def test_check_set_value_mismatch_errors(xp, xps, dtype_name, val, stored, match):
559+
"""check_set_value() explains elements which don't round-trip through the
560+
array module, with a subnormal-specific message where relevant."""
561+
dtype = getattr(xp, dtype_name)
562+
strategy = ArrayStrategy(
553563
xp=xp,
554564
api_version=xps.api_version,
555565
elements_strategy=st.just(0),
@@ -558,51 +568,14 @@ def _array_strategy(xp, xps, dtype):
558568
fill=st.nothing(),
559569
unique=False,
560570
)
561-
562-
563-
def test_check_set_value_raises_generic_error_for_non_float_mismatch(xp, xps):
564-
"""check_set_value() raises the generic mismatch error (not the
565-
subnormal-specific one) for non-float dtypes."""
566-
strategy = _array_strategy(xp, xps, xp.int8)
567-
val_0d = xp.asarray(6, dtype=xp.int8)
568-
with pytest.raises(InvalidArgument, match="cannot be represented"):
569-
strategy.check_set_value(5, val_0d, st.just(5))
570-
571-
572-
def test_check_set_value_raises_generic_error_for_non_subnormal_float_mismatch(xp, xps):
573-
"""check_set_value() raises the generic mismatch error for a float
574-
mismatch that is not subnormal."""
575-
strategy = _array_strategy(xp, xps, xp.float32)
576-
val_0d = xp.asarray(6.0, dtype=xp.float32)
577-
with pytest.raises(InvalidArgument, match="cannot be represented"):
578-
strategy.check_set_value(5.0, val_0d, st.just(5.0))
579-
580-
581-
def test_check_set_value_raises_generic_error_when_subnormal_check_errors(xp, xps):
582-
"""When the generated element doesn't support abs()/comparison (so the
583-
subnormal check itself raises), check_set_value() still reports the
584-
generic mismatch error rather than propagating the error."""
585-
strategy = _array_strategy(xp, xps, xp.float32)
586-
val_0d = xp.asarray(6.0, dtype=xp.float32)
587-
with pytest.raises(InvalidArgument, match="cannot be represented"):
588-
strategy.check_set_value("not a float", val_0d, st.just("not a float"))
589-
590-
591-
def test_check_set_value_raises_for_subnormal_mismatch(xp, xps):
592-
"""check_set_value() raises a subnormal-specific error when a subnormal
593-
float element does not round-trip through the array module."""
594-
strategy = _array_strategy(xp, xps, xp.float32)
595-
subnormal = width_smallest_normals(32) / 2
596-
val_0d = xp.asarray(0.0, dtype=xp.float32)
597-
with pytest.raises(InvalidArgument, match="Generated subnormal float"):
598-
strategy.check_set_value(subnormal, val_0d, st.just(subnormal))
571+
with pytest.raises(InvalidArgument, match=match):
572+
strategy.check_set_value(val, xp.asarray(stored, dtype=dtype), st.just(val))
599573

600574

601575
@pytest.mark.parametrize("shape", [3, 10])
602576
def test_reports_conversion_failure_for_dense_arrays(xp, xps, shape):
603577
"""When elements cannot be converted to the array's dtype in the fully
604-
dense (fill=nothing) code path, a helpful error is raised - listing the
605-
elements in full for small arrays, and a truncated form for larger ones."""
578+
dense (fill=nothing) code path, a helpful error is raised."""
606579
strat = xps.arrays(
607580
dtype=xp.int8, shape=shape, elements=st.just(300), fill=st.nothing()
608581
)

‎hypothesis/tests/array_api/test_from_dtype.py‎

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -129,19 +129,18 @@ def test_subnormal_generation(xp, xps, kwargs):
129129
find_any(strat, lambda n: -smallest_normal < n < smallest_normal)
130130

131131

132-
def _ftz_asarray(obj, dtype=None):
133-
"""Like np.asarray(), but flushes subnormal floats to zero - simulating an
134-
array module built with e.g. -ffast-math/-ftz."""
135-
arr = np.asarray(obj, dtype=dtype)
136-
if np.issubdtype(arr.dtype, np.floating):
137-
tiny = np.finfo(arr.dtype).tiny
138-
arr = np.where((arr != 0) & (np.abs(arr) < tiny), arr.dtype.type(0), arr)
139-
return arr
140-
141-
142132
def test_infers_flush_to_zero_from_asarray():
143133
"""from_dtype() infers allow_subnormal=False when the array module's
144134
asarray() flushes subnormal floats to zero."""
135+
136+
def _ftz_asarray(obj, dtype=None):
137+
"""Like np.asarray(), but flushes subnormal floats to zero"""
138+
arr = np.asarray(obj, dtype=dtype)
139+
if np.issubdtype(arr.dtype, np.floating):
140+
tiny = np.finfo(arr.dtype).tiny
141+
arr = np.where((arr != 0) & (np.abs(arr) < tiny), arr.dtype.type(0), arr)
142+
return arr
143+
145144
xp = copy(mock_xp)
146145
xp.asarray = _ftz_asarray
147146
strat = _from_dtype(xp, "draft", xp.float32).filter(lambda n: n != 0)

‎hypothesis/tests/array_api/test_scalar_dtypes.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
NUMERIC_NAMES,
1919
REAL_NAMES,
2020
UINT_NAMES,
21-
_numeric_dtypes,
21+
make_strategies_namespace,
2222
)
2323

2424
from tests.array_api.common import MIN_VER_FOR_COMPLEX
@@ -67,8 +67,9 @@ def test_all_generated_numeric_dtypes_are_numeric(xp, xps):
6767
def test_numeric_dtypes_excludes_complex_before_2022_12(xp):
6868
"""numeric_dtypes() only includes real dtypes for api_version=2021.12,
6969
which predates complex dtype support."""
70+
xps = make_strategies_namespace(xp, api_version="2021.12")
7071
dtypes = [getattr(xp, n) for n in REAL_NAMES]
71-
assert_all_examples(_numeric_dtypes(xp, "2021.12"), lambda dtype: dtype in dtypes)
72+
assert_all_examples(xps.numeric_dtypes(), lambda dtype: dtype in dtypes)
7273

7374

7475
def skipif_unsupported_complex(strat_name, dtype_name):

0 commit comments

Comments
 (0)