Python text input module: mixin

Is there a class under typingthat behaves like mixin?

for instance

from typing import Union
class A:
  pass

class B:
  pass

class C:
  pass

class D(A, B, C):
  pass

# current: ab is A or B, but not both
def f(ab: Union[A, B]):
    pass

# ideal: ab is A and B
def f(ab: Mixin[A, B]):
    pass

f(D())

note how Dis an instance Aand B, but also C. This would be a too big restriction for f(as it fdoes not require C), and therefore the parameter is abnot necessarily of type D, butMixin[A, B]

If the module typingdoes not provide any option, is there anything more elegant than creating my own class AB(A, B)?

+8
source share
1 answer

I know this is an old post, but it might help someone:

def isinstance_of_a_and_b(obj):
    return isinstance(obj, A) and isinstance(obj, B)
0
source

Source: https://habr.com/ru/post/1680741/


All Articles