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

New GroupedTypeVar type #1899

Open
MatthewMckee4 opened this issue Dec 16, 2024 · 2 comments
Open

New GroupedTypeVar type #1899

MatthewMckee4 opened this issue Dec 16, 2024 · 2 comments
Labels
topic: feature Discussions about new features for Python's type annotations

Comments

@MatthewMckee4
Copy link

MatthewMckee4 commented Dec 16, 2024

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.

from typing import GroupedTypeVar, Generic

# Define valid groups of types
TKey, TValue = GroupedTypeVar(
   "TKey, TValue"
    (str, int),  # If the key is str, the value must be int
    (bytes, float)  # If the key is bytes, the value must be float
)

# Create a generic class using the grouped types
class KeyValueStore(Generic[TKey, TValue]):
    def __init__(self):
        self.store: dict[TKey, TValue] = {}

    def add(self, key: TKey, value: TValue) -> None:
        self.store[key] = value

    def get(self, key: TKey) -> TValue:
        return self.store[key]
@MatthewMckee4 MatthewMckee4 added the topic: feature Discussions about new features for Python's type annotations label Dec 16, 2024
@JelleZijlstra
Copy link
Member

You can do this sort of thing with overloads. I think that's sufficient.

@MatthewMckee4
Copy link
Author

MatthewMckee4 commented Dec 16, 2024

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.
A better examples is:

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: feature Discussions about new features for Python's type annotations
Projects
None yet
Development

No branches or pull requests

2 participants