Repository navigation
fix(init): sanitize default package name derived from directory name - #10975
Conversation
There was a problem hiding this comment.
Hey - I've found 1 issue, and left some high level feedback:
- Consider handling the edge case where the sanitized name becomes empty after replacing invalid characters and stripping hyphens, as this could happen for directory names containing only disallowed characters.
- The
_sanitize_package_namedocstring currently describes the allowed character set for names but the canonicalization step produces only lowercase letters, numbers, and hyphens; clarifying that distinction would make the helper’s behavior easier to understand at a glance.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Consider handling the edge case where the sanitized name becomes empty after replacing invalid characters and stripping hyphens, as this could happen for directory names containing only disallowed characters.
- The `_sanitize_package_name` docstring currently describes the allowed character set for names but the canonicalization step produces only lowercase letters, numbers, and hyphens; clarifying that distinction would make the helper’s behavior easier to understand at a glance.
## 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 = self._sanitize_package_name(project_path.name)
if is_interactive:
</code_context>
<issue_to_address>
**issue:** Consider handling the case where sanitization produces an empty package name.
If `_sanitize_package_name` returns an empty string (e.g., when the directory name consists only of stripped characters), subsequent logic will operate on an invalid/empty project name. Please add a fallback for this case, such as raising an error or defaulting to a safe default name.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
|
The |
|
On the second point from the review — the docstring — you were right, and it was wrong rather than merely unclear. Fixed in a93ff3e. It claimed the return value conforms to the PyPA name format, "only ASCII letters, numbers, period, underscore and hyphen".
The last row is the other half of it: the fallback added in 5818d36 deliberately returns a value that does not conform to the format, which the old docstring flatly contradicted. The rewrite states both — that the conforming path is narrower than the format allows, and that the fallback is the one exception, by design. One thing worth flagging for a human reviewer, since it looks like an oversight and isn't: a directory named
|
a93ff3e to
208ff4f
Compare
|
Checking back — did the docstring fix address what you were looking for, or is there another round needed? |
c3d2446 to
7dd2474
Compare
|
no worries, went with full canonicalization |
`poetry init`/`poetry new` used the current directory's name verbatim as the default package name, which can contain spaces or other characters that are invalid per the PyPA name format (only ASCII letters, numbers, period, underscore and hyphen are allowed). This produced a `pyproject.toml` with an invalid `[project] name`, e.g. "my project with spaces" instead of "my-project-with-spaces". Sanitize the derived name the same way `packaging.utils.canonicalize_name` normalizes existing valid names, after first replacing any disallowed characters (including whitespace) with a hyphen. Co-Authored-By: Claude Sonnet 5 <[email protected]>
A directory whose name holds no ASCII alphanumerics — any non-Latin script, for instance — sanitized to an empty string, so `poetry init` offered an empty package name. Before this branch such a directory yielded its lowercased name, so this was a regression introduced here. Fall back to the lowercased directory name whenever sanitization leaves nothing behind.
The docstring promised the full PyPA name format, but canonicalize_name collapses period and underscore into hyphens and lowercases, so the return value is narrower. The fallback for names without ASCII alphanumerics is also outside the format by design.
5c01698 to
4067fae
Compare
This PR contains the following updates: | Package | Update | Change | Pending | |---|---|---|---| | [poetry](https://github.com/python-poetry/poetry) ([changelog](https://python-poetry.org/history/)) | minor | `2.4.3` → `2.5.0` | `2.5.1` | --- ### Release Notes <details> <summary>python-poetry/poetry (poetry)</summary> ### [`v2.5.0`](https://github.com/python-poetry/poetry/blob/HEAD/CHANGELOG.md#250---2026-09-19) [Compare Source](python-poetry/poetry@2.4.3...2.5.0) ##### Added - Add an `installer.builtin-uninstall` setting to uninstall packages with a built-in uninstaller instead of invoking `pip uninstall` ([#​10931](python-poetry/poetry#10931)). - Add official support for Python 3.15 ([#​11046](python-poetry/poetry#11046)). ##### Changed - Do not send credentials configured for an `https` repository via `http` ([#​11073](python-poetry/poetry#11073)). - Fail with an error when the current Python version is not compatible with the project and `virtualenvs.create` is `false` ([#​10941](python-poetry/poetry#10941)). - Validate version constraints that are entered interactively in `poetry init` ([#​10909](python-poetry/poetry#10909)). - Include the path of the `pyproject.toml` file in the message about already present packages in `poetry add` ([#​10908](python-poetry/poetry#10908)). - Improve performance of processing package links and repository pages ([#​10895](python-poetry/poetry#10895), [#​10896](python-poetry/poetry#10896), [#​10903](python-poetry/poetry#10903), [#​10949](python-poetry/poetry#10949), [#​10951](python-poetry/poetry#10951), [#​10953](python-poetry/poetry#10953)). - Improve performance of dependency resolution ([#​10907](python-poetry/poetry#10907), [#​10954](python-poetry/poetry#10954)). - Improve performance of choosing and installing wheels ([#​10905](python-poetry/poetry#10905), [#​10958](python-poetry/poetry#10958)). - Improve performance by avoiding redundant keyring lookups for repositories without credentials ([#​10959](python-poetry/poetry#10959)). - Improve performance by reducing the number of subprocesses to discover virtual environment data ([#​11042](python-poetry/poetry#11042)). - Improve performance of `poetry search` for single-token queries ([#​10906](python-poetry/poetry#10906)). - Improve startup time by deferring the import of `requests` ([#​11004](python-poetry/poetry#11004)). - Improve performance of schema validation by caching compiled JSON schema validators ([#​11033](python-poetry/poetry#11033)). ##### Fixed - Fix an issue where credentials of the wrong repository were used under certain circumstances when multiple repositories were configured on the same host ([#​11072](python-poetry/poetry#11072)). - Fix an issue where credentials of a repository on another host were used for git dependencies if the path of the URL was the same ([#​11074](python-poetry/poetry#11074)). - Fix an issue where dependency resolution failed for conflicting requirements of different packages even though the requirements had mutually exclusive markers ([#​10944](python-poetry/poetry#10944)). - Fix an issue where dependency resolution failed when the same package was required with different extras in several optional dependencies or dependency groups ([#​10943](python-poetry/poetry#10943)). - Fix an issue where dependency resolution failed with a `KeyError` ([#​11008](python-poetry/poetry#11008)). - Fix an issue where the dependencies of an extra were missing in the lock file after adding the extra to a locked dependency, e.g. a git dependency, in the `pyproject.toml` file ([#​10987](python-poetry/poetry#10987)). - Fix an issue where a path or git dependency was not reinstalled when its `develop` setting changed ([#​11022](python-poetry/poetry#11022)). - Fix an issue where scripts of type `file` were not installed when installing the project ([#​10736](python-poetry/poetry#10736)). - Fix an issue where GUI scripts were not installed when installing the project ([#​10973](python-poetry/poetry#10973)). - Fix an issue where a relative path was written to `direct_url.json` for path dependencies ([#​10917](python-poetry/poetry#10917)). - Fix an issue where `poetry show <package>` showed a version that was not relevant for the current environment if there were multiple versions of the package in the lock file ([#​11003](python-poetry/poetry#11003)). - Fix an issue where `poetry show --outdated` did not find newer versions of packages from sources with `explicit` priority ([#​10982](python-poetry/poetry#10982)). - Fix an issue where `poetry env activate` ignored the environment that was determined by the application, e.g. when using `--directory` ([#​10916](python-poetry/poetry#10916)). - Fix an issue where `poetry init` proposed an invalid package name if the directory name was not a valid package name ([#​10975](python-poetry/poetry#10975)). ##### Docs - Document the `--license` option of `poetry init` and `poetry new` ([#​11064](python-poetry/poetry#11064)). - Clarify which dependencies are locked when running `poetry update` with dependency groups ([#​11024](python-poetry/poetry#11024)). - Clarify the portability of path dependencies ([#​11020](python-poetry/poetry#11020)). - Clarify the usage of `poetry run` with console scripts ([#​10984](python-poetry/poetry#10984)). - Clarify what `--no-cache` disables ([#​10915](python-poetry/poetry#10915)). - Document how to use package sources for `poetry self update` ([#​10923](python-poetry/poetry#10923)). - Fix the stale minimum Python version ([#​11050](python-poetry/poetry#11050)). - Update outdated links ([#​10913](python-poetry/poetry#10913), [#​10938](python-poetry/poetry#10938), [#​11000](python-poetry/poetry#11000), [#​11043](python-poetry/poetry#11043)). ##### poetry-core ([`2.5.0`](https://github.com/python-poetry/poetry-core/releases/tag/2.5.0)) - Add Python 3.15 to the automatically generated classifiers ([#​961](python-poetry/poetry-core#961)). - Fix an issue where a `<V` version constraint wrongly allowed pre-releases of `V` in some cases ([#​939](python-poetry/poetry-core#939)). - Fix an issue where version ranges with coincident bounds were not recognized as empty ([#​939](python-poetry/poetry-core#939)). - Fix an issue where the string representation of a version union did not describe the same constraint after being parsed again ([#​939](python-poetry/poetry-core#939)). - Fix an issue where the intersection of a version range with a local version resulted in a wrong constraint ([#​949](python-poetry/poetry-core#949)). - Fix an issue where the union of a version range and a public version did not include all local versions of the public version ([#​950](python-poetry/poetry-core#950)). - Fix an issue where the union of a public version and one of its local versions did not result in the public version ([#​966](python-poetry/poetry-core#966)). - Fix an issue where the difference between a public version and one of its local versions still allowed the local version, which could result in an infinite loop during dependency resolution ([#​953](python-poetry/poetry-core#953)). - Fix an issue where a version range that excluded some local versions of a public version was wrongly considered to allow all versions of the public version ([#​959](python-poetry/poetry-core#959)). - Fix an issue where a `!= <value>` constraint was wrongly considered to allow all values of a `<value> not in` constraint ([#​955](python-poetry/poetry-core#955)). - Fix an issue where merging `platform_release` markers with incompatible constraint types failed ([#​956](python-poetry/poetry-core#956)). - Fix an issue where the string representation of a version range whose upper bound only consists of zeros raised an `IndexError` ([#​964](python-poetry/poetry-core#964)). - Fix an issue where formatting a Python constraint that only allows unknown Python versions raised an `IndexError` ([#​971](python-poetry/poetry-core#971)). - Fix an issue where marker values containing spaces could not be parsed ([#​972](python-poetry/poetry-core#972)). - Fix an issue where the upper bound of a `~=` constraint was wrong for versions with more than three release segments ([#​973](python-poetry/poetry-core#973)). - Fix an issue where the filename of a link created from a Windows path was wrong and reject filenames containing path separators ([#​974](python-poetry/poetry-core#974)). </details> --- ### Configuration 📅 **Schedule**: (UTC) - Branch creation - At any time (no schedule defined) - Automerge - At any time (no schedule defined) 🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied. ♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR has been generated by [Mend Renovate](https://github.com/renovatebot/renovate). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0My4yNjAuMCIsInVwZGF0ZWRJblZlciI6IjQzLjI2MC4wIiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6W119--> Reviewed-on: https://git.walbeck.it/mwalbeck/docker-python-poetry/pulls/1762
|
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. |
Summary
Closes #10974.
poetry init/poetry new(which shares the same code path) used the current directory's name verbatim as the default package name. Per the PyPA name normalization spec, a valid name may only contain ASCII letters, numbers, period, underscore and hyphen — so a directory likemy project with spacesproduced apyproject.tomlwith an invalidname = "my project with spaces"instead ofname = "my-project-with-spaces".This adds a small
InitCommand._sanitize_package_namehelper that replaces any disallowed character (including whitespace) with a hyphen, then reusespackaging.utils.canonicalize_name(already a dependency, already used elsewhere in this file for normalizing dependency names) to collapse/lowercase the result the same way it normalizes any other valid package name.This only changes the default value derived from the directory name; an explicit
--name/interactive value is left as-is, same as before.Test plan
test_sanitize_package_name— parametrized unit test covering the reported case plus a few edge cases (repeated separators, mixed./_, leading/trailing separators).test_noninteractive_sanitizes_default_name_from_directory— reproduces the exact scenario from the issue (poetry initrun non-interactively in a directory namedmy project with spaces), asserts the generatedpyproject.tomlhas a validname.pytest tests/console/commands/test_init.py- 73 passedpytest tests/console/commands/test_new.py- 24 passed (unaffected,NewCommandshares this code path)ruff check/ruff format --check- cleanmypy src/poetry/console/commands/init.py- no issuesAI disclosure
This PR was written with the help of Claude Code (Claude Sonnet 5). All code was reviewed and tested by me before submission.