Generic function that accepts any dataclass type and returns an instance of that dataclass type

I’m trying to write a function that accepts a dataclass type as a parameter, does some reflection on that type, and then returns an instance of that dataclass type.

How do I type hint this?

import dataclasses
from typing import Type

AnyDataclassT = TypeVar("AnyDataclassT", bound=dataclasses.DataclassInstance)

def gather_dataclass(
    dataclass_type: Type[AnyDataclassT],
) -> AnyDataclassT | None:
    
    try:
        dataclass = some_network_operation(dataclass_type)
        return dataclass
    except Exception:
        return None

This is giving me:

"DataclassInstance" is not a known attribute of module "dataclasses"

Why do you need to have a bound in the type variable?

If you think of dataclass classes as just being classes (which they are) then you have a function that takes a class and returns that class.

If you expect there to be any kind of attributes bound to DataclassInstance, perhaps you can just create a Protocol.

If not, just do bound=type.