Sujet : Re: Struggling to understand Callable type hinting
De : ram (at) *nospam* zedat.fu-berlin.de (Stefan Ram)
Groupes : comp.lang.pythonDate : 18. Jan 2025, 17:11:05
Autres entêtes
Organisation : Stefan Ram
Message-ID : <resolution-20250118170203@ram.dialup.fu-berlin.de>
References : 1 2
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.)