poetry icon indicating copy to clipboard operation
poetry copied to clipboard

Validate user input from init command

Open rbogart1990 opened this issue 8 months ago • 1 comments

This PR introduces validation for user input values during the poetry init command. It uses the rules defined in the project-schema.json schema. These rules are already enforced when running the poetry add command, so this change extends the same validation rules to the poetry init command.

Previously, user inputs were not validated against these schema rules, allowing the creation of Poetry projects with invalid or disallowed names (e.g. "new+project").

This change ensures that inputs conform to the schema before proceeding, preventing invalid project configurations.

Pull Request Check List

Resolves: #10170

  • [x] Added tests for changed code.
  • [ ] Updated documentation for changed code.

Summary by Sourcery

Validate user inputs against the project schema in the 'poetry init' command and abort on validation errors.

New Features:

  • Add schema-based validation of project metadata in the init command

Enhancements:

  • Extend existing validation rules from 'poetry add' to the 'poetry init' command

Tests:

  • Add parametrized tests for valid and invalid project names during initialization

rbogart1990 avatar Jul 01 '25 17:07 rbogart1990

Reviewer's Guide

A new validation step for user inputs in the poetry init command is implemented—reusing the existing project-schema rules via Factory.validate—and corresponding unit tests are added to cover valid and invalid project names.

Sequence diagram for user input validation in poetry init command

sequenceDiagram
    actor User
    participant InitCommand as poetry init
    participant Factory
    User->>InitCommand: Run poetry init and provide project info
    InitCommand->>Factory: validate(pyproject_data)
    Factory-->>InitCommand: Validation results
    alt Validation errors
        InitCommand-->>User: Show validation error and abort
    else Validation passes
        InitCommand->>InitCommand: Save pyproject.toml
        InitCommand-->>User: Project initialized
    end

File-Level Changes

Change Details Files
Extend InitCommand to validate pyproject data before saving
  • Insert pre-save validation call in _init_pyproject, aborts on errors
  • Add staticmethod _validate using Factory.validate
src/poetry/console/commands/init.py
Add parameterized tests for project name validation
  • Create build_pyproject_data helper
  • Add tests for valid project names
  • Add tests for invalid project names
tests/console/commands/test_init.py

Possibly linked issues

  • #10170: The PR adds validation to poetry init to prevent creating projects with invalid names, resolving the issue.

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

  • Contact our support team for questions or feedback.
  • Visit our documentation for detailed guides and information.
  • Keep in touch with the Sourcery team by following us on X/Twitter, LinkedIn or GitHub.

sourcery-ai[bot] avatar Jul 01 '25 17:07 sourcery-ai[bot]