Skip to content
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

[FEATURE] V1 - Type coercions feature #474

Open
george-zubrienko opened this issue Aug 15, 2023 · 0 comments
Open

[FEATURE] V1 - Type coercions feature #474

george-zubrienko opened this issue Aug 15, 2023 · 0 comments
Labels
api/v1 enhancement New feature or request

Comments

@george-zubrienko
Copy link
Collaborator

george-zubrienko commented Aug 15, 2023

Description

In v0 this PR aligned lib behaviour with marshmellow, but effectively triggered a couple of unexpected consequences:

  • People using from_json will start noticing builtin types being implicitly converted when reading json files into dataclasses, and especially booleans receiving valid values from not-very-common-sense values like integers outside 0-1 range and strings
  • People using from_dict will have even "better" experience when types in their dicts are being overridden by dataclass field type, with implicit conversion as a side-effect
  • People using marshmellow interop would rejoice

Possible solution

First of all, we should add a flag for implicit coercions:

[tool.dataclasses_json]
coerce_builtins = True

Second, we should do a follow-up discussion re coercion behaviours in from_dict, IMO this should differ from from_json behaviour because from_dict argument carries type information, while from_json has to work with a string.

Alternatives

N/A

Context

Problem discovered in #466

@george-zubrienko george-zubrienko added enhancement New feature or request api/v1 labels Aug 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api/v1 enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant