Fatal1ty / mashumaro

Fast and well tested serialization library
Apache License 2.0
761 stars 45 forks source link

Unable to use PEP 604 annotations #245

Closed epenet closed 5 days ago

epenet commented 5 days ago

Description

I am trying to use PEP 604 annotations in my project, replacing annotations such as Optional[int] with int | None.

What I Did

I added from __future__ import annotations at the top of the module, so that Python 3.9 could understand it, but mashumaro still errors. See https://github.com/hacf-fr/sfrbox-api/actions/runs/10827545154/job/30040944493?pr=344

____________________ ERROR collecting tests/test_bridge.py _____________________
tests/test_bridge.py:11: in <module>
    from sfrbox_api.bridge import SFRBox
.nox/tests-3-9/lib/python3.9/site-packages/sfrbox_api/bridge.py:26: in <module>
    from .models import DslInfo
.nox/tests-3-9/lib/python3.9/site-packages/sfrbox_api/models.py:11: in <module>
    class DslInfo(DataClassDictMixin):
.nox/tests-3-9/lib/python3.9/site-packages/mashumaro/mixins/dict.py:25: in __init_subclass__
    compile_mixin_unpacker(cls, **builder_params["unpacker"])
.nox/tests-3-9/lib/python3.9/site-packages/mashumaro/core/meta/mixin.py:49: in compile_mixin_unpacker
    builder.add_unpack_method()
.nox/tests-3-9/lib/python3.9/site-packages/mashumaro/core/meta/code/builder.py:586: in add_unpack_method
    self._add_unpack_method_lines(method_name)
.nox/tests-3-9/lib/python3.9/site-packages/mashumaro/core/meta/code/builder.py:[37](https://github.com/hacf-fr/sfrbox-api/actions/runs/10827545154/job/30040944493?pr=344#step:9:38)9: in _add_unpack_method_lines
    field_types = self.get_field_types(include_extras=True)
.nox/tests-3-9/lib/python3.9/site-packages/mashumaro/core/meta/code/builder.py:221: in get_field_types
    return self.__get_field_types(include_extras=include_extras)
.nox/tests-3-9/lib/python3.9/site-packages/mashumaro/core/meta/code/builder.py:183: in __get_field_types
    field_type_hints = typing_extensions.get_type_hints(
.nox/tests-3-9/lib/python3.9/site-packages/typing_extensions.py:1230: in get_type_hints
    hint = typing.get_type_hints(
/opt/hostedtoolcache/Python/3.9.19/x64/lib/python3.9/typing.py:1[45](https://github.com/hacf-fr/sfrbox-api/actions/runs/10827545154/job/30040944493?pr=344#step:9:46)9: in get_type_hints
    value = _eval_type(value, base_globals, localns)
/opt/hostedtoolcache/Python/3.9.19/x64/lib/python3.9/typing.py:292: in _eval_type
    return t._evaluate(globalns, localns, recursive_guard)
/opt/hostedtoolcache/Python/3.9.19/x64/lib/python3.9/typing.py:554: in _evaluate
    eval(self.__forward_code__, globalns, localns),
<string>:1: in <module>
    ???
E   TypeError: unsupported operand type(s) for |: 'type' and 'NoneType'
Fatal1ty commented 5 days ago

You can only use PEP 604 starting from Python 3.10. This library needs to evaluate annotations in order to work but this syntax is invalid on Python 3.9.

epenet commented 5 days ago

You can use PEP 604 in combination with from __future__ import annotations This is what is done with other modules in that project.

Can mashumaro not use this?

Fatal1ty commented 5 days ago

You can use PEP 604 in combination with from __future__ import annotations This is what is done with other modules in that project.

Can mashumaro not use this?

You don't understand the purpose of from __future__ import annotations. You can use this combination only for the tools that don't evaluate expressions — like mypy.