Giter VIP home page Giter VIP logo

Comments (3)

Fatal1ty avatar Fatal1ty commented on May 28, 2024 1

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.

MichelleArk avatar MichelleArk commented on May 28, 2024

We're also seeing a similar warning in dbt-core, from these two lines:

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.

marcelveldt avatar marcelveldt commented on May 28, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.