Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/3852.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix importing a Poetry project failing when a dependency uses a single-component tilde constraint such as `~1`, which was converted to the invalid PEP 440 specifier `~=1`.
5 changes: 5 additions & 0 deletions src/pdm/formats/poetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ def _convert_specifier(version: str) -> str:
parts = []
for op, ver in VERSION_RE.findall(str(version)):
if op == "~":
if "." not in ver:
# ``~=1`` is not a valid PEP 440 specifier, and Poetry's ``~1``
# allows the minor version to change, so it means ``>=1,<2``.
parts.append(f">={ver},<{_caret_upper_bound(ver)}")

@frostming frostming Aug 18, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

How about changing to ~=1.0, i think they are equivalent

continue
op += "="
elif op == "^":
parts.append(f">={ver},<{_caret_upper_bound(ver)}")
Expand Down
21 changes: 21 additions & 0 deletions tests/test_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,27 @@ def test_convert_poetry_caret_constraint(project, constraint, expected):
assert result["dependencies"] == [f"foo{expected}"]


@pytest.mark.parametrize(
"constraint,expected",
[
# ``~=1`` is not a valid PEP 440 specifier, Poetry's ``~1`` means ``>=1,<2``.
("~1", "<2,>=1"),
("~0", "<1,>=0"),
("~1.2", "~=1.2"),
("~1.2.3", "~=1.2.3"),
],
)
def test_convert_poetry_tilde_constraint(project, constraint, expected):
pyproject = project.root / "pyproject.toml"
pyproject.write_text(
f'[tool.poetry]\nname = "demo"\nversion = "0.1.0"\n[tool.poetry.dependencies]\nfoo = "{constraint}"\n',
encoding="utf-8",
)
result, _ = poetry.convert(project, pyproject, ns())

assert result["dependencies"] == [f"foo{expected}"]


def test_convert_poetry_12(project):
golden_file = FIXTURES / "poetry-new.toml"
with cd(FIXTURES):
Expand Down
Loading