You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
mypy permits overriding attributes of parent class if the child attribute is compatible with the parent attribute. This breaks Liskov Substitution Principle.
To Reproduce
fromtypingimportLiteralclassParent:
x: Literal["a", "b", "c"]
def__init__(self, x: Literal["a", "b", "c"]) ->None:
self.x=xclassChild(Parent):
x: Literal["a"]
deffoo(self) ->Literal["a"]:
returnself.xchild=Child("c") # no error from mypyx=child.foo()
# reveal_type(x) # Revealed type is "Literal['a']print(x) # outputs "c"
I believe mypy shouldn't allow overriding class attributes in child class, even with a type that is "compatible" with the parent attribute. Otherwise we can get into funny cases such as:
I think the dropbox folks found this was too strict to have on by default in practice. mypy is strict about this with protocols (which is good) and folks still file issues about that somewhat regularly. Anyway that^ issue is where to discuss
Bug Report
mypy permits overriding attributes of parent class if the child attribute is compatible with the parent attribute. This breaks Liskov Substitution Principle.
To Reproduce
https://mypy-play.net/?mypy=latest&python=3.11&gist=37e7f5a43c649a9ea5d6f0a9253d7e07
Expected Behavior
I believe mypy shouldn't allow overriding class attributes in child class, even with a type that is "compatible" with the parent attribute. Otherwise we can get into funny cases such as:
Actual Behavior
I got no error from mypy. Running
python -m mypy --strict main.py
printed:Success: no issues found in 1 source file
Your Environment
1.3.0
--strict
mypy.ini
(and other config files):None
3.10.6
The text was updated successfully, but these errors were encountered: