Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

idecomplain

>Solution :

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,

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading