[Python-ideas] Re: PEP 9999 (provisional): support for indexing with keyword arguments

2020-09-26 Thread Stefano Borini
On Sat, 26 Sep 2020 at 05:10, Steven D'Aprano  wrote:
>
> I recommend that the PEP be changed to allow `*args` inside subscripts.

Will do.


-- 
Kind regards,

Stefano Borini
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/BMD6SINF7JRMVA7CIXD6CDMRHIWCW2V7/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: PEP 9999 (provisional): support for indexing with keyword arguments

2020-09-25 Thread Steven D'Aprano
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
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/AKMR6WOE6HN5SQQAIBHVML6GPJTEY7TF/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: PEP 9999 (provisional): support for indexing with keyword arguments

2020-09-25 Thread Eric Wieser
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
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/ZQ4CHT37YUHYEFYFBQ423QNZMFCRULAW/
Code of Conduct: http://python.org/psf/codeofconduct/