[v15,2/5] dts: replace the or operator in third party types

Message ID 20240806151937.391917-3-juraj.linkes@pantheon.tech (mailing list archive)
State Superseded
Delegated to: Thomas Monjalon
Headers
Series API docs generation |

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

Juraj Linkeš Aug. 6, 2024, 3:19 p.m. UTC
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

Luca Vizzarro Aug. 7, 2024, 1:34 p.m. UTC | #1
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/
  
Juraj Linkeš Aug. 7, 2024, 2:24 p.m. UTC | #2
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/
>
  

Patch

diff --git a/dts/framework/remote_session/interactive_remote_session.py b/dts/framework/remote_session/interactive_remote_session.py
index 97194e6af8..4605ee14b4 100644
--- a/dts/framework/remote_session/interactive_remote_session.py
+++ b/dts/framework/remote_session/interactive_remote_session.py
@@ -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.