Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Discussion options

For a an complex API client I have build an decorator which injects a http session into the method inside a class.

def do_auth(
    fn: Callable[Concatenate['ApiClient', _HttpSession, P], RetType],
) -> Callable[Concatenate['ApiClient', str | None, P], RetType]:

The function inside the decorator looks ruffly like this:

    @functools.wraps(fn)
    def wrapper(self: 'ApiClient', client_id: str | None = None, *args: P.args, **kwargs: P.kwargs) -> RetType:

The typing of the decorator is slightly wrong since the wrapper function defines a default value of None. But I haven't found a way to tell my type checker that.

The mypy docs say that Protocol classes are the correct way to type this, but I wasn't able with the self parameter. Can someone point me into the correct direction?

You must be logged in to vote

Replies: 1 comment

Comment options

The protocol should look somewhat like this (untested):

class AuthWrapper[**P, R]:
    def __call__(self, client: ApiClient, client_id: str | None = None, /, *args: P.args, **kwargs: P.kwargs) -> R: ...

(The "self" here refers to the callable itself, not the client object.)

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
Morty Proxy This is a proxified and sanitized view of the page, visit original site.