I understand (as explained in this question and the docs) that a type hint of X or Y can be expressed as:
Union[X,Y]
But how does one express a type hint of X and Y? This would be useful when expressing that the object in question must be a subclass of both X and Y.
The following example works as long as all classes that inherit both X and Y are known in advance:
class X: pass
class Y: pass
class A(X,Y): pass
class B(X,Y): pass
def some_function(arg: Union[A,B]):
pass
# do stuff with arg that only depends on inherited members from X and Y
But what if another package which depends on the code above defines:
class C(X,Y): pass
C also will work in some_function by design. I am looking for a better type hint to use instead of Union[X,Y] that includes any possible class that sub-classes both X and Y.
I understand a workaround could be to define:
class XY(X,Y): pass
And then use it as a base class and type hint:
class A(XY): pass
class B(XY): pass
class C(XY): pass
def some_function(arg: XY): pass
But I am not sure if it would be worthwhile to define a new class only for type hint, which doesn't effect runtime anyway.
How do we create a type hint for any class that is a subclass of both X and Y?