Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #197201
| From | Paul Rubin <no.email@nospam.invalid> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Struggling to understand Callable type hinting |
| Date | 2025-01-18 01:03 -0800 |
| Organization | A noiseless patient Spider |
| Message-ID | <87o704jxy7.fsf@nightsong.com> (permalink) |
| References | <f01d0808-16fb-4b91-a518-a84d6973fee1@gmail.com> <mailman.87.1737156810.2912.python-list@python.org> |
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.
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar
Struggling to understand Callable type hinting Ian Pilcher <arequipeno@gmail.com> - 2025-01-17 17:33 -0600 Re: Struggling to understand Callable type hinting Paul Rubin <no.email@nospam.invalid> - 2025-01-18 01:03 -0800
csiph-web