We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mypy is ok with the following code. why?
class Animal(): pass class Bird(Animal): def fly(self): print("fly") class Farm(): animal = Animal() class FarmWithBirds(Farm): animal = Bird() # why mypy is ok? def func(f: Farm): f.animal = Animal() # the magic f = FarmWithBirds() f.animal.fly() # print "fly" func(f) f.animal.fly() # AttributeError: 'Animal' object has no attribute 'fly'
Related: #2984, Incompatible overrides
The text was updated successfully, but these errors were encountered:
Note that this works the way I expected when assigning the animal attribute in __init__:
animal
__init__
class Animal(): pass class Bird(Animal): def fly(self): print("fly") class Farm(): def __init__(self): # <-- added self.animal = Animal() class FarmWithBirds(Farm): def __init__(self): # <-- added self.animal = Bird() def func(f: Farm): f.animal = Animal() # the magic f = FarmWithBirds() reveal_type(f.animal) # note: Revealed type is "issue11720.Animal" f.animal.fly() # error: "Animal" has no attribute "fly"
Not sure why class variables are handled differently in this case.
Sorry, something went wrong.
Mypy also does not emit any errors for this variation:
from typing import ClassVar class Animal: pass class Bird(Animal): def fly(self) -> None: print("fly") class Farm: animal: ClassVar[Animal] = Animal() class FarmWithBirds(Farm): animal: ClassVar[Bird] = Bird() # why mypy is ok? def func(f: type[Farm]) -> None: f.animal = Animal() # the magic f = FarmWithBirds f.animal.fly() # print "fly" func(f) f.animal.fly() # AttributeError: 'Animal' object has no attribute 'fly'
I think this is a bug.
Duplicate of #3208
No branches or pull requests
Mypy is ok with the following code. why?
Related: #2984, Incompatible overrides
The text was updated successfully, but these errors were encountered: