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.

Allow adding custom types to _native_to_marshmallow

See original GitHub issue

Follow up to #15, this is to suggest exposting _native_to_marshmallow publicly or, better yet, allow registring custom types to the list for more flexibility on the types that could be user-defined and which fields these types should translate to.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
alextremblaycommented, Jul 30, 2019

There was an idea introduced in another issue (#23) That I think could work very well here:

from typing import List
from ipaddress import IPv4Address

from marshmallow.fields import Field
from marshmallow_dataclass import dataclass, NewType

class IPField(Field):
    ... # serializes an IPv4Address instance into a str, and deserializes back into an instance

#IP = NewType('IP', str, validate=my_validation_function) 
#This was the suggestion in the other issue

IP = NewType('IP', IPv4Address, marshmallow_field=IPField)

@dataclass
class Company:
    name: str
    ipv4s: List[IP]

The idea is to have a NewType implementation that works the same as typing.NewType, but also stores all the field metadata that we would normally store on a dataclass field, like validation functions and marshmallow_field selections.

Thoughts?

1reaction
evanfwelchcommented, May 16, 2019

I would also find this valuable… a mapping you can can pass to class_schema that gets merged with _native_to_marshmallow.

    if typ in _marshmallow_mapping:
        field_fn = _marshmallow_mapping[typ]
        if inpsect.isfunction(field_fn):
            return field_fn(typ, **metadata)
        else: #assume it's just a marshmallow field
             return field_fn(**metadata)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Quickstart — marshmallow 3.19.0 documentation
Want to create your own field type? See the Custom Fields page. Need to add schema-level validation, post-processing, or error handling behavior? See...
Read more >
Object validation and conversion with Marshmallow in Python
Marshmallow is a Python library that converts complex data types to and from Python data types. It is a powerful tool for both...
Read more >
How to use marshmallow to serialize a custom sqlalchemy field?
The ModelConverter class has a SQLA_TYPE_MAPPING attribute you can override in a subclass to add your custom GUID field to the types detected...
Read more >
Make typing easier for custom types defined by marshmallow ...
I've been giving it a look, but I'm a bit puzzled about which types are required and which are not. For example, Email...
Read more >
marshmallow - Read the Docs
To create a custom field class, create a subclass of marshmallow.fields. ... Serialize an object to native Python data types according to this...
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