Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Unpacking a Type Hint Possible? or Its Workarounds?

Is there a way to unpack a tuple type alias? For example,

ResultTypeA = tuple[float, float, dict[str, float]]
ResultTypeB = tuple[*ResultTypeA, str, str]

So that ResultTypeB evaluates to

  • tuple[float, float, dict[str, float], str, str]

instead of

  • tuple[tuple[float, float, dict[str, float]], str, str]

If not possible, what would be a workaround for this?

like image 313
Inyoung Kim 김인영 Avatar asked Nov 02 '25 17:11

Inyoung Kim 김인영


1 Answers

What you are looking for may be the new typing.TypeVarTuple as proposed by PEP 646. Due to how new it is (Python 3.11+) and how big of a change this produces, many static type checkers still do not fully support it (see this mypy issue for example).

Maybe typing.Unpack is actually more applicable in this case, but again hardly useful so long as type checkers don't support it.

But at a certain point, you should probably ask yourself, if your design is all that good, if your type annotations become this complex.

like image 93
Daniil Fajnberg Avatar answered Nov 04 '25 06:11

Daniil Fajnberg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!