Skip to content

DEP: Finalize the non-sequence stacking deprecation#23019

Merged
charris merged 1 commit intonumpy:mainfrom
seberg:finalize-non-sequence-stack
Jan 17, 2023
Merged

DEP: Finalize the non-sequence stacking deprecation#23019
charris merged 1 commit intonumpy:mainfrom
seberg:finalize-non-sequence-stack

Conversation

@seberg
Copy link
Copy Markdown
Member

@seberg seberg commented Jan 16, 2023

The __array_function__ API currently will exhaust iterators so we cannot accept sequences reasonably. Checking for __getitem__ is presumably enough to reject that (and was what the deprecation used).

Future changes could allow this again, although it is not a useful API anyway, since we have to materialize the iterable in any case.

@seberg seberg force-pushed the finalize-non-sequence-stack branch from 7a748fd to 4f886e3 Compare January 16, 2023 10:43
return ()
return arrays
def _arrays_for_stack_dispatcher(arrays):
if not hasattr(arrays, "__getitem__"):
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I didn't really see a reason to check whether it isn't an iterator. If it doesn't have an __iter__ it seems OK to rely on tuple(arrays) failing.

Python is weird (IMO) about:

class obj:
    def __len__(self):
        return 3
    def __getitem__(self, x):
        return x

ignoring the length (and thus iterating forever trying to find the end), but that is not something we need to worry about here.

@@ -453,7 +449,7 @@ def stack(arrays, axis=0, out=None, *, dtype=None, casting="same_kind"):
"""
if not overrides.ARRAY_FUNCTION_ENABLED:
# raise warning if necessary
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is the comment still needed?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

A, good catch. Maybe it still makes sense, but changed it to # reject non-sequences (and make tuple), since "warning" is definitely wrong now.

The `__array_function__` API currently will exhaust iterators so we
cannot accept sequences reasonably.  Checking for `__getitem__` is presumably
enough to reject that (and was what the deprecation used).

Future changes could allow this again, although it is not a useful API
anyway, since we have to materialize the iterable in any case.
@seberg seberg force-pushed the finalize-non-sequence-stack branch from 4f886e3 to ff78e59 Compare January 17, 2023 21:22
@charris charris merged commit 0013550 into numpy:main Jan 17, 2023
@charris
Copy link
Copy Markdown
Member

charris commented Jan 17, 2023

Thanks Sebastian.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants