attrs
attrs copied to clipboard
Inheritance breaks mypy type checking with optional attributes if Generics are involved
Inheritance breaks mypy type checking with optional attributes if Generics are involved:
from typing import Generic, Optional, TypeVar
import attr
T = TypeVar("T")
@attr.mutable()
class Parent(Generic[T]): # Removing `Generic` here and `[float]` below doesn't show `mypy` issues anymore
run_type: Optional[int] = None
@attr.mutable()
class Child(Parent[float]):
pass
Parent(
run_type =None,
)
Child(
run_type =None, # error: Argument "run_type" to "Child" has incompatible type "None"; expected "int" [arg-type]
)
There is an error displayed which is wrong. Doing the same without any Generic involved works as expected.
Not quite sure if this is a mypy issue or an attrs typing issue.
Same issue
Is this a dupe of https://github.com/python-attrs/attrs/issues/452?
I don't think so. In #452, the type itself is generic, which is causing issues. In my example, the types of the attributes are fixed and independent of "T", but inheriting from Generic still breaks the types.
@euresti? @Tinche? 🥺
Maybe @sobolevn might be able to give it a shot?
Weird. I don't get an error with mypy 0.950
$ mypy --version
mypy 0.950 (compiled: yes)
I do have a quick question since I can't repro. If this was re-written without attrs does it still have the same problem?
I think we only started seeing this in newer mypy version. Could also be a mypy issue?
Try with mypy 0.981
Yes, looks like a mypy issue. Please, open a new report there :)