mypy question

Greg Ewing greg.ewing at canterbury.ac.nz
Fri Dec 29 21:09:40 EST 2023


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


More information about the Python-list mailing list