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

RFC: Remove FormattedString field #1141

Closed
sloria opened this issue Feb 15, 2019 · 2 comments · Fixed by #1142
Closed

RFC: Remove FormattedString field #1141

sloria opened this issue Feb 15, 2019 · 2 comments · Fixed by #1142

Comments

@sloria
Copy link
Member

sloria commented Feb 15, 2019

fields.FormattedString only exists because it was ported from Flask-RESTful. It has a couple peculiarities:

  1. It converts the object to a dict in a way that is error-prone and inefficient.
  2. Its deserialization behavior is no-op, making it asymettrical with its serialization behavior.

fields.Function and fields.Method achieve the same use case in a more performant way.

from dataclasses import dataclass
from marshmallow import Schema, fields


class MySchema(Schema):
    formatted = fields.FormattedString("{first_name} {last_name}")
    function = fields.Function(lambda u: f"{u.first_name} {u.last_name}")


@dataclass
class User:
    first_name: str
    last_name: str


u = User(first_name="Steve", last_name="Loria")
data = MySchema().dump(u)
assert data["formatted"] == data["function"]
@lafrech
Copy link
Member

lafrech commented Feb 15, 2019

#1046

This field achieves a function that could be achieved otherwise (property, pre_dump,...). I think it could be dropped without too much collateral damage.

@sloria
Copy link
Member Author

sloria commented Feb 15, 2019

If there are no objections to this proposal, I'll plan on merging #1142 at the end of next week.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants