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

Missing types with custom class deserializers #167

Closed
mauvealerts opened this issue Nov 9, 2021 · 7 comments · Fixed by #169
Closed

Missing types with custom class deserializers #167

mauvealerts opened this issue Nov 9, 2021 · 7 comments · Fixed by #169
Labels
bug Bug report or fix

Comments

@mauvealerts
Copy link
Contributor

The code generation doesn't put Optional or Union in scope when calling a custom class deserializer. This results in a NameError during deserialization.

Example

from dataclasses import dataclass
from typing import Optional

from serde import deserialize, from_dict


def deserialize_maybe_str(_typ, value):
    return value


@deserialize(deserializer=deserialize_maybe_str)
@dataclass
class CustomDe:
    maybe_str: Optional[str]


if __name__ == "__main__":
    data = {"maybe_str": "bye"}
    print(f"from_dict({data}) = {from_dict(CustomDe, data)}")

Output

Traceback (most recent call last):
  File "C:\Users\mauve\dev\modlunky2\src\union_demo.py", line 19, in <module>
    print(f"from_dict({data}) = {from_dict(CustomDe, data)}")
  File "C:\Users\mauve\dev\modlunky2\.venv\lib\site-packages\serde\de.py", line 319, in from_dict
    return from_obj(cls, o, named=True, reuse_instances=reuse_instances)
  File "C:\Users\mauve\dev\modlunky2\.venv\lib\site-packages\serde\de.py", line 253, in from_obj
    return serde_scope.funcs[FROM_DICT](o, reuse_instances=reuse_instances)
  File "<string>", line 10, in from_dict
NameError: name 'Optional' is not defined
@yukinarit yukinarit added the bug Bug report or fix label Nov 9, 2021
@yukinarit
Copy link
Owner

Thanks for reporting! Will take a look!

@yukinarit
Copy link
Owner

hmm I got the different error 🤔

Traceback (most recent call last):
  File "/Users/yukinari/repos/pyserde/examples/a.py", line 13, in <module>
    class CustomDe:
  File "/Users/yukinari/Library/Caches/pypoetry/virtualenvs/pyserde-examples-eLlM1BJJ-py3.9/lib/python3.9/site-packages/serde/de.py", line 196, in wrap
    add_func(scope, FROM_ITER, render_from_iter(cls, deserializer), g)
  File "/Users/yukinari/Library/Caches/pypoetry/virtualenvs/pyserde-examples-eLlM1BJJ-py3.9/lib/python3.9/site-packages/serde/de.py", line 689, in render_from_iter
    return env.get_template('iter').render(func=FROM_ITER, serde_scope=getattr(cls, SERDE_SCOPE), fields=defields(cls))
  File "/Users/yukinari/Library/Caches/pypoetry/virtualenvs/pyserde-examples-eLlM1BJJ-py3.9/lib/python3.9/site-packages/jinja2/environment.py", line 1304, in render
    self.environment.handle_exception()
  File "/Users/yukinari/Library/Caches/pypoetry/virtualenvs/pyserde-examples-eLlM1BJJ-py3.9/lib/python3.9/site-packages/jinja2/environment.py", line 925, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 16, in top-level template code
  File "/Users/yukinari/Library/Caches/pypoetry/virtualenvs/pyserde-examples-eLlM1BJJ-py3.9/lib/python3.9/site-packages/serde/de.py", line 479, in render
    f'serde_custom_class_deserializer({arg.type.__name__}, {arg.datavar}, {arg.data}, '
  File "/opt/homebrew/Cellar/python@3.9/3.9.6/Frameworks/Python.framework/Versions/3.9/lib/python3.9/typing.py", line 701, in __getattr__
    raise AttributeError(attr)
AttributeError: __name__

@mauvealerts
Copy link
Contributor Author

Interesting. The output in my original-comment is from a Python 3.10 venv. Notably, I can use serde.inspect on it.

With a Python 3.9 venv, I get the exact same error you do. Since it fails sooner, serde.inspect doesn't work, of course.

@yukinarit
Copy link
Owner

OK, it reproduced with a small fix.

@yukinarit
Copy link
Owner

@mauvealerts

I think #169 fixed the issue. Could you please verify?

@mauvealerts
Copy link
Contributor Author

Yes, it now works for me. Thanks!

@yukinarit
Copy link
Owner

Great! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Bug report or fix
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants