[issue46399] Addition of `mapping` attribute to dict views classes has inadvertently broken type-checkers

Jelle Zijlstra report at bugs.python.org
Wed Jan 19 10:38:45 EST 2022


Jelle Zijlstra <jelle.zijlstra at gmail.com> added the comment:

I see the concerns about exposing too many implementation details.

But I'm also not sure how much this will really help static typing use cases. Alex's examples just call super().keys(), but if you do that, there's not much point in overriding keys() in the first place.

These classes don't allow subclassing or instantiation:

>>> t = type({}.items())
>>> class X(t):
...     pass
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: type 'dict_items' is not an acceptable base type
>>> t({})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cannot create 'dict_items' instances

So I can't think of much useful, well-typed code that you could write if these classes were public. For a use case like SortedDict, you'd still need to write your own class implementing KeysView, and you'd get an error from the type checker because it's incompatible with dict_keys.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue46399>
_______________________________________


More information about the Python-bugs-list mailing list