[v15,2/5] dts: replace the or operator in third party types
Checks
Commit Message
When the DTS dependencies are not installed when building DTS API
documentation, the or operator produces errors when used with types from
those libraries:
autodoc: failed to import module 'remote_session' from module
'framework'; the following exception was raised:
Traceback (most recent call last):
...
TypeError: unsupported operand type(s) for |: 'Transport' and 'NoneType'
The third part type here is Transport from the paramiko library.
Signed-off-by: Juraj Linkeš <juraj.linkes@pantheon.tech>
---
dts/framework/remote_session/interactive_remote_session.py | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
Comments
Hi Juraj,
In the past, I have noticed this problem appear only on Python versions
prior to 3.10. Before PEP 604[1] – introduced in Python 3.10 – the pipe
operator was always used as an operator between objects instead of an
alias for Union in the annotations. A quick test verifies this:
Python 3.8.18 (default, Aug 25 2023, 13:20:30)
[GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from collections import Counter
>>> from typing import TypedDict
>>> class t(TypedDict):
... a: Counter | None
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in t
TypeError: unsupported operand type(s) for |: 'type' and 'NoneType'
>>>
I have also attempted to build the docs removing this commit on my local
setup (outside of the Poetry shell) and it appears to be working with no
problems.
Best,
Luca
[1] https://peps.python.org/pep-0604/
On 7. 8. 2024 15:34, Luca Vizzarro wrote:
> Hi Juraj,
>
> In the past, I have noticed this problem appear only on Python versions
> prior to 3.10. Before PEP 604[1] – introduced in Python 3.10 – the pipe
> operator was always used as an operator between objects instead of an
> alias for Union in the annotations. A quick test verifies this:
>
> Python 3.8.18 (default, Aug 25 2023, 13:20:30)
> [GCC 11.4.0] on linux
> Type "help", "copyright", "credits" or "license" for more information.
> >>> from collections import Counter
> >>> from typing import TypedDict
> >>> class t(TypedDict):
> ... a: Counter | None
> ...
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> File "<stdin>", line 2, in t
> TypeError: unsupported operand type(s) for |: 'type' and 'NoneType'
> >>>
>
> I have also attempted to build the docs removing this commit on my local
> setup (outside of the Poetry shell) and it appears to be working with no
> problems.
>
My local build also works fine, but the problem was found in CI:
https://github.com/ovsrobot/dpdk/actions/runs/10261380458/job/28389032405
I included a script that checks the python version, so the running
version should be at least 3.10. I can't find the actual version
anywhere in the logs, but the environment is Ubuntu22.04 which should
run 3.10.
The TypeError only happens with the Transport from paramiko. This is
likely related to the new feature that I've added in these last
versions: the Python dependencies don't need to be installed. If they're
not found, they're added to the autodoc_mock_imports config option and
basically ignored. CI likely doesn't have these dependencies so I'd say
this is the reason. My local testing (building docs without paramiko)
confirms this.
> Best,
> Luca
>
> [1] https://peps.python.org/pep-0604/
>
@@ -5,6 +5,7 @@
import socket
import traceback
+from typing import Union
from paramiko import AutoAddPolicy, SSHClient, Transport # type: ignore[import-untyped]
from paramiko.ssh_exception import ( # type: ignore[import-untyped]
@@ -52,7 +53,7 @@ class InteractiveRemoteSession:
session: SSHClient
_logger: DTSLogger
_node_config: NodeConfiguration
- _transport: Transport | None
+ _transport: Union[Transport, None]
def __init__(self, node_config: NodeConfiguration, logger: DTSLogger) -> None:
"""Connect to the node during initialization.