| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
`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.
|
Why should we use these rather than more precise protocols like SupportsGetItem? |
Sorry, something went wrong.
|
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. |
Sorry, something went wrong.
|
Also, cf. PyCQA/flake8-pyi#525 |
Sorry, something went wrong.
There was a problem hiding this comment.
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.
Sorry, something went wrong.
|
MappingLike currently lacks __iter__, is that intentional? |
Sorry, something went wrong.
Good catch, added. |
Sorry, something went wrong.
|
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)
So if the implementation uses match-case, one cannot safely replace argument Mapping with MappingLike and Sequencewith SequenceLike arguments. |
Sorry, something went wrong.
|
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
Sorry, something went wrong.
| Back | FazBrowse Home | New Git URL |
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.