On Sat, Oct 16, 2021 at 09:54:13AM +0100, Alex Waygood wrote:
> > On 16 Oct 2021, at 06:13, Steven D'Aprano <[email protected]> wrote:
> > Be careful about believing what you are told.
>
> Indeed, MyPy will correctly raise errors if you assign {None: []} to a
> variable annotated with dict[str, Number]. However, you'll find that
> MyPy also raises an error if you assign {'foo': 4} to a variable
> annotated with dict[str, Number]:
Hah, serves me right for not testing it for both positive and negative
cases.
In my very limited testing now, I see that the problem appears to be
with the Number type. mypy correctly accepts this:
Data = Dict[str, int]
a: Data = {'spam': 42}
(no errors), but if you change the annotation to use Number instead of
int, it wrongly flags that as a type error.
Possibly mypy doesn't know that ints and floats are instances of Number?
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/FJY3QKAUJOJYTODMZH72X2ZS63GKT6AN/
Code of Conduct: http://python.org/psf/codeofconduct/