Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #197204
| Path | csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Ian Pilcher <arequipeno@gmail.com> |
| Newsgroups | comp.lang.python |
| Subject | Re: Struggling to understand Callable type hinting |
| Date | Sat, 18 Jan 2025 08:52:02 -0600 |
| Lines | 95 |
| Message-ID | <mailman.89.1737211929.2912.python-list@python.org> (permalink) |
| References | <f01d0808-16fb-4b91-a518-a84d6973fee1@gmail.com> <113709c0-0530-47c7-ad8d-8294dd00924f@DancesWithMice.info> <3da391eb-b245-429b-bcc1-3c88ef557241@gmail.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8; format=flowed |
| Content-Transfer-Encoding | 8bit |
| X-Trace | news.uni-berlin.de xXn9y7fusmP5giFiJbfqiAn8JEUMGR3ZrbLpT5FFUwHw== |
| Cancel-Lock | sha1:eZicOJJCe7rS2yiJdIaSFWy2jBY= sha256:bXKC7BNxf+czjUMDhylkjFkncFB8p7wRUQu/Jpuh5lI= |
| Return-Path | <arequipeno@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| Authentication-Results | mail.python.org; dkim=pass reason="2048-bit key; unprotected key" header.d=gmail.com header.i=@gmail.com header.b=D1YYBYK+; dkim-adsp=pass; dkim-atps=neutral |
| X-Spam-Status | OK 0.013 |
| X-Spam-Evidence | '*H*': 0.97; '*S*': 0.00; 'project,': 0.03; 'skip:@ 10': 0.03; 'this:': 0.03; 'argument': 0.04; 'def': 0.04; 'parameter': 0.04; 'bunch': 0.05; 'comment:': 0.05; 'error:': 0.05; 'hitting': 0.07; 'problem?': 0.09; 'coding': 0.13; 'import': 0.15; 'that.': 0.15; 'arguments': 0.16; 'gradually': 0.16; 'hints': 0.16; 'incompatible': 0.16; 'instance': 0.16; 'intuitive': 0.16; 'specify': 0.16; 'subject:hinting': 0.16; 'threading': 0.16; 'typing': 0.16; 'wrote:': 0.16; 'message- id:@gmail.com': 0.18; "aren't": 0.19; 'figure': 0.19; 'pm,': 0.19; 'to:addr:python-list': 0.20; 'unable': 0.21; "i've": 0.22; 'skip:_ 10': 0.22; 'version': 0.23; 'to:name:python-list@python.org': 0.24; '(and': 0.25; 'stuff': 0.25; 'cannot': 0.25; 'cc:2**0': 0.25; 'interface': 0.26; "isn't": 0.27; 'expect': 0.28; 'fact': 0.28; 'sense': 0.28; 'header:User-Agent:1': 0.30; 'attempt': 0.31; 'seem': 0.31; "doesn't": 0.32; 'disabled': 0.32; 'issues.': 0.32; 'keyword': 0.32; 'python-list': 0.32; 'unexpected': 0.32; "wouldn't": 0.32; 'but': 0.32; "i'm": 0.33; 'there': 0.33; 'header :In-Reply-To:1': 0.34; 'received:google.com': 0.34; 'able': 0.34; 'skip:" 20': 0.34; 'from:addr:gmail.com': 0.34; 'trying': 0.35; 'complex': 0.35; 'yes,': 0.35; 'also,': 0.36; 'really': 0.36; 'errors': 0.36; 'source': 0.36; '...': 0.37; 'missing': 0.37; "skip:' 10": 0.37; "it's": 0.37; 'could': 0.37; 'class': 0.37; 'example': 0.37; 'file': 0.38; 'way': 0.38; 'read': 0.38; 'list': 0.39; 'use': 0.39; 'two': 0.39; 'this,': 0.39; 'rest': 0.39; 'place.': 0.40; 'learn': 0.40; 'something': 0.40; 'should': 0.40; 'method': 0.61; 'skip:\xc2 10': 0.62; 'gives': 0.62; 'come': 0.62; 'here': 0.63; 'once': 0.63; 'skip:b 20': 0.63; 'email': 0.63; 'skip:b 10': 0.63; 'delivery': 0.64; 'your': 0.64; 'tool': 0.65; 'back': 0.66; 'now,': 0.67; 'types': 0.67; 'areas': 0.67; 'respond': 0.68; 'deal': 0.73; 'easy': 0.74; 'name,': 0.75; 'read,': 0.75; '8bit%:100': 0.76; 'combination': 0.76; 'supposed': 0.76; 'quickly': 0.80; 'indicated': 0.81; 'client': 0.82; 'happens': 0.84; 'go,': 0.84; 'motivated': 0.84; 'situations,': 0.84; 'skip:= 70': 0.84; 'word-': 0.84; '\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0': 0.84; 'subject:type': 0.93 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1737211925; x=1737816725; darn=python.org; h=content-transfer-encoding:in-reply-to:from:cc:content-language :references:to:subject:user-agent:mime-version:date:message-id:from :to:cc:subject:date:message-id:reply-to; bh=mbC5J83BSWV1iaWvfJMG182ZLMQab6EOdJag1eQ7mxo=; b=D1YYBYK+pn5H6Q8sNvd/QwcD2m6Brn1tADQ709sdEdrvkg5Kf+uAQ2pqDXL6CMg3nR hspDOwW06h9lb5vMUIck2XDupdyQswVDpVY7dVcoMdcdHB46KojpXRGtPR/B89wkWNvf l93vOTiL0OTvMFJKhassCzSVQXrWOby0OmS4iB6Y2BH80zYWgQJYKq6K1A1NCCaM7jV6 /z5wiCjM9/gl8LFVdb+W3heFk4/QKNfOS670r7AFhM1PYpBqLNRO0kWYHMOvXkCE3BrE axF4T/xBWQO5bP6v8gvWmRxF6bIIZPebD250x73rVQU8sImQP/B5wHnr41ZhA/wXeuo9 RN9A== |
| X-Google-DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737211925; x=1737816725; h=content-transfer-encoding:in-reply-to:from:cc:content-language :references:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=mbC5J83BSWV1iaWvfJMG182ZLMQab6EOdJag1eQ7mxo=; b=ovInVPcsqOz+X/719P1liTTuUsx0ESXipZQsqDC4PJSMuoai4I/hFmaFniL5Ajo5wj 8hCx/FMqxIUC31pH69AT0j3HpVyqL/40saSBNwJWF0NabePV6NH13CrPzmFroAbacXux PG9KjNHhGoOmyXPbcqMcCOigKQQjbYGzIqFJKv5BxuaM7cO1yeDeowzgblCne6+LBDSa xe44dkXmyveWNmAytpu+fJF3Svx4+b92AHzHqURpLCufQ0qOEV8fn62QxLfpkxNOw2I9 yjPGRbw8pMZKXkQujCGOa+X9kX245RQ7ADGFnePxb0tO3XdtSYsFULOA0385N8uYTw2H Lbyw== |
| X-Gm-Message-State | AOJu0Yy8LtdcWnQI04uzYn1PmDOOY9V3JTEmK7ikjlwjFGBi65n7tIRK yh7ZOdm7MK/ve+N2g1KM6arQTtY5sLusHbtoFUD4JSQBw7qAbnwT6Bg5XA== |
| X-Gm-Gg | ASbGnctcKbqFvbClVWn7L8QorW0MTCMdH/LjlsvTs2nisjU6gO5l8f1n6nRc4JmPkl3 e3T6QUCZUWkuUPiXV7IxVZN7Z8pZ7CB0tHbe/iUrixzlt7aZSCWFUZkNNBQTywVeInLNpunZsdx wJASK1ikiLsIzaWQtvwDlxXWfJawbDSd+cDF2v9k+p/Clxdz3nrb1LgVqhNM1L16q63GIMfogm7 w967bsnzioamWLGd+njBnyCawu55BoonRjGx1IG3m6tMBYFoycpkdGkOCkcJwjFmS8edtjanh9L ReDtGtUEKYa9RyZ+G4/83XZ/rB6ahyO1STySxo0= |
| X-Google-Smtp-Source | AGHT+IHlY6DFNZAw9TZEms0VE/htC4M8/5Jy40ODSyEm/PFNIEKXedsRijYN+lwvJb3KWPjyLn6djA== |
| X-Received | by 2002:a05:6871:538b:b0:297:274d:1c38 with SMTP id 586e51a60fabf-2b1c0944144mr3530347fac.18.1737211925497; Sat, 18 Jan 2025 06:52:05 -0800 (PST) |
| User-Agent | Mozilla Thunderbird |
| Content-Language | en-US |
| In-Reply-To | <113709c0-0530-47c7-ad8d-8294dd00924f@DancesWithMice.info> |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.39 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <https://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| X-Mailman-Original-Message-ID | <3da391eb-b245-429b-bcc1-3c88ef557241@gmail.com> |
| X-Mailman-Original-References | <f01d0808-16fb-4b91-a518-a84d6973fee1@gmail.com> <113709c0-0530-47c7-ad8d-8294dd00924f@DancesWithMice.info> |
| Xref | csiph.com comp.lang.python:197204 |
Show key headers only | View raw
(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/17/25 7:26 PM, dn via Python-list wrote:
> On 18/01/25 12:33, Ian Pilcher via Python-list wrote:
>> I am making my first attempt to use type hinting in a new project, and
>> I'm quickly hitting areas that I'm having trouble understanding. One of
>> them is how to write type hints for a method decorator.
>>
>> Here is an example that illustrates my confusion. (Sorry for the
>> length.)
>>
>>
>> import collections.abc
>>
>> class BufferScanner(object):
>>
> ...
>
>> @staticmethod
>> def _check_eof(method: collections.abc.Callable -> (
>> collections.abc.Callable
>> ):
> ...
>>
>> I cannot figure out how to correctly specify the Callable argument and
>> return type for _check_eof(). As indicated by the name, method should
>> be a method (of the BufferScanner class), so its first positional
>> argument should always be an instance of BufferScanner, but it could
>> have any combination of positional and/or keyword arguments after that.
>
> Is it a typing problem?
> The def is not syntactically-correct (parentheses).
Yes, but only when I re-typed it in my email client to avoid word-
wrapping issues.
> What happens once corrected?
Once the missing parenthesis is added, the version in my original post
works just fine, but it's incomplete, because the Callables aren't
parameterized.
Based on what I've read, I would expect to be able to replace:
@staticmethod
def _check_eof(method: collections.abc.Callable) -> (
collections.abc.Callable
):
...
with something like this:
@staticmethod
def _check_eof(
method: collections.abc.Callable[[BufferScanner, ...], None]
) -> (
collections.abc.Callable[[BufferScanner, ...], bool]
):
...
But Mypy gives me a bunch of errors when I do that.
bs.py:19: error: Unexpected "..." [misc]
bs.py:21: error: Unexpected "..." [misc]
bs.py:32: error: Argument 1 to "_check_eof" of "BufferScanner" has
incompatible type "Callable[[BufferScanner], None]"; expected
"Callable[[BufferScanner, Any], None]" [arg-type]
Found 3 errors in 1 file (checked 1 source file)
(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.
> Also, which tool is 'complaining', and what does it have to say?
Mypy 1.14.1.
> General comment: as far as type-hints go, rather than trying to learn
> how to deal with complex situations, it might be better to ease-in
> gradually - add the easy stuff now, and come back to deal with the rest
> later (otherwise the typing 'tail' is wagging the coding 'dog'!)
Makes sense ... except that this decorator is what motivated me to use
type hinting in this project in the first place.
--
========================================================================
If your user interface is intuitive in retrospect ... it isn't intuitive
========================================================================
Back to comp.lang.python | Previous | Next | Find similar
Re: Struggling to understand Callable type hinting Ian Pilcher <arequipeno@gmail.com> - 2025-01-18 08:52 -0600
csiph-web