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

Untyped decorator makes function untyped for strategies.composite decorator #3786

Closed
TomNicholas opened this issue Nov 10, 2023 · 2 comments · Fixed by #3789
Closed

Untyped decorator makes function untyped for strategies.composite decorator #3786

TomNicholas opened this issue Nov 10, 2023 · 2 comments · Fixed by #3789
Assignees
Labels
legibility make errors helpful and Hypothesis grokable

Comments

@TomNicholas
Copy link
Contributor

TomNicholas commented Nov 10, 2023

I expected this to pass mypy - am I typing this incorrectly or is hypothesis not typing the strategies.composite decorator correctly?

from __future__ import annotations

import hypothesis.strategies as st


@st.composite
def my_integers(
    draw: st.DrawFn,
) -> st.SearchStrategy[int]:
    return draw(st.integers())
error: Untyped decorator makes function "my_integers" untyped  [misc]

hypothesis==6.88.3, mypy==1.7.0

Came up in dask/dask#9374

@TomNicholas
Copy link
Contributor Author

Actually maybe I was just supposed to type it like this instead

@st.composite
def my_integers(
    draw: st.DrawFn,
) -> int:
    return draw(st.integers())

@Zac-HD Zac-HD self-assigned this Nov 12, 2023
@Zac-HD
Copy link
Member

Zac-HD commented Nov 12, 2023

Yep, I think the latter is correct - although you could go a step further and

def my_integers() -> SearchStrategy[int]:
    return st.integers()

This does look a little silly, but I often see uses of @st.composite which could be an undecorated function which defines some strategies for arguments and then returns st.builds(...).


I can't find an overload which makes typecheckers provide a more useful message, but I can add a runtime warning easily enough 🙂

@Zac-HD Zac-HD added the legibility make errors helpful and Hypothesis grokable label Nov 12, 2023
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

Successfully merging a pull request may close this issue.

2 participants