Skip to content

Conversation

@radoering
Copy link
Member

@radoering radoering commented Jan 10, 2026

Pull Request Check List

Resolves: https://github.com/orgs/python-poetry/discussions/10523

use-poetry-python should only be relevant if no specific python version has been requested.

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

This bug originates from #4852 when the condition was not as clear as it is now:

if not executable and prefer_active_python:
    executable = self._detect_active_python(io)

...

if executable and not prefer_active_python:
    raise NoCompatiblePythonVersionFound(
        self._poetry.package.python_versions, python_patch
    )

Summary by Sourcery

Ensure env use and virtualenv creation fail cleanly when a specifically requested Python version is not compatible with the project, regardless of the use-poetry-python setting.

Bug Fixes:

  • Prevent creation or activation of virtualenvs when a user explicitly requests an unsupported Python version, independent of the virtualenvs.use-poetry-python configuration.

Tests:

  • Add tests verifying that env use does not create or activate environments for unsupported Python versions and that executable-based venv creation skips compatibility lookups for both values of use-poetry-python.

Chores:

  • Adjust test fixtures to use a dedicated virtualenv cache directory and assert its initial emptiness before environment creation.

@sourcery-ai
Copy link

sourcery-ai bot commented Jan 10, 2026

Reviewer's Guide

Ensures env use fails fast when a requested Python version is not supported by the project, regardless of the virtualenvs.use-poetry-python setting, and tightens tests around venv cache behavior and Python version selection.

Sequence diagram for env_use failing on unsupported_python_version

sequenceDiagram
    actor Developer
    participant EnvUseCommand
    participant EnvManager
    participant PoetryConfig
    participant PoetryPackage

    Developer->>EnvUseCommand: env use python_spec
    EnvUseCommand->>EnvManager: create_venv(python_spec)
    EnvManager->>EnvManager: determine specific_python_requested
    EnvManager->>PoetryConfig: get(virtualenvs.create)
    EnvManager->>PoetryConfig: get(virtualenvs.prompt)
    EnvManager->>PoetryPackage: read python_versions

    alt python_spec not_compatible_with PoetryPackage.python_versions
        EnvManager->>EnvManager: detect_no_compatible_python
        EnvManager-->>EnvUseCommand: raise NoCompatiblePythonVersionFoundError
        EnvUseCommand-->>Developer: display error unsupported Python version
    else python_spec compatible
        EnvManager->>EnvManager: proceed_with_venv_creation
        EnvManager-->>EnvUseCommand: return created_virtualenv
        EnvUseCommand-->>Developer: show success message
    end
Loading

Class diagram for EnvManager_create_venv Python_version_handling

classDiagram
    class EnvManager {
        +create_venv(python, io)
        -use_in_project_venv() bool
        -_poetry Poetry
    }

    class Poetry {
        +config Config
        +package Package
    }

    class Config {
        +get(key) any
    }

    class Package {
        +python_versions str
    }

    class NoCompatiblePythonVersionFoundError {
    }

    EnvManager --> Poetry : uses
    Poetry --> Config : has
    Poetry --> Package : has
    EnvManager ..> NoCompatiblePythonVersionFoundError : raises

    note for EnvManager "In create_venv:
- specific_python_requested is true when python is not None
- If specific_python_requested and requested version is incompatible
  with Package.python_versions, EnvManager now always raises
  NoCompatiblePythonVersionFoundError, regardless of
  Config[virtualenvs.use-poetry-python]"
Loading

File-Level Changes

Change Details Files
Change EnvManager.create_venv so that specifying an explicit Python executable always triggers an incompatibility error when the version is unsupported, independent of the use-poetry-python configuration.
  • Remove retrieval of the virtualenvs.use-poetry-python flag from the configuration in create_venv.
  • Treat the presence of an explicit python argument as the only condition for raising NoCompatiblePythonVersionFoundError when the resolved executable is incompatible.
  • Leave the fallback logic for auto-detecting compatible Python versions unchanged when no explicit Python is requested.
src/poetry/utils/env/env_manager.py
Add and adjust tests to cover behavior when unsupported Python versions are requested and to ensure venv cache directories are cleanly handled.
  • In the env use command tests, assert that the venv cache directory starts empty, invoke env use with an unsupported Python version, and verify that no virtualenvs or envs.toml are created when NoCompatiblePythonVersionFoundError is raised, parameterized over use-poetry-python.
  • Update the venv_cache fixture to create and return a dedicated subdirectory to avoid mixing with other tmp_path contents.
  • Parameterize the EnvManager test for skipping compatible-version search when an executable is provided over both True and False values of virtualenvs.use-poetry-python, instead of forcing it to True.
tests/console/commands/env/test_use.py
tests/console/commands/env/conftest.py
tests/utils/env/test_env_manager.py

Possibly linked issues

  • #0: PR changes create_venv logic so explicit env use Python wins over use-poetry-python, fixing the reported behavior.

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey - I've left some high level feedback:

  • Consider adding a short inline comment near the if specific_python_requested: check in create_venv() to explain why use-poetry-python is intentionally ignored when an explicit Python is requested, to prevent future re-introduction of the previous behavior.
  • In the new tests that assert an empty cache (assert not list(venv_cache.iterdir())), you could factor this into a small helper or use a more explicit check (e.g. assert not any(venv_cache.iterdir())) to make the intent clearer and avoid repeating the pattern.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider adding a short inline comment near the `if specific_python_requested:` check in `create_venv()` to explain why `use-poetry-python` is intentionally ignored when an explicit Python is requested, to prevent future re-introduction of the previous behavior.
- In the new tests that assert an empty cache (`assert not list(venv_cache.iterdir())`), you could factor this into a small helper or use a more explicit check (e.g. `assert not any(venv_cache.iterdir())`) to make the intent clearer and avoid repeating the pattern.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@radoering radoering force-pushed the fix-env-use-not-supported branch from 9001c58 to 9bc4196 Compare January 16, 2026 16:25
@radoering radoering merged commit 0e02be7 into python-poetry:main Jan 17, 2026
54 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant