In the latest version of Python (3.12.3), type annotation for single variable assignment is available:
a: int = 1
However, in some scenarios like when we want to annotate the tuple of variables in return, the syntax of type annotation is invalid:
from typing import Any
def fun() -> Any: # when hard to annotate the strict type
return 1, True
a: int, b: bool = fun() # INVALID
In this case, I propose two new syntaxes to support this feature:
- Annotate directly after each variable:
a: int, b: bool = 1, True
(a: int, b: bool) = 1, True
- Annotate the tuple of return:
(a, b): tuple[int, bool] = 1, True
Appendix
In other programming languages, as I know, Julia and Rust support this feature in there approaches:
- Julia
a::Int, b::Bool = 1, true # VALID
(a::Int, b::Bool) = 1, true # VALID
(a, b)::Tuple{Int, Bool} = 1, true # INVALID
- Rust
let (a, b): (i64, bool) = (1, true); // VALID
let (a: i64, b: bool) = (1, true); // INVALID