-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Closed
Labels
bugmypy got something wrongmypy got something wrongfalse-positivemypy gave an error on correct codemypy gave an error on correct codepriority-1-normaltopic-attrs
Description
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
carterv, bluetech and natemcmaster
Metadata
Metadata
Assignees
Labels
bugmypy got something wrongmypy got something wrongfalse-positivemypy gave an error on correct codemypy gave an error on correct codepriority-1-normaltopic-attrs