Skip to content

Commit e6119d3

Browse files
r-b-g-bjayqi
andauthored
Resolve python version to work with compatible operator (#446)
* Resolve python version to work with compatible operator * Fix version specifier resolution * Format * Test virtual environment Python version Use uv in CI Fix python version of created environment in tests Pipenv should create its own virtualenv Remove sys import Remove extraneous parenthesis Fix wrong input to configs product Fix virtualenvwrapper to set Python version instead of Python interpreter Install Python executables into PATH * Revert "Test virtual environment Python version" This reverts commit f16bcc7. --------- Co-authored-by: Jay Qi <jayqi@users.noreply.github.com>
1 parent 3cfe39f commit e6119d3

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

ccds/hook_utils/dependencies.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,49 @@
3030
]
3131

3232

33+
def resolve_python_version_specifier(python_version):
34+
"""Resolves the user-provided Python version string to a version specifier.
35+
36+
Examples:
37+
38+
User provides: 3.12
39+
Resolved version specifier: ~=3.12.0
40+
Compatible versions: 3.12.0, 3.12.1, 3.12.2, etc.
41+
42+
User provides: 3.12.2
43+
Resolved version specifier: ==3.12.2
44+
Compatible versions: 3.12.2
45+
46+
See https://packaging.python.org/en/latest/specifications/version-specifiers/#compatible-release
47+
"""
48+
version_parts = python_version.split(".")
49+
if len(version_parts) == 2:
50+
major, minor = version_parts
51+
patch = "0"
52+
operator = "~="
53+
elif len(version_parts) == 3:
54+
major, minor, patch = version_parts
55+
operator = "=="
56+
else:
57+
raise ValueError(
58+
f"Invalid Python version specifier {python_version}. "
59+
"Please specify version as <major>.<minor> or <major>.<minor>.<patch>, "
60+
"e.g., 3.10, 3.10.1, etc."
61+
)
62+
63+
resolved_python_version = ".".join((major, minor, patch))
64+
return f"{operator}{resolved_python_version}"
65+
66+
67+
def write_python_version(python_version):
68+
with open("pyproject.toml", "r") as f:
69+
doc = tomlkit.parse(f.read())
70+
71+
doc["project"]["requires-python"] = resolve_python_version_specifier(python_version)
72+
with open("pyproject.toml", "w") as f:
73+
f.write(tomlkit.dumps(doc))
74+
75+
3376
def write_dependencies(
3477
dependencies, packages, pip_only_packages, repo_name, module_name, python_version
3578
):

hooks/post_gen_project.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
ruff,
1313
scaffold,
1414
write_dependencies,
15+
write_python_version,
1516
)
1617

1718
#
@@ -72,6 +73,8 @@
7273
python_version="{{ cookiecutter.python_version_number }}",
7374
)
7475

76+
write_python_version("{{ cookiecutter.python_version_number }}")
77+
7578
write_custom_config("{{ cookiecutter.custom_config }}")
7679

7780
# Remove LICENSE if "No license file"

{{ cookiecutter.repo_name }}/pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ classifiers = [
1515
"Programming Language :: Python :: 3",
1616
{% if cookiecutter.open_source_license == 'MIT' %}"License :: OSI Approved :: MIT License"{% elif cookiecutter.open_source_license == 'BSD-3-Clause' %}"License :: OSI Approved :: BSD License"{% endif %}
1717
]
18-
requires-python = "~={{ cookiecutter.python_version_number }}"
1918
{% if cookiecutter.linting_and_formatting == 'flake8+black+isort' %}
2019
[tool.black]
2120
line-length = 99

0 commit comments

Comments
 (0)