mypy question
Antoon Pardon
antoon.pardon at vub.be
Fri Jan 12 08:23:43 EST 2024
Op 29/12/2023 om 16:02 schreef Karsten Hilbert via Python-list:
> 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.
Sorry for the late reaction and may be I am missing something, but 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.
--
Antoon Pardon.
More information about the Python-list
mailing list