[Python] python typing from a json get operation

Discussão em 'Python' iniciado por Stack, Julho 13, 2025.

  1. Stack

    Stack Membro Participativo

    This question is about python typing, and type hints when used with json dictonaries.

    I am using VS Code and it uses PyLance if that is important

    In my case I have JSON object that I load from a file, within the file I have any number of list, and dicts. Python typing does not seem to like this, or should I say PyLance.

    As an example:

    # Extract the variables from the json configuration file
    variables = json_dict.get('variables', None)
    # In my case, variables are optional hence I use "get()" with None.
    if variables is not None:
    # PROBLEM HERE, pylance thinks that 'variables is a string'
    for name, value in variables.items():
    define_var(name, value)


    In this case, json_dict.get() would return dict[str, str]. But if I add : dict[str, str] at the end of the line that does not work.

    While I am using VS Code and thus PyLance, the question is also generically Python because I need this to work with other implementations, e.g. PyCharm.

    Continue reading...

Compartilhe esta Página