Skip to content

Commit b2dafaa

Browse files
Handle unspecified python versioning on Pipfile
1 parent 951c382 commit b2dafaa

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

news/6164.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Pipenv only supports absolute python version. If the user specifies a Python version with inequality signs like >=3.12, <3.12 in the [requires] field, the code has been modified to explicitly express in an error log that absolute versioning must be used.

pipenv/utils/virtualenv.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import contextlib
22
import os
3+
import re
34
import shutil
45
import sys
56
from pathlib import Path
@@ -237,6 +238,22 @@ def abort(msg=""):
237238
# Find out which python is desired.
238239
if not python:
239240
python = project.required_python_version
241+
if python:
242+
range_pattern = r"^[<>]=?|!="
243+
if re.search(range_pattern, python):
244+
click.echo(
245+
"{}: Python version range specifier '{}' is not supported. {}".format(
246+
click.style("Error", fg="red", bold=True),
247+
click.style(python, fg="cyan"),
248+
click.style(
249+
"Please use an absolute version number or specify the path to the Python executable on Pipfile.",
250+
fg="yellow",
251+
),
252+
),
253+
err=True,
254+
)
255+
abort()
256+
240257
if not python:
241258
python = project.s.PIPENV_DEFAULT_PYTHON_VERSION
242259
path_to_python = find_a_system_python(python)

tests/integration/test_install_basic.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,64 @@ def test_create_pipfile_requires_python_full_version(pipenv_instance_private_pyp
403403
'python_version': python_version
404404
}
405405

406+
@pytest.mark.basic
407+
@pytest.mark.install
408+
@pytest.mark.virtualenv
409+
def test_install_with_pipfile_including_exact_python_version(pipenv_instance_pypi):
410+
valid_version = f"{sys.version_info.major}.{sys.version_info.minor}"
411+
412+
with pipenv_instance_pypi() as p:
413+
with open(p.pipfile_path, 'w') as f:
414+
f.write(f"""
415+
[[source]]
416+
url = "https://test.pypi.org/simple"
417+
verify_ssl = true
418+
name = "testpypi"
419+
420+
[packages]
421+
pytz = "*"
422+
423+
[requires]
424+
python_version = "{valid_version}"
425+
""")
426+
427+
c = p.pipenv("install")
428+
assert c.returncode == 0
429+
assert os.path.isfile(p.pipfile_path)
430+
assert p.pipfile["requires"]["python_version"] == valid_version
431+
432+
c = p.pipenv("--rm")
433+
assert c.returncode == 0
434+
435+
@pytest.mark.basic
436+
@pytest.mark.install
437+
@pytest.mark.virtualenv
438+
def test_install_with_pipfile_including_invalid_python_version(pipenv_instance_pypi):
439+
invalid_versions = [
440+
f">={sys.version_info.major}.{sys.version_info.minor}",
441+
f"<={sys.version_info.major}.{sys.version_info.minor}",
442+
f">{sys.version_info.major}.{sys.version_info.minor}",
443+
f"<{sys.version_info.major}.{sys.version_info.minor}",
444+
]
445+
446+
with pipenv_instance_pypi() as p:
447+
for version in invalid_versions:
448+
with open(p.pipfile_path, 'w') as f:
449+
f.write(f"""
450+
[[source]]
451+
url = "https://test.pypi.org/simple"
452+
verify_ssl = true
453+
name = "testpypi"
454+
455+
[packages]
456+
pytz = "*"
457+
458+
[requires]
459+
python_version = "{version}"
460+
""")
461+
c = p.pipenv("install --verbose")
462+
assert c.returncode != 0
463+
406464

407465
@pytest.mark.basic
408466
@pytest.mark.install

0 commit comments

Comments
 (0)