Ghostwriter omits imports for filters built from annotated_types bounds
Author: ZacKienzle2Created Sep 25, 2026Updated Sep 25, 2026
Related to #3549 and #4303 (missing imports for regex and numpy strategies).
Reproduced with hypothesis 6.168.1, Python 3.13, Windows 11.
# dice.py
from itertools import product
from typing import Annotated
from annotated_types import Ge, Le
def expected_max_enumerated(
faces: Annotated[int, Ge(1), Le(12)], rolls: Annotated[int, Ge(1), Le(4)]
) -> float:
outcomes = list(product(range(1, faces + 1), repeat=rolls))
return sum(max(o) for o in outcomes) / len(outcomes)
def expected_max_closed_form(
faces: Annotated[int, Ge(1), Le(12)], rolls: Annotated[int, Ge(1), Le(4)]
) -> float:
return sum(1 - (k / faces) ** rolls for k in range(faces))hypothesis write --style=pytest --equivalent dice.expected_max_enumerated dice.expected_max_closed_form prints:
import dice
import typing
from hypothesis import given, strategies as st
@given(
faces=st.integers()
.filter(functools.partial(le, 1))
.filter(functools.partial(ge, 12)),
rolls=st.integers()
.filter(functools.partial(le, 1))
.filter(functools.partial(ge, 4)),
)
def test_equivalent_expected_max_enumerated_expected_max_closed_form(
faces: typing.Annotated[int, Ge(ge=1), Le(le=12)],
rolls: typing.Annotated[int, Ge(ge=1), Le(le=4)],
) -> None:
...Collecting the written test fails with NameError: name 'functools' is not defined. le and ge (from operator) and Ge and Le (from annotated_types) are also used without being imported.
Expected: the written test imports everything it uses. Alternatively the bounds could be printed as st.integers(min_value=1, max_value=12): the strategy's wrapped_strategy already reports integers(1, 12) after filter rewriting, so that form would need no extra imports.
Source: HypothesisWorks/hypothesis