From 7e0bef28a08129470b5e9248fedf204fb42a1934 Mon Sep 17 00:00:00 2001 From: Sven van Ashbrook Date: Sat, 28 Sep 2024 15:11:39 +0000 Subject: [PATCH] Make Env an ABC (Abstract Base Class) Env appears to be used as an ABC, yet uses an older Python pattern: its abstract methods raise NotImplementedError(). Improve by making the class inherit from ABC, and use @abstractmethod to mark its abstract methods. --- src/poetry/utils/env/base_env.py | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/poetry/utils/env/base_env.py b/src/poetry/utils/env/base_env.py index 5a8f2fc6146..79cfec600a0 100644 --- a/src/poetry/utils/env/base_env.py +++ b/src/poetry/utils/env/base_env.py @@ -7,6 +7,8 @@ import sys import sysconfig +from abc import ABC +from abc import abstractmethod from functools import cached_property from pathlib import Path from subprocess import CalledProcessError @@ -32,7 +34,7 @@ PythonVersion = Tuple[int, int, int, str, int] -class Env: +class Env(ABC): """ An abstract Python environment. """ @@ -223,8 +225,8 @@ def is_path_relative_to_lib(self, path: Path) -> bool: return False @property - def sys_path(self) -> list[str]: - raise NotImplementedError() + @abstractmethod + def sys_path(self) -> list[str]: ... @property def paths(self) -> dict[str, str]: @@ -262,8 +264,8 @@ def get_base_prefix(cls) -> Path: return Path(sys.prefix) - def get_marker_env(self) -> dict[str, Any]: - raise NotImplementedError() + @abstractmethod + def get_marker_env(self) -> dict[str, Any]: ... def get_pip_command(self, embedded: bool = False) -> list[str]: if embedded or not Path(self._bin(self._pip_executable)).exists(): @@ -271,11 +273,11 @@ def get_pip_command(self, embedded: bool = False) -> list[str]: # run as module so that pip can update itself on Windows return [str(self.python), "-m", "pip"] - def get_supported_tags(self) -> list[Tag]: - raise NotImplementedError() + @abstractmethod + def get_supported_tags(self) -> list[Tag]: ... - def get_paths(self) -> dict[str, str]: - raise NotImplementedError() + @abstractmethod + def get_paths(self) -> dict[str, str]: ... def is_valid_for_marker(self, marker: BaseMarker) -> bool: valid: bool = marker.validate(self.marker_env) @@ -351,8 +353,8 @@ def execute(self, bin: str, *args: str, **kwargs: Any) -> int: exe.communicate() return exe.returncode - def is_venv(self) -> bool: - raise NotImplementedError() + @abstractmethod + def is_venv(self) -> bool: ... @property def script_dirs(self) -> list[Path]: