Should PEP 637 legalize "obj[]"?

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

On Wed, Dec 23, 2020 at 11:16 PM Brandt Bucher <brandtbucher@gmail.com> wrote:
I'm -0.5 on allowing that now. There was SOME discussion of this in the long threads, but not any strong consensus as I read it. However, it has no clear use *outside of type hints*, and hence is a slight invitation to abuse or misunderstanding. I would rather it be its own proposal, or part of some more general proposal around type hints. Not allowing the empty subscript in the initial implementation does not preclude allowing it later when it has an actual use case that has been presented a bit more formally than in this email. -- The dead increasingly dominate and strangle both the living and the not-yet born. Vampiric capital and undead corporate persons abuse the lives and control the thoughts of homo faber. Ideas, once born, become abortifacients against new conceptions.

I am +0.5 on allowing this, unless I misunderstand the implications, this would still raise errors in the cases where an empty tuple was not an intended key/index/argument. I can see that `obj[]` is less explicit than `obj[()]` but is no less clear (if it is allowed). However, I am only +0.5 because I am not sure how common a useful use case for `obj[]` would occur, so it is not all that much of an sugar coating. That being said, I agree that allowing this would undoubtedly open the doors to "abuse" of this syntax in ways that `obj[()]` would not - that is, if it was allowed, people would find ways use it and be aware that an empty tuple is an allowed argument (which I believe will be possible regardless, yes?), whereas not allowing it may lead to less prevalent use of empty tuples as arguments (make of that what you will). Whether that "abuse" is really abuse or the exciting growth/usage of new syntax is debatable but I edge toward the latter as a mad pythonist myself.

On Wed, Dec 23, 2020 at 11:16 PM Brandt Bucher <brandtbucher@gmail.com> wrote:
I'm -0.5 on allowing that now. There was SOME discussion of this in the long threads, but not any strong consensus as I read it. However, it has no clear use *outside of type hints*, and hence is a slight invitation to abuse or misunderstanding. I would rather it be its own proposal, or part of some more general proposal around type hints. Not allowing the empty subscript in the initial implementation does not preclude allowing it later when it has an actual use case that has been presented a bit more formally than in this email. -- The dead increasingly dominate and strangle both the living and the not-yet born. Vampiric capital and undead corporate persons abuse the lives and control the thoughts of homo faber. Ideas, once born, become abortifacients against new conceptions.

I am +0.5 on allowing this, unless I misunderstand the implications, this would still raise errors in the cases where an empty tuple was not an intended key/index/argument. I can see that `obj[]` is less explicit than `obj[()]` but is no less clear (if it is allowed). However, I am only +0.5 because I am not sure how common a useful use case for `obj[]` would occur, so it is not all that much of an sugar coating. That being said, I agree that allowing this would undoubtedly open the doors to "abuse" of this syntax in ways that `obj[()]` would not - that is, if it was allowed, people would find ways use it and be aware that an empty tuple is an allowed argument (which I believe will be possible regardless, yes?), whereas not allowing it may lead to less prevalent use of empty tuples as arguments (make of that what you will). Whether that "abuse" is really abuse or the exciting growth/usage of new syntax is debatable but I edge toward the latter as a mad pythonist myself.
participants (3)
-
Brandt Bucher
-
David Mertz
-
Mathew Elman