You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This week, I've started to use marshmallow and marshmallow_dataclass at work for the first time and it's absolutely great. However, I was wondering if there was a reason not to use a generic schema to keep track of the wrapped dataclass when using class_schema factory.
The basic idea is pretty simple:
# have a custom schema class that is used only for type checkingclassDataclassSchema(Generic[_T]):
ifTYPE_CHECKING:
# use some overloading here to chose between the two result typesdefload(...) ->Union[_T, List[_T]]: ...
# update `class_schema` signaturedefclass_schema(clazz: Typ[_T], ...) ->Type[DataclassSchema[_T]]: ...
Then we can create a dataclass and the corresponding schema:
@dataclassclassTest:
a : intb : str# The types can actually be inferred by Pyrightschema: DataclassSchema[Test] =class_schema(Test)()
data: Test=schema.load({"a": 0, "b": "hellow world :)")
I have created a first implementation of this proposal in this fork. I am not familiar with mypy (I use Pyright through Pylance) and really advanced typing so I don't know if this is the best way to achieve my goal. If this subject interests you I'd be glad to contribute :)
Cheers,
Vincent
Edit: Sorry for the tag....
The text was updated successfully, but these errors were encountered:
Yes, I'm proposing something similar. Actually, my first implementation works really well in real conditions (used it at work this week). I'm going to try another implentation by using marshmallow-generic but it would still require to modify the signature of marshmallow_dataclass.class_schema to use this generic Schema and a dataclass TypeVar instead of type and Schema.
Hello,
This week, I've started to use marshmallow and marshmallow_dataclass at work for the first time and it's absolutely great. However, I was wondering if there was a reason not to use a generic schema to keep track of the wrapped dataclass when using
class_schema
factory.The basic idea is pretty simple:
Then we can create a dataclass and the corresponding schema:
I have created a first implementation of this proposal in this fork. I am not familiar with mypy (I use Pyright through Pylance) and really advanced typing so I don't know if this is the best way to achieve my goal. If this subject interests you I'd be glad to contribute :)
Cheers,
Vincent
Edit: Sorry for the tag....
The text was updated successfully, but these errors were encountered: