reagento / adaptix

An extremely flexible and configurable data model conversion library.
https://adaptix.readthedocs.io
Apache License 2.0
348 stars 24 forks source link

Allow redefining coercer inside ``Optional`` using inner type if source and destination types are same #278

Closed zhPavel closed 1 month ago

zhPavel commented 1 month ago
from dataclasses import dataclass
from datetime import datetime, UTC
from typing import Optional

from adaptix.conversion import get_converter, coercer

@dataclass
class X:
    a: Optional[datetime]

@dataclass
class Y:
    a: Optional[datetime]

x_to_y = get_converter(
    X, Y, recipe=[coercer(datetime, datetime, lambda x: x.replace(tzinfo=UTC))]
)

print(x_to_y(X(datetime.now())))

This code prints Y(a=datetime.datetime(2024, 6, 1, 19, 33, 17, 289641)) with no timezone