Fix init default project name normalization - #11023
Conversation
Normalize whitespace in the inferred project name so non-interactive init does not create invalid names when the current directory contains spaces. Closes python-poetry#10974 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: arpan sahu <28574248+arpansahu@users.noreply.github.com>
There was a problem hiding this comment.
Hey - I've found 1 issue
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="src/poetry/console/commands/init.py" line_range="131" />
<code_context>
name = self.option("name")
if not name:
- name = project_path.name.lower()
+ name = canonicalize_name(re.sub(r"\s+", "-", project_path.name.strip()))
if is_interactive:
</code_context>
<issue_to_address>
**issue (broader_impact):** Interactive `poetry init` now changes the inferred default package name for directories containing whitespace before displaying it and accepting the answer. This changes the existing interactive flow despite the stated requirement that only non-interactive default-name inference change.
**Triggers:** When `poetry init` is interactive, `--name` is omitted, and the current directory name contains whitespace.
**Suggested fix:** Apply the whitespace normalization only to the non-interactive default path, or preserve the prior inferred value for the interactive prompt.
</issue_to_address>Sourcery assessment
Needs a human reviewer. 1 finding to address first, and if the normalization is wrong, init writes an incorrect project/distribution name into the generated pyproject.toml, and reverting the code will not change files already created. The value is bounded and can be corrected by editing or regenerating the project metadata.
Blocking findings: src/poetry/console/commands/init.py:131
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
| name = self.option("name") | ||
| if not name: | ||
| name = project_path.name.lower() | ||
| name = canonicalize_name(re.sub(r"\s+", "-", project_path.name.strip())) |
There was a problem hiding this comment.
issue (broader_impact): Interactive poetry init now changes the inferred default package name for directories containing whitespace before displaying it and accepting the answer. This changes the existing interactive flow despite the stated requirement that only non-interactive default-name inference change.
Triggers: When poetry init is interactive, --name is omitted, and the current directory name contains whitespace.
Suggested fix: Apply the whitespace normalization only to the non-interactive default path, or preserve the prior inferred value for the interactive prompt.
|
Disclosure: this contribution was prepared with AI assistance (GitHub Copilot CLI), and The defect, the fix and the regression test were verified locally: the new test fails on If your project would prefer not to take AI-assisted contributions, or you would rather this |
|
explain why not #10975 |
|
@dimbleby fair point — #10975 is the more complete fix. It sanitizes every character disallowed by the PyPA name spec via a dedicated helper, whereas this PR only normalizes whitespace (so names like |
Problem
poetry init -ninfers the project name from the current directory when--nameis not provided. If that directory contains spaces, Poetry writes an invalid package name such asmy project with spaceseven though project names must use only letters, digits,.,_, or-.Reproducer
Expected:
Fix
Normalize whitespace to hyphens before canonicalizing the default name inferred from the current directory. Explicit
--namevalues and the existing interactive flow remain unchanged.Closes #10974
Testing