Skip to content

Conversation

@Alex-Wasowicz
Copy link

@Alex-Wasowicz Alex-Wasowicz commented Jul 23, 2024

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: ::
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Expressions like: ::
Expressions like::

a[1:2, 2:3]

is translated to ::
are translated into:. ::
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
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.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
improper type.
incorrect type.

Comment on lines +2883 to +2885
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.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
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
Copy link
Member

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:.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Second, there is expected behaviour for :term:`mapping` types:.
Second, there is expected behaviour for :term:`mapping` types:

Comment on lines +2909 to +2914
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`.
Copy link
Member

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``,
Copy link
Member

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])
Copy link
Member

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]
Copy link
Member

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.

@picnixz
Copy link
Member

picnixz commented Jul 23, 2024

There are various points that you could improve:

  • Check the Sphinx syntax with colons (and do not put a period after a colon in general).
  • Complete your example.
  • Try to avoid compact paragraphs with requirements. Your bullet list was good for sequences, but for mappings everything was in compact form making it hard to read.
  • I wouldn't say anything about type checkers in general, at least not in this section. Instead, you can just recommend inheriting from collections.abc.Mapping or collections.abc.Sequence in general.

@Alex-Wasowicz
Copy link
Author

No point in keeping this open. I will try to make those changes in separate PR-s, with step by step commits.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting changes docs Documentation in the Doc dir skip news

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

3 participants