Closed as not planned
Description
Feature or enhancement
Currently, the dataclasses
module only provides the dataclasses.is_dataclass
function to check whether a class is in fact a dataclass.
Lines 1258 to 1267 in 0a7936a
Pitch
Create a Dataclass(Protocol)
. Classes satisfying the Protocol should be compatible with dataclass methods such as dataclasses.astuple
, dataclasses.asdict
, dataclasses.fields
and dataclasses.replace
It seems that currently the following is possible sufficient:
@runtime_checkable
class Dataclass(Protocol):
r"""Protocol for dataclasses."""
@property
def __dataclass_fields__(self) -> Mapping[str, Field]:
r"""Return the fields of the dataclass."""
Remarks:
- Additionally one might want to add a
MutableDataclass
, with__setattr__
and__delattr__
. - As a side benefit, the
dataclasses.is_dataclass
can be equipped with aTypeguard
forDataclass
-Protocol (issubclass(cls, Dataclass)
currently won't work because only methods are checked)