Skip to content

feat: add --use-tool-poetry flag to poetry init command #10455

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

04bhavyaa
Copy link

@04bhavyaa 04bhavyaa commented Jun 30, 2025

Resolves: #10454

Summary

This PR adds a --use-tool-poetry flag to the poetry init command, allowing users to generate the classic [tool.poetry] format instead of the modern [project] format (PEP 621).

Motivation

  • The [project] format can cause issues for some multi-package Django projects, especially on Linux.
  • The classic [tool.poetry] format is more compatible for certain app-style structures.

Implementation

  • Adds a new flag: --use-tool-poetry to poetry init
  • Updates the layout logic to support both formats
  • Updates documentation and help output

Usage

# Default (PEP 621)
poetry init

# Classic format
poetry init --use-tool-poetry

Checklist

  • Feature works as described
  • Tests added for both formats
  • Documentation updated

Summary by Sourcery

Add a --use-tool-poetry flag to the poetry init command to allow generating the classic [tool.poetry] format alongside the default PEP 621 [project] format, update layout generation logic, CLI documentation, and help output, and add tests for both formats.

New Features:

  • Add --use-tool-poetry option to the poetry init command.

Enhancements:

  • Extend layout generator to produce either [project] or classic [tool.poetry] formats based on the new flag.

Documentation:

  • Update CLI documentation and help text to describe the --use-tool-poetry option and format behavior.

Tests:

  • Add tests verifying the presence of the flag and correct content generation for both formats.

- Add POETRY_TOOL_ONLY template for classic [tool.poetry] format
- Modify Layout class to support both project and tool.poetry formats
- Add --use-tool-poetry option to InitCommand
- Update documentation to include the new flag
- Maintain backward compatibility with existing behavior

This addresses the need for classic [tool.poetry] format that works better
with Django projects containing multiple packages.
Copy link

sourcery-ai bot commented Jun 30, 2025

Reviewer's Guide

This PR introduces a new --use-tool-poetry flag to the poetry init command, toggles between PEP 621 ([project]) and classic ([tool.poetry]) formats in the layout generator, updates documentation to include the new option, and adds tests covering CLI behavior and content generation for both formats.

Sequence diagram for poetry init command with --use-tool-poetry flag

sequenceDiagram
    actor User
    participant CLI as Poetry CLI
    participant InitCommand
    participant Layout
    User->>CLI: poetry init [--use-tool-poetry]
    CLI->>InitCommand: parse options
    InitCommand->>Layout: create(..., use_tool_poetry=flag)
    Layout->>Layout: generate_project_content()
    Layout-->>InitCommand: content
    InitCommand-->>CLI: write pyproject.toml
    CLI-->>User: pyproject.toml created in selected format
Loading

Class diagram for updated Layout class supporting both formats

classDiagram
    class Layout {
        - _use_tool_poetry: bool
        + __init__(..., use_tool_poetry: bool = False)
        + generate_project_content() TOMLDocument
    }
    class Factory {
        + create_dependency(dep_name, dep_constraint)
    }
    Layout --> Factory : uses
Loading

File-Level Changes

Change Details Files
Add CLI support for the new --use-tool-poetry flag
  • Register use-tool-poetry option in InitCommand
  • Extend help text to describe flag behavior
  • Pass flag value into layout constructor
src/poetry/console/commands/init.py
Extend layout generator to support classic tool.poetry format
  • Define a new POETRY_TOOL_ONLY template
  • Add use_tool_poetry parameter and store in Layout
  • Branch template selection and populate content for both formats
src/poetry/layouts/layout.py
Update CLI documentation to mention the new flag
  • Insert --use-tool-poetry under init options
  • Clarify default format and flag effect in docs
docs/cli.md
Add tests for --use-tool-poetry functionality
  • Verify flag presence in InitCommand options
  • Test layout accepts and stores use_tool_poetry
  • Assert correct content generation for both formats and author formatting
