python / mypy

Optional static typing for Python
https://www.mypy-lang.org/
Other
18.35k stars 2.81k forks source link

Base class type incorrectly overwritten by assignment in extending class #17255

Open 0xhtml opened 5 months ago

0xhtml commented 5 months ago

Bug Report

Defining a variable with a type union or optional, etc. and then setting the variable in an extending class overwrites the type of the variable to the narrower type used in the assignment. E.g. int | str to int. If the class is extended again, the type of the variable is the narrower type and can't be broadened again by a simple assignment or changed to another type compatible with the original base class.

To Reproduce

The following code results in an incompatible types in assignment error in the Eggs class:

class Spam:
    var: int | str

class Ham(Spam):
    var = 10

class Eggs(Ham):
    var = "abc"

Expected Behavior

I would expect the explicitly set type of the base class Spam to be inherited by the first extending class Ham and that an assignment doesn't change the general type of the variable. So that the second extending class can still set var to a str.

Just like the following code is valid:

var: str | int
var = 10
var = "abc"

Actual Behavior

The assignment in Ham sets the type of var to int and the class Eggs can't set var to "abc" of type str.

mypy output: a.py:8: error: Incompatible types in assignment (expression has type "str", base class "Ham" defined the type as "int") [assignment]

Your Environment

JamesParrott commented 5 months ago

Repeat the union type hint on Ham, instead of using an overrided type (int) implied from the default value alone, and mypy passes it:

class Spam:
    var: int | str

class Ham(Spam):
    var: int | str = 10

class Eggs(Ham):
    var = "abc"