Comments (3)
now I'm confused why this change is needed.
In short, this is closely related to the following issue:
When you define a field of type str
or bool
, the value is passed through as is during deserialization. So, if your Union
type contains str
or bool
on the left, it is equivalent to having Any
there instead. This is a historically accepted design decision to do this, due to the fact that, as it seemed to me, validation would not be needed, but over time I began to understand that such a clumsy method could easily lead to errors.
I added this warning just to check how many people would be affected by this change, but I didn't foresee that there would be so many of them. Now I'm going to take time out to come up with a smarter way to deserialize a Union containing str
and bool
. I'd like to thank all those who made me understand that it is better not to make destructive changes right now.
Right now you can remove this warning using custom deserialization methods for str
and bool
or for the the specific Union
types:
@dataclass
class Foo(DataClassDictMixin):
foo: str | bool | date
class Config(BaseConfig):
serialization_strategy = {
bool: {"deserialize": ...},
str: {"deserialize": ...},
}
Another way it to keep the default implementation and suppress the warning using warnings.filterwarnings
:
import warnings
warnings.filterwarnings('ignore', category=UserWarning, module='mashumaro')
I will leave this issue open for now to collect more feedback and discuss the behavior with Union
. I'd love to hear about your deserialization expectations for the following examples:
@dataclass
class Foo(DataClassDictMixin):
foo: Union[str, int]
Foo.from_dict({"foo": "1"})
Foo.from_dict({"foo": 1})
@dataclass
class Foo(DataClassDictMixin):
foo: Union[int, str]
Foo.from_dict({"foo": "1"})
Foo.from_dict({"foo": 1})
@dataclass
class Foo(DataClassDictMixin):
foo: Union[str, date]
Foo.from_dict({"foo": "2024-05-28"})
@dataclass
class Foo(DataClassDictMixin):
foo: Union[date, str]
Foo.from_dict({"foo": "2024-05-28"})
@dataclass
class Foo(DataClassDictMixin):
foo: Union[bool, int]
Foo.from_dict({"foo": 1})
Foo.from_dict({"foo": True})
Foo.from_dict({"foo": "1"})
@dataclass
class Foo(DataClassDictMixin):
foo: Union[int, bool]
Foo.from_dict({"foo": 1})
Foo.from_dict({"foo": True})
Foo.from_dict({"foo": "1"})
from mashumaro.
We're also seeing a similar warning in dbt-core, from these two lines:
- https://github.com/dbt-labs/dbt-core/blob/main/core/dbt/contracts/graph/unparsed.py#L540
- https://github.com/dbt-labs/dbt-core/blob/main/core/dbt/contracts/graph/unparsed.py#L623
UserWarning: dbt.contracts.graph.unparsed.UnparsedMetricTypeParams.expr (typing.Union[str, bool, None]): In the next release, data marked with Union type containing 'str' and 'bool' will be coerced to the value of the type specified first instead of passing it as is
Also curious what the recommended workaround here would be
from mashumaro.
Thanks for the detailed answer and the recommended workarounds.
I understand the reasoning and at the same time I think I also can give you the answer to your question about expectations.
To me at least, I don't expect mashumaro to automatically coerce values into a type. That would be a nice to have but only if everything else failed (and even then only as option).
So, some examples;
foo: Union[int, str]
Foo.from_json({"foo": "1"}). --> "1" and not 1
Foo.from_json({"foo": 1}) --> 1 and not "1"
foo: Union[int, bool]
Foo.from_json({"foo": "1"}). --> ValueError
Foo.from_json({"foo": 1}) --> 1 and not True
Foo.from_json({"foo": "true"}) --> ValueError
Foo.from_json({"foo": true}) --> True
Maybe, just maybe, accept coercing only for Non-union types with some config parameter.
from mashumaro.
Related Issues (20)
- "`dict[str, T]` as a field type is not supported" HOT 2
- InitVar with no default value HOT 6
- The types defined inside the function result in syntactically invalid generated code
- Allow for more complex logic for subclass Discrimnator field matching HOT 3
- TypedDicts not working with `from __future__ import annotations` HOT 2
- omit_default breaks IntFlag serialization HOT 1
- Using Union with int/float casts to whichever appears first HOT 3
- Not parsing Generics correctly HOT 2
- Unserializable field in 3.12 if defined as a Generic TypeVar with mixin bounds HOT 6
- Allow propagation of class based discriminator settings to subclasses HOT 3
- Reject extra keys on deserialization HOT 7
- Investigate support for recursive Union types HOT 2
- Supports `numpy.ndarray` type for `orjson` HOT 2
- Add support for PEP 695
- Union type of [int | float] not serialized correctly HOT 2
- Add an alternative way to assign a field alias with annotations
- Add support for PEP 696
- Annotated SerializationStrategy used as a field serialization strategy leads to RecursionError
- Take description from docstring HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mashumaro.