[Python] Python: how to type hint a dataclass?

Discussão em 'Python' iniciado por Stack, Julho 31, 2025.

  1. Stack

    Stack Membro Participativo

    The code below works, but I'm getting the following warning by PyCharm:


    Cannot find reference __annotations__ in '(...) -> Any'.
    ​

    I guess it's because I'm using Callable. I didn't find something like Dataclass. Which type should I use instead?

    from __future__ import annotations
    from dataclasses import dataclass
    from typing import Callable

    @dataclass
    class Fruit:
    color: str
    taste: str

    def get_cls() -> Callable:
    return Fruit

    attrs = get_cls().__annotations__ # <- IDE warning
    print(attrs)

    Continue reading...

Compartilhe esta Página