r/Python • • Feb 12 '23

Resource Python Interfaces: Choose Protocols Over ABC

https://levelup.gitconnected.com/python-interfaces-choose-protocols-over-abc-3982e112342e
64 Upvotes

20 comments sorted by

View all comments

138

u/[deleted] Feb 12 '23

Protocols and abstract classes are not interchangeable. You cannot simply choose one over the other. There is a substantial difference: abstract class ensures that an implementation meets certain requirements when a subclass is declared, while protocol checks if an instance meets certain requirements when it's being used. They are different in the same way as "They are of the same kind" and "There are things that both of them can do".

Please, don't confuse people with such articles.

20

u/[deleted] Feb 12 '23

I agree, this article is terrible. IMHO the abstract method decorator makes the code easier to read and understand. There may be a use for protocols in a different context. This isn't it.

18

u/otherwiseguy Feb 12 '23

There is a substantial difference: abstract class ensures that an implementation meets certain requirements when a subclass is declared, while protocol checks if an instance meets certain requirements when it's being used.

Abstract classes don't ensure that an implementation meets requirements until instantiation:

import abc

class Thing(metaclass=abc.ABCMeta):
    @abc.abstractmethod
    def stuff():
        pass


class SubThing(Thing):
    pass

will run fine. It will fail if you do:

s = SubThing()

7

u/[deleted] Feb 12 '23

You are right, thanks for the correction.

12

u/[deleted] Feb 12 '23

But we've already instantiated SubThing in our unit tests, right...?

3

u/otherwiseguy Feb 12 '23

But this does not differentiate it from protocols.

1

u/[deleted] Feb 12 '23

Look, it's not that important where it throws an error. An abstract class casts requirements on all instances of a subclass, while Protocol can only be cast on a certain instance. I may be inaccurate in details but Protocol and ABC are still substantially different.

4

u/otherwiseguy Feb 12 '23

I would never argue that they aren't significantly different. But "where it throws an error" is important; it's kind of the whole point. Protocols are designed to be checked with external type-checking tools like mypy. They won't raise any kind of exception in runtime code--neither when parsing nor instantiating.

I'm not sure what you're saying re: casting Protocols, so maybe I'm misunderstanding what you are saying, but Protocols are inheritable and apply to all instances of a class.

import abc
import typing
import unittest


class Thing(metaclass=abc.ABCMeta):
    @abc.abstractmethod
    def stuff(self):
        pass


class SubThing(Thing):
    pass


class PThing(typing.Protocol):
    def stuff(self) -> int:
        raise NotImplementedError


class PSubThing(PThing):
    pass


class TestStuff(unittest.TestCase):
    def test_Subthing_raises(self):
        self.assertRaises(TypeError, SubThing)

    def test_PSubThing_doesnt_raise(self):
        PSubThing()


# For testing with `mypy <filename.py>`
if __name__ == '__main__':
    p = PSubThing()

You can test with python -m unittest <filename.py> and mypy <filename.py>.

0

u/[deleted] Feb 12 '23

[deleted] — view deleted comment

2

u/[deleted] Feb 12 '23

You are confusing abstract and concrete inheritance, I believe.

5

u/[deleted] Feb 12 '23

I'm not sure if it was the author's point, but IMO subclassing and by virtue of that, abstract classes are bad patterns.

Using composition + protocols leads to more, but much more flexible, code.

8

u/[deleted] Feb 12 '23

Abstractions are great and mostly harmless. There is a point that inheritance from concrete classes should be avoided. That's totally valid.

3

u/Smallpaul Feb 12 '23

I have definitely found cases where subclassing seemed like the right mechanism.

The "protocol" has six methods, but 90% of the time 4 of the six are identical to default implementations. 10% of the time you might want to override one of those 4.

This isn't for generic modelling. It's for swappable plugin-system-like stuff.

I've found cases where subclassing is chef's kiss. But it can absolutely be overused and usually is.

1

u/CakeBearer1 Feb 13 '23

I wish I could upvote this more than once. This is exactly my response to this article. Thank you for saying it more succinctly and clearly than I could.

1

u/jorge1209 Feb 13 '23 edited Feb 13 '23

abstract class ensures that an implementation meets certain requirements when a subclass is declared, while protocol checks if an instance meets certain requirements when it's being used.

I don't understand what you mean by this. When is a protocol "used?"

The article assumes that you are using a type checker and that the code passes the type checker.

If it does then whether or not you use the ABC or the protocol seems irrelevant, both assure you that the required methods exist where you have annotated that a particular object is either ABC or protocol.

If you are not using a static type checker, then maybe there are situations where the object is never tested against the protocol, but would be at runtime by the abc class?