Python's typing module defines a number of duck types, e.g., typing.SupportsAbs to represent any type that implements the __abs__ special method.
Is it possible to define custom duck types in a way such that I can use them as valid type annotations?
For example, I would like to be able to annotate that an argument should be a duck-type equivalent of a threading.Lock, i.e., any object that implements acquire and release methods. Ideally, I could annotate such an argument as SupportsAcquireAndRequire or DuckLock, rather than object.
SupportsAbsis only a few lines of code inheriting from the privatetyping._Protocol. Is there a way to do this that doesn't involve using private API? - shoyer