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

Add field-level pre/post-load API #2787

Open
sloria opened this issue Jan 17, 2025 · 5 comments
Open

Add field-level pre/post-load API #2787

sloria opened this issue Jan 17, 2025 · 5 comments

Comments

@sloria
Copy link
Member

sloria commented Jan 17, 2025

As suggested by @lafrech in #2786 (review), we can enable pre/post-load on fields to address use cases like whitespace trimming (#1391) via an API similar to the schema-level pre/post_load/dump API.

Something like:

from marshmallow import Schema, fields

class ArtistSchema(Schema):
    name = fields.Str(post_load=(str.strip, str.upper))
    email = fields.Str(post_load=(str.strip, str.upper))

# and/or

from marshmallow import Schema, fields, field_post_load

class ArtistSchema(Schema):
    name = fields.Str()
    email = fields.Str()

    @field_post_load("name", "email")
    def strip_whitespace(self, field_name: str, value: str) -> str:
        return value.strip()
  • pro: allows field-specific transformations without having to implement a custom field
  • pro: non-breaking change
  • con: overlaps with existing functionality provided by custom fields and the schema-level @pre/post_load

Alternatives considered

Allow validators to transform values

  • same as pydantic and sqlalchemy
  • pro: minimal API surface added
  • pro: minimal internal code changes
  • con: it's not intuitive that validators can mutate values
  • con: doesn't solve for pre-validation transformations or dumping/serialization
  • con: breaking change
@llucax
Copy link

llucax commented Jan 20, 2025

Why is this considered a breaking change?

@sloria
Copy link
Member Author

sloria commented Jan 20, 2025

sorry for the confusion. The breaking change refers to the 'validators transform values' alternative, not the proposed approach

@sloria
Copy link
Member Author

sloria commented Jan 20, 2025

on second thought, i don't think field-level pre/post-dump makes as much sense as pre/post-load. field-level transformations are much less common on serialization than in deserialization, and custom fields already address the rare cases when they're necessary.

@sloria sloria changed the title Add field-level pre/post-load/dump API Add field-level pre/post-load API Jan 20, 2025
@sloria
Copy link
Member Author

sloria commented Jan 20, 2025

I also have medium confidence that @field_pre/post_load is unnecessary. the schema-level @pre/post_load already provides functionality for doing transformations on one or more fields.

@sloria
Copy link
Member Author

sloria commented Jan 20, 2025

proof of concept of the pre_load and post_load parameters in #2799

@sloria sloria pinned this issue Jan 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants