Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

"A car is just a big purse on wheels." -- Johanna Reynolds


devel / comp.lang.python / Re: Aw: Re: mypy question

SubjectAuthor
o Re: Aw: Re: mypy questionThomas Passin

1
Re: Aw: Re: mypy question

<mailman.94.1703955442.2892.python-list@python.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=29159&group=comp.lang.python#29159

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!fu-berlin.de!uni-berlin.de!not-for-mail
From: list1@tompassin.net (Thomas Passin)
Newsgroups: comp.lang.python
Subject: Re: Aw: Re: mypy question
Date: Sat, 30 Dec 2023 11:57:15 -0500
Lines: 76
Message-ID: <mailman.94.1703955442.2892.python-list@python.org>
References: <ZY64YbzKqfGbZcpC@hermes.hilbert.loc>
<fd6bbad8-2736-4305-982d-eabbf212a920@wichmann.us>
<ZY7faSj-OA-lDqxC@hermes.hilbert.loc>
<1d9aa14c-fd26-4afd-b2fc-a9e516878491@tompassin.net>
<trinity-0dd8a644-4582-4c0c-a73e-4de250802749-1703948907120@3c-app-gmx-bap02>
<8fc100d4-d38d-4eb8-a590-0a00acd625a9@tompassin.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: news.uni-berlin.de IXB/O/Qf+q92OX/qKMgETAQQQVLq8O5QW0y/cw7nWY5A==
Cancel-Lock: sha1:1bdeOj9Og5G/T4e4FWbvmAAduL4= sha256:UElno1nd17g7LpKAQ9MvcMLzyCzV2Eivl9AUCbHqE7A=
Return-Path: <list1@tompassin.net>
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=tompassin.net header.i=@tompassin.net header.b=K93iBTfX;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.017
X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'fairly': 0.05; 'issue.':
0.05; 'string': 0.07; 'karsten': 0.09; 'reasoning': 0.09;
'arguments': 0.16; 'conversion': 0.16; 'dict': 0.16; 'dicts':
0.16; 'grasp': 0.16; 'hilbert': 0.16; 'hints': 0.16;
'received:10.0.0': 0.16; 'received:64.90': 0.16;
'received:64.90.62': 0.16; 'received:64.90.62.162': 0.16;
'received:dreamhost.com': 0.16; 'skip:. 20': 0.16; 'step,': 0.16;
'str.': 0.16; 'tuples': 0.16; 'wrote:': 0.16; 'python': 0.16;
'subject:question': 0.17; 'values': 0.17; 'probably': 0.17;
'to:addr:python-list': 0.20; 'first,': 0.22; 'maybe': 0.22;
'code': 0.23; 'run': 0.23; 'function': 0.27; 'header:User-
Agent:1': 0.30; 'am,': 0.31; 'python-list': 0.32; 'received:10.0':
0.32; 'received:mailchannels.net': 0.32;
'received:relay.mailchannels.net': 0.32; 'simple,': 0.32; "i'm":
0.33; 'work.': 0.34; 'header:In-Reply-To:1': 0.34; 'complex':
0.35; 'respect': 0.35; 'change': 0.36; 'those': 0.36; 'lists':
0.37; 'could': 0.38; 'thanks': 0.38; 'least': 0.39; 'list': 0.39;
'explain': 0.40; 'want': 0.40; 'should': 0.40; 'helps': 0.60;
'above': 0.62; 'dear': 0.62; 'complete': 0.64; 'your': 0.64;
'look': 0.65; 'now,': 0.67; 'header:Received:6': 0.67;
'received:64': 0.67; 'further': 0.69; 'complexity': 0.69;
'types,': 0.69; 'database': 0.80; 'queries': 0.81; '-->': 0.84;
'chose': 0.84; 'converted': 0.84; 'reducing': 0.84; 'strings':
0.84; 'skip:q 20': 0.95
X-Sender-Id: dreamhost|x-authsender|tpassin@tompassin.net
ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1703955437; a=rsa-sha256;
cv=none;
b=C8N253N3FLDt3EO3NubJG2cXB96YCvw0rtYgAGyhtdUUqQZKJwY5NgBfex3/4SIm9sCacO
eLTLypAF46IZqUKebcgIDX87N2lNRfWRqPn6hXOmgENaI5DNOoynHUvXdDTrW0Lb/5Z+Mj
bVLT9KjtTIbL1Q3gatmmJWfLsPUJkEzgRHQLfd4xhAxBZ4RdglBKgdUWVJZULe5n+99sDs
jnlaVrPkL1kWZWcRZf+45nfq58WuxPJ0S83FtQraratywUGF1Fn1u8fLuLzYmGpA9E3Tkj
okfASPn6oZQGyTeDJ+Vp7hZuGtPcXPvt32NPfn2SMSaI4ojC9sL68KfSIFLeXA==
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed;
d=mailchannels.net; s=arc-2022; t=1703955437;
h=from:from:reply-to:subject:subject:date:date:message-id:message-id:
to:to:cc:mime-version:mime-version:content-type:content-type:
content-transfer-encoding:content-transfer-encoding:
in-reply-to:in-reply-to:references:references:dkim-signature;
bh=GdQdrL4b7CgZS1uqQ+RmVbgjbkjVDnQK8FhqgcyT6Jg=;
b=3b6UslsKnQG1/JzfaRB2qaXAhDv+UIgPOsU9TpCXnDGMHgGV6BzqOSIuhYwtKjFqi2n6hu
2jHJ2oAdj2+5DabkKuOBUSiFlZedugIkM9j97EKBTgnRfWRwDbCZ20CSSKO+Io2LPhOvn8
CAfk3W93ceuwvzXEbo3EaMO7KiyREQXtjqDGMuJiMeBKKRfmEv/iiqLVPkDmgMqLsI/cHd
NRBmyXm5ooLOncP34Rniw9s3XOE8CLLIWqWWJ7MDJ9EGNTtEzCCo6p+uKmBzSc/P8p7zN/
LSJzvZeQ7yrvrdIJKgJHWKHmjwSC4MAPhoz2rlNaAqaCa8xW9xTEIT7PuR3pww==
ARC-Authentication-Results: i=1; rspamd-659dcc87c8-hshrf;
auth=pass smtp.auth=dreamhost smtp.mailfrom=list1@tompassin.net
X-Sender-Id: dreamhost|x-authsender|tpassin@tompassin.net
X-MC-Relay: Neutral
X-MailChannels-SenderId: dreamhost|x-authsender|tpassin@tompassin.net
X-MailChannels-Auth-Id: dreamhost
X-Continue-Broad: 2bc1b99e116b29c2_1703955437223_1518873268
X-MC-Loop-Signature: 1703955437223:1959353016
X-MC-Ingress-Time: 1703955437222
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tompassin.net;
s=dreamhost; t=1703955436;
bh=GdQdrL4b7CgZS1uqQ+RmVbgjbkjVDnQK8FhqgcyT6Jg=;
h=Date:Subject:To:From:Content-Type:Content-Transfer-Encoding;
b=K93iBTfXZ42EfHjhaGy21XVTTb88jlv9WJEyodK/d1KxbpdGpUfbnefc/UmeBys5V
UXxZfLVKbgmWKD9QVFihFi31BdNzVNuLJurlTTrJiEd7bbRzGNDkfgvLU1eq/kpd5o
5LHYk1PgjAwiyW65927KoxGFmHgrNb0yQESVpJv6nfnuyfvEbkLEuOi9NXizOIPo4O
J2icBGalUMcaG2Kpbe5bfB/BzpILzqUxbuL0PUoDlx1WiAE3RW8tUlHJzBhlzngw2o
wsUTT81ngiYXq8SFa7/Y47JrafnKqnmfh6P8ED5wUZyCxbmA9JiTtg/n7RzkU4HEEP
rsN542Wk8ESlg==
User-Agent: Mozilla Thunderbird
Content-Language: en-US
In-Reply-To: <trinity-0dd8a644-4582-4c0c-a73e-4de250802749-1703948907120@3c-app-gmx-bap02>
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: <8fc100d4-d38d-4eb8-a590-0a00acd625a9@tompassin.net>
X-Mailman-Original-References: <ZY64YbzKqfGbZcpC@hermes.hilbert.loc>
<fd6bbad8-2736-4305-982d-eabbf212a920@wichmann.us>
<ZY7faSj-OA-lDqxC@hermes.hilbert.loc>
<1d9aa14c-fd26-4afd-b2fc-a9e516878491@tompassin.net>
<trinity-0dd8a644-4582-4c0c-a73e-4de250802749-1703948907120@3c-app-gmx-bap02>
 by: Thomas Passin - Sat, 30 Dec 2023 16:57 UTC

On 12/30/2023 10:08 AM, Karsten Hilbert via Python-list wrote:
> Dear Thomas,
>
> thanks for taking the time to look into my issue.
>
> Maybe it helps if I explain what I want (sorry that my web mailer does not respect
> indentation, I will insert dots).
>
> I want a function to run SQL queries:
>
> run_queries(conn, queries):
> ...for q in queries:
> ......conn.execute(q)
>
> I now want to add type hints such that my large codebase can
> be checked for silly doings. First, queries is to be a list
> of the SQL to run:
>
> run_queries(conn, queries:list):
>
> Then, each list entry can be
>
> ...a string holding simple, complete SQL (say "SELECT 1")
>
> run_queries(conn, queries:list[str]):

It occurs to me that you could simplify things if you converted those
plain query strings to dicts:

'SELECT 1' --> {'SQL': 'SELECT 1'}

I'm fairly sure your database queries don't actually give you strings or
dicts, right? You probably get lists (or iterators) of tuples and
somewhere you convert them to the arguments you are feeding to
run_queries(). At least, that is how the standard Python db adapters
work. If you change that conversion step, your arguments to
run_queries() will all be lists of dicts, making your code simpler and
reducing the complexity of the type hints.

> or
>
> ...a dict holding the SQL and arguments for parameters
>
> run_queries(conn, queries:list[dict]):
>
> So, taken together:
>
> run_queries(conn, queries:list[str|dict]):
>
> (yes, this is in Python 3.11/3.12)
>
> Now, when it is a list of dicts I want to further constrain the
> dicts. Each is to contain the keys "SQL" and "args". So the keys
> are of type str. The values for the keys will be of various types,
> such that I chose Any as pseudo-type, so that each list entry that
> is of type dict should be dict[str, Any], hence:
>
> queries = [{'SQL': 'SELECT %(value)s', 'args': {'value': 1}}]
>
> and
>
> run_queries(conn, queries:list[str|dict[str, Any]]):
>
> If I now call this function with a simple SQL query:
>
> SQL_query = 'SELECT 1' # should be type str ?
> queries = [SQL_query] # should be type list[str] ?
> run_queries(conn, queries = queries)
>
> and run mypy over that (at least inside my complex codebase) I will
> get a type mismatch being hinted at.
>
> So far I don't grasp at which point my reasoning above is faulty.
>
> Karsten


devel / comp.lang.python / Re: Aw: Re: mypy question

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor