python-pdm/findpython-022.patch

101 lines
3.2 KiB
Diff

From f8ec8b7bda1b88e60b554b5713ca615c9bf94367 Mon Sep 17 00:00:00 2001
From: Frost Ming <mianghong@gmail.com>
Date: Wed, 16 Nov 2022 09:25:21 +0800
Subject: [PATCH] fix: test against the latest findpython
---
news/1516.bugfix.md | 1 +
src/pdm/cli/actions.py | 16 +++++-----------
src/pdm/models/python.py | 11 +++++++----
tests/cli/test_use.py | 4 ++--
4 files changed, 15 insertions(+), 17 deletions(-)
create mode 100644 news/1516.bugfix.md
--- /dev/null
+++ b/news/1516.bugfix.md
@@ -0,0 +1 @@
+Fix the test failure with the latest `findpython` installed.
--- a/src/pdm/cli/actions.py
+++ b/src/pdm/cli/actions.py
@@ -31,12 +31,7 @@ from pdm.cli.utils import (
set_env_in_reg,
translate_groups,
)
-from pdm.exceptions import (
- InvalidPyVersion,
- NoPythonVersion,
- PdmUsageError,
- ProjectError,
-)
+from pdm.exceptions import NoPythonVersion, PdmUsageError, ProjectError
from pdm.formats import FORMATS
from pdm.formats.base import array_of_inline_tables, make_array, make_inline_table
from pdm.models.caches import JSONFileCache
@@ -584,8 +579,10 @@ def do_use(
python = python.strip()
def version_matcher(py_version: PythonInfo) -> bool:
- return ignore_requires_python or project.python_requires.contains(
- str(py_version.version), True
+ return (
+ ignore_requires_python
+ or py_version.valid
+ and project.python_requires.contains(str(py_version.version), True)
)
if not project.cache_dir.exists():
@@ -647,9 +644,6 @@ def do_use(
if python:
use_cache.set(python, selected_python.path.as_posix())
- if not selected_python.valid:
- path = str(selected_python.path)
- raise InvalidPyVersion(f"Invalid Python interpreter: {path}")
if not save:
return selected_python
old_python = (
--- a/src/pdm/models/python.py
+++ b/src/pdm/models/python.py
@@ -5,7 +5,7 @@ from pathlib import Path
from typing import Any
from findpython import PythonVersion
-from packaging.version import Version
+from packaging.version import InvalidVersion, Version
from pdm.utils import cached_property
@@ -72,6 +72,9 @@ class PythonInfo:
@property
def identifier(self) -> str:
- if os.name == "nt" and self.is_32bit:
- return f"{self.major}.{self.minor}-32"
- return f"{self.major}.{self.minor}"
+ try:
+ if os.name == "nt" and self.is_32bit:
+ return f"{self.major}.{self.minor}-32"
+ return f"{self.major}.{self.minor}"
+ except InvalidVersion:
+ return "unknown"
--- a/tests/cli/test_use.py
+++ b/tests/cli/test_use.py
@@ -6,7 +6,7 @@ from pathlib import Path
import pytest
from pdm.cli import actions
-from pdm.exceptions import InvalidPyVersion
+from pdm.exceptions import NoPythonVersion
from pdm.models.caches import JSONFileCache
@@ -56,7 +56,7 @@ echo hello
shim_path = project.root.joinpath("python_shim.sh")
shim_path.write_text(wrapper_script)
shim_path.chmod(0o755)
- with pytest.raises(InvalidPyVersion):
+ with pytest.raises(NoPythonVersion):
actions.do_use(project, shim_path.as_posix())