I am trying to type hint a walrus operator expression, i.e.
while (var: int := some_func()): ...
How can I do this?
I am trying to type hint a walrus operator expression, i.e.
while (var: int := some_func()): ...
How can I do this?
I don't believe you can.
A variable can be annotated because the grammar rule for assignment is
assignment:
| NAME ':' expression ['=' annotated_rhs ]
...
Note that the type hint is explicit between the :
following the name and the =
.
An assignment expression, on the other hand, only provides for a name, no type hint, preceding the :=
:
named_expression:
| NAME ':=' ~ expression
| expression !':='
It's not possible. From PEP 572
You need to declare the variable before the
while
loop, and you can specify the type there.