question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

fields.Method result should be able to be wrapped into other field types

See original GitHub issue

fields.Method is pretty much always used to return a certain type of value. It doesn’t allow to choose options for the return value though.

For example, if one designes a field which should return a generic datetime and format it using a given format string, one is forced to use fields.Method which is linked to a schema method which returns a string(!), not a datetime. Because it is now impossible to provide a format string for the datetime anywhere except of the method itself.

In other words, something like this:

class MySchema(Schema):
    created_at = fields.Datetime(method='_created_at', format='%Y-%m-%dT%H:%M:%S%z')

    def _created_at(self, obj):
        return obj.metadata().creation_time()

would be much more convenient and reusable than this:

class MySchema(Schema):
    created_at = fields.Method('_formatted_created_at')

    def _formatted_created_at(self, obj):
        return obj.metadata().created_at().strftime('%Y-%m-%dT%H:%M:%S%z')

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:4
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
lafrechcommented, Oct 12, 2018

from_field already exists. It is the attribute parameter.

We could add parameters dump_method and load_method. And deprecate Method field.

And likewise *_function, unless there is a way to do both functions and methods with the same parameter.

0reactions
1Markcommented, Aug 9, 2022

Any progress on this?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Customizing the behavior of cached fields - Apollo GraphQL
If a field requires numerous parameters then each parameter must be wrapped in a variable that is then destructured and returned. Each parameter...
Read more >
Fields - C# Programming Guide | Microsoft Learn
A field in C# is a variable of any type that is declared directly in a class or struct. Fields are members of...
Read more >
Fields — marshmallow 3.19.0 documentation
Concrete Field classes should implement this method. Parameters: value – The value to be serialized. attr (str) – The attribute or key on...
Read more >
Factory function for dataclass fields - python - Stack Overflow
I was thinking I could write a factory function to wrap dataclass.field and help reduce the amount of repetition. However, I can't seem...
Read more >
Fields Method - TechDocs - Broadcom
Gen™ 8.6. Version 8.6. 8.6 8.5. Open/Close Topics Navigation. Product Menu. Topics. Announcement and News · Release Notes.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found