Skip to content

Commit

Permalink
Tests for non-runtime-checkable Protocols
Browse files Browse the repository at this point in the history
  • Loading branch information
Zac-HD committed Aug 3, 2022
1 parent f3282ce commit 72efbcf
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions hypothesis-python/tests/cover/test_lookup_py38.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import re
import sys
import typing
from types import SimpleNamespace

import pytest

Expand Down Expand Up @@ -222,3 +223,29 @@ def test_example_argument_validation():
),
):
example(x=None)(has_posonly_args)(1)


class FooProtocol(typing.Protocol):
def frozzle(self, x):
pass


class BarProtocol(typing.Protocol):
def bazzle(self, y):
pass


@given(st.data())
def test_can_resolve_registered_protocol(data):
with temp_registered(
FooProtocol,
st.builds(SimpleNamespace, frozzle=st.functions(like=lambda x: ...)),
):
obj = data.draw(st.from_type(FooProtocol))
assert obj.frozzle(x=1) is None


def test_cannot_resolve_un_registered_protocol():
msg = "Instance and class checks can only be used with @runtime_checkable protocols"
with pytest.raises(TypeError, match=msg):
st.from_type(BarProtocol).example()

0 comments on commit 72efbcf

Please sign in to comment.