Skip to content

Custom serializer with Optional (aka | None) #436

Answered by yukinarit
davetapley asked this question in Q&A
Discussion options

You must be logged in to vote

How about using *args and **kwargs?

def field_timedelta(*args, **kwrags):
    serde.field(*args, **kwargs, serializer=lambda s: None if s is None else parse_duration(s))


@dataclass
class ReportConfig:
    schedule: timedelta | None = field_timedelta(default=None)

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@yukinarit
Comment options

Answer selected by davetapley
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants