OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Type annotation for multiple inheritance

  • Thread starter Thread starter V T
  • Start date Start date
V

V T

Guest
Consider the following minimal example:

Code:
class A:
    def f(self): pass
class B(A): pass
class C(A): pass
class D:
    def g(self): pass
class E(B,D): pass
class F(C,D): pass
AandD = ...
def h(obj: AandD): pass

The class A in my example can have a complex inheritance tree with many subclasses (B, C, etc.). I want that some instances of the subclasses of A have additional functionality g, so I introduced another class D and used multiple inheritance to define E and F.

How to annotate properly the parameter obj of the function h, which should accept instances that inherit from A and D simultaneously, i.e., what should be instead of AandD?

I know that I can define a protocol:

Code:
from typing import Protocol
class AandD(Protocol):
    def f(self): pass
    def g(self): pass

But I'd like to avoid repetitions in the code, since my classes contain a lot of methods. Also it won't work for a Python version prior to 3.8.

I'd also like to avoid using Union[E,F], since it will force me to update the list everywhere in my code, if I add a new subclass of A and D.
<p>Consider the following minimal example:</p>
<pre class="lang-py prettyprint-override"><code>class A:
def f(self): pass
class B(A): pass
class C(A): pass
class D:
def g(self): pass
class E(B,D): pass
class F(C,D): pass
AandD = ...
def h(obj: AandD): pass
</code></pre>
<p>The class <code>A</code> in my example can have a complex inheritance tree with many subclasses (<code>B</code>, <code>C</code>, etc.). I want that some instances of the subclasses of <code>A</code> have additional functionality <code>g</code>, so I introduced another class <code>D</code> and used multiple inheritance to define <code>E</code> and <code>F</code>.</p>
<p>How to annotate properly the parameter <code>obj</code> of the function <code>h</code>, which should accept instances that inherit from <code>A</code> and <code>D</code> simultaneously, i.e., what should be instead of <code>AandD</code>?</p>
<p>I know that I can define a protocol:</p>
<pre class="lang-py prettyprint-override"><code>from typing import Protocol
class AandD(Protocol):
def f(self): pass
def g(self): pass
</code></pre>
<p>But I'd like to avoid repetitions in the code, since my classes contain a lot of methods. Also it won't work for a Python version prior to 3.8.</p>
<p>I'd also like to avoid using <code>Union[E,F]</code>, since it will force me to update the list everywhere in my code, if I add a new subclass of <code>A</code> and <code>D</code>.</p>
 

Latest posts

Top