Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use "with" as a key in a TypedDict?

I want to use with as a key in a TypedDict in python 3.10.

I have:

from typing import TypedDict, Optional

class Operation(TypedDict, total=False):
    uses: str
    with: Optional[ActionCheckout]

But my IDE says I cannot do this?

idecomplain

like image 411
Joey Stout Avatar asked Nov 21 '25 16:11

Joey Stout


1 Answers

You won't be able to use the declarative syntax, as with (being a hard keyword defined by the grammar) is not a valid identifier; use the functional syntax instead.

Operation = TypedDict('Operation', {'uses': str, 'with': Optional[ActionCheckout]})

This is specifically addressed in the documentation:

The functional syntax should also be used when any of the keys are not valid identifiers, for example because they are keywords

like image 94
chepner Avatar answered Nov 24 '25 04:11

chepner



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!