Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: take virtualenvs.prefer-active-python into account on EnvManager.get() #6986

Merged

Conversation

finswimmer
Copy link
Member

@finswimmer finswimmer commented Nov 6, 2022

Until now virtualenvs.prefer-active-python wasn't respected by the EnvManager when it tries to get the venv. This is fixed by the PR.

I took the chance to refactor EnvManager in that way, that IO is now set during initializing a new object. Thus it is not necessary to pass it in into the several methods.

Pull Request Check List

Resolves: #6893
Resolves: #5947

  • Added tests for changed code.
  • Updated documentation for changed code.

@finswimmer finswimmer changed the title Fix/env info activated python fix: take virtualenvs.prefer-active-python into account on EnvManager.get() Nov 6, 2022
@finswimmer finswimmer marked this pull request as ready for review November 6, 2022 19:14
@finswimmer finswimmer requested a review from a team November 6, 2022 19:14
Comment on lines 559 to 577
def _get_python_version(self) -> list[int]:
version_info = list(sys.version_info[:3])

if self._poetry.config.get("virtualenvs.prefer-active-python"):
executable = self._detect_active_python()

if executable:
python_patch = decode(
subprocess.check_output(
list_to_shell_command(
[executable, "-c", GET_PYTHON_VERSION_ONELINER]
),
shell=True,
).strip()
)

version_info = [int(v) for v in python_patch.split(".")[:3]]

return version_info
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def _get_python_version(self) -> list[int]:
version_info = list(sys.version_info[:3])
if self._poetry.config.get("virtualenvs.prefer-active-python"):
executable = self._detect_active_python()
if executable:
python_patch = decode(
subprocess.check_output(
list_to_shell_command(
[executable, "-c", GET_PYTHON_VERSION_ONELINER]
),
shell=True,
).strip()
)
version_info = [int(v) for v in python_patch.split(".")[:3]]
return version_info
def _get_python_version(self) -> tuple[int, int, int]:
if self._poetry.config.get("virtualenvs.prefer-active-python"):
executable = self._detect_active_python()
if executable:
python_patch = decode(
subprocess.check_output(
list_to_shell_command(
[executable, "-c", GET_PYTHON_VERSION_ONELINER]
),
shell=True,
).strip()
)
return tuple(int(v) for v in python_patch.split(".")[:3])
return sys.version_info[:3]

Using a tuple would more clearly indicate that we expect 3 items.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I implemented a version that returns a tuple. mypy was a little bit in the way here ...

Copy link
Member

@neersighted neersighted left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather we had one source of truth for this/it feels like the abstraction is leaking even more, but this is a fix for a real issue and it doesn't make the amount of work needed to refactor this file greater.

@neersighted neersighted enabled auto-merge (rebase) November 22, 2022 17:48
@neersighted neersighted merged commit d02c71c into python-poetry:master Nov 22, 2022
@neersighted neersighted added kind/bug Something isn't working as expected area/venv Related to virtualenv management impact/changelog Requires a changelog entry labels Dec 21, 2022
@neersighted neersighted added this to the 1.3 milestone Dec 21, 2022
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 29, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area/venv Related to virtualenv management impact/changelog Requires a changelog entry kind/bug Something isn't working as expected
Projects
None yet
3 participants