Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

st.from_types with Union and Protocol weirdly requires int not to throw #4194

Open
ClementWalter opened this issue Dec 1, 2024 · 1 comment
Labels
legibility make errors helpful and Hypothesis grokable

Comments

@ClementWalter
Copy link

I've managed to extract this MWE from my buggy tests

from typing import ClassVar, Dict, ForwardRef, Protocol, Sequence, TypeAlias, Union

from hypothesis import given, settings
from hypothesis import strategies as st


class IsDataclass(Protocol):
    __dataclass_fields__: ClassVar[Dict]


Extended: TypeAlias = Union[Sequence["Extended"], int, bool, bytes, IsDataclass]

st.register_type_strategy(IsDataclass, st.just(b""))
st.register_type_strategy(
    ForwardRef("Extended"),
    st.recursive(st.one_of(st.integers(), st.booleans(), st.binary()), st.tuples),
)


@given(a=...)
@settings(max_examples=1000)
def test_extended(a: Extended):
    print(a)

Defining Extended without int, ie just like

Extended: TypeAlias = Union[Sequence["Extended"], bool, bytes, IsDataclass]

will crash with

E           TypeError: Instance and class checks can only be used with @runtime_checkable protocols

I'm using python3.10 and from my uv.lock I see

[[package]]
name = "hypothesis"
version = "6.112.1"
source = { registry = "https://pypi.org/simple" }
dependencies = [
    { name = "attrs" },
    { name = "exceptiongroup", marker = "python_full_version < '3.11'" },
    { name = "sortedcontainers" },
]
@ClementWalter ClementWalter changed the title st.from_types weirdly requires int not to throw st.from_types with Union and Protocol weirdly requires int not to throw Dec 1, 2024
@Zac-HD Zac-HD added the legibility make errors helpful and Hypothesis grokable label Dec 2, 2024
@Zac-HD
Copy link
Member

Zac-HD commented Dec 2, 2024

Well, that's definitely not ideal - but I think the intended behavior is to throw even if int is included, because isinstance(x, IsDataclass) will raise TypeError: Instance and class checks can only be used with @runtime_checkable protocols without Hypothesis involved at all. (and st.from_type() has to be able to check isinstance()/issubclass() to work)

So our main project is to work out why adding int stops that error from appearing, and see if we can make it consistent 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
legibility make errors helpful and Hypothesis grokable
Projects
None yet
Development

No branches or pull requests

2 participants