How to parameterize a TypeVar by another TypeVar #1640
Answered
by
dmoisset
eike-fokken
asked this question in
Q&A
|
Hi! I have the following Protocol: from typing import Protocol, TypeVar
ValueType = TypeVar("ValueType")
# Protocol for types, that provide a [] operator for reading and writing.
class IndexibleProtocol(Protocol[ValueType]):
def __setitem__(self, key: int, value: ValueType) -> None:
...
def __getitem__(self, key: int) -> ValueType:
...
Indexible = TypeVar("Indexible", bound=IndexibleProtocol)I can now use this for type annotations like so: def get_some_value(container: Indexible) -> Any:
return container[0]But if I try def get_some_value(container: Indexible) -> ValueType:
return container[0]I get a mypy error telling me "A function returning TypeVar should receive at least one argument containing the same TypeVar. Can I somehow index the Indexible type by ValueType, so that I could (hopefully, I don't understand it super well) write something like this: def get_some_value(container: Indexible[ValueType]) -> ValueType:
return container[0] |
Answered by
dmoisset
Feb 28, 2024
Replies: 2 comments 1 reply
|
It is not possible right now. See: |
0 replies
|
At least in that example, you don't need the second typevar You could write: |
1 reply
Answer selected by
eike-fokken
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
At least in that example, you don't need the second typevar
You could write: