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 unsoundly accepts a generic function with a generic mutable default parameter value. This value is evaluated once at runtime, but can be referred to with multiple incompatible types, breaking type safety.
fromtypingimportTypeVarT=TypeVar("T")
deff(x: T, y: list[T] = []) ->list[T]:
returnyy_strs: list[str] =f("a")
y_ints: list[int] =f(4)
# Uh oh, y_strs and y_ints refer to the same list with incompatible typesy_strs.append("b")
print(y_ints[0] +6) # oops
(This is a sort of converse to #3737, where the complaint is that mypy fails to accept a generic function with a non-generic default parameter value. It further supports my claim that default parameter values should not be generalized to the generic type of the parameter at function definition time.)
Mypy unsoundly accepts a generic function with a generic mutable default parameter value. This value is evaluated once at runtime, but can be referred to with multiple incompatible types, breaking type safety.
mypy Playground link
(This is a sort of converse to #3737, where the complaint is that mypy fails to accept a generic function with a non-generic default parameter value. It further supports my claim that default parameter values should not be generalized to the generic type of the parameter at function definition time.)
Related:
ClassVar
cannot contain type variables, refs #11538 #11585The text was updated successfully, but these errors were encountered: