Can We Use Parentheses for Tuple Type Hints?

June 28, 2025 5 answers
Asked by CuriousCoder123
I'm curious if there's a way to use parentheses for type hinting tuples, like writing `def f() -> (int, str): ...` instead of using `Tuple[int, str]`. I couldn't find a PEP addressing this, so I wonder what the arguments against proposing or implementing this change might be.

Answers

DataClassDev
Why not just use dataclasses if you're after heterogeneous typed tuples? They provide a clear structure and are easy to work with.
Reply to this answer
HintsGuru
You can already hint a tuple without needing any imports! It's straightforward as is.
Reply to this answer
TypeGuru77
The main concern here is about consistency across all type hints. If we start using a syntax like (int, str) for tuples, it could lead to confusion when used in different contexts, like `list[dict[str,(int, str)]]`. The current `list[dict[str, tuple[int, str]]]` keeps things pretty straightforward. Plus, (int, str) is a valid tuple expression, which could create ambiguity in various cases, particularly with custom types that allow subscripting.

TypeFan43: I get where you're coming from. The parser was updated in 3.10, but adding this would still take some time.

RustyCoder55: I think there's nothing wrong with having syntactic sugar for tuples, like in Rust. It could make type hinting feel cleaner.

Reply to this answer
TupleExpert99
Itโ€™s actually quite tricky to implement this without causing issues elsewhere. Jelle Zijlstra's post titled 'Why Can't We...?' dives into these challenges. For instance, using parentheses could conflict with subscript syntax (like `Foo[(A, B)]` which is interpreted the same as `Foo[A, B]`), and could confuse the type system around unions. Check out his article for a deeper look!

CuriousCoder123: Thanks so much!

Reply to this answer
FreeFormCoder
Who said itโ€™s not possible? With `from __future__ import annotations`, you can indeed define functions with tuples pretty easily like this: `def sus() -> (int, int): return 69, 420`. Just keep in mind that itโ€™s not exactly semantically meaningful in the type system, as itโ€™s returning a tuple instance instead of a true tuple type.

AnnotationWizard: That's interesting! It seems like itโ€™s possible syntactically, but the type meaning gets lost.

Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions