fix: unexpected behavior of new command in existing empty directories#10431
fix: unexpected behavior of new command in existing empty directories#10431radoering merged 6 commits intopython-poetry:mainfrom
Conversation
Add note explaining that `poetry new .` in empty directories creates only pyproject.toml, not full project structure. Closes python-poetry#10429
Reviewer's GuideThis PR introduces a flag to control project layout creation in empty directories by updating the internal init logic and enabling it in the Sequence diagram for poetry new in existing empty directorysequenceDiagram
actor User
participant PoetryNew as poetry new
participant InitCommand
User->>PoetryNew: Run 'poetry new .' (in empty dir)
PoetryNew->>InitCommand: _init_pyproject(..., allow_layout_creation_on_empty=True)
InitCommand->>InitCommand: Check if directory is empty
InitCommand->>InitCommand: Create full project layout
InitCommand-->>PoetryNew: Return success
PoetryNew-->>User: Project scaffold created
Sequence diagram for poetry init in existing empty directorysequenceDiagram
actor User
participant PoetryInit as poetry init
participant InitCommand
User->>PoetryInit: Run 'poetry init' (in empty dir)
PoetryInit->>InitCommand: _init_pyproject(..., allow_layout_creation_on_empty=False)
InitCommand->>InitCommand: Check if directory is empty
InitCommand->>InitCommand: Only create pyproject.toml
InitCommand-->>PoetryInit: Return success
PoetryInit-->>User: pyproject.toml created (no layout)
Class diagram for InitCommand and new allow_layout_creation_on_empty parameterclassDiagram
class InitCommand {
+_init_pyproject(allow_interactive: bool, layout_name: str, readme_format: str, allow_layout_creation_on_empty: bool = False)
}
class NewCommand {
+handle()
}
InitCommand <|-- NewCommand
InitCommand : +_init_pyproject(..., allow_layout_creation_on_empty)
NewCommand : +handle() calls _init_pyproject with allow_layout_creation_on_empty=True
File-Level Changes
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
|
I think we should prefer to fix/change the behavior rather than document it. See #10429 (comment). |
This reverts commit ea5b7c3.
Fix `poetry new` behavior when used in an existing empty directory This commit addresses unexpected behavior when running `poetry new` in an already existing empty directory, including the current directory (`.`). It ensures that `poetry new` handles these cases correctly, while maintaining the expected behavior of `poetry init`. The fix introduces a new argument, `create_layout_if_empty`, to the `_init_pyproject` method in the `InitCommand` class. Additionally, it adds a conditional check to determine whether a full project layout should be created or only a `pyproject.toml` file.
poetry new behavior in existing empty directories|
@sourcery-ai review |
There was a problem hiding this comment.
Hey @SoSaymon - I've reviewed your changes - here's some feedback:
- Consider extracting the “empty directory” check (
not any(project_path.iterdir())) into a reusable helper (e.g.Path.is_empty()) to improve readability and avoid duplication. - The flag name
create_layout_if_emptyis a bit opaque—something likeallow_layout_creation_on_emptyor similar might more clearly convey its intent. - Right now hidden files (e.g.
.gitignore) count as directory contents; if your intent is to treat directories with only dotfiles as “empty,” you may want to adjust the emptiness check to ignore those patterns.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Consider extracting the “empty directory” check (`not any(project_path.iterdir())`) into a reusable helper (e.g. `Path.is_empty()`) to improve readability and avoid duplication.
- The flag name `create_layout_if_empty` is a bit opaque—something like `allow_layout_creation_on_empty` or similar might more clearly convey its intent.
- Right now hidden files (e.g. `.gitignore`) count as directory contents; if your intent is to treat directories with only dotfiles as “empty,” you may want to adjust the emptiness check to ignore those patterns.
## Individual Comments
### Comment 1
<location> `src/poetry/console/commands/init.py:93` </location>
<code_context>
allow_interactive: bool = True,
layout_name: str = "standard",
readme_format: str = "md",
+ create_layout_if_empty: bool = False,
) -> int:
from poetry.core.vcs.git import GitConfig
</code_context>
<issue_to_address>
Consider documenting the new parameter's intended usage and default behavior.
Clarifying how this parameter behaves when the project path exists but is empty will help prevent confusion or misuse.
</issue_to_address>
<suggested_fix>
<<<<<<< SEARCH
allow_interactive: bool = True,
layout_name: str = "standard",
readme_format: str = "md",
create_layout_if_empty: bool = False,
) -> int:
from poetry.core.vcs.git import GitConfig
=======
allow_interactive: bool = True,
layout_name: str = "standard",
readme_format: str = "md",
create_layout_if_empty: bool = False,
) -> int:
"""
Initializes a new Poetry project in the specified directory.
Parameters:
allow_interactive (bool): If True, allows interactive prompts during initialization.
layout_name (str): The name of the project layout to use.
readme_format (str): The format for the README file.
create_layout_if_empty (bool): If True, the project layout will be created if the target
project path exists and is empty. Defaults to False. If False, the layout is only
created if the project path does not exist.
When `create_layout_if_empty` is True and the specified project path exists but contains no files,
the command will proceed to create the project layout as if the directory were new. This helps
avoid errors or confusion when initializing in an empty directory.
"""
from poetry.core.vcs.git import GitConfig
>>>>>>> REPLACE
</suggested_fix>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
|
I’ve renamed the flag to something more descriptive. I decided not to extract the not any(project_path.iterdir()) check into a helper like Path.is_empty(), since it's only used once and introducing a helper felt unnecessary in this context. Regarding the suggestion about dotfiles: in this case, directories containing dotfiles are deliberately treated as non-empty, which is intentional. Happy to revisit either the helper function or the dotfile treatment if maintainers feel strongly about it. |
|
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. |
Pull Request Check List
Resolves: #10429
Fix
poetry newbehavior when used in an existing empty directoryThis pull request addresses unexpected behavior when running
poetry newin an already existing empty directory, including the current directory (.). It ensures thatpoetry newhandles these cases correctly, while maintaining the expected behavior ofpoetry init.The fix introduces a new argument,
create_layout_if_empty, to the_init_pyprojectmethod in theInitCommandclass. Additionally, it adds a conditional check to determine whether a full project layout should be created or only apyproject.tomlfile.Summary by Sourcery
Ensure 'poetry new' generates full project layout in existing empty directories while 'poetry init' only creates a pyproject.toml in such cases
Bug Fixes:
Enhancements:
Tests: