Re: Struggling to understand Callable type hinting

Liste des GroupesRevenir à cl python 
Sujet : Re: Struggling to understand Callable type hinting
De : no.email (at) *nospam* nospam.invalid (Paul Rubin)
Groupes : comp.lang.python
Date : 18. Jan 2025, 10:03:28
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <87o704jxy7.fsf@nightsong.com>
References : 1 2
User-Agent : Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Ian Pilcher <arequipeno@gmail.com> writes:
I cannot figure out how to correctly specify the Callable argument and
return type for _check_eof().

It looks like _check_eof() is supposed to be a decorator, which is a
function that accepts a callable and returns another callable with the
same signature.  Could something like this work?  The idea is to use
type variables to capture the parameter lists.  I'm not sure how to
specialize it even further to methods of that class.  I haven't tested
any of this.  Looking at the 3.12 docs I see that the typing stuff has
changed a noticeably since last time I tried to use it.

T = TypeVar('T')
U = TypeVar('U')

def _check_eof(method: Callable[T: ..., U]) -> Callable[T,U]:
   ...

Actually it might be best to do this with the new generics features.
I'll look at it some more out of general interest, but I think the other
poster is right to say start out with something approximate.  In the
Haskell world there are well known opportunities to go completely crazy
with highly precise types, and with Python (mypy), I found as of 3.8
that there were often no good ways to do exactly what you wanted.  The
Mypy type system isn't really sound anyway.  It's more of a bug catching
device that works some of the time but not always, so don't expect too
much from it.

Date Sujet#  Auteur
18 Jan 25 * Struggling to understand Callable type hinting6Ian Pilcher
18 Jan 25 +- Re: Struggling to understand Callable type hinting1Paul Rubin
18 Jan 25 `* Re: Struggling to understand Callable type hinting4Stefan Ram
18 Jan 25  +- Re: Struggling to understand Callable type hinting1Stefan Ram
18 Jan 25  +- Re: Struggling to understand Callable type hinting1Stefan Ram
18 Jan 25  `- Re: Struggling to understand Callable type hinting1Stefan Ram

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal