frequenz-floss / frequenz-core-python

Core utilities to complement Python's standard library
https://frequenz-floss.github.io/frequenz-core-python/
MIT License
0 stars 2 forks source link

Add a `Dataclass` protocol type #38

Open llucax opened 1 week ago

llucax commented 1 week ago

What's needed?

It is sometimes useful when working with dataclasses in a generic way to be able to have a type-hint that will detect passing non-dataclasses by mypy instead of at runtime.

Proposed solution

Add a new class to frequenz.core.typing like:

from typing import Protocol
class Dataclass(Protocol):
    __dataclass_fields__: dict

Use cases

Wrappers using marshmallow_dataclass, where we want to make sure we take a dataclass, for example for loading as if it where a schema.

Alternatives and workarounds

No response

Additional context

No response