-
Notifications
You must be signed in to change notification settings - Fork 586
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
Issues with recursive data classes #3016
Comments
|
Ah, this looks like a bug - Thanks for the report - I'm on a camping trip this month, but if nobody else manages to fix it before July I should get to it then 🙂 |
|
Awesome, thanks. I'll try using the old school List until then. |
|
Just a note: using the old typing.List does work, but is painfully slow. Running the tests above takes about ~7 seconds. I have a hunch |
|
I will have a look! 👍 |
|
I found the reason for this. And as always it is So, let's see the difference: from typing import List
from dataclasses import dataclass
@dataclass
class UserOld:
following: List['User']
@dataclass
class UserNew:
following: list['User']And now the interesting part: print(UserOld.__annotations__)
# {'following': typing.List[ForwardRef('User')]}
print(UserNew.__annotations__)
# {'following': list['User']}Since we treat @Zac-HD what should we do? I know that typing API in python3.9 is different. Should we treat strings in 3.9 the same way as we treat |
|
Unclear what we could do, since we can't resolve the names in scope if typing failed to do so. Feel free to try, but I think the best we can manage in general might be a more specific error message. |
|
It looks like a bug in Python to me, because it does not typecheck type arguments to >>> from typing import List
>>> List[int] # no error, ok
typing.List[int]
>>> List[1] # error, ok
Traceback (most recent call last):
...
TypeError: Parameters to generic types must be types. Got 1.
>>> list[int] # no error, ok
list[int]
>>> list[1] # no error, not ok
list[1]You can try the same with And PEP does not mention this decision: https://www.python.org/dev/peps/pep-0585/#backwards-compatibility P.S. Not just |
|
Does this work with |
No, still fails for the same reason. |
|
But in the |
|
Yes, this works: from __future__ import annotations
from dataclasses import dataclass
from hypothesis import given, strategies as st
from typing import List
@dataclass
class User():
following: list[User] # works with typing.List
print(st.from_type(User).example())@JelleZijlstra thanks a lot for your help! I guess we need to improve the docs for this error. That's the best we can do. |
|
I've opened a PR to improve the error message and suggest |
Given the following:
I always receive the error
hypothesis.errors.InvalidArgument: thing=User must be a type. I have tried various permutations of usingstrategies.recursiveandstrategies.deferredto define custom strategies and pass them to@given, but have been unable to get this to work. I'm not sure if this is a bug or an issue with the documentation (perhaps it requires more recursive examples).The text was updated successfully, but these errors were encountered: