Path.parents does not play as good sequence
Recent commit 045d415 introduced
parents attribute of paths, which is declared to behave like a sequence. A notable difference from most sequences is that its
__getitem__() method returns a value for any positive index, instead of raising
IndexError for those greater than
One of the consequences is that its
__iter__() method, derived from
Sequence, never stops yielding values, since it expects a sequence to raise
IndexError at its end, and does not pay any attention to the value of
len() (which is well-defined in this case). Hence loop
for p in path.parents: will never exhaust. Moreover, a condition
if Path('/not/a/parent') in path.parents: will hang forever too (in false case), because
__contains__() implementation of
Sequence is bounded to iteration over the sequence.
I propose reimplementing
_PathParents.__iter__() method, so it will yield only
Alternative can be changing the semantics of
__getitem__() to make it raise IndexError for indices greater than
len()-1 (this will more strictly conform to the language definition of
__getitem__ special method, but make
path.parents == path.parent.parent not always true).
On the other hand, this can be considered a bug in
Sequence.__iter__() implementation, which probably should pay attention to the value of
len(), since the abstract class requires it to be defined anyway.