Detect undefined attribute in child class (when defined in parent)
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
mypy does not detect an undefined attribute in a child class if it is defined in a parent class.
Given the following code, where the __init__ of a child class forgets to call super().__init__:
class Base():
def __init__(self, x: int) -> None:
self.x = x
def whatsx(self) -> int:
return self.x
class Child(Base):
def __init__(self, x: int) -> None:
pass
# Note: no call to super(), nor defining self.x.
c = Child(3)
print(c.whatsx()) # mypy does not catch undefined c.x
The real issue is obviously that the child class does not define the x attribute, failing Liskov.
Perhaps this is out-of-scope for mypy, but since programmers make stupid mistakes like this (/me raises hand), it would be awesome if mypy could warn about this error, making said programmers look less stupid ;)
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 running mypy on the provided Base and Child example, focusing on the child init, the inherited x attribute, and the missing super().init call. Trace how mypy handles inherited attributes and constructor overrides. Done means mypy reliably warns about the missing initialization without incorrectly rejecting valid subclasses.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100