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

"dict[str, T] as a field type is not supported" #177

Closed
aldanor opened this issue Nov 8, 2023 · 2 comments · Fixed by #178
Closed

"dict[str, T] as a field type is not supported" #177

aldanor opened this issue Nov 8, 2023 · 2 comments · Fixed by #178
Labels
bug Something isn't working

Comments

@aldanor
Copy link

aldanor commented Nov 8, 2023

  • mashumaro version: 3.10
  • Python version: 3.11.5

In a seemingly simple example with generics, close to the one presented in the docs, mashumaro seems to stumble on dict[str, T] annotation; wonder if this is intended, or a bug, or some misunderstanding on my side?

from __future__ import annotations

from dataclasses import dataclass
from typing import Generic, TypeVar

from mashumaro import DataClassDictMixin
from mashumaro.types import SerializableType

T = TypeVar('T')

class Foo(Generic[T], SerializableType, use_annotations=True):
    a: T

    def __init__(self, a: T) -> None:
        self.a = a

    @classmethod
    def _deserialize(cls, value: dict[str, T]) -> Foo[T]:
        return cls(**value)

    def _serialize(self) -> dict[str, T]:
        return {'a': self.a}
    
@dataclass
class Bar(DataClassDictMixin):
    x: Foo[int]

print(Bar(Foo(1)).to_dict())

yields

UnserializableDataError: dict[str, T] as a field type is not supported by mashumaro
@aldanor
Copy link
Author

aldanor commented Nov 9, 2023

Edit: after some trial and error, it seems to work if and only if you quote 'Foo[T]' which isn't immediately obvious from the docs, since it's valid code (e.g. with from __future__ import annotations).

What makes it even worse is that some linters (e.g. ruff) force-strip quotes from type annotations by default (link).

@Fatal1ty Fatal1ty added the bug Something isn't working label Nov 14, 2023
@Fatal1ty
Copy link
Owner

@aldanor Thank you for bringing it in.

Postponed evaluation of annotations is pain in the ass. Function inspect.signature returns string instead of ForwardRef object in this case and we need to evaluate it. Fortunately, almost everything is ready for this since I've been working on ForwardRef support:

I need to tweak evaluate_forward_ref method and turn the string annotation into ForwardRef by hand. This fix won't take much time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants