aiidateam / aiida-core

The official repository for the AiiDA code
https://aiida-core.readthedocs.io
Other
413 stars 184 forks source link

`InstalledCode` and `PortableCode` pydantic models emit serialization warnings #6364

Closed danielhollas closed 1 month ago

danielhollas commented 2 months ago

Describe the bug

I noticed a couple of warnings / errors in the nightly tests log (in "Setup environment" step), not sure if those are pertinent to anything, see full log

/opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/pydantic/main.py:308: UserWarning: Pydantic serializer warnings:
  Expected `str` but got `Computer` - serialized value may not be as expected
  return self.__pydantic_serializer__.to_python(
Opening connection... Error: Error connecting to 'localhost' through SSH: [SshTransport] [Errno None] Unable to connect to port 5001 on 127.0.0.1 or ::1, connect_args were: {'username': 'xenon', 'port': 5001, 'look_for_keys': True, 'key_filename': '/home/runner/.ssh/slurm_rsa', 'timeout': 60, 'allow_agent': True, 'proxy_jump': '', 'proxy_command': '', 'compress': True, 'gss_auth': False, 'gss_kex': False, 'gss_deleg_creds': False, 'gss_host': 'localhost'}
[FAILED]: Error while trying to connect to the computer
  Full traceback:
  Traceback (most recent call last):
    File "/home/runner/work/aiida-core/aiida-core/src/aiida/cmdline/commands/cmd_computer.py", line 542, in computer_test
      with transport:
    File "/home/runner/work/aiida-core/aiida-core/src/aiida/transports/transport.py", line 129, in __enter__
      self.open()
    File "/home/runner/work/aiida-core/aiida-core/src/aiida/transports/plugins/ssh.py", line 498, in open
      self._client.connect(self._machine, **connection_arguments)
    File "/opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/paramiko/client.py", line 381, in connect
      raise NoValidConnectionsError(errors)
  paramiko.ssh_exception.NoValidConnectionsError: [Errno None] Unable to connect to port 5001 on 127.0.0.1 or ::1
Warning: 1 out of 0 tests failed
sphuber commented 2 months ago

The SSH error was handled in #6372 and was not really of any consequence.

I have renamed the issue to reflect the maining issue which is separate. Essentially, the mentioned code models declare the fields computer and filepath_file, with the types str. However, they have a serializer that converts them to a Computer instance and a pathlib.Path instance. This is allowed by pydantic but it does warn that the return type of the custom serializer does not match the type declaration. The reason for the type change is that the return type is easier to deal with in the Python API, so it is convenience for the user. I think I only added a single type instead of a Union because the dynamic CLI verdi code create needs to determine the option type based on the type annotation and click does not support unions, so it has to choose one, which currently emits a warning as well. But I guess we can simply silence this warning.