Validate user input from init command
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
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 |
|
src/poetry/console/commands/init.py |
| Add parameterized tests for project name validation |
|
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 reviewon 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 issueto create an issue from it. -
Generate a pull request title: Write
@sourcery-aianywhere in the pull request title to generate a title at any time. You can also comment@sourcery-ai titleon the pull request to (re-)generate the title at any time. -
Generate a pull request summary: Write
@sourcery-ai summaryanywhere in the pull request body to generate a PR summary at any time exactly where you want it. You can also comment@sourcery-ai summaryon the pull request to (re-)generate the summary at any time. -
Generate reviewer's guide: Comment
@sourcery-ai guideon the pull request to (re-)generate the reviewer's guide at any time. -
Resolve all Sourcery comments: Comment
@sourcery-ai resolveon 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 dismisson 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 reviewto 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.