Skip to content

Commit d1efe56

Browse files
committed
Distinguish globals/locals tests
1 parent 9cb67a2 commit d1efe56

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

hypothesis-python/src/hypothesis/strategies/_internal/core.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,6 +1077,12 @@ def as_strategy(strat_or_callable, thing, final=True):
10771077
# See https://github.com/python/cpython/pull/17214 for details.
10781078
optional = getattr(thing, "__optional_keys__", ())
10791079
anns = {k: from_type(v) for k, v in get_type_hints(thing).items()}
1080+
if (
1081+
(not anns)
1082+
and thing.__annotations__
1083+
and ".<locals>." in getattr(thing, "__qualname__", "")
1084+
):
1085+
raise InvalidArgument("Failed to retrieve type annotations for local type")
10801086
return fixed_dictionaries( # type: ignore
10811087
mapping={k: v for k, v in anns.items() if k not in optional},
10821088
optional={k: v for k, v in anns.items() if k in optional},

hypothesis-python/tests/nocover/test_type_lookup_future_annotations.py

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,37 @@
1212

1313
from typing import TypedDict, Union
1414

15+
import pytest
16+
1517
from hypothesis import given, strategies as st
18+
from hypothesis.errors import InvalidArgument
1619

20+
alias = Union[int, str]
1721

18-
@given(st.data())
19-
def test_complex_forward_ref_in_typed_dict(data):
20-
alias = Union[int, bool]
2122

22-
class A(TypedDict):
23-
a: int
23+
class A(TypedDict):
24+
a: int
2425

25-
class B(TypedDict):
26-
a: A
27-
b: alias
2826

29-
b_strategy = st.from_type(B)
30-
d = data.draw(b_strategy)
27+
class B(TypedDict):
28+
a: A
29+
b: alias
30+
31+
32+
@given(st.from_type(B))
33+
def test_complex_forward_ref_in_typed_dict(d):
3134
assert isinstance(d["a"], dict)
3235
assert isinstance(d["a"]["a"], int)
33-
assert isinstance(d["b"], (int, bool))
36+
assert isinstance(d["b"], (int, str))
37+
38+
39+
def test_complex_forward_ref_in_typed_dict_local():
40+
local_alias = Union[int, str]
41+
42+
class C(TypedDict):
43+
a: A
44+
b: local_alias
45+
46+
c_strategy = st.from_type(C)
47+
with pytest.raises(InvalidArgument):
48+
c_strategy.example()

0 commit comments

Comments
 (0)