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

Static type check fails for proposed everything_except() recipe. #3753

Closed
jenstroeger opened this issue Sep 24, 2023 · 4 comments · Fixed by #3751
Closed

Static type check fails for proposed everything_except() recipe. #3753

jenstroeger opened this issue Sep 24, 2023 · 4 comments · Fixed by #3751
Labels
interop how to play nicely with other packages

Comments

@jenstroeger
Copy link
Contributor

jenstroeger commented Sep 24, 2023

Hello, following the example recipe given with the from_type() strategy (I removed the excluded_types: type | tuple[type] arg to simplify the code), I noticed that said recipe fails to type check:

# test.py

import typing

import hypothesis
import hypothesis.strategies as st

def everything_except() -> st.SearchStrategy[typing.Any]:
    return (
        st.from_type(type)
        .flatmap(st.from_type)
        .filter(lambda x: not isinstance(x, int))
    )

fails with this error:

test.py:1139:50: error: Cannot use a covariant type variable as a parameter  [misc]

A few observations:

  • Note the line number — it’s always that line no matter where in the code I place this function.
  • Placing a # type: ignore[misc] comment doesn’t work for any line in the above test.
  • Removing flatmap() passes the type checks.

Running that strategy seems to work, though:

@hypothesis.given(data=everything_except())
def test_data(data: typing.Any) -> None:
    print(type(data), data)

test_data()

produces the expected output.

This is for hypothesis v6.86.1 and mypy v1.5.1. Please let me know if you need more information, or if you’re able to reproduce.

I’m going to dig around some more later today to try and find the cause of the failing type check. For the time being, I’m going to (have to) decorate the function with @no_type_check to disable type checking.

@Zac-HD Zac-HD added the interop how to play nicely with other packages label Sep 24, 2023
@Zac-HD
Copy link
Member

Zac-HD commented Sep 24, 2023

Huh, that line number points to

def from_type(thing: Type[Ex]) -> SearchStrategy[Ex]:
and sure enough, if I change Ex -> Ex_Inv the error goes away. Patch incoming!

You might also want to report a bug against mypy for the confusing location report though, claiming it's in test.py sure seems like a bug to me.

@jenstroeger
Copy link
Contributor Author

Thank you for the instant fix @Zac-HD, I opened an issue with mypy also.

I’ll try again when the new Hypothesis release rolls out.

@Zac-HD
Copy link
Member

Zac-HD commented Sep 25, 2023

Hypothesis 6.87.0 has the fix and was released three hours ago 😁

@jenstroeger
Copy link
Contributor Author

Indeed it worked 👍🏼

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interop how to play nicely with other packages
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants