Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

The sooner all the animals are extinct, the sooner we'll find their money. -- Ed Bluestone


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

SubjectAuthor
* Re: mypy questionKarsten Hilbert
`* Re: mypy questionGreg Ewing
 `- Aw: Re: mypy questionKarsten Hilbert

1
Re: mypy question

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

  copy mid

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

  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: Karsten....@gmx.net (Karsten Hilbert)
Newsgroups: comp.lang.python
Subject: Re: mypy question
Date: Fri, 29 Dec 2023 16:02:01 +0100
Lines: 66
Sender: <karsten.hilbert@gmx.net>
Message-ID: <mailman.75.1703862125.2892.python-list@python.org>
References: <ZY64YbzKqfGbZcpC@hermes.hilbert.loc>
<fd6bbad8-2736-4305-982d-eabbf212a920@wichmann.us>
<ZY7faSj-OA-lDqxC@hermes.hilbert.loc>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: quoted-printable
X-Trace: news.uni-berlin.de UiqN2+PjOqUM5iBvdWn3nwfA1EVu7mS3zOzkb8HuktNw==
Cancel-Lock: sha1:6KHdJHScwISoGx+0uBFyzaHdEI0= sha256:XSzf35VnhpJgsuYVh6hFzjEJW6EiXWUTdp59uY0MW0w=
Return-Path: <karsten.hilbert@gmx.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=gmx.net header.i=karsten.hilbert@gmx.net
header.b=WFhw9SnL; dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.000
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'argument': 0.04; 'def':
0.04; 'error:': 0.05; 'indicate': 0.05; '2023': 0.07; 'matches':
0.07; 'received:212.227': 0.07; 'gpg': 0.09; 'karsten': 0.09;
'much,': 0.09; 'schrieb': 0.09; 'thinks': 0.09; '1713': 0.16;
'construct.': 0.16; 'dict': 0.16; 'dicts': 0.16; 'grasp': 0.16;
'incompatible': 0.16; 'intent': 0.16; 'mats': 0.16;
'received:212.227.15': 0.16; 'skip:> 10': 0.16; 'strings,': 0.16;
'wichmann': 0.16; 'subject:question': 0.17; 'values': 0.17;
'to:addr:python-list': 0.20; 'fri,': 0.22; 'anything': 0.25;
'seems': 0.26; 'dec': 0.31; 'guess': 0.32; 'but': 0.32;
'particular': 0.33; 'header:In-Reply-To:1': 0.34; "it's": 0.37;
'means': 0.38; 'otherwise': 0.39; 'list': 0.39; 'both': 0.40;
'should': 0.40; 'above': 0.62; 'received:212': 0.62; 'pass': 0.64;
'key': 0.64; 'skip:r 20': 0.64; 'your': 0.64; 'skip:t 30': 0.67;
'signature': 0.76; 'queries': 0.81; 'accurate.': 0.84;
'indicates': 0.84; 'recreation': 0.84; 'skip:q 20': 0.95
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net;
s=s31663417; t=1703862122; x=1704466922; i=karsten.hilbert@gmx.net;
bh=OFwHgwt1YvE6Cht6JzK36OCLVCjV+IWYd3QCrCSvi9c=;
h=X-UI-Sender-Class:Date:From:To:Subject:References:In-Reply-To;
b=WFhw9SnL5BJK9ySC3MEww6guGiTjP6Uh4W0TCTDRn53pX0dLeCEUli3YOvTbRzqJ
Fu4wy8Ui1uiS3006uzA5U1P6hQo1ed7jw53FGQeXU0JuLCP2AIk5pzV7fqvvULhLJ
450p5CNuA0SZ8kQPsHVPbfmiEEP3ucd/efq9sL93YDr3NW7CP23o0uAGEvY+A+OSg
hO5pchb/zFCQuHWxgPjZ0K5ojAQxMSrGXEe3p7NriykSWCOMVXkse4u/Y492nd/mw
w9N9AALUz2K6PCALXhMf/dskU/9+035RJDF7t1P5hXmcgNmbNQ9chLveto+3wu3rW
wqdsit4wGb/G2IP1LA==
X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a
Content-Disposition: inline
In-Reply-To: <fd6bbad8-2736-4305-982d-eabbf212a920@wichmann.us>
Ma_X_il-Followup-to: d
Re_X_turn-receipt-to: Karsten.Hilbert@gmx.net
Di_X_sposition-Notification-To: Karsten.Hilbert@gmx.net
X-Confi_X_rm-Reading-To: Karsten.Hilbert@gmx.net
X-Pri_X_ority: 2 (High)
X-Provags-ID: V03:K1:/WosOEQGKvM663xnvlE6BafYiZCptzjukLY5AZ9ZCtvBySxEWU/
u0hUT0Bi6oZktitBdLIv6KfBbg2NCyVeYlc2ZYslbcJXICBmW6iVYHyTUcYheHxkynfzLBn
vLNJOZMGoWhCZ1NmmnI1t/Vmx5/t6onYSdZ92PiBch29do4MccVyfBal6yf3+er9k20e85B
Kn2RNft3uwyJ28dECevtQ==
X-Spam-Flag: NO
UI-OutboundReport: notjunk:1;M01:P0:Wcj1LLggRRE=;KrJNUOrz/r6112fmVNlfi8Miyjf
i6HZlJrt/BlEBbXR1DGwzk1wldJsZl4WfCy3m0g4LKMD3leNyZq25iFAC8KFCnMxM5eQp/Xkz
ZTmxytcRyMpTxNsOIzjVnKYdNsRJdnodeyV75zMquAdxYdUsxJuJck1QhfFgfAKtlSShA7OgF
1Fj4+rHSMNjxwSvcmhni3o5clYAcC1WRfhmjr288om+/BIx1ptWs9DHv9B5+FpWcTxvxsBHOx
buN9g8l+deosD5nM8kCjCmJ2UzsaHXNCpKIsBcrDbM07IEbqRdJ3DuJG6MbA+O03hGID0lmGl
joXSpNFZhQWtHyrhEgljoOJ8vIN07NQ+Mx4qDoLIPxMaFD0VIH58LISDtFlbAv71tdmXsfErE
dVNGah2apQo+Bib3oJfJBEvvSWKlD4042Iexo8qLsMjv30loQMlBe7bQKu5JbHDGaeNJtHjpp
fiUXynz9ol0algabtI3/MpR5q+4Lox0/gnFmro1Jnexi5QvbhDvn3tSRThRTMWOk8Fx3xS0yo
dLa0frZemz9QDpxtfq5YIGE2N3zEeUI4EH/5s1vg5yf3tY5R0mslRRO4IAR5XyJHKWU5MsPvP
J3VvN9q0Y8g9PnLdAef1Q2URBMOxl6iNu1k5WrJDCu5xY2NAXfHE3CM5fIQCAvhp5GY1hshaL
v7zTYf0bL6H9js0iy8EZKOABOXPB3gPtnZwQVF4p449kLW39x0MTr3Dy/AAtcVUTe3eaWzZ7X
MeLAgg5ZWSs1EgPgGOoyvLkAXgOLHK/XSVr7KUQgEqhPVg8UMc4ftJ+GSvR/Su7gk2JkKTdVx
9uOIsO5blpqNOdRRqL3Z2ByDl2xbh+y8N4+wcDnt7sUKdJxB9UNzv8WIJINy2UtMlWmPRyzwU
fIiPX89AcTcN/dA/2JptvNRdwY/YVs761VQXUK7xl8BV6CU+IL/fRYeloA3pw95Mg7e3WBp+1
3Kb2ebHfJA3LlMAjF/owN4goLw0=
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: <ZY7faSj-OA-lDqxC@hermes.hilbert.loc>
X-Mailman-Original-References: <ZY64YbzKqfGbZcpC@hermes.hilbert.loc>
<fd6bbad8-2736-4305-982d-eabbf212a920@wichmann.us>
 by: Karsten Hilbert - Fri, 29 Dec 2023 15:02 UTC

Am Fri, Dec 29, 2023 at 07:49:17AM -0700 schrieb Mats Wichmann via Python-list:

> >I am not sure why mypy thinks this
> >
> >gmPG2.py:554: error: Argument "queries" to "run_rw_queries" has incompatible type "List[Dict[str, str]]"; expected
> >"List[Dict[str, Union[str, List[Any], Dict[str, Any]]]]" [arg-type]
> > rows, idx = run_rw_queries(link_obj = conn, queries = queries, return_data = True)
> > ^~~~~~~
> >
> >should be flagged. The intent is for "queries" to be
> >
> >a list
> > of dicts
> > with keys of str
> > and values of
> > str OR
> > list of anything OR
> > dict with
> > keys of str
> > and values of anything
> >
> >I'd have thunk list[dict[str,str]] matches that ?
>
> Dict[str, str] means the key type and value type should both be strings,

Indeed, I know that much, list[dict[str, str]] is what is getting
passed in in this particular invocation of run_rw_queries().

For what it's worth here's the signature of that function:

def run_rw_queries (
link_obj:_TLnkObj=None,
queries:list[dict[str, str | list | dict[str, Any]]]=None,
end_tx:bool=False,
return_data:bool=None,
get_col_idx:bool=False,
verbose:bool=False
) -> tuple[list[dbapi.extras.DictRow], dict[str, int] | None]:

Given that I would have thought that passing in
list[dict[str, str]] for "queries" ought to be type safe.
Mypy indicates otherwise which I am not grokking as to why.

> but in your
> retelling above you indicate lots of possible value types... actually the mypy guess
> seems to be a pretty good recreation of your psuedo-code description.

I agree that mypy's grasp of my intent from

queries:list[dict[str, str | list | dict[str, Any]]]=None,

into

"List[Dict[str, Union[str, List[Any], Dict[str, Any]]]]"

seems accurate. I just don't understand why list[dict[str,
str]] should not pass that construct.

Karsten
--
GPG 40BE 5B0E C98E 1713 AFA6 5BC0 3BEA AC80 7D4F C89B

Re: mypy question

<kv9cf4Fk7tqU1@mid.individual.net>

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: greg.ew...@canterbury.ac.nz (Greg Ewing)
Newsgroups: comp.lang.python
Subject: Re: mypy question
Date: Sat, 30 Dec 2023 15:09:40 +1300
Lines: 21
Message-ID: <kv9cf4Fk7tqU1@mid.individual.net>
References: <ZY64YbzKqfGbZcpC@hermes.hilbert.loc>
<fd6bbad8-2736-4305-982d-eabbf212a920@wichmann.us>
<ZY7faSj-OA-lDqxC@hermes.hilbert.loc>
<mailman.75.1703862125.2892.python-list@python.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: individual.net Q/eKAapfTE3QRgwYatXbpgiP1S5DBPHcs1B3gmiFQKhbdQv+u9
Cancel-Lock: sha1:GWn/TOZCUm7qnz0nQueAH13f42U= sha256:ZekFzTsOnzZfC8vMxWT9e/DXj9tVQDowwknOHtM/iBk=
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:91.0)
Gecko/20100101 Thunderbird/91.3.2
Content-Language: en-US
In-Reply-To: <mailman.75.1703862125.2892.python-list@python.org>
 by: Greg Ewing - Sat, 30 Dec 2023 02:09 UTC

On 30/12/23 4:02 am, Karsten Hilbert wrote:

> def run_rw_queries (
> link_obj:_TLnkObj=None,
> queries:list[dict[str, str | list | dict[str, Any]]]=None,

> Given that I would have thought that passing in
> list[dict[str, str]] for "queries" ought to be type safe.

dict[str, str] is not a subtype of dict[str, str | something_else]
because you can assign a value of type something_else to the latter
but not the former.

In this case it happens to be okay because the function is (presumably)
treating the dict passed in as immutable, but MyPy has no way to be sure
of that.

You could try declaring it as a collections.Mapping, which is immutable.

--
Greg

Aw: Re: mypy question

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

  copy mid

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

  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: Karsten....@gmx.net (Karsten Hilbert)
Newsgroups: comp.lang.python
Subject: Aw: Re: mypy question
Date: Sat, 30 Dec 2023 12:23:53 +0100
Lines: 33
Message-ID: <mailman.89.1703935435.2892.python-list@python.org>
References: <ZY64YbzKqfGbZcpC@hermes.hilbert.loc>
<fd6bbad8-2736-4305-982d-eabbf212a920@wichmann.us>
<ZY7faSj-OA-lDqxC@hermes.hilbert.loc>
<mailman.75.1703862125.2892.python-list@python.org>
<kv9cf4Fk7tqU1@mid.individual.net>
<trinity-a03772f0-20d6-4f29-9db2-466923373787-1703935433094@3c-app-gmx-bap02>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
X-Trace: news.uni-berlin.de GQb5z0W70Kqe5FyhmWC81QVgdOANespJSY25rjZuDJWg==
Cancel-Lock: sha1:f9GoQANxmm65AJObj4WtZpsUBkM= sha256:2kJ2MmuJVs8CacatPgkrFDICb7+y9u9sk+lZN6ELlGU=
Return-Path: <Karsten.Hilbert@gmx.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=gmx.net header.i=karsten.hilbert@gmx.net
header.b=TTjeiUnp; dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.012
X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'def': 0.04; 'yet.': 0.04;
'received:212.227': 0.07; 'cc:addr:python-list': 0.09; 'int':
0.09; 'karsten': 0.09; 'cc:no real name:2**0': 0.14; 'applies':
0.16; 'case:': 0.16; 'formally': 0.16; 'idea.': 0.16;
'received:(via http)': 0.16; 'python': 0.16; 'subject:question':
0.17; 'cc:addr:python.org': 0.20; 'input': 0.21; 'code': 0.23;
'skip:p 30': 0.23; 'saying': 0.25; 'cc:2**0': 0.25; 'seems': 0.26;
'else': 0.27; 'seem': 0.31; "doesn't": 0.32; 'but': 0.32; "i'll":
0.33; 'header:In-Reply-To:1': 0.34; 'use': 0.39; 'hand': 0.40;
'something': 0.40; 'should': 0.40; 'to:none': 0.60; "there's":
0.61; 'above': 0.62; 'received:212': 0.62; 'share': 0.63; 'pass':
0.64; 'received:217': 0.67; 'right': 0.68; 'currently,': 0.69;
'latter': 0.69; 'head': 0.73; 'narrow': 0.84
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net;
s=s31663417; t=1703935433; x=1704540233; i=karsten.hilbert@gmx.net;
bh=SD+S/ue2tWqjdeSPv7aAOQ4GShwWDj9JUcBx+njJ9g4=;
h=X-UI-Sender-Class:From:Cc:Subject:Date:In-Reply-To:References;
b=TTjeiUnpYJhnHgXHAkWB+/rhwe21cXb5w9r6nXugGg+OBSebfhgptcdbdiKySpbb
EOq4y5OPuQL169Uu2nYCX8DofFrqGvj4eEqEwSfCefA/bl/Czspqbgg95s0OrAFOm
YWCz9Nj8XKa0lnsYskHvFCC5sD4QJUkEQ1LxeJnMofCeBidynaPpP0cXhzsXbUkH/
gSsnYFuUKEpnmy732eOuW1lpkOuIm/Wlvk4IJuZubjMF+oFD7/1PYDAcLPDbvroyT
g6T5DvtG5bZoqgUZUNQCljc7RXWj4Ne8HEbVqjafTtUPqG8USYBM0PiZ8+sMhMgQ0
k3Nj2VeHHL+XgEigrQ==
X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a
Importance: normal
Sensitivity: Normal
In-Reply-To: <kv9cf4Fk7tqU1@mid.individual.net>
X-UI-Message-Type: mail
X-Priority: 3
X-Provags-ID: V03:K1:PySmnbZZUQT+pBIj8xy9Whngl+In+MM6r7pDZTLbt94rFvo6oAa1SzUk5HmcRnAKUWyAY
73EVg+yvKeH45MjUThUnV4n0jdm6cziQIl43ir1Yyt/d13hooIOq7K/Y+QrILvZm/xE7nGp4Duzq
hG1ivLMAPB7VfXxbXDPQNWqIt0CVW1gG+F2flerdYenjQzqS5SHf4GYg5oF280HxLqQxf/G78183
KLTuzHU9pwd2u59y8q7fvOI16SJn35cGPto9py7M+KL8cbg/UitmgvVHH7xP8zsUBiyvXx+mq9Qp
OM=
X-Spam-Flag: NO
UI-OutboundReport: notjunk:1;M01:P0:v3Sp+ftlj/Y=;JwrwYNT50GMlv/ospjP10Kof1Sp
4gE+vVAq40ywbLkdjUTmbMKqpKAD5XBJbl6szkr+0wu+/DKd/dYtrcOc2HkVn6cg7NmOpldyZ
CRJ6qoLK12RKGGJM+rD7PPXehP31pxTUNMNA6I2Z4x8iAV1MwkURu1Wm+MMCU6nUIBrn4JIqp
XyF2iB5+DLbzd7m3yf1MdRORc4sIQenJ8A3IlkZjDQe3l/+lfpZk0S1Sd1ea8mUOg5m/5ewCR
lHXKhJQ6nCBXrT/wjex/JEm3YmkxcJRlf6XLszZNl5xp24tv9TGf0MDo0W4ECKCwYqcQxm0A/
J2Lwrc5lbZUKW0m9Oqyq58Vv+tB0NbhH4zI7nefg6MOLFN1p0d3xMVWKhgV+xVotqrv+X+b0o
5zi90gXtYO2/5XFelBopEa4/XPm64OdyxS0jRwJU2kxzYwKd9q3Nt4fyXdY9JhGuKbWZAek+p
/RbuuPGzZQS4I+bDtwakm9FFVZLUn5OYDUqGqpeMb3XvnKPqeEEb2h+ApBrmlQkhl1P1is4Y1
BmJi35RW2TP5oK937nhW8oqemgBSVQl6VQZGCAxBtdyMDhv/2u+rzou3/Up1ZercW21UcGDRs
tr5v4Z2TDgkwb922Cp+xRLgCiPvWXh1NdbyF9Wak/IkMkdUoa8l+Rv96VBiGHmen1ahj0cL57
rO9y1Z9z+bEoo7Z6OCIybZkrr6vbcxLsQoubApxWHrw5lQPIHWyrO11DuPddvI3FCOY5f7xGl
YeCjU8gdriGadwNP8v0UuK0r7RDWmmO9vXLCGAM3EcmDc/Paxq5hJC3Ps8YAyTbHd+G7Yqxpo
Ps2qSZwUl/QuUatc/MzQmggA==
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: <trinity-a03772f0-20d6-4f29-9db2-466923373787-1703935433094@3c-app-gmx-bap02>
X-Mailman-Original-References: <ZY64YbzKqfGbZcpC@hermes.hilbert.loc>
<fd6bbad8-2736-4305-982d-eabbf212a920@wichmann.us>
<ZY7faSj-OA-lDqxC@hermes.hilbert.loc>
<mailman.75.1703862125.2892.python-list@python.org>
<kv9cf4Fk7tqU1@mid.individual.net>
 by: Karsten Hilbert - Sat, 30 Dec 2023 11:23 UTC

Hi Greg,

> dict[str, str] is not a subtype of dict[str, str | something_else]
> because you can assign a value of type something_else to the latter
> but not the former.

I understand what you are saying but I do not yet understand why this
applies to my situation.

I don't have Python at hand currently, so I'll write untested pseudocode:

def print_greeting(greeting:int|str):
print(greeting)

print_greeting('hello')

The above snippet should be equivalent to my more complicated code over
which mypy complains to the equivalent of

"input" is of type "str"
but expected type "Union[str,int]

I do understand that "str" is formally more narrow than "Union [str,int]" and
the type system has every right to not consider them equivalent.

However, this seems like a very common use case: "allow passing in either str or int
and have type checking pass either input as valid" -- yet mypy doesn't seem
to share that idea.

Or else there's something I haven't wrapped my head around yet. But what ?

Karsten

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor