Skip to content

Commit c9d82b1

Browse files
committed
chore(doc): document Python class methods
1 parent 5c11499 commit c9d82b1

File tree

1 file changed

+47
-2
lines changed

1 file changed

+47
-2
lines changed

src/poetry/utils/env/python/manager.py

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,27 @@ def minor_version(self) -> Version:
8585

8686
@classmethod
8787
def get_active_python(cls) -> Python | None:
88+
"""
89+
Fetches the active Python interpreter from available system paths or falls
90+
back to finding the first valid Python executable named "python".
91+
92+
An "active Python interpreter" in this context is an executable (or a symlink)
93+
with the name `python`. This is done so to detect cases where pyenv or
94+
alternatives are used.
95+
96+
This method first uses the `ShutilWhichPythonProvider` to detect Python
97+
executables in the path. If no interpreter is found using, it attempts
98+
to locate a Python binary named "python" via the `findpython` library.
99+
100+
:return: An instance representing the detected active Python,
101+
or None if no valid environment is found.
102+
"""
88103
for python in ShutilWhichPythonProvider().find_pythons():
89104
return cls(python=python)
90105

91-
# fallback to findpython
92-
if python := findpython.find():
106+
# fallback to findpython, restrict to finding only executables
107+
# named "python" as the intention here is just that, nothing more
108+
if python := findpython.find("python"):
93109
return cls(python=python)
94110

95111
return None
@@ -103,6 +119,9 @@ def from_executable(cls, path: Path | str) -> Python:
103119

104120
@classmethod
105121
def get_system_python(cls) -> Python:
122+
"""
123+
Creates and returns an instance of the class representing the Poetry's Python executable.
124+
"""
106125
return cls(
107126
python=findpython.PythonVersion(
108127
executable=Path(sys.executable),
@@ -128,6 +147,17 @@ def get_by_name(cls, python_name: str) -> Python | None:
128147

129148
@classmethod
130149
def get_preferred_python(cls, config: Config, io: IO | None = None) -> Python:
150+
"""
151+
Determine and return the "preferred" Python interpreter based on the provided
152+
configuration and optional input/output stream.
153+
154+
This method first attempts to get the active Python interpreter if the configuration
155+
does not mandate using Poetry's Python. If an active interpreter is found, it is returned.
156+
Otherwise, the method defaults to retrieving the Poetry's Python interpreter (System Python).
157+
158+
This method **does not** attempt to sort versions or determine Python version constraint
159+
compatibility.
160+
"""
131161
io = io or NullIO()
132162

133163
if not config.get("virtualenvs.use-poetry-python") and (
@@ -142,6 +172,21 @@ def get_preferred_python(cls, config: Config, io: IO | None = None) -> Python:
142172

143173
@classmethod
144174
def get_compatible_python(cls, poetry: Poetry, io: IO | None = None) -> Python:
175+
"""
176+
Retrieve a compatible Python version based on the given poetry configuration
177+
and Python constraints derived from the project.
178+
179+
This method iterates through all available Python candidates and checks if any
180+
match the supported Python constraint as defined in the specified poetry package.
181+
182+
:param poetry: The poetry configuration containing package information,
183+
including Python constraints.
184+
:param io: The input/output stream for error and status messages. Defaults
185+
to a null I/O if not provided.
186+
:return: A Python instance representing a compatible Python version.
187+
:raises NoCompatiblePythonVersionFoundError: If no Python version matches
188+
the supported constraint.
189+
"""
145190
io = io or NullIO()
146191
supported_python = poetry.package.python_constraint
147192

0 commit comments

Comments
 (0)