NameError when unrelated parameters are annotated with types only imported at type checking time on python 3.14
The following example errors with a NameError when running under python 3.14. I discovered this a more complex example when trying to drop python 3.13 support from code that either used from __future__ import annotations or quoted types in >=3.13 code
from typing import TYPE_CHECKING
from hypothesis import given
from hypothesis import strategies as st
if TYPE_CHECKING:
class OnlyAvailableToTypeCheckers:
pass
@given(value=st.integers())
def test_example(unresolved: OnlyAvailableToTypeCheckers, value: int) -> None:
pass
❯ uv run python hyp_example.py
Traceback (most recent call last):
File "d:\source\repos\Qcodes\hyp_example.py", line 11, in <module>
@given(value=st.integers())
~~~~~^^^^^^^^^^^^^^^^^^^^^
File "d:\source\repos\Qcodes\.venv\Lib\site-packages\hypothesis\core.py", line 1987, in run_test_as_given
original_sig = get_signature(test)
File "d:\source\repos\Qcodes\.venv\Lib\site-packages\hypothesis\internal\reflection.py", line 162, in get_signature
sig = inspect.signature(target, follow_wrapped=follow_wrapped, eval_str=eval_str)
File "C:\Users\jensh\AppData\Roaming\uv\python\cpython-3.14-windows-x86_64-none\Lib\inspect.py", line 3334, in signature
return Signature.from_callable(obj, follow_wrapped=follow_wrapped,
~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
globals=globals, locals=locals, eval_str=eval_str,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
annotation_format=annotation_format)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\jensh\AppData\Roaming\uv\python\cpython-3.14-windows-x86_64-none\Lib\inspect.py", line 3049, in from_callable
return _signature_from_callable(obj, sigcls=cls,
follow_wrapper_chains=follow_wrapped,
globals=globals, locals=locals, eval_str=eval_str,
annotation_format=annotation_format)
File "C:\Users\jensh\AppData\Roaming\uv\python\cpython-3.14-windows-x86_64-none\Lib\inspect.py", line 2519, in _signature_from_callable
return _signature_from_function(sigcls, obj,
skip_bound_arg=skip_bound_arg,
globals=globals, locals=locals, eval_str=eval_str,
annotation_format=annotation_format)
File "C:\Users\jensh\AppData\Roaming\uv\python\cpython-3.14-windows-x86_64-none\Lib\inspect.py", line 2342, in _signature_from_function
annotations = get_annotations(func, globals=globals, locals=locals, eval_str=eval_str,
format=annotation_format)
File "C:\Users\jensh\AppData\Roaming\uv\python\cpython-3.14-windows-x86_64-none\Lib\annotationlib.py", line 987, in get_annotations
ann = _get_dunder_annotations(obj)
File "C:\Users\jensh\AppData\Roaming\uv\python\cpython-3.14-windows-x86_64-none\Lib\annotationlib.py", line 1180, in _get_dunder_annotations
ann = getattr(obj, "__annotations__", None)
File "d:\source\repos\Qcodes\hyp_example.py", line 12, in __annotate__
def test_example(unresolved: OnlyAvailableToTypeCheckers, value: int) -> None:
^^^^^^^^^^^^^^^^^^^^^^^^^^^
NameError: name 'OnlyAvailableToTypeCheckers' is not defined
Python 3.14.8 Windows 11
Confirmed on current main (6.168.5, ca2a4c6) with Python 3.14.6. The repro raises NameError: name 'OnlyAvailableToTypeCheckers' is not defined as soon as @given is applied, before any examples run. The failure path is hypothesis/src/hypothesis/internal/reflection.py:162, where inspect.signature(target, ..., eval_str=eval_str) triggers Python 3.14's lazy __annotate__ evaluation for parameters Hypothesis doesn't need to fill. Passing annotation_format=inspect.Format.STRING in that call avoids the NameError while still returning parameter names.
Hi, I'd like to work on this and open a PR for it if it's still available.
Go for it — nothing in flight from my side. The two facts from my repro that should help: the failure is the inspect.signature call at src/hypothesis/internal/reflection.py:162 triggering 3.14's lazy annotate evaluation, and passing annotation_format=inspect.Format.STRING there still returns the parameter names Hypothesis needs without evaluating the TYPE_CHECKING-only reference. Worth adding a regression test shaped like the repro in the issue, since it fails at decoration time, before any examples run.