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
Problem with decorated __init__
#3029
Comments
A reproducing example would be really helpful, thanks! Seems related to #961, #2603, #2495 and the "use |
Hi @Zac-HD , import functools
from hypothesis import given
import hypothesis.strategies as st
def some_decorator(func):
def wrapper(*args, **kwargs):
return func(*args, **kwargs)
functools.update_wrapper(wrapper=wrapper, wrapped=func)
return wrapper
class SomeClass:
@some_decorator
def __init__(self, x: int) -> None:
self.x = x
def main() -> None:
@given(something=st.builds(SomeClass))
def test(something):
print(something.x)
test()
# Raises:
# Traceback (most recent call last):
# ...
# TypeError: __init__() missing 1 required positional argument: 'x'
if __name__ == "__main__":
main() Please let me know if you need any more details etc. (Edit: added traceback) |
Hi,
While investigating #3026, I noticed that
required_args
function usesinspect.getfullargspec
:Unfortunately,
inspect.getfullargspec
will not inspect the wrapper chain according toinspect.getfullargspec
's current implementation in the stdlib:This means that when you use
required_args
in_from_type
, you will get different behavior depending on whether the constructor has been decorated (norequired
) or not (required
reflects the actual arguments). Mind that decorator in many cases does not change the signature; moreover, this behavior is the same even iffunctools.update_wrapper
is called (due toinspect.getfullargspec
).Here is the relevant snippet from
_from_type
:In particular, this will cause
hypothesis.strategies.builds
to ignore the arguments of the decorated__init__
with a rather confusing error message for the user.Please let me know how you'd like to proceed with this issue. I could write a small snippet so that you can reproduce the issue etc.
The text was updated successfully, but these errors were encountered: