Indeed — we essentially lie to mypy about the method resolution order for 
list, dict, etc (mypy thinks that list directly inherits from 
collections.abc.MutableSequence — see the typeshed stub here: 
https://github.com/python/typeshed/blob/32bc2161a107db20c2ebc85aad31c29730db3e38/stdlib/builtins.pyi#L746),
 but numeric ABCs are not special-cased by typeshed in the same way as 
collections ABCs. Fundamentally, mypy has no knowledge about virtual 
subclassing.

Again, I covered this very comprehensively on StackOverflow ;) 
https://stackoverflow.com/questions/69334475/how-to-hint-at-number-types-i-e-subclasses-of-number-not-numbers-themselv/69383462#69383462

Best,
Alex 

> On 16 Oct 2021, at 13:54, Steven D'Aprano <st...@pearwood.info> wrote:
> 
> On Sat, Oct 16, 2021 at 09:54:13AM +0100, Alex Waygood wrote:
>>>> On 16 Oct 2021, at 06:13, Steven D'Aprano <st...@pearwood.info> 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 -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at 
> https://mail.python.org/archives/list/python-ideas@python.org/message/FJY3QKAUJOJYTODMZH72X2ZS63GKT6AN/
> Code of Conduct: http://python.org/psf/codeofconduct/
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/JFS65SMVETZ7W4KARJYJUNP6KA5XOFPL/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to