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

[BUG] global configuration of encoder/decoder for type T is not used for Optional[T] field #465

Closed
PJCampi opened this issue Aug 8, 2023 · 5 comments
Assignees
Labels
api/v0 bug Something isn't working

Comments

@PJCampi
Copy link
Contributor

PJCampi commented Aug 8, 2023

Description

Let's say I register a custom encoder/decoder for type T in the global_config. I would expect that a dataclass with a field of type Optional[T] to use the encoder/decoder registered. Instead I have to manually register the same encoder for the type Optional[T] for the encoder to be picked up.
_user_overrides_or_exts does not unwrap the optional type.

Code snippet that reproduces the issue

from dataclasses import dataclass
from dataclasses_json import dataclass_json, global_config
from datetime import date
from typing import Optional

global_config.encoders[date] = date.isoformat
# global_config.encoders[Optional[date]] = date.isoformat

@dataclass_json
@dataclass
class DataClassWithIsoDatetime:
    created_at: Optional[date] = None

print(DataClassWithIsoDatetime(date.today()).to_json())

Describe the results you expected

{"created_at": "2023-08-08"}

Python version you are using

3.9

Environment description

dataclasses-json==0.5.14

@PJCampi PJCampi added the bug Something isn't working label Aug 8, 2023
@george-zubrienko
Copy link
Collaborator

Should be solved in v1, but I suggest we still fix this in v0

@PJCampi
Copy link
Contributor Author

PJCampi commented Apr 16, 2024

This is becoming a bigger problem for me (global configs are also not applied from collections or mappings) so I will try to see if I can fix this tomorrow. It should be relatively simple.

@PJCampi
Copy link
Contributor Author

PJCampi commented Apr 18, 2024

I have a fix for the bug. I can create a PR if someone can grant me write access to the repo.

@george-zubrienko
Copy link
Collaborator

george-zubrienko commented Apr 18, 2024

@PJCampi tagged you in another issue - please fork the repo and create a PR, you do not need contributor access to the repo itself

@PJCampi
Copy link
Contributor Author

PJCampi commented Apr 18, 2024

here you go: #524

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api/v0 bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants