Alternative syntax for unions requires Python 3.10 or newer

Hello,

I’m starting to develop my own integration and decided to use IPP printing as the example code to start from because my integration will also obtain the sensor values from a REST call.

I notice that the syntax of the conflig_flow.py example contains parameters like:

    async def async_step_user(
        self, user_input: dict[str, Any] | None = None
    ) -> FlowResult:

but when I copy exactly the same code into my own config_flow.py the compiler raises syntax errors related with python version 3.9 not supporting that.

Surprisingly, the code from IPP does not raise any error!

Also the python version in code and in home assistant is 3.9 so I don’t know how is possible for the IPP integration to use features that are available from version 3.10

How can I use the same syntax used by IPP?

Thank you for your help

You need to upgrade your local python version to v3.10.
The reason is the use of the |character, I read this one is supported from v3.10.

Hi,

Thank you for your response. It is true the pipe ‘|’ character is supported in python 3.10. I don’t understand why the IPP integration is using it while the python version in the production environment is 3.9.9 and the python version in the development environment is 3.9.10

I’m not an expert in python, but is very confusing and I don’t see a clear explanation why the IPP source code is using features that are not supported but it works for them and don’t work for my own code.

Wait, I might be wrong, I see that 3.9 does support the pipe character.

What is the exact error that you are seeing?

In two places:

While editing code:

And while executing it:

In the same line numbers

Try adding this:

from __future__ import annotations

at the top of the python file, directly below the header line: “”" This is a header line."""
If I remember correctly, this solved it for me.

I found more info; according to this PEP 584 – Add Union Operators To dict | peps.python.org the pipe character is part of the dict class and the purpose is to merge dictionaries.

There must be something else regarding the dict class but I don’t know what :frowning_face_with_open_mouth:

I solved the issue.

The “magic” is this line at the beginning of the file

from __future__ import annotations

After adding this line to my class the syntax errors are gone!

I think your problem is related to the typing: user_input: dict[str, Any]
With typing I mean then : and what is following after.

The extra line I propose should solve this.

Very good. Please mark one of my posts as the Solution.

Thank you very much!