Text of discuss.python.org › t › allow-zero-arguments-for-subscript-syntax › 53084 as Styx crawled it on Nov 19, 2026. The page may have changed since. Only its words are kept, without pictures or layout, and only the first part of a long page.

Open the live page

Allow zero arguments for subscript syntax - Ideas - Discussions on Python.org

Allow zero arguments for subscript syntax Ideas Nineteendo (Nice Zombies) May 11, 2024, 7:56am 1 Could we allow calling X.__getitem__() or X.__class_getitem__() for X[] ? This allows to do something special in this case. At least the current SyntaxError for X[] isn’t very clear: >>> tuple[] File "<python-input-1>", line 1 tuple[] ^ SyntaxError: invalid syntax Previous discussion: Default argument for `__getitem__()` · Issue #118937 · python/cpython · GitHub Nineteendo (Nice Zombies) May 11, 2024, 7:59am 2 Copying the response from Jelle here: That would be a change to the grammar, so definitely not a minor feature that we can accept without prior discussion.

In fact, it would probably need a PEP. Your link shows 30k uses of [()] . That’s a lot, but in Implicit tuple return type - #61 by Nineteendo you found more than 30 million uses of x[a, b] —indicating that the x[()] syntax is not especially common. I glanced through the first few results; some are uses of the empty tuple type tuple[()] , others are in various array or tensor libraries. I can’t speak for the latter, but from a typing perspective I think tuple[()] is a clearer spelling for the empty tuple type than tuple[] .

1 Like Rosuav (Chris Angelico) May 11, 2024, 8:48am 3 Why should this one protocol get a default argument? And if it gets a default argument, why this default? Why not zero, or None , or any other plausible value? And as to improving the error message: Remember that tuple is not a keyword. Whatever change you propose has to make just as much sense for all of these, which are equivalent as far as the parser’s concerned: tuple[] int[] [][] print("Hello, world")[] 42[] stuff[] I understand that a basic “invalid syntax” isn’t very helpful, but it’s also not wrong , and any proposed change has to be careful to avoid being incorrect. pf_moore (Paul Moore) May 11, 2024, 10:28am 4 You didn’t include a “do nothing” option, which is what I would have voted for.

You’re starting from an assumption that there is a problem worth solving here, but I don’t think that has been established yet. Yes, improved error messages are always good, but there’s nothing about this situation that is any more deserving of an improved message than many other places - and, as @Rosuav pointed out, improving error messages is often a lot harder than it seems at first glance. My vote would be “do nothing, and assume that any improvement in the error message will happen in due course as part of the wider ongoing work to improve error reporting”.

3 Likes Nineteendo (Nice Zombies) May 11, 2024, 10:57am 5 It’s not documented that tuple[int, int] is actually tuple[(int, int)] , only that it accepts any number of type arguments. So it’s confusing why tuple[] isn’t allowed and you need to use tuple[()] . tuple[()] is 30x as common as tuple[None] and is the more natural thing to replace. It could be SyntaxError: missing argument , but you can get better error messages at runtime: tuple[] # OK int[] # TypeError: type 'int' is not subscriptable [][] # TypeError: missing argument print("Hello, world")[] # TypeError: 'NoneType' object is not subscriptable 42[] # TypeError: 'int' object is not subscriptable stuff[] # NameError: name 'stuff' is not defined I replaced the poll (editing it is not possible).

Rosuav (Chris Angelico) May 11, 2024, 11:08am 6 But what if it isn’t tuple that’s being subscripted? What if it’s a list or dictionary - how often do you subscript those with empty tuples? Lists are frequently subscripted with zero. Dictionaries might be subscripted with pretty much any hashable value, but strings and numbers are certainly fairly common. The empty tuple is an extremely uncommon subscript, in the scheme of things.

It would make a very odd choice for the default. I agree with Paul that there is no proof yet that there is a problem to be solved. 1 Like mikeshardmind (Michael H) May 11, 2024, 11:13am 7 There’s another notion of default that this conflicts with as well. Unsubscripted types are generally interpreted in their most permissive form (though with typevar defaults having been recently accepted, user defined generics can have behavior like this beyond what is cased for the builtin collections), for Tuple, that’s tuple[Any, ...] , for list that’s list[Any] , I’d rather leave tuple[] invalid, there’s enough varying ways to spell things as it is, and in the face of something which could be ambiguous that isn’t allowed, there’s not a good reason to introduce new places for people to get things mixed up.

Nineteendo (Nice Zombies) May 11, 2024, 11:36am 8 I haven’t said anything about adding a default there, and I don’t know if anything other than this makes sense: User defined classes can define their own behaviour if desired. Let’s wait for the result of the poll. 2 votes tells me nothing. It’s not possible to figure out how to type hint the empty tuple without reading the docs if you have only seen tuples with 1 or more arguments: typing module: empty tuple syntax is undocumented · Issue #81995 · python/cpython · GitHub Is there a way to represent an empty tuple type? · Issue #4211 · python/mypy · GitHub pf_moore (Paul Moore) May 11, 2024, 12:14pm 9 You replaced it with something that still doesn’t allow me to express my view as stated above.

Don’t bother trying to change it again, though - I’ve said all I wanted, I simply won’t vote (but please remember when interpreting the vote results to allow for people like me who didn’t vote to express a “none of the above” sentiment [1] …) Any number of votes in a poll that doesn’t ask “is there a problem to be solved” won’t answer that question in any case. and no, if you change the vote again, to add that option, I still won’t vote for it - you’re ignoring my main point which is that there isn’t a problem to solve here, so no vote is needed ↩︎ layday (layday) May 11, 2024, 12:27pm 10 There’s little benefit to gauging uninformed opinion.

A poll should come after a healthy discussion, once the pros and cons of your proposal are analysed and understood. 1 Like plannigan (Patrick Lannigan) May 11, 2024, 12:32pm 11 Being a bit pedantic but , “No preference” is different than “Do nothing”. “No preference” implies “I’m fine with either”. Which is not what Paul’s message was suggesting. [1] As for my stance, I’m not sold yet on tuple[] .

It might make it fewer characters to say “I want an empty tuple”. But I think some people who see it for the first time might confuse it for “I want a tuple, but I’m not specifying the types of the values it contains”. I’m also hesitant to make a specific type from the stdlib have a meaning for nothing supplied for subscript, when everything else still requires it. Is there a plan for all of the other collections?

What about all of the other generic types? Also, I looked at the first page of result for the code search. 9 of the 20 results on that page were not about type annotations [2] . Apparently Paul types faster than I do. Leaving it here anyways. ↩︎ apparently indexing into some kind of collection with an empty tuple is a somewhat common pattern for scientific libraries ↩︎ 1 Like Nineteendo (Nice Zombies) May 11, 2024, 12:35pm 12 Sorry for being nitpicky (I have autism), but: “Do nothing” is NOT an answer on “What notation do you prefer for the empty tuple?”.

And “None of the above” would mean that you want yet another notation for the empty tuple. Should I have asked a different question? Maybe, but you probably won’t vote for that either. you’re ignoring my main point which is that there isn’t a problem to solve here, so no vote is needed I would like to disagree here: tuple[()] is confusing if you have only seen tuple[int] , tuple[int, int] , … That’s a valid argument, I have closed the poll for the time being. mikeshardmind (Michael H) May 11, 2024, 12:44

The rest of this page is on the live site.