-
-
Notifications
You must be signed in to change notification settings - Fork 374
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
Decorator API with auto_attribs=True doesn't work. #466
Comments
Yes, unfortunately this is to be expected. There is no way for us to cheat x into the class namespace. It's just type declared but doesn't exist until This should be documented in http://www.attrs.org/en/stable/types.html |
I understand, thanks for the prompt response. That's a bummer, but I guess using attr.ib() for only attributes with validators isn't that much of an ugly workaround. |
This should be mentioned in the Validators section of the examples page, and in my opinion the existing notice in the Validators section of the init page is insufficiently clear, and should explicitly mention that it won't work when using the |
It might help to note that this still works:
So you still can use type annotations, validators, and |
attrs should provide a workaround for this. Like @attr.s(auto_attribs=True)
class A:
x: float
@attr.validator
def _check_x(self, attrib, value):
print("Validator x called.")
print(self, attrib, value)
y: float
z: float
@attr.validator_for('y')
def _check_y(self, attrib, value):
print("Validator y called.")
print(self, attrib, value)
a = A(5, 6, 7) I think the first case requires python >= 3.7 |
When I try to use attribute decorators via auto attributes style, I get an error saying that the name doesn't exist. This doesn't happen if I initiate attributes with attr.ib(). Is this expected behavior?
Here is an example that fails:
gives
The text was updated successfully, but these errors were encountered: