Closed kpodp0ra closed 6 months ago
@kpodp0ra
Hi, thank you for opening this issue.
TypedDicts are not working with from future import annotations.
I managed to add support for ForwardRef
in JSON Schema generation here, so your example will work as expected now.
Also, it's worth noting that adding this line on top of
tests/test_jsonschema/test_jsonschema_generation.py
breaks tests:
test_jsonschema_for_named_tuple_with_overridden_serialization_method
test_jsonschema_for_typeddict
test_overridden_serialization_method_with_return_annotation
test_dataclass_overridden_serialization_method
test_third_party_overridden_serialization_method
Thank you for pointing out. This is due to restrictions in PEP 563 implementation:
Annotations can only use names present in the module scope as postponed evaluation using local names is not reliable (with the sole exception of class-level names resolved by typing.get_type_hints()).
So, as long as you don't define the data types inside the function, everything will be fine. We could try to hack it by juggling f_locals
of frame objects to support forward refs to local types, but I just don't see much need for it right now. In some cases it will work without hacks, but in others it won't. Do you need JSON Schema for local types?
Confirm that #191 fixes all my codebase.
What I Did
instance.origin_type
is ForwardRef('int') https://github.com/Fatal1ty/mashumaro/blob/ab8242448631ab7a1ad2e437561377cd698a99e9/mashumaro/jsonschema/schema.py#L718C23-L718C43Description
TypedDicts are not working with
from __future__ import annotations
.Also, it's worth noting that adding this line on top of
tests/test_jsonschema/test_jsonschema_generation.py
breaks tests:test_jsonschema_for_named_tuple_with_overridden_serialization_method
test_jsonschema_for_typeddict
test_overridden_serialization_method_with_return_annotation
test_dataclass_overridden_serialization_method
test_third_party_overridden_serialization_method