Actually correctly implement custom hypothesis settings
Marco Ricci

Marco Ricci commited on 2024-12-12 12:03:30
Zeige 2 geänderte Dateien mit 17 Einfügungen und 4 Löschungen.


The `hypothesis.settings` decorators are intended to modify the
currently active `hypothesis` settings profile for singular tests.
However, the profiles are defined and selected only *after* the `tests`
module has been loaded, so any settings defined in the `tests` module
actually modify the `hypothesis` default settings, not the selected
profile's settings.

Fix this by defining decorators that construct and apply the correct
settings, which defines the settings objects deferredly, instead of
defining them immediately.
... ...
@@ -1329,7 +1329,11 @@ skip_if_no_cryptography_support = pytest.mark.skipif(
1329 1329
     reason='no "cryptography" support',
1330 1330
 )
1331 1331
 
1332
-hypothesis_settings_coverage_compatible = (
1332
+
1333
+def hypothesis_settings_coverage_compatible(
1334
+    f: Any = None,
1335
+) -> Any:
1336
+    settings = (
1333 1337
         hypothesis.settings(
1334 1338
             # Running under coverage with the Python tracer increases
1335 1339
             # running times 40-fold, on my machines.  Sadly, not every
... ...
@@ -1340,19 +1344,27 @@ hypothesis_settings_coverage_compatible = (
1340 1344
                 if (deadline := hypothesis.settings().deadline) is not None
1341 1345
                 else None
1342 1346
             ),
1347
+            stateful_step_count=32,
1343 1348
             suppress_health_check=(hypothesis.HealthCheck.too_slow,),
1344 1349
         )
1345 1350
         if sys.gettrace() is not None
1346 1351
         else hypothesis.settings()
1347 1352
     )
1353
+    return settings if f is None else settings(f)
1348 1354
 
1349
-hypothesis_settings_coverage_compatible_with_caplog = hypothesis.settings(
1350
-    parent=hypothesis_settings_coverage_compatible,
1355
+
1356
+def hypothesis_settings_coverage_compatible_with_caplog(
1357
+    f: Any = None,
1358
+) -> Any:
1359
+    parent_settings = hypothesis_settings_coverage_compatible()
1360
+    settings = hypothesis.settings(
1361
+        parent=parent_settings,
1351 1362
         suppress_health_check={
1352 1363
             hypothesis.HealthCheck.function_scoped_fixture,
1353 1364
         }
1354
-    | set(hypothesis_settings_coverage_compatible.suppress_health_check),
1365
+        | set(parent_settings.suppress_health_check),
1355 1366
     )
1367
+    return settings if f is None else settings(f)
1356 1368
 
1357 1369
 
1358 1370
 def list_keys(self: Any = None) -> list[_types.KeyCommentPair]:
... ...
@@ -2399,6 +2399,7 @@ _vault_full_config = strategies.builds(
2399 2399
 )
2400 2400
 
2401 2401
 
2402
+@tests.hypothesis_settings_coverage_compatible
2402 2403
 class ConfigManagementStateMachine(stateful.RuleBasedStateMachine):
2403 2404
     def __init__(self) -> None:
2404 2405
         super().__init__()
2405 2406