mypy icon indicating copy to clipboard operation
mypy copied to clipboard

Incompatible types in assignment

Open iddan opened this issue 8 years ago • 4 comments

  • 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

iddan avatar Oct 02 '18 10:10 iddan

This is not a bug, see https://github.com/python/mypy/issues/4186

ilevkivskyi avatar Oct 02 '18 10:10 ilevkivskyi

But:

class Animal:
    pass

class Person:
    animal: Animal
    
class Dog(Animal):
    pass

class DogLover(Person):
    animal: Dog

is valid

iddan avatar Oct 02 '18 11:10 iddan

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?

tyronexj avatar Mar 29 '22 12:03 tyronexj

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

iddan avatar Apr 01 '22 14:04 iddan