Skip to content

Synthesizer drops _-prefixed module imports even when referenced by context type stubs #674

Description

@datvo06

Failing test that should pass

A Template that synthesises a callable, called from a pytest test method, fails mypy validation in the synthesizer because the request fixture's type (_pytest.fixtures.TopRequest) is added to the context stubs but _pytest is never imported.

import pytest
from typing import Callable
from effectful.handlers.llm import Template
from effectful.handlers.llm.evaluation import UnsafeEvalProvider
from effectful.ops.semantics import handler


class TestSynthesisRegression:
    @pytest.fixture(autouse=False)
    def _dummy(self): yield

    def test_synthesis_with_pytest_request_in_scope(self, request):
        threshold = 0.85

        @Template.define
        def make() -> Callable[[float], bool]:
            """Use the `threshold` reader, then `def above(x: float) -> bool: return x > <value>`."""
            raise NotImplementedError

        with handler(UnsafeEvalProvider()):
            fn = make()           # currently raises ResultDecodingError

        assert fn(0.9) is True

The LLM call itself succeeds and emits def above(x: float) -> bool: return x > 0.85. The failure surfaces inside mypy_type_check.

Root cause

effectful/handlers/llm/evaluation.py:251 collect_imports:

modules: set[tuple[str, str | None]] = set(
    (k, None)
    for k in sys.modules.keys()
    if k not in SKIPPED_GLOBALS and not k.startswith("_") and k[0].isalpha()
)

The not k.startswith("_") clause filters out every module whose name begins with an underscore — including _pytest, which pytest's public surface re-exports types from. When collect_variable_declarations later emits a stub line like

request: _pytest.fixtures.TopRequest

(because the runtime request value's class is _pytest.fixtures.TopRequest), the qualified type name still references _pytest, but the import has been silently dropped. mypy then reports

<string>:17: error: Name "_pytest" is not defined  [name-defined]

and mypy_type_check raises TypeError, which surfaces as ResultDecodingError.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions