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

to_dict and from_dict do not support None as value for an Optional nested in Tuple #73

Closed
Mindstan opened this issue Mar 15, 2022 · 6 comments
Labels
bug Something isn't working

Comments

@Mindstan
Copy link

Mindstan commented Mar 15, 2022

  • mashumaro version: 3.0
  • Python version: 3.6.9 (PyEnv, CPython)
  • Operating System: Ubuntu 20.04

Description

I'm trying to set an optional value in a Tuple (in the example Tuple[Optional[int], int]) and export it in YAML, but it results in a TypeError. By looking at the generated code (see below), we can see the Optional is ignored by the builder.

What I Did

from dataclasses import dataclass, field
from typing import Tuple, Optional

from mashumaro.mixins.yaml import DataClassYAMLMixin

@dataclass
class Foo(DataClassYAMLMixin):
    bar: Tuple[Optional[int], int] = field(default_factory=lambda: (None, 42))

print(Foo().to_dict())

Output:

Traceback (most recent call last):
  File "test2.py", line 17, in <module>
    print(Foo().to_dict())
  File "<string>", line 7, in to_dict
TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'

Generated code for to_dict:

__main__.Foo:
def to_dict(self):
    kwargs = {}
    value = getattr(self, 'bar')
    if value is None:
        kwargs['bar'] = None
    else:
        kwargs['bar'] = [int(value[0]), int(value[1])]
        # should have been [int(value[0]) if value[0] is not None else None, int(value[1])]
    return kwargs
setattr(cls, 'to_dict', to_dict)

EDIT: from_dict also suffers from the same issue

@Mindstan Mindstan changed the title to_dict does not support None as value for an Optional nested in Tuple to_dict and from_dict do not support None as value for an Optional nested in Tuple Mar 15, 2022
@Fatal1ty
Copy link
Owner

Fatal1ty commented Mar 15, 2022

It seems like could_be_none should be set to True for tuples and in other cases possibly. Thank you for spotting this, I will check different cases and try to fix it as soon as possible.

@Fatal1ty Fatal1ty added the bug Something isn't working label Mar 15, 2022
@peku-jamf
Copy link

peku-jamf commented Mar 16, 2022

@Fatal1ty May I ask, is this related problem? Can't get Optional working as I would expect. In my yaml file there is nothing like setup2, but I'd expect, since it is Optional, it will be silently ignored.

Traceback (most recent call last):
  File "<string>", line 12, in from_dict
  File "<string>", line 19, in from_dict
mashumaro.exceptions.MissingField: Field "setup2" of type Optional[str] is missing in MqSetup instance

code

@dataclass
class MqSetup(DataClassYAMLMixin):
    setup: int
    setup2: Optional[str]

@Fatal1ty
Copy link
Owner

Fatal1ty commented Mar 16, 2022

@peku-jamf type Optional[str] means no more than that you can pass to setup2 values of type str or None. If you want to have a default value than you should write setup2: Optional[str] = None.

See docs: https://docs.python.org/3/library/typing.html#typing.Optional

@peku-jamf
Copy link

Thank you @Fatal1ty

@Fatal1ty
Copy link
Owner

Fatal1ty commented Apr 3, 2022

@Mindstan Sorry for such a long delay, I was on vacation and tried to distract myself from the Internet. I have fixed this issue and released 3.0.1 version. You can give it a try!

@Mindstan
Copy link
Author

Mindstan commented Apr 4, 2022

Thanks, it works now in our project.

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

No branches or pull requests

3 participants