mypy question

Karsten Hilbert Karsten.Hilbert at gmx.net
Fri Dec 29 15:33:36 EST 2023


Am Fri, Dec 29, 2023 at 11:04:59AM -0700 schrieb Mats Wichmann via Python-list:

> >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.

Never mind, the attempt to help is appreciated.

> So you are passing something that has been typed more
> narrowly than the function parameter.

That would then sort of skirt on violation of the Liskov
principle, of which I learned while trying to research this
mypy behaviour.

However, I would not think the above to be a narrowing-down
as it just *selects* one of the explicitely "legal" options.

	list[dict[str, str | list | dict[str, Any]]]

should AFAICT expand to:

	list[dict[str, dict[str, Any]]]

		OR

	list[dict[str, list]]

		OR

	list[dict[str, str]]

the last of which should provide coverage of

	[{'some key': 'some value'}]

> Can you use a TypeGuard here?

Not from what I understand about them...

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


More information about the Python-list mailing list