Convert the helper function `_load_keys_optimistically` into a per-key function
Marco Ricci

Marco Ricci commited on 2025-12-27 17:01:22
Zeige 1 geänderte Dateien mit 11 Einfügungen und 10 Löschungen.


The function is now called `_load_key_optimistically`, and returns a
success indication whether the key was (potentially already) loaded or
not.  Also, the private key blob cannot be `None` anymore, so the
function no longer has degenerate cases.
... ...
@@ -856,15 +856,13 @@ def _prepare_payload(
856 856
     return (return_code, bytes(payload) + lifetime_constraint)
857 857
 
858 858
 
859
-def _load_keys_optimistically(
859
+def _load_key_optimistically(
860 860
     spawned_agent_info: data.SpawnedSSHAgentInfo,
861
-) -> set[str]:
862
-    successfully_loaded_keys: set[str] = set()
863
-    for key_type, key_struct in data.ALL_KEYS.items():
861
+    key_type: str,
862
+) -> bool:
864 863
         agent_type, client, isolated = spawned_agent_info
864
+        key_struct = data.ALL_KEYS[key_type]
865 865
         private_key_data = key_struct.private_key_blob
866
-        if private_key_data is None:  # pragma: no cover [failsafe]
867
-            continue
868 866
         request_code, payload = _prepare_payload(
869 867
             private_key_data, isolated=isolated, time_to_live=30
870 868
         )
... ...
@@ -915,10 +913,9 @@ def _load_keys_optimistically(
915 913
             OSError,
916 914
             ssh_agent.SSHAgentFailedError,
917 915
         ):  # pragma: no cover [external]
918
-            pass
916
+            return False
919 917
         else:  # pragma: no cover [external]
920
-            successfully_loaded_keys.add(key_type)
921
-    return successfully_loaded_keys
918
+            return True
922 919
 
923 920
 
924 921
 @pytest.fixture
... ...
@@ -970,7 +967,11 @@ def ssh_agent_client_with_test_keys_loaded(
970 967
     # (But see below, too.)
971 968
 
972 969
     try:
973
-        successfully_loaded_keys = _load_keys_optimistically(spawn_ssh_agent)
970
+        successfully_loaded_keys = {
971
+            key_type
972
+            for key_type, key_struct in data.ALL_KEYS.items()
973
+            if _load_key_optimistically(spawn_ssh_agent, key_type)
974
+        }
974 975
         if (
975 976
             agent_type != data.KnownSSHAgent.StubbedSSHAgent
976 977
             and not successfully_loaded_keys
977 978