Skip to content

Conversation

@jorenham
Copy link
Owner

cat examples/generic.py
from typing import Protocol, overload

class Simple: ...

class Stack[T]:
    def push(self, value: T, /) -> None: ...
    @overload
    def pop(self, /) -> T: ...
    @overload
    def pop[D](self, default: D, /) -> T | D: ...

class CanSubscript[T_in, T_out](Protocol):
    def __getitem__(self, k: T_in, /) -> T_out: ...
pythoff examples/generic.py
from typing import Generic, Protocol, overload
from typing_extensions import TypeVar

class Simple: ...

T = TypeVar('T', infer_variance=True)
D = TypeVar('D')

class Stack(Generic[T]):
    def push(self, value: T, /) -> None: ...
    @overload
    def pop(self, /) -> T: ...
    @overload
    def pop(self, default: D, /) -> T | D: ...

T_in = TypeVar('T_in', contravariant=True)
T_out = TypeVar('T_out', covariant=True)

class CanSubscript(Protocol[T_in, T_out]):
    def __getitem__(self, k: T_in, /) -> T_out: ...

@jorenham jorenham merged commit 321b832 into master Sep 15, 2024
3 checks passed
@jorenham jorenham deleted the pep695-generics branch September 15, 2024 00:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant