Skip to content

Commit

Permalink
Make Env an ABC (Abstract Base Class)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
TheSven73 committed Sep 28, 2024
1 parent 69dbca2 commit 7e0bef2
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions src/poetry/utils/env/base_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -32,7 +34,7 @@
PythonVersion = Tuple[int, int, int, str, int]


class Env:
class Env(ABC):
"""
An abstract Python environment.
"""
Expand Down Expand Up @@ -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]:
Expand Down Expand Up @@ -262,20 +264,20 @@ 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():
return [str(self.python), str(self.pip_embedded)]
# 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)
Expand Down Expand Up @@ -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]:
Expand Down

0 comments on commit 7e0bef2

Please sign in to comment.