mypy question
Thomas Passin
list1 at tompassin.net
Sat Dec 30 07:45:15 EST 2023
On 12/29/2023 10:02 AM, Karsten Hilbert via Python-list wrote:
> 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.
Maybe better to ask the mypy people directly.
> Karsten
> --
> GPG 40BE 5B0E C98E 1713 AFA6 5BC0 3BEA AC80 7D4F C89B
More information about the Python-list
mailing list