Closed estyxx closed 10 months ago
@matt035343 take a look? 🐱
As Python documentation states, __future__.annotations
is a feature that will be removed in future releases. This is caused by the changed design direction/philosophy of the official Python community.
from __future__ import annotations
was previously scheduled to become mandatory in Python 3.10, but the Python Steering Council twice decided to delay the change (announcement for Python 3.10; announcement for Python 3.11). No final decision has been made yet. See also PEP 563 and PEP 649.
If accepted (editor's note: accepted, scheduled to Python 3.13), this PEP would supersede PEP 563, and PEP 563’s behavior would be deprecated and eventually removed.
In a nutshell, they are re-implementing __annotations__
to always return a (name -> type hint)
dictionary, but changing its evaluation time from "always on module execution" to "by demand" by using a cached data-descriptorr.
@USSX-Hares then what would you recommend we do/not do about this?
Closing as wontfix, for now - README mentions that import should be avoided. Will revisit in v1.
Description
I found a bug where the use of
from __future__ import annotations
causesdataclasses_json
to fail when trying to override the global configuration for specific types. Sincefrom __future__ import annotations
changes the behaviour of annotations to be stored as string literals rather than evaluated expressions, the comparison of types in functions like_user_overrides_or_exts
fails, leading to incorrect matching of types.This issue particularly affects the overriding of the global config for encoders and decoders, and as a result, the library is not able to match the type correctly when type annotations are imported, leading to a failure in using the correct decoder method.
As a result I'm getting an exception:
Can be related to #382
A possible solution could be using
typing.get_type_hints
instead.Code snippet that reproduces the issue
Describe the results you expected
Expected Behavior
The type annotations should be correctly matched with the encoders and decoders, whether or not
from __future__ import annotations
is used, allowing the correct overriding of the global config.Actual Behavior
The comparison of types fails when
from __future__ import annotations
is used, causing the incorrect decoder method to be used; as a result, the conversation fails.Python version you are using
Python 3.11.2
Environment description