Skip to content

Commit 6fd3dca

Browse files
authored
Merge pull request #3325 from Zac-HD/investigate-ghostwriter-empty-bug
Fix rare type hints issue
2 parents af284f2 + 67f0546 commit 6fd3dca

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

hypothesis-python/RELEASE.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
RELEASE_TYPE: patch
2+
3+
This patch fixes a rare bug where we could incorrectly treat
4+
:obj:`~python:inspect.Parameter.empty` as a type annotation,
5+
if the callable had an explicitly assigned ``__signature__``.

hypothesis-python/src/hypothesis/internal/compat.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,11 @@ def get_type_hints(thing):
9191

9292
vkinds = (inspect.Parameter.VAR_POSITIONAL, inspect.Parameter.VAR_KEYWORD)
9393
for p in inspect.signature(thing).parameters.values():
94-
if p.kind not in vkinds and is_a_type(p.annotation):
94+
if (
95+
p.kind not in vkinds
96+
and is_a_type(p.annotation)
97+
and p.annotation is not p.empty
98+
):
9599
if p.default is None:
96100
hints[p.name] = typing.Optional[p.annotation]
97101
else:

hypothesis-python/tests/cover/test_type_lookup.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import abc
1212
import enum
13+
from inspect import Parameter as P, Signature
1314
from typing import Callable, Dict, Generic, List, Sequence, TypeVar, Union
1415

1516
import pytest
@@ -20,6 +21,7 @@
2021
InvalidArgument,
2122
ResolutionFailed,
2223
)
24+
from hypothesis.internal.compat import get_type_hints
2325
from hypothesis.internal.reflection import get_pretty_function_description
2426
from hypothesis.strategies._internal import types
2527
from hypothesis.strategies._internal.core import _from_type
@@ -416,3 +418,11 @@ def _pos_and_kwd_only(x: int, *, y: str):
416418
def test_infer_all(func):
417419
# tests @given(...) against various signatures
418420
settings(max_examples=1)(given(...))(func)()
421+
422+
423+
def test_does_not_add_param_empty_to_type_hints():
424+
def f(x):
425+
pass
426+
427+
f.__signature__ = Signature([P("y", P.KEYWORD_ONLY)], return_annotation=None)
428+
assert get_type_hints(f) == {}

0 commit comments

Comments
 (0)