[New-bugs-announce] [issue39134] can't construct dataclass as ABC (or runtime check as data protocol)

Alexander Hirner report at bugs.python.org
Wed Dec 25 19:05:40 EST 2019


New submission from Alexander Hirner <a.hirner at gmail.com>:

At runtime, we want to check whether objects adhere to a data protocol. This is not possible due to problematic interactions between ABC and @dataclass.

The attached file tests all relevant yet impossible cases. Those are:

1) A(object): Can't check due to "Protocols with non-method members don't support issubclass()" (as outlined in PEP 554)
2) B(ABC): "Can't instantiate abstract class B with abstract methods x, y"
3) C(Protocol): same as A or same as B if @property is @abstractmethod

The problem can be solved in two parts. First allowing to implement @abstractproperty in a dataclass (B). This doesn't involve typing and enables the expected use case of dataclass+ABC. I analysed this problem as follows:
Abstract properties evaluate to a default of property, not to dataclasses.MISSING. Hence, `dataclasses._init_fn` throws TypeError because of deriving from class vars without defaults.

Second, eliding the exception of @runtime_checkable Protocols with non-method members if and only if the the the protocol is in its MRO. I didn't think that through fully, but instantiation could e.g. fail for missing implementations as expected from ABC behaviour (see case D in attached file). I'm not sure about the runtime overhead of this suggestion.

----------
files: dc_repro.py
messages: 358876
nosy: cybertreiber, eric.smith
priority: normal
severity: normal
status: open
title: can't construct dataclass as ABC (or runtime check as data protocol)
type: behavior
versions: Python 3.6, Python 3.8
Added file: https://bugs.python.org/file48802/dc_repro.py

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue39134>
_______________________________________


More information about the New-bugs-announce mailing list