[SOLVED] Struggling to understand Callable type hinting

Ian Pilcher arequipeno at gmail.com
Sat Jan 18 11:17:31 EST 2025


(Note:  I have mail delivery disabled for this list and read it through
GMane, so I am unable to respond with correct threading if I'm not cc'ed
directly.)

On 1/18/25 8:52 AM, Ian Pilcher wrote:
> (And even that wouldn't really be correct, if it worked, because it
> doesn't express the fact that the arguments of the two Callables are the
> same.  I believe that ParamSpecs are supposed to address this, but there
> doesn't seem to be any way to use a ParamSpec when one or more of the
> parameter types is known.

I think that I figured it out.

     __P = typing.ParamSpec('__P')

     @staticmethod
     def _check_eof(
         method: collections.abc.Callable[
             typing.Concatenate[BufferScanner, __P], None
         ]
     ) -> (
         collections.abc.Callable[
             typing.Concatenate[BufferScanner, __P], bool
         ]
     ):
         ...

Mypy is happy, and it catches me if I try to use the decorator on some-
thing that isn't a method of BufferScanner (or some other callable that
takes an instance of BufferScanner as its first positional argument).

-- 
========================================================================
If your user interface is intuitive in retrospect ... it isn't intuitive
========================================================================


More information about the Python-list mailing list