| Deutsch English Français Italiano |
|
<resolution-20250118170203@ram.dialup.fu-berlin.de> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!fu-berlin.de!uni-berlin.de!not-for-mail
From: ram@zedat.fu-berlin.de (Stefan Ram)
Newsgroups: comp.lang.python
Subject: Re: Struggling to understand Callable type hinting
Date: 18 Jan 2025 16:11:05 GMT
Organization: Stefan Ram
Lines: 27
Expires: 1 Jan 2026 11:59:58 GMT
Message-ID: <resolution-20250118170203@ram.dialup.fu-berlin.de>
References: <mailman.87.1737156810.2912.python-list@python.org> <Callable-20250118125426@ram.dialup.fu-berlin.de>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de fpIBH9MyUD+Hdoua1JNqVgD0DHq4SI8lEI54ovrqJ9ZNIP
Cancel-Lock: sha1:F/SSt46emWEUbhmLWSqlx4whvAE= sha256:OkMDKwBuCCSyLzyBgnA6DxTBNf7OdtSNL3/8Ui785lg=
X-Copyright: (C) Copyright 2025 Stefan Ram. All rights reserved.
Distribution through any means other than regular usenet
channels is forbidden. It is forbidden to publish this
article in the Web, to change URIs of this article into links,
and to transfer the body without this notice, but quotations
of parts in other Usenet posts are allowed.
X-No-Archive: Yes
Archive: no
X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some
services to mirror the article in the web. But the article may
be kept on a Usenet archive server with only NNTP access.
X-No-Html: yes
Content-Language: en-US
Bytes: 2611
ram@zedat.fu-berlin.de (Stefan Ram) wrote or quoted:
> @staticmethod
> def _check_eof(method: Callable[Concatenate['BufferScanner', P], T]) -> Callable[Concatenate['BufferScanner', P], bool]:
> def wrapper(self: 'BufferScanner', *args: P.args, **kwargs: P.kwargs) -> bool:
"P" (ParamSpec) is resolved when the decorator is applied to any
method.
- "P" (ParamSpec) captures all parameters of the decorated
method except for the first "self" parameter.
"Concatenate['BufferScanner', P]" then adds the
"BufferScanner" type as the first parameter, effectively
representing the complete parameter list of the method.
"T" (TypeVar) is inferred based on the return type of the method
being decorated.
So, those type variables are resolved based on the specific
method the decorator is applied to, allowing for generic and
reusable type annotations across different classes and methods
(This "resolution" of a type name corresponds to the
initialization of a traditional name. But a single type name
can have multiple values when the wrapper is being applied
to multiple methods - one value per application.)