mypy
mypy copied to clipboard
Incompatible types in assignment
- Are you reporting a bug, or opening a feature request? bug
- Please insert below the code you are checking with mypy, or a mock-up repro if the source is private. We would appreciate if you try to simplify your case to a minimal repro.
from typing import List
class Animal:
pass
class Person:
animals: List[Animal]
class Dog(Animal):
pass
class DogLover(Person):
animals: List[Dog]
- What is the actual behavior/output?
main.py:13: error: Incompatible types in assignment (expression has type "List[Dog]", base class "Person" defined the type as "List[Animal]")
- What is the behavior/output you expect? to work
- What are the versions of mypy and Python you are using? latest for both Do you see the same issue after installing mypy from Git master?
- What are the mypy flags you are using? (For example --strict-optional) none
This is not a bug, see https://github.com/python/mypy/issues/4186
But:
class Animal:
pass
class Person:
animal: Animal
class Dog(Animal):
pass
class DogLover(Person):
animal: Dog
is valid
hi @iddan I am quite agree with you, the derived class could override the value and type from base class. what do you think now?
Hey @tyronexj, it's been like three years since I posted it and I haven't worked with Python for the past two years so I don't think I can answer this intelligently