[Python] type hint for an instance of a non specific dataclass

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

  1. Stack

    Stack Membro Participativo

    I have a function that accepts an instance of any dataclass. what would be an appropriate type hint for it ?

    haven't found something official in the python documentation


    this is what I have been doing, but i don't think it's correct

    from typing import Any, NewType

    DataClass = NewType('DataClass', Any)
    def foo(obj: DataClass):
    ...


    another idea is to use a Protocol with these class attributes __dataclass_fields__, __dataclass_params__.

    Continue reading...

Compartilhe esta Página