Skip to content

Document that scalar Groups are considered to be abc.Collection but are not #1174

@gdementen

Description

@gdementen

Collections (https://docs.python.org/3/library/collections.abc.html#collections-abstract-base-classes) are defined by anything which has __len__, __iter__ and __contains__. This is the case for Group, including scalar ones.

>>> import collections.abc
>>> g = la.LGroup(1)
>>> isinstance(g, collections.abc.Collection)
True
>>> len(g)
TypeError: len() of unsized object (1)
>>> iter(g)
TypeError: 'int' object is not iterable
>>> 'a' in g
TypeError: argument of type 'int' is not iterable

Note that the same problem is present in Numpy for scalar arrays:

sc = np.array(1)
>>> len(sc)
TypeError: len() of unsized object
>>> iter(sc)
TypeError: iteration over a 0-d array
>>> isinstance(sc, collections.abc.Collection)
True

I do not think this problem is properly fixable. We could probably only define the methods depending on the instance (inside getattr) but this feels wrong and the fact that NumPy does not do it like that is probably an hint that we shouldn't do it either. However, we should at least have some comment in the code explaining why and the consequences thereof.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions