Skip to content

attrs: Inheritance breaks mypy type checking with optional attributes if Generics are involved #13794

@rggjan

Description

@rggjan

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.

It seems to check fine in older mypy versions (eg 0.950), but breaks in the newest (0.981).

See also: python-attrs/attrs#1028

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions