-
-
Notifications
You must be signed in to change notification settings - Fork 50
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
Enum items property is incorrect when a validate method is provided #190
Comments
So you are definitively in the gray region of what is valid when customizing members. Enum is not the only that can be broken in this way I believe. The central issue here is that the items are only stored in the validate_mode so if you customize it there is no way to recover items. It won't fix your issue but we could have items raise if the validate mode is not Enum. At least it would avoid ending with a broken beahvior silently. WDYT ? |
I think that'll work, have the error suggest overriding it as I did. |
What would you think about adding a pre_setattr that would run before validate? Another thing I ran into recently is trying to use a custom list (MutableMapping) with a List field. If I use coerced then it does not validate the list items. |
If you want an homogeneous user experience it would have to be a Could you expand on your use case ? |
I am trying to have a list member with custom methods to commit changes to the database. Basically the same as django's related manager. See code and usage. It uses post_getattr to convert to a RelatedList which works, but the problem is that it breaks assignment unless I cast it back with list(). With a pre_validate I could convert it back to a normal list while still having the list items be validated by atom. |
Out of cusiority have you considered directly coercing to your custom type and make it wrap an atom list ? And use post_setattr to manage observers ? |
For your particular case I feel like you could use a Coerced and some post_setattr customization to handle observers. That being said I was always a bit bothered by the way coercion and type validation worked (or rather did not work together). So if you feel up to implementing a pre_validate mechanism please go for it ! The addition of the getstate mode mean you have room to store the mode on the member. |
And sorry for the late reply |
I actually gave some extra though about having coercion as a pre-validate mode and it does not really work out that well. Typically one wants coercion to occur if the item fails to validate so it cannot run before validation. One thing we could however consider is to introduce a coerce mode and to modify the generic validation path to attempt coercion if validation fails on the input. Such a solution would viable I believe and reduce the coupling between type validation and coercion. WDYT ? |
Edit. From looking at the code I suppose this could just be considered incorrect usage. I'm trying to track which fields are invalid. Maybe there is a better way to do it? I just decided to subclass the Enum and override the items property.
The text was updated successfully, but these errors were encountered: