Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subscriptable TypeVar #8228

Closed
pbabics opened this issue Jan 2, 2020 · 2 comments
Closed

Subscriptable TypeVar #8228

pbabics opened this issue Jan 2, 2020 · 2 comments

Comments

@pbabics
Copy link

pbabics commented Jan 2, 2020

Are you reporting a bug, or opening a feature request?

Feature request if behaviour is not supported, otherwise bug

Example:

from typing import Generic, Any, TypeVar, List, Dict, cast, Callable

import dataclasses


MetaDataType = TypeVar('MetaDataType')

@dataclasses.dataclass
class Item(Generic[MetaDataType]):
	value: int
	metadata: MetaDataType


ItemType = TypeVar('ItemType', bound = Item)

# Generic[ItemType[MetaDataType]] fails at runtime, but mypy does not complain about it
class BasicStorage(Generic[ItemType[MetaDataType]]):

	def __init__(self) -> None:
		self._storage: List[ItemType] = []

	def item_factory(self, value: int, metadata: MetaDataType) -> ItemType:
		# This fails without cast altought `ItemType` has upper bound on `Item`
		return cast(ItemType, Item[MetaDataType](value, metadata))

	def get_last_item(self) -> ItemType:
		return self._storage[-1]

	def append(self, value: int, metadata: MetaDataType) -> ItemType:
		item = self.item_factory(value, metadata)
		self._storage.append(item)
		return item


class ExtendedItem(Item[Dict[str, Any]]):
	''' Some helper properties '''


class ExtendedStorage(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]]`
	def item_factory( # type: ignore
		self,
		value: int,
		metadata: Dict[str, Any]
	) -> ExtendedItem:
		return ExtendedItem(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)

No additional flags

Similar issues

#3331

@msullivan
Copy link
Collaborator

This is higher kinded types, I think: python/typing#548. They are great but niche enough in this setting that they aren't on the road map currently.

We should probably reject writing class BasicStorage(Generic[ItemType[MetaDataType]]):

@hauntsaninja
Copy link
Collaborator

Closing as a duplicate of python/typing#548. Mypy isn't going to do anything non standard here

@hauntsaninja hauntsaninja closed this as not planned Won't fix, can't repro, duplicate, stale Aug 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants