Skip to content

Fix init default project name normalization - #11023

Closed
arpansahu wants to merge 1 commit into
python-poetry:mainfrom
arpansahu:fix/init-normalize-default-name
Closed

Fix init default project name normalization#11023
arpansahu wants to merge 1 commit into
python-poetry:mainfrom
arpansahu:fix/init-normalize-default-name

Conversation

@arpansahu

Copy link
Copy Markdown

Problem

poetry init -n infers the project name from the current directory when --name is not provided. If that directory contains spaces, Poetry writes an invalid package name such as my project with spaces even though project names must use only letters, digits, ., _, or -.

Reproducer

PS> mkdir "my project with spaces"; cd "my project with spaces"
PS> poetry init -n
PS> Select-String 'name =' pyproject.toml
name = "my project with spaces"  # invalid

Expected:

name = "my-project-with-spaces"

Fix

Normalize whitespace to hyphens before canonicalizing the default name inferred from the current directory. Explicit --name values and the existing interactive flow remain unchanged.

Closes #10974

Testing

PS> .\.venv\Scripts\python -m pytest tests\console\commands\test_init.py::test_noninteractive_normalizes_default_project_name -q
# before fix: FAILED, wrote name = "my project with spaces"
# after fix: 1 passed in 9.18s

PS> .\.venv\Scripts\python -m pytest tests\console\commands\test_init.py -q
68 passed in 11.97s

PS> .\.venv\Scripts\python -m pre_commit run --files src\poetry\console\commands\init.py tests\console\commands\test_init.py
Passed

PS> .\.venv\Scripts\python -m mypy src\poetry\console\commands\init.py tests\console\commands\test_init.py
Success: no issues found in 2 source files

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>

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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


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.

name = self.option("name")
if not name:
name = project_path.name.lower()
name = canonicalize_name(re.sub(r"\s+", "-", project_path.name.strip()))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@arpansahu

Copy link
Copy Markdown
Author

Disclosure: this contribution was prepared with AI assistance (GitHub Copilot CLI), and
I am flagging that explicitly rather than leaving you to guess.

The defect, the fix and the regression test were verified locally: the new test fails on
unmodified upstream and passes with the change applied, and the surrounding suite is green.

If your project would prefer not to take AI-assisted contributions, or you would rather this
were reworked and resubmitted by hand, please just close it -- no objection at all, and
apologies for the noise.

@dimbleby

Copy link
Copy Markdown
Contributor

explain why not #10975

@arpansahu

Copy link
Copy Markdown
Author

@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 my@project would still come out invalid). It's also the earlier PR and already through a review round. Happy to defer — closing this in favor of #10975. Thanks for the pointer, and sorry for the duplication.

@arpansahu arpansahu closed this Sep 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

init generates project with spaces in name

2 participants