How should we annotate cooperative inheritance?
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Consider annotating this example of cooperative multiple inheritance:
from typing import Any
class Hidden:
def __init__(self, z: float, **kwargs: Any):
super().__init__(**kwargs)
self.z = z
class Base:
def __init__(self, x: int, **kwargs: Any):
super().__init__(**kwargs)
self.x = x
class Inherited(Base):
def __init__(self, y: int, **kwargs: Any):
super().__init__(**kwargs)
self.y = y
class Final(Inherited, Hidden):
pass
MyPy gives errors because of the super-calls, which is addressed by https://github.com/python/mypy/issues/4001.
It would be nice for Inherited to ensure in its super-call that x has the right type, which is the topic of https://github.com/python/mypy/issues/4441 although it would also be nice for Final's constructor to only accept x, y, and z. I can't find any issue for that.
This example is done with __init__, but the problem can occur in any such “augmenting” method pattern. It is not restricted to __init__. This augmenting pattern always needs a top level method in some class. In __init__'s case, it's in object.
One idea is to have a special annotation on **kwargs, for example, typing.Collector? MyPy could then figure out the necessary arguments and their types in all derived classes. An inherited class would only be allowed to explicitly forward variables accepted by parents, but they should be able to forward anything. MyPy would make a best effort to ensure that all required arguments get in somehow.
I recognize that not everyone is a fan of cooperative MI, but I personally think it's quite beautiful. I would love to see MyPy handle it elegantly.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading the cooperative multiple-inheritance example and the linked mypy issues 4001 and 4441. The work would need an agreed annotation and type-checking behavior for augmenting methods, including the constructor arguments accepted by Final; completion should be demonstrated by mypy checks for the example.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- compilers, devtools
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100