-
Notifications
You must be signed in to change notification settings - Fork 246
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
New GroupedTypeVar type #1899
Comments
You can do this sort of thing with overloads. I think that's sufficient. |
There is no sort of enforcing of types between generics though, even with overloads i dont think, if you could show me a way that would be great. class Type1: ...
class Type2: ...
class UsesType1:
def method(self) -> Type1: ...
class UsesType2:
def method(self) -> Type2: ...
TUses, TType = GroupedTypeVar(
"TUses, TType"
(UsesType1, Type1),
(UsesType2, Type2)
)
# Create a generic class using the grouped types
class SomeClass(Generic[TUses, TType]):
def __init__(self, uses: TUses) -> None:
self.uses = uses
def add(self) -> TType:
return self.uses.method() Im aware and i apologise that my example has gotten more abstract, but i hope im conveying my problem well enough. |
This example is still trivially expressible with overloads: from __future__ import annotations
from typing import Generic, TypeVar, overload
class Type1: ...
class Type2: ...
class UsesType1:
def method(self) -> Type1: ...
class UsesType2:
def method(self) -> Type2: ...
TUses = TypeVar("TUses", UsesType1, UsesType2)
# Create a generic class using the grouped types
class SomeClass(Generic[TUses]):
def __init__(self, uses: TUses) -> None:
self.uses: TUses = uses
@overload
def add(self: SomeClass[UsesType1]) -> Type1: ...
@overload
def add(self: SomeClass[UsesType2]) -> Type2: ...
def add(self) -> Type1 | Type2:
return self.uses.method() Or more clever using a protocol: from typing import Any, Generic, Protocol, TypeVar
class Type1: ...
class Type2: ...
T = TypeVar("T")
T_co = TypeVar("T_co", covariant=True)
class UsesProto(Protocol[T_co]):
def method(self) -> T_co: ...
class UsesType1:
def method(self) -> Type1: ...
class UsesType2:
def method(self) -> Type2: ...
# Create a generic class using the grouped types
class SomeClass(Generic[T]):
def __init__(self, uses: UsesProto[T]) -> None:
self.uses = uses
def add(self) -> T:
return self.uses.method()
reveal_type(SomeClass(UsesType1()).add()) |
Thank you, we were able to solve it simply with generic protocols. Apologies for not closing sooner |
I have had a very specific issue in my own time, where two (can be thought of as n) generic types relate to each other and need to be grouped together. My use case was the return type of a function from one generic type should be another generic type, but only one specific concrete type of that generic type. I'm not sure if that makes sense, but the example below shows the rough implementation and syntax i am imagining for this. Frankly i have no idea if this is realistic or not, but i thought some others may have had some kind of issue similar to this.
The text was updated successfully, but these errors were encountered: