Skip to content

Commit

Permalink
code review
Browse files Browse the repository at this point in the history
  • Loading branch information
dimbleby committed Apr 22, 2023
1 parent ee15708 commit 89d3e9c
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions src/poetry/utils/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,11 @@ def get_python_version(

return Version.parse(version)

@property
def in_project_venv(self) -> Path:
venv: Path = self._poetry.file.parent / ".venv"
return venv

def activate(self, python: str) -> Env:
venv_path = self._poetry.config.virtualenvs_path
cwd = self._poetry.file.parent
Expand Down Expand Up @@ -621,12 +626,12 @@ def activate(self, python: str) -> Env:
patch = python_version.text

create = False
is_root_venv = self.use_root_env()
is_root_venv = self.use_in_project_venv()
# If we are required to create the virtual environment in the root folder,
# create or recreate it if needed
if is_root_venv:
create = False
venv = self._poetry.file.parent / ".venv"
venv = self.in_project_venv
if venv.exists():
# We need to check if the patch version is correct
_venv = VirtualEnv(venv)
Expand Down Expand Up @@ -730,8 +735,8 @@ def get(self, reload: bool = False) -> Env:

if not in_venv or env is not None:
# Checking if a local virtualenv exists
if self.use_root_env():
venv = cwd / ".venv"
if self.use_in_project_venv():
venv = self.in_project_venv

return VirtualEnv(venv)

Expand Down Expand Up @@ -768,8 +773,8 @@ def list(self, name: str | None = None) -> list[VirtualEnv]:
venv_path = self._poetry.config.virtualenvs_path
env_list = [VirtualEnv(p) for p in sorted(venv_path.glob(f"{venv_name}-py*"))]

venv = self._poetry.file.parent / ".venv"
if self.use_root_env():
if self.use_in_project_venv():
venv = self.in_project_venv
env_list.insert(0, VirtualEnv(venv))
return env_list

Expand Down Expand Up @@ -883,13 +888,13 @@ def remove(self, python: str) -> Env:

return VirtualEnv(venv_path, venv_path)

def use_root_env(self) -> bool:
def use_in_project_venv(self) -> bool:
in_project: bool | None = self._poetry.config.get("virtualenvs.in-project")
if in_project is not None:
return in_project

root_venv: Path = self._poetry.file.parent / ".venv"
return root_venv.exists() and root_venv.is_dir()
return root_venv.is_dir()

def create_venv(
self,
Expand Down Expand Up @@ -918,7 +923,7 @@ def create_venv(
return env

create_venv = self._poetry.config.get("virtualenvs.create")
root_venv = self.use_root_env()
root_venv = self.use_in_project_venv()
prefer_active_python = self._poetry.config.get(
"virtualenvs.prefer-active-python"
)
Expand All @@ -927,12 +932,11 @@ def create_venv(
if not executable and prefer_active_python:
executable = self._detect_active_python()

venv_path: Path = (
cwd / ".venv" if root_venv else self._poetry.config.virtualenvs_path
venv_path = (
self.in_project_venv if root_venv else self._poetry.config.virtualenvs_path
)
if not name:
name = self._poetry.package.name
assert name is not None

python_patch = ".".join([str(v) for v in sys.version_info[:3]])
python_minor = ".".join([str(v) for v in sys.version_info[:2]])
Expand Down

0 comments on commit 89d3e9c

Please sign in to comment.