attrs icon indicating copy to clipboard operation
attrs copied to clipboard

Inheritance breaks mypy type checking with optional attributes if Generics are involved

Open rggjan opened this issue 3 years ago • 8 comments

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.

rggjan avatar Sep 19 '22 08:09 rggjan

Same issue

billtrik avatar Sep 27 '22 12:09 billtrik

Is this a dupe of https://github.com/python-attrs/attrs/issues/452?

hynek avatar Sep 27 '22 12:09 hynek

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.

rggjan avatar Sep 27 '22 15:09 rggjan

@euresti? @Tinche? 🥺

hynek avatar Sep 28 '22 12:09 hynek

Maybe @sobolevn might be able to give it a shot?

Tinche avatar Sep 29 '22 23:09 Tinche

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?

euresti avatar Sep 29 '22 23:09 euresti

I think we only started seeing this in newer mypy version. Could also be a mypy issue?

Try with mypy 0.981

rggjan avatar Sep 30 '22 08:09 rggjan

Yes, looks like a mypy issue. Please, open a new report there :)

sobolevn avatar Sep 30 '22 08:09 sobolevn