Path: ...!weretis.net!feeder9.news.weretis.net!news.quux.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail From: Paul Rubin Newsgroups: comp.lang.python Subject: Re: Struggling to understand Callable type hinting Date: Sat, 18 Jan 2025 01:03:28 -0800 Organization: A noiseless patient Spider Lines: 27 Message-ID: <87o704jxy7.fsf@nightsong.com> References: MIME-Version: 1.0 Content-Type: text/plain Injection-Date: Sat, 18 Jan 2025 10:03:32 +0100 (CET) Injection-Info: dont-email.me; posting-host="822a5c3e8dbad8f82317990e10d7cf3b"; logging-data="758679"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19RnlYAyGjuml8V2TXtO68x" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux) Cancel-Lock: sha1:tWP3nxGfSup8osWhcqYIuJmQQUw= sha1:Pt+0m2Gq1ED+xn5FcZwTVzdl3AQ= Bytes: 2404 Ian Pilcher 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.