Aw: Re: mypy question
Chris Angelico
rosuav at gmail.com
Sat Dec 30 14:05:02 EST 2023
On Sun, 31 Dec 2023 at 03:38, Thomas Passin via Python-list
<python-list at python.org> wrote:
> I am not very expert in Python type hints. In working up the example
> program I just posted, I got an error message from mypy that remarked
> that "list" is invariant, and to try Sequence which is "covariant". I
> don't know what that means (and I haven't looked into it yet), but when
> I changed from list to Sequence as suggested, mypy stopped complaining.
>
Ah, I think you've hit on the problem there. Consider this:
def add_item(stuff: dict[str: str | int]):
stuff["spam"] = "ham"
stuff["vooom"] = 1_000_000
Is it valid to pass this function a dict[str: str]? No, because it's
going to add an integer into it.
Hopefully that helps explain what's going on a bit.
ChrisA
More information about the Python-list
mailing list