-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
gh-120802: Providing more information on __getitem__.
#122178
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
Conversation
Result of (discussion about children of abc Sequence)[https://discuss.python.org/t/minimizing-requirements-for-children-of-collections-abc-sequence/55678]. 1. Full description of expected behaviour, with example of using `slice.indices`. 2. Describing behaviour of commas in indexing syntax. 3. Fixing information on what to do, if object should be matching ABC-s, but don't have better implementation of method.
[1st commit](python@70de382) 2nd commit was small changes, but building Python edited line ending in source files for some reason. I did't expect that. This 3rd commit fixes line endings.
| Slicing is done exclusively with the following three methods. A call like :: | ||
| Slicing is done exclusively with the following three methods. | ||
| :meth:`__getitem__`, :meth:`__setitem__`, :meth:`__delitem__`. | ||
| Expressions like: :: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| Expressions like: :: | |
| Expressions like:: |
| a[1:2, 2:3] | ||
|
|
||
| is translated to :: | ||
| are translated into:. :: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| are translated into:. :: | |
| are translated into:: |
| duck types: :term:`sequence` types, :term:`mapping` types, and any type | ||
| with custom behaviour on indexing. Syntax doesn't perform any checks, | ||
| so it is this method responsibility to raise :exc:`TypeError` on keys of | ||
| improper type. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| improper type. | |
| incorrect type. |
| with custom behaviour on indexing. Syntax doesn't perform any checks, | ||
| so it is this method responsibility to raise :exc:`TypeError` on keys of | ||
| improper type. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| with custom behaviour on indexing. Syntax doesn't perform any checks, | |
| so it is this method responsibility to raise :exc:`TypeError` on keys of | |
| improper type. | |
| with custom behaviour on indexing. The implementation is responsible for | |
| handling keys of inappropriate type, e.g., by raising a :exc:`TypeError`. |
|
|
||
| .. note:: | ||
|
|
||
| :keyword:`for` loops expect that an :exc:`IndexError` will be raised for |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where did this note disappear?
| if not, it should return a similar type. Meaning, ``self[::]`` should | ||
| create shallow copy. | ||
|
|
||
| Second, there is expected behaviour for :term:`mapping` types:. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| Second, there is expected behaviour for :term:`mapping` types:. | |
| Second, there is expected behaviour for :term:`mapping` types: |
| for *keys* of different type. For implicit (ABC) children of | ||
| :class:`collections.abc.Mapping`, static type checkers are allowed to | ||
| deduce *keys* type as all types accepted by ``__getitem__``. | ||
| To avoid such errors, :term:`mapping` types with extended ``__getitem__`` | ||
| need to override this behaviour by explicitly deriving from | ||
| :ref:`types-genericalias` of :class:`collections.abc.Mapping`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This paragraph is too compact for the reader. You should split the requirements from the static type checkers (which I'm not sure should be mentioned here at all).
| need to override this behaviour by explicitly deriving from | ||
| :ref:`types-genericalias` of :class:`collections.abc.Mapping`. | ||
|
|
||
| Lastly, here is an example of possible implementation for ``Matrix``, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When you say "first, second, lastly", it appears like you are describing requirements but all of them are actually unrelated to each other. You can just start your paragraphs normally like "For Sequence types, ...", "For mapping types,..." and use an Example section.
| if getattr(key, '__index__', None) is not None: | ||
| return self._rows_list[key] | ||
| if isinstance(key, slice): | ||
| return Matrix(self._rows_list[key]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You return is not covariant for subclasses here. Ideally, it should be self.__class__(...).
|
|
||
| def __getitem__(self, key): | ||
| if getattr(key, '__index__', None) is not None: | ||
| return self._rows_list[key] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What would be rows_list? please provide a complete example instead of a partial one.
|
There are various points that you could improve:
|
|
No point in keeping this open. I will try to make those changes in separate PR-s, with step by step commits. |
📚 Documentation preview 📚: https://cpython-previews--122178.org.readthedocs.build/