FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Add protocols `SequenceLike` and `MappingLike` by srittau · Pull Request #15152 · python/typeshed · GitHub

Repository navigation

Add protocols SequenceLike and MappingLike - #15152

Open
srittau wants to merge 11 commits into
python:mainfrom
srittau:like-protos
Open

srittau wants to merge 11 commits into
python:mainfrom
srittau:like-protos

Conversation

srittau commented Dec 19, 2025

Copy link
Copy Markdown
Collaborator

Sequence and Mapping are commonly used in argument types. This can
be problematic, since they are not protocols, making it impossible to
safely duck type or mock them. Also, not all sequence- or mapping-like
types derive from these ABCs at runtime, although in typeshed we often
pretend they do. Ideally we'd be able to avoid this discrepancy in the
far future. Finally, the ABCs sometimes make more API promises than
their sub-classes fulfill. For example, Mapping arguments are
keyword-or-positional, while its most important subtype dict only
accepts positional arguments. These protocols have tighter guarantees.

SequenceLike contains most, MappingLike all methods from their
respective types, making them an easy substitute in argument types.

srittau and others added 2 commits December 19, 2025 18:35
`Sequence` and `Mapping` are commonly used in argument types. This can
be problematic, since they are not protocols, making it impossible to
safely duck type or mock them. Also, not all sequence- or mapping-like
types derive from these ABCs at runtime, although in typeshed we often
pretend they do. Ideally we'd be able to avoid this discrepancy in the
far future. Finally, the ABCs sometimes make more API promises than
their sub-classes fulfill. For example, `Mapping` arguments are
keyword-or-positional, while its most important subtype `dict` only
accepts positional arguments. These protocols have tighter guarantees.

`SequenceLike` contains most, `MappingLike` all methods from their
respective types, making them an easy substitute in argument types.

This comment has been minimized.

Copy link
Copy Markdown
Member

Why should we use these rather than more precise protocols like SupportsGetItem?

srittau commented Dec 22, 2025

Copy link
Copy Markdown
Collaborator Author

For convenience. I still think using the more precise protocols is the better option in 90% of cases, but sometimes this would be inconvenient, especially as long as we don't have easy protocol composition (via intersections). See also the large amount of Mapping and Sequence annotations we still have, which could easily be replaced with these protocols for now, and refined later.

srittau commented Dec 22, 2025

Copy link
Copy Markdown
Collaborator Author

Also, cf. PyCQA/flake8-pyi#525

jorenham left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Yea this seems useful, and I like the NumPy-esque naming :)

Speaking of NumPy, I left a suggestion that would make np.ndarray assignable to SequenceLike.

Comment thread stdlib/_typeshed/__init__.pyi Outdated
Comment thread stdlib/_typeshed/__init__.pyi Outdated
Comment thread stdlib/_typeshed/__init__.pyi Outdated

This comment has been minimized.

This comment has been minimized.

This comment has been minimized.

Copy link
Copy Markdown
Contributor

MappingLike currently lacks __iter__, is that intentional?

srittau commented Feb 12, 2026

Copy link
Copy Markdown
Collaborator Author

MappingLike currently lacks __iter__, is that intentional?

Good catch, added.

This comment has been minimized.

Copy link
Copy Markdown
Contributor

Actually, we need to be careful with such protocols, because match-case relies on collections.abc.Mapping and collections.abc.Sequence subclassing: (see: https://peps.python.org/pep-0634/#sequence-patterns and https://peps.python.org/pep-0634/#mapping-patterns)

For a sequence pattern to succeed the subject must be a sequence, where being a sequence is defined as its class being one of the following:

  • a class that inherits from collections.abc.Sequence
  • a Python class that has been registered as a collections.abc.Sequence
  • a builtin class that has its Py_TPFLAGS_SEQUENCE bit set
  • a class that inherits from any of the above (including classes defined before a parent’s Sequence registration)

So if the implementation uses match-case, one cannot safely replace argument Mapping with MappingLike and Sequencewith SequenceLike arguments.

This comment has been minimized.

This comment has been minimized.

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

This branch has not been deployed

No deployments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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.

6 participants


Back | FazBrowse Home | New Git URL