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

Nested class serialization breaks with future annotations import #62

Closed
sizmailov opened this issue Dec 19, 2021 · 5 comments
Closed

Nested class serialization breaks with future annotations import #62

sizmailov opened this issue Dec 19, 2021 · 5 comments

Comments

@sizmailov
Copy link

Minimal code to reproduce:

from __future__ import annotations # <-- Brings troubles
from dataclasses import dataclass
from mashumaro import DataClassDictMixin


@dataclass
class Root(DataClassDictMixin):
    @dataclass
    class Nested(DataClassDictMixin):
        x: int

    nested: Nested


cfg = Root.from_dict({"nested": {"x": 1}}) 

print(cfg)
Traceback [python==3.9.5 / mashumaro==2.9.1]:
Traceback (most recent call last):
  File "bug.py", line 15, in <module>
    cfg = Root.from_dict({"nested": {"x": 1}})
  File "<path-to-venv>/lib/python3.9/site-packages/mashumaro/serializer/base/dict.py", line 50, in from_dict
    builder.add_from_dict()
  File "<path-to-venv>/lib/python3.9/site-packages/mashumaro/serializer/base/metaprogramming.py", line 282, in add_from_dict
    for fname, ftype in self.field_types.items():
  File "<path-to-venv>/lib/python3.9/site-packages/mashumaro/serializer/base/metaprogramming.py", line 178, in field_types
    return self.__get_field_types()
  File "<path-to-venv>/lib/python3.9/site-packages/mashumaro/serializer/base/metaprogramming.py", line 152, in __get_field_types
    raise UnresolvedTypeReferenceError(self.cls, name) from None
mashumaro.exceptions.UnresolvedTypeReferenceError: Class Root has unresolved type reference Nested in some of its fields

Code runs fine without from __future__ import annotations line.

Note: with DataClassYAMLMixin you get a rather cryptic error message:

Traceback (most recent call last):
  File "bug-yml.py", line 14, in <module>
    cfg = Root.from_yaml("""
  File "<path-to-venv>/lib/python3.9/site-packages/mashumaro/serializer/yaml.py", line 51, in from_yaml
    return cls.from_dict(
  File "<string>", line 10, in from_dict
TypeError: __init__() missing 1 required positional argument: 'nested'
@Fatal1ty
Copy link
Owner

Hi @sizmailov

I see this can't be reproduced on 3.10. But on the previous versions it could be fixed by adding self.cls.__dict__ to typing.get_type_hints here as suggested here https://bugs.python.org/issue42829#msg384423. I'll see if there's any problem with that.

@sizmailov
Copy link
Author

I confirm, bug affects 3.8, 3.9, but not 3.10.
Interesting. The bug marked as "won't fix" disappears in next release 🙃

@Fatal1ty
Copy link
Owner

Fixed in 2.10.

@sizmailov
Copy link
Author

Fixed in 2.10.

Just tried new release. Looks like the initial snippet still fails with mashumaro==2.10 with python 3.8 and 3.9...

Traceback
Traceback (most recent call last):
  File "/tmp/bug/main.py", line 15, in <module>
    cfg = Root.from_dict({"nested": {"x": 1}})
  File "/tmp/bug/venv-3.9/lib/python3.9/site-packages/mashumaro/serializer/base/dict.py", line 55, in from_dict
    builder.add_from_dict()
  File "/tmp/bug/venv-3.9/lib/python3.9/site-packages/mashumaro/serializer/base/metaprogramming.py", line 375, in add_from_dict
    return self._add_from_dict()
  File "/tmp/bug/venv-3.9/lib/python3.9/site-packages/mashumaro/serializer/base/metaprogramming.py", line 288, in _add_from_dict
    self._add_from_dict_lines()
  File "/tmp/bug/venv-3.9/lib/python3.9/site-packages/mashumaro/serializer/base/metaprogramming.py", line 311, in _add_from_dict_lines
    for fname, ftype in self.field_types.items():
  File "/tmp/bug/venv-3.9/lib/python3.9/site-packages/mashumaro/serializer/base/metaprogramming.py", line 194, in field_types
    return self.__get_field_types()
  File "/tmp/bug/venv-3.9/lib/python3.9/site-packages/mashumaro/serializer/base/metaprogramming.py", line 168, in __get_field_types
    raise UnresolvedTypeReferenceError(self.cls, name) from None
mashumaro.exceptions.UnresolvedTypeReferenceError: Class Root has unresolved type reference Nested in some of its fields

@Fatal1ty
Copy link
Owner

Oops, I forgot to merge the branch. You can check the version 2.10.1 :)

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

2 participants