You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Are you reporting a bug, or opening a feature request?
Feature request if behaviour is not supported, otherwise bug
Example:
fromtypingimportGeneric, Any, TypeVar, List, Dict, cast, CallableimportdataclassesMetaDataType=TypeVar('MetaDataType')
@dataclasses.dataclassclassItem(Generic[MetaDataType]):
value: intmetadata: MetaDataTypeItemType=TypeVar('ItemType', bound=Item)
# Generic[ItemType[MetaDataType]] fails at runtime, but mypy does not complain about itclassBasicStorage(Generic[ItemType[MetaDataType]]):
def__init__(self) ->None:
self._storage: List[ItemType] = []
defitem_factory(self, value: int, metadata: MetaDataType) ->ItemType:
# This fails without cast altought `ItemType` has upper bound on `Item`returncast(ItemType, Item[MetaDataType](value, metadata))
defget_last_item(self) ->ItemType:
returnself._storage[-1]
defappend(self, value: int, metadata: MetaDataType) ->ItemType:
item=self.item_factory(value, metadata)
self._storage.append(item)
returnitemclassExtendedItem(Item[Dict[str, Any]]):
''' Some helper properties '''classExtendedStorage(BasicStorage[ExtendedItem]):
# Causes error: Argument 2 of "item_factory" is incompatible with supertype "BasicStorage"; supertype defines the argument type as "MetaDataType"# Although ExtendedItem is basically `Item[Dict[str, Any]]`defitem_factory( # type: ignoreself,
value: int,
metadata: Dict[str, Any]
) ->ExtendedItem:
returnExtendedItem(value, metadata)
Actual behaviour
> basic_storage2 = BasicStorage[Item[Dict[str, Any]]]()
> basic_storage2.append(42, 14) # <~ This should cause error, "metadata" is `Dict[str, Any]`
> reveal_type(basic_storage2.get_last_item())
Revealed type is 'test.Item*[builtins.dict[builtins.str, Any]]'
> reveal_type(basic_storage2.get_last_item().metadata)
Revealed type is 'builtins.dict*[builtins.str, Any]'
> storage = ExtendedStorage()
> storage.append(42, 14) # <~ This should cause error, "metadata" is `Dict[str, Any]`
> reveal_type(storage.get_last_item())
Revealed type is 'test.ExtendedItem*'
> reveal_type(storage.get_last_item().metadata)
Revealed type is 'builtins.dict*[builtins.str, Any]'
Expected behaviour
Previous examples should cause errors and probably cast could be omitted in BasicStorage.item_factory, as result of this method is same as upper bound of ItemType
What are the versions of mypy and Python you are using?
Python 3.7.6
Mypy 0.761
Do you see the same issue after installing mypy from Git master?
Yes
What are the mypy flags you are using? (For example --strict-optional)
Are you reporting a bug, or opening a feature request?
Feature request if behaviour is not supported, otherwise bug
Example:
Actual behaviour
Expected behaviour
Previous examples should cause errors and probably
cast
could be omitted inBasicStorage.item_factory
, as result of this method is same as upper bound ofItemType
What are the versions of mypy and Python you are using?
Python 3.7.6
Mypy 0.761
Do you see the same issue after installing mypy from Git master?
Yes
What are the mypy flags you are using? (For example --strict-optional)
No additional flags
Similar issues
#3331
The text was updated successfully, but these errors were encountered: