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 ?
This is on Python 3.11.2 with mypy 1.0.1 on Debian.
Hi all,
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 ?
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,
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.
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.
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.
ah... didn't grok what you were asking, sorry - ignore my attempt then.
So you are passing something that has been typed more
narrowly than the function parameter.
Can you use a TypeGuard here?
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.
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.
On 30 Dec 2023, at 15:11, Karsten Hilbert via Python-list <python-list@python.org> wrote:
queries = [{'SQL': 'SELECT %(value)s', 'args': {'value': 1}}]
and
run_queries(conn, queries:list[str|dict[str, Any]]):
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
On 12/29/2023 10:02 AM, Karsten Hilbert via Python-list wrote:
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.
I made a tiny test program with your type signature, and got this error message from mypy:
c:\temp\python\typing_test.py:3: error: X | Y syntax for unions requires Python 3.10 [syntax]
Aside from that, this variation causes no mypy error (you must use
Sequence instead of List), and is also more clear about what you are
trying to get:
from typing import Union, Sequence, Dict
DictType1 = Dict[str, str]
DictType2 = Dict[str, Sequence]
DictType3 = Dict[str, Dict]
QueryType = Sequence[Union[DictType1, DictType2, DictType3]]
def test_typing(queries:QueryType=None):
print(type(queries))
d1 = {'k1': 'v1', 'k2': 'v2'}
queries = [d1,]
test_typing(queries)
I'm not sure if this captures exactly what you want, but it avoids the problem where mypy does not regard str and Union[str, list] as
equivalent types. I tested this using Python 3.12.
my suggestion above does
work, *except* that you cannot mix-and-match different DictTypex types
Am Fri, Dec 29, 2023 at 07:49:17AM -0700 schrieb Mats Wichmann via Python-list:
Indeed, I know that much, list[dict[str, str]] is what is gettingI am not sure why mypy thinks thisDict[str, str] means the key type and value type should both be strings,
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 ?
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 yourI agree that mypy's grasp of my intent from
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.
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.
I was wondering if
your type hint for queries shouldn't be the following.
queries:list[dict[str,str]|dict[str,list]|dict[str,dict[str, dict[str, Ant]]]
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.
Sorry for the late reaction
I was wondering if
your type hint for queries shouldn't be the following.
queries:list[dict[str,str]|dict[str,list]|dict[str,dict[str, dict[str, Ant]]]
My impression at this moment is that you are write something like: dict[str, str | int] as
as shorthand for dict[str, str] | dict[str, int].
But those two are different types.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 300 |
Nodes: | 16 (2 / 14) |
Uptime: | 64:24:43 |
Calls: | 6,712 |
Files: | 12,244 |
Messages: | 5,356,126 |