tests/console/commands/test_init_tool_poetry.py

Possibly linked issues


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 @04bhavyaa - I've reviewed your changes - here's some feedback:

  • The generate_project_content method duplicates a lot of logic across the two branches (author, license, readme, dependency handling, etc.); consider extracting the common parts into helper methods to reduce code duplication and improve maintainability.
  • In the tool-poetry branch you insert dependencies directly rather than using Factory.create_dependencyto_pep_508, which may skip handling markers/extras consistently; you might want to reuse the same factory logic for both formats.
  • The POETRY_TOOL_ONLY template literal in layout.py feels like a big embedded constant—consider moving it to a dedicated defaults/configuration module or file so it’s easier to find and update in the future.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The `generate_project_content` method duplicates a lot of logic across the two branches (author, license, readme, dependency handling, etc.); consider extracting the common parts into helper methods to reduce code duplication and improve maintainability.
- In the tool-poetry branch you insert dependencies directly rather than using `Factory.create_dependency``to_pep_508`, which may skip handling markers/extras consistently; you might want to reuse the same factory logic for both formats.
- The `POETRY_TOOL_ONLY` template literal in `layout.py` feels like a big embedded constant—consider moving it to a dedicated defaults/configuration module or file so it’s easier to find and update in the future.

## Individual Comments

### Comment 1
<location> `src/poetry/layouts/layout.py:161` </location>
<code_context>
-            project_content["dependencies"].append(dependency.to_pep_508())
-
-        poetry_content = content["tool"]["poetry"]
+        if self._use_tool_poetry:
+            # Handle tool.poetry format
+            poetry_content = content["tool"]["poetry"]
+            poetry_content["name"] = self._project
</code_context>

<issue_to_address>
The logic for handling the tool.poetry format duplicates much of the project format logic.

Refactor by extracting shared logic for setting fields like name, version, description, author, license, readme, and dependencies to minimize duplication and ease maintenance.

Suggested implementation:

```python

        def _set_project_metadata(metadata_content):
            metadata_content["name"] = self._project
            metadata_content["version"] = self._version
            metadata_content["description"] = self._description

            m = AUTHOR_REGEX.match(self._author)
            if m is None:
                # This should not happen because author has been validated before.
                raise ValueError(f"Invalid author: {self._author}")
            else:
                author = f"{m.group('name')}"
                if email := m.group("email"):
                    author += f" <{email}>"
                metadata_content["authors"] = [author]

            if self._license:
                metadata_content["license"] = self._license
            if self._readme:
                metadata_content["readme"] = self._readme

            metadata_content["dependencies"] = [
                dependency.to_pep_508() for dependency in self._dependencies
            ]

        if self._use_tool_poetry:
            # Handle tool.poetry format
            poetry_content = content["tool"]["poetry"]
            _set_project_metadata(poetry_content)

```

- You should also replace the corresponding logic in the `project` format handling (not shown in your snippet) with a call to `_set_project_metadata`.
- If the `dependencies` field or other fields are handled differently between formats, you may need to parameterize `_set_project_metadata` or add conditional logic.
- Make sure `_set_project_metadata` is defined at the appropriate scope (likely as a private method of the class, or as a nested function if only used locally).
</issue_to_address>

### Comment 2
<location> `src/poetry/layouts/layout.py:205` </location>
<code_context>
+                        dep_constraint
+                    )
+            else:
+                del poetry_content["group"]

-        packages = self.get_package_include()
</code_context>

<issue_to_address>
Using del on poetry_content["group"] may raise a KeyError if the key does not exist.

Consider using poetry_content.pop("group", None) to avoid exceptions if the key is missing.
</issue_to_address>

<suggested_fix>
<<<<<<< SEARCH
            else:
                del poetry_content["group"]
=======
            else:
                poetry_content.pop("group", None)
>>>>>>> REPLACE

</suggested_fix>

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants