Skip to content

Add Protocols for mapping inside dictionary view objects #14574

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Gatsik
Copy link

@Gatsik Gatsik commented Aug 13, 2025

Resolves #14555

This comment has been minimized.

Copy link
Collaborator

@srittau srittau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The primer output is promising. Just one note about the protocols below.

Comment on lines 381 to 383
# Suitable for dictionary view objects
class Viewable(Sized, Iterable[_KT_co], Protocol): ...
class SupportsGetItemViewable(Viewable[_KT], SupportsGetItem[_KT, _VT_co], Protocol): ...
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you move these up to the other Supports* protocols?

Also, in typeshed (or at least in the _typeshed module) we have a slight preference for spelling out the methods supported in protocol, instead of deriving from other protocols, see for example SupportsItemAccess above. This makes it slightly easier for users to understand what methods are needed to fulfill a protocol instead of traversing through the class hierarchy.

Copy link
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

zope.interface (https://github.com/zopefoundation/zope.interface)
- src/zope/interface/common/collections.py:233: error: Incompatible types in assignment (expression has type "type[ItemsView[_KT_co, _VT_co]]", base class "ISet" defined the type as "type[AbstractSet[_T_co]]")  [assignment]
+ src/zope/interface/common/collections.py:233: error: Incompatible types in assignment (expression has type "type[ItemsView[_KT_co, _VT_co]]", base class "IMappingView" defined the type as "type[MappingView]")  [assignment]
- src/zope/interface/common/collections.py:237: error: Incompatible types in assignment (expression has type "type[KeysView[_KT_co]]", base class "ISet" defined the type as "type[AbstractSet[_T_co]]")  [assignment]
+ src/zope/interface/common/collections.py:237: error: Incompatible types in assignment (expression has type "type[KeysView[_KT_co]]", base class "IMappingView" defined the type as "type[MappingView]")  [assignment]
+ src/zope/interface/common/collections.py:241: error: Incompatible types in assignment (expression has type "type[ValuesView[_VT_co]]", base class "IMappingView" defined the type as "type[MappingView]")  [assignment]

static-frame (https://github.com/static-frame/static-frame)
+ static_frame/core/series_mapping.py:21: error: Unused "type: ignore" comment  [unused-ignore]
+ static_frame/core/series_mapping.py:26: error: Unused "type: ignore" comment  [unused-ignore]
+ static_frame/core/series_mapping.py:32: error: Unused "type: ignore" comment  [unused-ignore]

ibis (https://github.com/ibis-project/ibis)
- ibis/common/collections.py:140: error: Argument 1 to "KeysView" has incompatible type "ibis.common.collections.Mapping[K, V]"; expected "typing.Mapping[K, Any]"  [arg-type]
- ibis/common/collections.py:143: error: Argument 1 to "ItemsView" has incompatible type "ibis.common.collections.Mapping[K, V]"; expected "typing.Mapping[K, V]"  [arg-type]

discord.py (https://github.com/Rapptz/discord.py)
- discord/ext/commands/hybrid.py:508: error: Overlap between argument names and ** TypedDict items: "name", "description"  [misc]
+ discord/ext/commands/hybrid.py:508: error: Overlap between argument names and ** TypedDict items: "description", "name"  [misc]
- discord/ext/commands/hybrid.py:629: error: Overlap between argument names and ** TypedDict items: "name", "description"  [misc]
+ discord/ext/commands/hybrid.py:629: error: Overlap between argument names and ** TypedDict items: "description", "name"  [misc]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

No structural typing for mapping inside KeysView, ValuesView and ItemsView
2 participants