Skip to content

Commit

Permalink
Fix rare type hints issue
Browse files Browse the repository at this point in the history
I noticed this via the ghostwriter when playing around with the recent keyword-related issues.
  • Loading branch information
Zac-HD committed May 1, 2022
1 parent e627a0a commit 67f0546
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
5 changes: 5 additions & 0 deletions hypothesis-python/RELEASE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
RELEASE_TYPE: patch

This patch fixes a rare bug where we could incorrectly treat
:obj:`~python:inspect.Parameter.empty` as a type annotation,
if the callable had an explicitly assigned ``__signature__``.
6 changes: 5 additions & 1 deletion hypothesis-python/src/hypothesis/internal/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,11 @@ def get_type_hints(thing):

vkinds = (inspect.Parameter.VAR_POSITIONAL, inspect.Parameter.VAR_KEYWORD)
for p in inspect.signature(thing).parameters.values():
if p.kind not in vkinds and is_a_type(p.annotation):
if (
p.kind not in vkinds
and is_a_type(p.annotation)
and p.annotation is not p.empty
):
if p.default is None:
hints[p.name] = typing.Optional[p.annotation]
else:
Expand Down
10 changes: 10 additions & 0 deletions hypothesis-python/tests/cover/test_type_lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import abc
import enum
from inspect import Parameter as P, Signature
from typing import Callable, Dict, Generic, List, Sequence, TypeVar, Union

import pytest
Expand All @@ -20,6 +21,7 @@
InvalidArgument,
ResolutionFailed,
)
from hypothesis.internal.compat import get_type_hints
from hypothesis.internal.reflection import get_pretty_function_description
from hypothesis.strategies._internal import types
from hypothesis.strategies._internal.core import _from_type
Expand Down Expand Up @@ -416,3 +418,11 @@ def _pos_and_kwd_only(x: int, *, y: str):
def test_infer_all(func):
# tests @given(...) against various signatures
settings(max_examples=1)(given(...))(func)()


def test_does_not_add_param_empty_to_type_hints():
def f(x):
pass

f.__signature__ = Signature([P("y", P.KEYWORD_ONLY)], return_annotation=None)
assert get_type_hints(f) == {}

0 comments on commit 67f0546

Please sign in to comment.