Why does AbstractSet not include union and intersection?

Viewed 423

Is there a documented reason for excluding union and intersection from the defining methods of abc.Set and thereby from typing.AbstractSet? As a result, I often have to use Union[Set,FrozenSet] where I expected to be able to use AbstractSet. This is particularly puzzling given that the docs suggest giving preference to AbstractSet for argument type annotations.

2 Answers

Authoratative Source

PEP 3119 defines the abstract bases classes.

The overall design goal was for "the ABCs define a minimal set of methods that establish the characteristic behavior of the type. Code that discriminates objects based on their ABC type can trust that those methods will always be present."

The specific rationale for the minimal abc.Set API is mentioned only in passing, "the issubset and issuperset methods found on the set type in Python 2 are not supported, as these are mostly just aliases for __le__ and __ge__", and "this ABC does not provide the named methods present on the built-in concrete set type that perform (almost) the same operations."

collections.abc.Set only includes the operator forms of those operations, | and &. Otherwise, the interface wouldn't cover the set-like objects returned by dict.keys and dict.items, which don't have union or intersection methods.

Related