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_type doesn't work for type alias (typing.TypeAliasType) #4242

Open
ilkecan opened this issue Jan 14, 2025 · 1 comment
Open

st.from_type doesn't work for type alias (typing.TypeAliasType) #4242

ilkecan opened this issue Jan 14, 2025 · 1 comment
Labels
enhancement it's not broken, but we want it to be better

Comments

@ilkecan
Copy link

ilkecan commented Jan 14, 2025

Given a type alias (which is of type TypeAliasType) defined like:

>>> type Point = tuple[float, float]
>>> type(Point)
<class 'typing.TypeAliasType'>

I want to create a strategy and an example from that:

from hypothesis import strategies as st
st.from_type(Point).example()

which results in an error

...
.venv/lib/python3.13/site-packages/hypothesis/strategies/_internal/core.py", line 1287, in _from_type
    raise InvalidArgument(f"{thing=} must be a type")  # pragma: no cover
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
hypothesis.errors.InvalidArgument: thing=Point must be a type

Accessing the underlying value through __value__ works as expected:

>>> st.from_type(Point.__value__).example()
(1.192092896e-07, -7420061444470370.0)

I am not familiar with the internals of hypothesis but since Point.__value__ works, I am inclined to think Point should also just work by handling typing.TypeAliasType internally.

python version: Python 3.13.1 (main, Dec 3 2024, 17:59:52) [GCC 14.2.1 20241116] on linux
hypothesis version: 6.123.17

@tybug tybug added the enhancement it's not broken, but we want it to be better label Jan 14, 2025
@Zac-HD
Copy link
Member

Zac-HD commented Jan 24, 2025

That makes sense to me - we could handle it just below NewType, and in much the same way. Tests would go in a new tests/cover/test_typealias_py312.py file, and add this config for 3.12 so it doesn't give SyntaxError on older versions.

Would you be interested in opening a PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement it's not broken, but we want it to be better
Projects
None yet
Development

No branches or pull requests

3 participants