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

decoding optional decimal fields #514

Open
rsarili opened this issue Feb 1, 2024 · 0 comments
Open

decoding optional decimal fields #514

rsarili opened this issue Feb 1, 2024 · 0 comments

Comments

@rsarili
Copy link

rsarili commented Feb 1, 2024

I have following class with optional Decimal Field with default value None, I got TypeError: conversion from FieldInfo to Decimal is not supported

Is this a bug or should I use different way to define optional Decimal Field with default None value

dataclasses-json version 0.6.3 is used

@dataclass_json
@dataclass
class TestClass:
    price: Decimal
    price_optional: Optional[Decimal] = Field(default=None)

def test_decoder():
    data = TestClass.from_json('{"price": "123.45"}')
    assert data.price == Decimal("123.45")

full error log

_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
.env/lib/python3.10/site-packages/dataclasses_json/api.py:63: in from_json
    return cls.from_dict(kvs, infer_missing=infer_missing)
.env/lib/python3.10/site-packages/dataclasses_json/api.py:70: in from_dict
    return _decode_dataclass(cls, kvs, infer_missing)
.env/lib/python3.10/site-packages/dataclasses_json/core.py:220: in _decode_dataclass
    init_kwargs[field.name] = _decode_generic(field_type,
.env/lib/python3.10/site-packages/dataclasses_json/core.py:313: in _decode_generic
    res = _support_extended_types(type_arg, value)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

field_type = <class 'decimal.Decimal'>
field_value = FieldInfo(annotation=Union[Decimal, NoneType], required=False)

    def _support_extended_types(field_type, field_value):
        if _issubclass_safe(field_type, datetime):
            # FIXME this is a hack to deal with mm already decoding
            # the issue is we want to leverage mm fields' missing argument
            # but need this for the object creation hook
            if isinstance(field_value, datetime):
                res = field_value
            else:
                tz = datetime.now(timezone.utc).astimezone().tzinfo
                res = datetime.fromtimestamp(field_value, tz=tz)
        elif _issubclass_safe(field_type, Decimal):
            res = (field_value
                   if isinstance(field_value, Decimal)
>                  else Decimal(field_value))
E           TypeError: conversion from FieldInfo to Decimal is not supported

.env/lib/python3.10/site-packages/dataclasses_json/core.py:243: TypeError
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant