From 6e1556028c15ba92fff32ad68c88edf143d42c16 Mon Sep 17 00:00:00 2001 From: Eli Schwartz Date: Mon, 25 Dec 2023 02:32:14 -0500 Subject: [PATCH] python dependency: use exceptions to pass failure state around Instead of returning Optional, a state that is only possible during `__init__` and which prevents mypy from knowing what it is safe to assume it will get. --- mesonbuild/dependencies/python.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/mesonbuild/dependencies/python.py b/mesonbuild/dependencies/python.py index 6620eff20..b9b17f854 100644 --- a/mesonbuild/dependencies/python.py +++ b/mesonbuild/dependencies/python.py @@ -8,7 +8,7 @@ from pathlib import Path import typing as T from .. import mesonlib, mlog -from .base import process_method_kw, DependencyMethods, DependencyTypeName, ExternalDependency, SystemDependency +from .base import process_method_kw, DependencyException, DependencyMethods, DependencyTypeName, ExternalDependency, SystemDependency from .configtool import ConfigToolDependency from .detect import packages from .factory import DependencyFactory @@ -245,7 +245,7 @@ class PythonSystemDependency(SystemDependency, _PythonDependencyBase): self.link_args = largs self.is_found = True - def get_windows_python_arch(self) -> T.Optional[str]: + def get_windows_python_arch(self) -> str: if self.platform.startswith('mingw'): if 'x86_64' in self.platform: return 'x86_64' @@ -254,16 +254,14 @@ class PythonSystemDependency(SystemDependency, _PythonDependencyBase): elif 'aarch64' in self.platform: return 'aarch64' else: - mlog.log(f'MinGW Python built with unknown platform {self.platform!r}, please file a bug') - return None + raise DependencyException(f'MinGW Python built with unknown platform {self.platform!r}, please file a bug') elif self.platform == 'win32': return 'x86' elif self.platform in {'win64', 'win-amd64'}: return 'x86_64' elif self.platform in {'win-arm64'}: return 'aarch64' - mlog.log(f'Unknown Windows Python platform {self.platform!r}') - return None + raise DependencyException('Unknown Windows Python platform {self.platform!r}') def get_windows_link_args(self, limited_api: bool) -> T.Optional[T.List[str]]: if self.platform.startswith('win'): @@ -330,8 +328,10 @@ class PythonSystemDependency(SystemDependency, _PythonDependencyBase): Find python3 libraries on Windows and also verify that the arch matches what we are building for. ''' - pyarch = self.get_windows_python_arch() - if pyarch is None: + try: + pyarch = self.get_windows_python_arch() + except DependencyException as e: + mlog.log(str(e)) self.is_found = False return arch = detect_cpu_family(env.coredata.compilers.host)