Skip to content

Commit 10295ae

Browse files
authored
Merge pull request #4285 from tybug/fromtype-forwardref-fix
Fix `from_type` error on certain protocols
2 parents aab0141 + 531c926 commit 10295ae

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

hypothesis-python/RELEASE.rst

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
RELEASE_TYPE: patch
2+
3+
This patch fixes a bug where :func:`~hypothesis.strategies.from_type` would error on certain types involving :class:`~python:typing.Protocol` (:issue:`4194`).

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,9 @@ def from_typing_type(thing):
541541
else:
542542
union_elems = ()
543543
if not any(
544-
isinstance(T, type) and issubclass(int, get_origin(T) or T)
544+
# see https://github.com/HypothesisWorks/hypothesis/issues/4194 for
545+
# try_issubclass.
546+
isinstance(T, type) and try_issubclass(int, get_origin(T) or T)
545547
for T in [*union_elems, elem_type]
546548
):
547549
mapping.pop(bytes, None)

hypothesis-python/tests/cover/test_lookup.py

+18
Original file line numberDiff line numberDiff line change
@@ -1237,3 +1237,21 @@ def test_from_type_resolves_required_posonly_args(n: CustomInteger):
12371237
# st.builds() does not infer for positional arguments, but st.from_type()
12381238
# does. See e.g. https://stackoverflow.com/q/79199376/ for motivation.
12391239
assert isinstance(n, CustomInteger)
1240+
1241+
1242+
class MyProtocol(typing.Protocol):
1243+
pass
1244+
1245+
1246+
def test_issue_4194_regression():
1247+
# this was an edge case where we were calling issubclass on something
1248+
# that was not a type, which errored. I don't have a more principled test
1249+
# case or name for this.
1250+
inner = typing.Union[typing.Sequence["A"], MyProtocol]
1251+
A = typing.Union[typing.Sequence[inner], MyProtocol]
1252+
1253+
with (
1254+
temp_registered(MyProtocol, st.just(b"")),
1255+
temp_registered(typing.ForwardRef("A"), st.integers()),
1256+
):
1257+
find_any(st.from_type(A))

0 commit comments

Comments
 (0)