On Sat, 16 Oct 2021 at 14:07, Alex Waygood <alex.wayg...@gmail.com> wrote:
>
> 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.

IMO, that's a bug in mypy. Maybe the fix is "too hard", maybe it
isn't. But if we added runtime isinstance checks, they would pass, so
arguing that the type is wrong or that the variable "needs" to be
typed differently is incorrect.

I guess my argument here is that flagging an error when you're not
100% sure the code is wrong is a problem - "in the case of ambiguity,
refuse the temptation to guess" seems relevant here. Personally, I'd
be unwilling to mess around with the typing in a situation like this -
I'd be more likely to just remove the types (the type checking
equivalent of `#noqa` when you don't agree with what your style
checker says). I'd much prefer mypy to miss the odd problem rather
than flagging usages that aren't actually errors, precisely because
you don't (as far as I know) have a mechanism for telling it you know
what you're doing...

But this is probably off-topic. I'm not 100% sure what the OP's
proposal was, but as far as I can tell it seems to me that "dict[str,
Number] is the correct usage for static typing" is the answer,
regardless of mypy's ability to process it.

Paul
_______________________________________________
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/WNR3A6YSC4PPLMZOTAYRFO5DYZ6PXQYX/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to