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

from_type raises TypeError for Union of builtin generic types #3080

Closed
ghost opened this issue Sep 2, 2021 · 3 comments · Fixed by #3163
Closed

from_type raises TypeError for Union of builtin generic types #3080

ghost opened this issue Sep 2, 2021 · 3 comments · Fixed by #3163
Labels
bug something is clearly wrong here

Comments

@ghost
Copy link

ghost commented Sep 2, 2021

On the current version of hypothesis (6.17.4) on python 3.9.2

st.from_type(Union[list[str], str]).example()

raises TypeError("issubclass() arg 1 must be a class") because list[int] is an instance of types.GenericAlias and not an instance of type.

@ghost
Copy link
Author

ghost commented Sep 2, 2021

I think this guard against types that are not really types

should be

if isinstance(t, types.GenericAlias):
    ....

Bonus fact: if this needs to guard against Union too the check should also include typing._UnionGenericAlias as that is not a subclass of GenericAlias for some reason.

@Zac-HD Zac-HD added the bug something is clearly wrong here label Sep 3, 2021
@Zac-HD
Copy link
Member

Zac-HD commented Sep 3, 2021

Huh, yeah - thanks for the report! Looks like we missed/under-tested the equivalent of #2979 for Python 3.9 (PEP 585).

@Zac-HD
Copy link
Member

Zac-HD commented Sep 6, 2021

OK, that specific check can just use a try/except TypeError.

The underlying problem is that we have never really tested Hypothesis with the new-in-3.9 types.GenericAlias type, which I expect to see a lot more of over the next few years (e.g. list[int] is a lot more attractive than constantly importing stuff from the typing module). I expect that we'll need to update a few things to check "is this type generic" in a more, um, generic way; at least accessing __origin__ and __args__ seems to work the same way.

@Zac-HD Zac-HD changed the title from_type raises TypeError for Union of generic types from_type raises TypeError for Union of builtin generic types Sep 9, 2021
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.

1 participant