Skip to content

Is there a way to prevent many @overload? #6424

Answered by erictraut
pbelskiy asked this question in Q&A
Discussion options

You must be logged in to vote

For polymorphic functions like this, @overload is the go-to solution.

Here's another approach that reduces the redundancy but isn't quite as clean.

Code sample in pyright playground

import asyncio
from typing import Any, Coroutine, TypeVar, Generic, cast

R = TypeVar("R")

class Builds(Generic[R]):
    def __init__(self, jenkins: "JenkinsClient | AsyncJenkinsClient") -> None:
        self.jenkins = jenkins

    def get(self, name: str) -> R:
        return cast(R, self.jenkins._request(name))

class AsyncJenkinsClient:
    def __init__(self) -> None:
        self.builds = Builds[Coroutine[Any, Any, str]](self)

    async def _request(self, name: str) -> Any:
        return name

class Jen…

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@pbelskiy
Comment options

Answer selected by pbelskiy
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants