-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Closed
Labels
bugmypy got something wrongmypy got something wrong
Description
Bug Report
When a List class variable is declared in a base class, subclasses that assign new values to the class variable will limit their children to the type of that assignment.
To Reproduce
from typing import ClassVar, List
class Seat(object):
...
class LeatherSeat(Seat):
...
class PlasticSeat(Seat):
...
class SomeBucket(Seat):
...
##############
# Failing Case 1
class TransportMedium(object):
seats: ClassVar[List[Seat]] = []
class Car(TransportMedium):
...
class ElectricCar(Car):
seats = [LeatherSeat()]
class ManufacturerCar(ElectricCar):
# List item 0 has incompatible type "PlasticSeat"; expected "LeatherSeat"
seats = [PlasticSeat()]
############
# Failing Case 2
class Train(TransportMedium):
# Need type annotation for 'seats' (hint: "seats: List[<type>] = ...")
seats = []
############
# Probably successful case
class Plane(TransportMedium):
# List item 0 has incompatible type "None"; expected "Seat"
seats = [None]
################################
# Simple class var type checking works
class Base(object):
class_var: ClassVar[str] = "this is a class var"
class Sub(Base):
...
class SubSub(Sub):
# Incompatible types in assignment (expression has type "None", base class "Base" defined the type as "str")
class_var = None
class Last(Sub):
class_var = "should be OK"Expected Behavior
ManufacturerCar.seats should not have been marked as an error since the list can contain Seats. The class isn't a generic and (to my knowledge) there isn't a way to say List[? inherits Seat] like in Java with wildcards.
If that's what the problem is, then warning should come already at ElectricCar.seats = [LeatherSeat()] and the type shouldn't be erased or overwritten to then fail in the next class.
Actual Behavior
.seats type is overwritten (erased?) in one subclass and the children of that are then limited to that type.
Your Environment
- Mypy version used: 0.812
- Mypy command-line flags: -
- Mypy configuration options from
mypy.ini(and other config files):
[mypy]
ignore_missing_imports = True
warn_return_any = False- Python version used: 3.8.7
- Operating system and version: Using docker image
python:3.8.7-slim
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
bugmypy got something wrongmypy got something wrong