-
Notifications
You must be signed in to change notification settings - Fork 76
When type hint is not recognized (e.g. pd.Timestamp
), we recursively try to convert into a dataclass which mutates the constructor throughout the codebase
#51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
marshmallow_dataclass allows you to have simple classes, like : class A:
a: int
class B:
b: A
marshmallow_dataclass.class_schema(B) In order to create a schema for B, it also has to create a schema for A. And in order to apply I don't think it's possible to fix this issue without breaking backward compatibility... |
If you want a custom type in your class, the best way is to use a NewType declaration |
Ok I see your point. In my (and my colleagues) use case, we would already have made both
However I guess you've primarily designed for the case where folks are using the What if we modified the logic for checking whether |
I think that would be acceptable... |
In order to avoid breaking compatibility, I am simply adding a new warning that will be displayed before marshmallow_dataclass tries to alter a class :
To anyone who would end up here after seeing this warning: to fix this , you should make sure that all the fields of all your classes are either simple base types, or dataclasses themselves. For instance, the following is good : import marshmallow_dataclass
import dataclasses
@dataclasses.dataclass
class A:
a: int
class B:
b: A
marshmallow_dataclass.class_schema(B) and the following is bad : import marshmallow_dataclass
class A:
a: int
class B:
b: A
marshmallow_dataclass.class_schema(B) and this is the worse, because marshmallow_dataclass will change the Path class, and break it globally in your whole application : from pathlib import Path
class B:
b: Path
marshmallow_dataclass.class_schema(B) |
Hi all, I think this is quite a serious side effect, for example, it breaks JSON encoding of classes that are handled by a custom JSON encoder, because they are now considered to be dataclasses so they are converted natively. Can you add an even bigger warning? I didn't actually notice it because it's printed with other messages at flask startup. Also, the warning could contain a suggestion to fix it by adding a |
@prihoda : Could you make a PR with the changes you want to see on the message ? We'll discuss the changes there. |
I got burned by this one trying to use class _HasPaths:
"""Workaround to ensure we get ``pathlib.Path`` types where needed."""
def __post_init__(self) -> None:
for fld in fields(self):
if fld.type == Path:
attr = getattr(self, fld.name)
setattr(self, fld.name, Path(attr)) and have any dataclass that uses The warnings introduced in #130 were helpful in figuring this out but for several hours I was confounded by a bunch of pytest |
I think this is a request for marshmallow itself. |
I think this auto-dataclassification is too much magic, and likely to create hard-to-suss bugs. Note that the Here's a possible fix. ==== I think we can handle "simple annotated classes"(*) as in the above example without converting them to dataclasses. For the sake of concreteness, let's say "simple annotated classes" are defined as objects,
(This definition could perhaps be tweaked/widened a bit.) Our
So the proposed fix is to change
|
This implement the suggestions made in lovasoa#51. See lovasoa#51 (comment)
This implement the suggestions made in lovasoa#51. See lovasoa#51 (comment)
This implement the suggestions made in lovasoa#51. See lovasoa#51 (comment)
This implement the suggestions made in lovasoa#51. See lovasoa#51 (comment)
This implement the suggestions made in lovasoa#51. See lovasoa#51 (comment)
This implement the suggestions made in lovasoa#51. See lovasoa#51 (comment)
I agree that the library should not be optimistically trying to create dataclasses out of types it does not understand. I didn't even realise the library could do that until today. Raise an error and let the user wrap with Today I lost several hours to I couldn't even debug it properly because the debugger was crashing trying to use the clobbered type to show values. Maybe to keep backwards compatibility there could be some kind of strict mode flag / settable module option? |
Reproducible error:
versions:
pandas 0.23.4
marshmallow 2.x
marshmallow_dataclass: 0.6.6
@lovasoa what is the intution behind trying to convert
clazz
to dataclass here after we fail to get the fields?https://github.com/lovasoa/marshmallow_dataclass/blob/master/marshmallow_dataclass/__init__.py#L281
I feel that an error (or some kind of
strict
mode) would be more helpfulThe text was updated successfully, but these errors were encountered: