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

Closed
ClementWalter opened this issue Dec 1, 2024 · 2 comments · Fixed by #4285
Closed

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

ClementWalter opened this issue Dec 1, 2024 · 2 comments · Fixed by #4285
Labels
bug something is clearly wrong here

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 🙂

@tybug tybug added bug something is clearly wrong here and removed legibility make errors helpful and Hypothesis grokable labels Mar 3, 2025
@tybug
Copy link
Member

tybug commented Mar 3, 2025

I think this is actually a bug since you explicitly registered IsDataclass, which should be fixed by #4285!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug something is clearly wrong here
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants