@typing.protocol decorator to create a class of type Protocol

I’m trying to recall how we decided that dataclasses should use a class decorator instead of a metaclass.

A problem with metaclasses is that they are a pain to combine with another metaclass (you have to construct a new metaclass that multiply inherits from both metaclasses).

But I think that ABC and Protocol have a requirement that is hard to satisfy with a class decorator: If your class derives from an abstract base class and doesn’t implement all abstract methods, it should still be considered abstract.

At least, that is how it is for ABC. I’m not sure how strong the case is for doing the same for Protocol: If my class inherits from a protocol class, is my class a protocol itself or does this just declare that it implements that protocol? As it is, the intention is ambiguous and this is resolved the same way as abstractness – if not all abstract methods are implemented, it is still a protocol class.

Perhaps the main argument against the proposal here is backwards compatibility, and/or the way it works with ABCs?

3 Likes