-
-
Notifications
You must be signed in to change notification settings - Fork 630
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
[RFC] Don't pass attr, obj, data to fields (de)serialize methods #2039
Comments
I gave it a quick look.
In I guess some users rely on those fields. OTOH, if they didn't exist, I doubt we'd modify (de)ser methods to pass I don't have a simple idea to replace those fields, though. pre_load/dump methods don't really answer because we don't want to modify the object. Unless we add some sort of proxy around the object but this sounds complicated. |
An option could be to remove So it wouldn't be a field by itself but rather a customization of an existing field. There would be a benefit in terms of auto-documentation, since the type of the value would be known (the user may choose to use We could add this feature and deprecate I was thinking of opening another issue about moving |
We could add class Field(FieldABC):
def __init__(..., accessor,...):
...
self.get_value = accessor
... Allowing the user to do that: def ser_function(...):
...
class MySchema(Schema):
def ser_method(...):
...
# Deprecated
old_function = fields.Function(ser_function)
# Replace with
new_function = fields.Field(accessor=ser_function)
# Deprecated
old_method = fields.Method(ser_method)
# Replace with
new_method = fields.Field(accessor=ser_method) |
I still need to get my head around this. If But then we wouldn't call Also, it is worth noting that fields working at |
We could keep Case 2 could be addressed by offering a way to pass custom accessors to any field constructor. We could even offer to define one for loading and one for dumping. As I wrote above, the user would benefit from the field being correctly typed and offering correct validation, etc. This would separate access and (de)serialization concerns. So basically,
I think this may finally be the way to get this right. Thoughts welcome. |
From a quick look, those are not used by existing fields. I suspect this might be or have been useful in specific fields such as
Method
.I can't investigate this thoroughly right now and anyway that would be a breaking change, but opening this here for discussion.
For separation of concerns, (de)ser methods shouldn't need the parent obj/data and field name.
Also, this means that one needs an object and a field name to serialize some data with a field, which may not always be the case. They may pass
None
but then they don't respect the API (since it won't work with a field (de)ser method that actually relies on those). I faced this specific problem withEnumValue
field, when buildingself.choices
, see #2017.The text was updated successfully, but these errors were encountered: