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

builds fails when used with attrs classes containing private attributes #3791

Closed
AdrianSosic opened this issue Nov 15, 2023 · 1 comment · Fixed by #3807
Closed

builds fails when used with attrs classes containing private attributes #3791

AdrianSosic opened this issue Nov 15, 2023 · 1 comment · Fixed by #3807
Labels
bug something is clearly wrong here

Comments

@AdrianSosic
Copy link

Hi, I think I may have found a bug that occurs when using builds with automatic attribute inference from attrs-built classes that have required but "private" attributes. The bug is probably due to the fact that the name exposed via the constructor differs from the attribute name in these cases, since attrs by default strips the leading underscore.

Here a minimal example:

from attrs import define
from hypothesis import given, strategies as st


@define
class MyClass:
    _x: int


@given(st.builds(MyClass))
def test_class(my_obj):
    assert isinstance(my_obj, MyClass)

The error you get:

AttributeError: 'MyClassAttributes' object has no attribute 'x'
@Zac-HD Zac-HD added the bug something is clearly wrong here label Nov 15, 2023
@AdrianSosic
Copy link
Author

Actually, the problem is not restricted to required arguments but also appears when explicitly requesting strategies for default argument via the ... syntax, since the same name matching machinery is used and fails.

To be concrete, if the class was instead

@define
class MyClass:
    _x: int = 0

then neither builds(MyClass, x=...) nor builds(MyClass, _x=...) would work.

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.

2 participants