Re: PEP 9999 (provisional): support for indexing with keyword arguments
Thanks for the PEP, and for pinging the numpy list about it. Some comments: Sequence unpacking remains a syntax error inside subscripts: Reason: unpacking items would result it being immediately repacked into a tuple A simple counter-example is [:, *args, :], which could be treated as [(slice(None), *args, slice(None))]. When there are index objects to the left or right of *args, it enables : syntax in places that would otherwise be forbidden. I’d argue this should be folded into the scope of the PEP - if the parser for indexing is going to be overhauled, it would be nice to cross this one off. I think I requested this change independently at some point Keyword arguments must allow Ellipsis This is sort of uninteresting now, Ellipsis is not special-cased in the parser, all places where Ellipsis is allowed also allow ... - even ....__class__! a similar case occurs with setter notation It would perhaps be a good idea to recommend users declare their setter arguments as positional-only too,def __setitem__(self, index, value, /, **kwargs):, to avoid this problem Keyword-only subscripts are permitted. The positional index will be the empty tuple As discussed on the numpy mailing list, a better approach might be to not pass an argument at all, so that obj[spam=1, eggs=2] calls type(obj).__getitem__(obj, spam=1, eggs=2). Objects which want to support keyword-only indexing can provide a default of their own (and are free to choose between () and None, while objects which do not would just raise TypeError due to the missing positional argument the solution relies on the assumption that all keyword indices necessarily map into positional indices, or that they must have a name This seems like a non-issue to me. Keyword-only arguments already exist, and could be used in the relevant magic method def __getitem_ex__(self, *, x, y):. I would be inclined to remove this argument. Eric
Thanks Eric, On Fri, Sep 25, 2020 at 09:23:56AM +0100, Eric Wieser wrote:
Thanks for the PEP, and for pinging the numpy list about it. Some comments:
Sequence unpacking remains a syntax error inside subscripts: Reason: unpacking items would result it being immediately repacked into a tuple
A simple counter-example is [:, *args, :], which could be treated as [(slice(None), *args, slice(None))]. When there are index objects to the left or right of *args, it enables : syntax in places that would otherwise be forbidden.
This use-case is persuasive to me. I recommend that the PEP be changed to allow `*args` inside subscripts.
Keyword-only subscripts are permitted. The positional index will be the empty tuple
As discussed on the numpy mailing list, a better approach might be to not pass an argument at all, so that obj[spam=1, eggs=2] calls type(obj).__getitem__(obj, spam=1, eggs=2). Objects which want to support keyword-only indexing can provide a default of their own (and are free to choose between () and None, while objects which do not would just raise TypeError due to the missing positional argument
That's fine for getitem and delitem, but doesn't work with setitem. -- Steve
participants (3)
-
Eric Wieser
-
Stefano Borini
-
Steven D'Aprano