When also confronted with the same problem, I was not happy seeing Martijn Pieters answer. Since I wanted a "fast" and "easy" way to solve this problem.
So I tried the other suggestions listed here first.
Note: I used VSCode with Pylance as Language Server
Zaffys answer was my favorite
def demystify(mystery: Annotated[Tuple[int], 6]):
a, b, c, d, e, f = mystery
print(a, b, c, d, e, f)
Hint for the function then looks like this: demystify: (mystery: Tuple[int]) -> None
Also I get a Pylance Error Tuple size mismatch: expected 6 but received
for the line a, b, c, d, e, f = mystery
Next I tried Tuple[6 * (int, )]
which was mentioned by balu in the comments of Martijn Pieters answer
def demystify(mystery: Tuple[6 * (int,)]):
a, b, c, e, f, g = mystery
print(a, b, c, e, f, g)
Resulting in the same Pylance Error as before.
Hint for the function was this: demystify: (mystery: Tuple[Tuple[Type[int], ...]]) -> None
Going back to writing down the expected length:
def demystify(mystery: Tuple[int, int, int, int, int, int]):
a, b, c, e, f, g = mystery
print(a, b, c, e, f, g)
This resolved the Pylance Error, and got me a "clear" function hint: demystify: (mystery: Tuple[int, int, int, int, int, int]) -> None
But just like John Brodie, I was not happy with this solution.
Now back to the, at first, unwanted answer:
class MysteryType(NamedTuple):
a: int
b: int
c: int
d: int
e: int
f: int
g: int
def demystify(mystery: MysteryType):
print(*mystery)
The function hint now seems more mystic: demystify: (mystery: MysteryType) -> None
but creating a new MysteryType gives me all the information I need: (a: int, b: int, c: int, d: int, e: int, f: int, g: int)
Also I can use the MysteryType in other methods and functions without the need of counting the type hints.
So, to make a long story short and paraphrase the Zen of Python:
NamedTuples are one honking great idea -- let's do more of those!