Fix some coverage slipups
Marco Ricci

Marco Ricci commited on 2025-06-10 19:31:24
Zeige 2 geänderte Dateien mit 9 Einfügungen und 11 Löschungen.


The first slipup deals with two different wrappers around `os.getenv`,
depending on whether the system support bytes-based environment
variables. We care about whether the wrapper function (whichever
implementation) is called correctly, not which wrapper function is
selected. So, rewrite the branch to put the system-specific definitions
under coverage exclusion, and the rest not.

The second slipup concerns the concurrency limit. The detection is
system- and runtime-dependent, but the error paths were previously not
excluded from coverage. Additionally, we claimed to manually support the
`PYTHON_CPU_COUNT` environment variable on Python 3.12, but contained no
matching code. So, rewrite the code to add that missing support, in
a coverage-friendly way.
... ...
@@ -63,16 +63,16 @@ def get_vault_key() -> bytes:
63 63
             Please set `VAULT_KEY` manually to the desired value.
64 64
 
65 65
     """
66
-    if os.supports_bytes_environ:
67 66
 
68
-        def getenv(env_var: str) -> bytes:
67
+    def getenv_environ(env_var: str) -> bytes:  # pragma: no cover
69 68
         return os.environb.get(env_var.encode('UTF-8'), b'')  # type: ignore[attr-defined]
70 69
 
71
-    else:
72
-
73
-        def getenv(env_var: str) -> bytes:
70
+    def getenv_environb(env_var: str) -> bytes:  # pragma: no cover
74 71
         return os.environ.get(env_var, '').encode('UTF-8')
75 72
 
73
+    getenv: Callable[[str], bytes] = (
74
+        getenv_environb if os.supports_bytes_environ else getenv_environ
75
+    )
76 76
     username = (
77 77
         getenv('VAULT_KEY')
78 78
         or getenv('LOGNAME')
... ...
@@ -1651,12 +1651,10 @@ def get_concurrency_limit() -> int:
1651 1651
     if sys.version_info >= (3, 13):
1652 1652
         result = os.process_cpu_count()
1653 1653
     else:
1654
-        try:
1655
-            cpus = os.sched_getaffinity(os.getpid())
1656
-        except AttributeError:
1657
-            pass
1658
-        else:
1659
-            result = len(cpus)
1654
+        with contextlib.suppress(ValueError):
1655
+            result = result or int(os.environ['PYTHON_CPU_COUNT'], 10)
1656
+        with contextlib.suppress(AttributeError):
1657
+            result = result or len(os.sched_getaffinity(os.getpid()))
1660 1658
     return max(result if result is not None else 0, MIN_CONCURRENCY)
1661 1659
 
1662 1660
 
1663 1661