Skip to content

[Attrs] Invalid typing of init arguments in subclasses of generics #5744

@galcik

Description

@galcik
from typing import Generic, List, TypeVar
import attr

T_BASE_ITEM = TypeVar("T_BASE_ITEM", bound="BaseItem")


class BaseItem(object):
    pass


class ConcreteItem(BaseItem):
    pass


@attr.s
class Container(Generic[T_BASE_ITEM]):
    items = attr.ib(type=List[T_BASE_ITEM], factory=list)


@attr.s
class ConcreteContainer(Container[ConcreteItem]):
    pass


container1 = ConcreteContainer()
container1.items.append(ConcreteItem())
container1.items.extend([ConcreteItem()])
container1.items = [ConcreteItem()]

container2 = ConcreteContainer(items=[ConcreteItem()])

fails ONLY on the last line:

List item 0 has incompatible type "ConcreteItem"; expected "T_BASE_ITEM"

It seems that typing for the attribute items works as expected. However, if items is used as a keyword argument of the __init__ generated by attrs, typing fails.

Note also that if the following class is added:

@attr.s
class ItemWrapper(Generic[T_BASE_ITEM]):
    item = attr.ib(type=T_BASE_ITEM)

mypy fails with error:

'T_BASE_ITEM' is a type variable and only valid in type context

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions