diff --git a/rsconnect/pyproject.py b/rsconnect/pyproject.py index e52c6b7e..6252ddd9 100644 --- a/rsconnect/pyproject.py +++ b/rsconnect/pyproject.py @@ -149,7 +149,10 @@ def _adapt_contraint(constraints: typing.List[str]) -> typing.Generator[str, Non if "*" in constraint: yield f"=={constraint}" else: - yield f"~={constraint.rstrip('0').rstrip('.')}" # Remove trailing zeros and dots + # only major specified “3” → ~=3.0 → >=3.0,<4.0 + # major and minor specified “3.8” or “3.8.11” → ~=3.8.0 → >=3.8.0,<3.9.0 + constraint = ".".join(constraint.split(".")[:2] + ["0"]) + yield f"~={constraint}" return ",".join(_adapt_contraint(current_contraints)) diff --git a/tests/test_pyproject.py b/tests/test_pyproject.py index 58e3f643..2863de0c 100644 --- a/tests/test_pyproject.py +++ b/tests/test_pyproject.py @@ -149,8 +149,10 @@ def test_detect_python_version_requirement(): @pytest.mark.parametrize( # type: ignore ["content", "expected"], [ - ("3.8", "~=3.8"), - ("3.8.0", "~=3.8"), + ("3", "~=3.0"), + ("3.8", "~=3.8.0"), + ("3.8.0", "~=3.8.0"), + ("3.8.11", "~=3.8.0"), ("3.8.0b1", InvalidVersionConstraintError("pre-release versions are not supported: 3.8.0b1")), ("3.8.0rc1", InvalidVersionConstraintError("pre-release versions are not supported: 3.8.0rc1")), ("3.8.0a1", InvalidVersionConstraintError("pre-release versions are not supported: 3.8.0a1")),