Untyped dict & walrus type hints


using Python 3.10.12, I have a couple of ideas about type hints for you to ponder.

  1. Adding type hints to key/value iteration over items of a dictionary whose type is unknown:

    for key:'str', value:'int' in untyped_dict.items():
  2. Adding type hints to the walrus operator would also come in handy with untyped dicts:

    if status_code:'str' := untyped_dict.get('status_code', ''):

Please note that this is not a request to find a workaround for the lack of these features.

Rather, adding clean-looking type hints in their natural places, meaning next to where a given object / variable comes into existence, is what I have in mind here.

Specifically, in the case of key/value iteration, these are often short-lived variables and it would be more convenient if they did not have to be declared in advance before the loop.

As to the walrus operator, if we can say this:

abc:'str' = get_abc()

then it would make the language more consistent if we were able to say that too:

if abc:'str' := get_abc():

I see the walrus operator as a mere assignment here, only that it is conditional.


I find that extremely difficult to read. The following would work today, and are at least (IMO) not worse to read:

for key, value in ty.cast(tuple[str, int], untyped_dict.items()):

if status_code := ty.cast(str, untyped_dict.get('status_code', '')):

You can already do this by declaring the type prior to entering the loop or using the walrus expression:

key: str
value: int
for key, value in untyped_dict.items():

status_code: str
if status_code := untyped_dict.get('status_code', ''):

Unlike @effigies’s suggestion, this has the advantage that the type checker will emit an error if it believes its inferred types for key, value and/or status_code are incompatible with the declared types for key, value, and/or status_code.


Hi @AlexWaygood,

yes, this is correct, and my original message was about extending what is possible, about adding a new capability to the language, not about finding a way to add this kind of type hints using the existing syntax.

Perhaps this is really about consistency.

For instance, from your perspective, if we cannot do this …

if status_code:'str' := untyped_dict.get('status_code', ''):

… then the question is why we can do this:

status_code:'str' = self.get_status_code()

In other words, why are we not required to always do this?

status_code = self.get_status_code()

The way I see it, there is no difference between the two, if a simple assignment accepts a type hint then it should be no different with the walrus operator, and neither should it be with what is such a common operation as going through all the keys and values in a dictionary, the majority of which will be almost always untyped.