Skip to content

Commit

Permalink
if there is a syntax error in the annotations the should be ignored f…
Browse files Browse the repository at this point in the history
…or the function
  • Loading branch information
ThunderKey committed Jan 26, 2023
1 parent 38ac9d9 commit f5e4ee4
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
10 changes: 8 additions & 2 deletions hypothesis-python/src/hypothesis/extra/ghostwriter.py
Expand Up @@ -831,8 +831,14 @@ def _annotate_args(
) -> Iterable[str]:
arg_parameters: DefaultDict[str, Set[Any]] = defaultdict(set)
for func in funcs:
for key, param in _get_params(func, eval_str=True).items():
arg_parameters[key].add(param.annotation)
try:
params = _get_params(func, eval_str=True)
except Exception:
# don't add parameters if the annotations could not be evaluated
pass
else:
for key, param in params.items():
arg_parameters[key].add(param.annotation)

for argname in argnames:
parameters = arg_parameters.get(argname)
Expand Down
Expand Up @@ -28,3 +28,7 @@ def merge_dicts(
map1: collections.abc.Mapping[str, int], map2: collections.abc.Mapping[str, int]
) -> collections.abc.Mapping[str, int]:
return {**map1, **map2}


def invalid_types(attr1: int, attr2: UnknownClass, attr3: str) -> None:
pass
12 changes: 12 additions & 0 deletions hypothesis-python/tests/ghostwriter/recorded/invalid_types.txt
@@ -0,0 +1,12 @@
# This test code was written by the `hypothesis.extra.ghostwriter` module
# and is provided under the Creative Commons Zero public domain dedication.

import example_code.future_annotations
from hypothesis import given, strategies as st

# TODO: replace st.nothing() with appropriate strategies


@given(attr1=st.nothing(), attr2=st.nothing(), attr3=st.nothing())
def test_fuzz_invalid_types(attr1, attr2, attr3) -> None:
example_code.future_annotations.invalid_types(attr1=attr1, attr2=attr2, attr3=attr3)
10 changes: 9 additions & 1 deletion hypothesis-python/tests/ghostwriter/test_expected_output.py
Expand Up @@ -25,7 +25,11 @@

import numpy
import pytest
from example_code.future_annotations import add_custom_classes, merge_dicts
from example_code.future_annotations import (
add_custom_classes,
invalid_types,
merge_dicts,
)

import hypothesis
from hypothesis.extra import ghostwriter
Expand Down Expand Up @@ -132,6 +136,10 @@ def union_sequence_parameter(items: Sequence[Union[float, int]]) -> float:
("merge_dicts", ghostwriter.magic(merge_dicts)),
marks=pytest.mark.skipif("sys.version_info[:2] < (3, 10)"),
),
pytest.param(
("invalid_types", ghostwriter.magic(invalid_types)),
marks=pytest.mark.skipif("sys.version_info[:2] < (3, 10)"),
),
("magic_base64_roundtrip", ghostwriter.magic(base64.b64encode)),
(
"magic_base64_roundtrip_with_annotations",
Expand Down

0 comments on commit f5e4ee4

Please sign in to comment.