mypy not flagging subclasses with incompatible constructors
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
reproducer
from typing import Type
class Foo:
def __init__(self, a):
self.a = a
class Bar(Foo):
def __init__(self):
super().__init__("Bar")
def takes_foo(foo: Type[Foo]):
x = foo("Foo")
takes_foo(Foo)
takes_foo(Bar)
observed behavior
mypy raises no errors, but the code raises a TypeError at runtime.
expected behavior
pep 484 states:
when
new_user()callsuser_class()this implies that all subclasses ofUsermust support this in their constructor signature...A type checker ought to flag violations of such assumptions, but by default constructor calls that match the constructor signature in the indicated base class (User in the example above) should be allowed.
My understanding of the above is that mypy should be raising an error on the Bar definition because its signature is incompatible with that of its superclass. More generally, I tend to be surprised when code that passes mypy raises a TypeError at runtime.
environment
master as of 2019-06-10
$ pipenv graph
mypy==0.710+dev.e2f31ed71bd1edd60bffc86d3fda9da15ba63b3d
- mypy-extensions [required: >=0.4.0,<0.5.0, installed: 0.4.1]
- typed-ast [required: >=1.4.0,<1.5.0, installed: 1.4.0]
$ pipenv run python --version
Python 3.7.3
Thank you all for an amazing tool!
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 with the reproducer in the issue and compare its behavior with the constructor and class-object guidance in PEP 484. Investigate mypy's handling of subclass constructor signatures and determine whether the expected result is an error on Bar or at takes_foo(Bar). Done means the behavior is covered by a regression test and the checker reports the intended incompatibility.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- compilers, devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100