Skip to content

Fix the optional-dependency guard for the hypothesmith fuzz test - #117

Merged
sigmavirus24 merged 1 commit into
PyCQA:masterfrom
bukzor:fix-hypothesmith-optional
Jun 28, 2026
Merged

sigmavirus24 merged 1 commit into
PyCQA:masterfrom
bukzor:fix-hypothesmith-optional

Conversation

@bukzor

@bukzor bukzor commented Jun 27, 2026

Copy link
Copy Markdown
Contributor

The property-based fuzz test (test_idempotent_any_syntatically_valid_python)
is meant to be optional — it's guarded with
@pytest.mark.skipif(not hypothesmith, ...) so the suite still runs when the
hypothesis/hypothesmith extras aren't installed. But that guard never takes
effect.

skipif only marks an already-collected test. The decorators stacked above it
run at import time:

  • @settings(... suppress_health_check=HealthCheck.all()) and @given(...)
    reference settings, given, HealthCheck, st — names that only exist
    when the try: import hypothesmith / from hypothesis import ... succeeds.
  • @given(src_contents=hypothesmith.from_grammar() | hypothesmith.from_node())
    calls into hypothesmith eagerly.

So without the optional deps, importing the test module raises
NameError: name 'settings' is not defined during collection — which fails the
entire test_mccabe.py, taking every other test down with it. The skipif
is dead code.

Fix

Guard the decorated definition on the import result instead of relying on
skipif, with a visibly-skipped stub when the dep is absent:

if hypothesmith is None:
    @pytest.mark.skip(reason="hypothesmith could not be imported")
    def test_idempotent_any_syntatically_valid_python():
        """Property-based fuzz test; requires the optional hypothesmith dep."""
else:
    @settings(...)
    @given(...)
    def test_idempotent_any_syntatically_valid_python(src_contents, max_complexity):
        ...

Verification

  • Without hypothesmith/hypothesis: the module now collects; the fuzz
    test reports as skipped; all other tests run. (Previously: collection error,
    whole file failed.)
  • With hypothesmith: the test is collected and passes (1000 examples).
  • flake8 --max-line-length=88 test_mccabe.py clean.

The @settings/@given decorators run at import time and call into
hypothesmith, so the existing @pytest.mark.skipif never took effect:
without the optional deps the module raised NameError during collection,
taking every other test in the file down with it.

Guard the decorated definition on the import result instead, falling
back to a visibly-skipped stub when hypothesmith is unavailable.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants