I didn't participate in the mailing list discussions for PEP 637, but I have 
become somewhat involved in the reference implementation and final revisions. 
While reviewing the PEP, I noticed what I think is a missed opportunity to 
finally allow empty subscripting (`obj[]`). It seems to me to be a natural 
consequence of the `obj[*a]` and `obj[**k]` syntax which the PEP proposes, and 
I'm quite surprised that the PEP still outlaws it (the latest revision has 
language that says we're punting on this, but I'd like to change that).

I'm wondering how others feel. Do you think we should allow `obj[]` as sugar 
for `obj[()]`, or leave it a `SyntaxError`? Steven, Stefano, and I are in 
agreement that we should at least consider allowing it. I didn't see it being 
discussed much at all (although I may be missing a thread or two).

My primary motivation here is type-hinting generics (like `tuple[]`, currently 
spelled as `tuple[()]`). I'm worried that this could become more of an 
annoyance with the potential introduction of variadic generics (PEP 646) - not 
only for end-users who need to add type hints for empty generics, but also for 
library authors who will constantly need to special-case their reprs to turn 
`"MyType[]"` into `"MyType[()]"` or similar (I found that even mypy's error 
messages still don't do this correctly for `tuple` / `Tuple`!). But I don't 
really have much insight into how pervasive that issue would be, in practice.

The way I see it, there are sort of four paths forward:

- Add this to PEP 637. **I think this option makes the most sense.**
- Add this to the new variadic generics PEP.
- Propose this as its own PEP some time after 637 is accepted (sort of like PEP 
618).
- Don't pursue it, it's a bad idea.

The only drawback I anticipate is beginners doing stuff with `some_mapping[]` 
and being surprised that it's adding/removing/looking up an empty tuple. But to 
me, `some_mapping[]` being the same as `some_mapping[()]` is no more confusing 
than `some_mapping[1]` being different from `some_mapping[1,]`. If anything, 
it's just a good teaching moment, where they can learn that positional indices 
are treated "as if" they are wrapped in parentheses. Or, for more advanced 
users, asking them what `x[y=z]` without the keyword argument would mean.

Thoughts?

Brandt
_______________________________________________
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/6PXH6KCVT5PK2G3MR4U5PPYR6AKI3FMR/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to