Would another option be to just stop using the tuple-less index in the presence
of new syntax - so by example,
```
SYNTAX INDEX KWARGS
d[*[]] () {}
d[*[],] () {}
d[**{}] () {}
d[**{},] () {}
d[foo=1] () {'foo': 1}
d[foo=1,] () {'foo': 1}
d[x] x {} (valid existing syntax)
d[x,] (x,) {} (valid existing syntax)
d[x, *[]] (x,) {}
d[x, *[],] (x,) {}
d[x, **{}] (x,) {}
d[x, **{},] (x,) {}
d[x, foo=1] (x,) {'foo': 1}
d[x, foo=1,] (x,) {'foo': 1}
```
Essentially, the rules are:
* Does the indexing contain any of the new syntax proposed in this PEP (`*`,
`**`, or explicit kwarg)? If yes:
* Always pass a tuple as the index. This is new-style indexing, lets leave
behind the weird corner cases. `()` is a natural choice of singleton.
* If no:
* Use the existing rule of parsing the contents of `[]` as a tuple
The one downside of this approach is that perfect forwarding of `__getitem__`
requires a small dance
```
def __getitem__(self, args, **kwargs):
if not type(args) is tuple and not kwargs:
# this is pre-PEP-637 indexing
return self._wrapped[args]
else:
# the presence of `*` or `**` is enough to force PEP-637 indexing, even
# if those are empty
return self._wrapped[*args, **kwargs]
```
This would only come up in new code though - any forwarding `__getitem__`
without `**kwargs` would already be written correctly.
I'd argue it would be sufficient to note this a usage note in the PEP.
Eric
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/ZGPKU3EGXH72OGNGSBJO26ITHOREBGJ4/
Code of Conduct: http://python.org/psf/codeofconduct/