Skip to content

Commit

Permalink
Lessons fromt testing this code against the pipenv integrations--simp…
Browse files Browse the repository at this point in the history
…lifying assumptions.
  • Loading branch information
matteius committed Jan 26, 2024
1 parent c3746ef commit 627bd73
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 28 deletions.
17 changes: 5 additions & 12 deletions src/pythonfinder/models/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
if TYPE_CHECKING:
from pathlib import Path

from pythonfinder.models.python import PythonVersion
from .python import PythonVersion


@dataclasses.dataclass(unsafe_hash=True)
Expand Down Expand Up @@ -292,17 +292,10 @@ def version_matcher(py_version):
if self.is_python and self.as_python and version_matcher(self.py_version):
return self

matching_pythons = [
[entry, entry.as_python.version_sort]
for entry in self._iter_pythons()
if (
entry is not None
and entry.as_python is not None
and version_matcher(entry.py_version)
)
]
results = sorted(matching_pythons, key=lambda r: (r[1], r[0]), reverse=True)
return next(iter(r[0] for r in results if r is not None), None)
for entry in self._iter_pythons():
if entry is not None and entry.as_python is not None:
if version_matcher(entry.as_python):
return entry

def _filter_children(self) -> Iterator[Path]:
if not os.access(str(self.path), os.R_OK):
Expand Down
12 changes: 5 additions & 7 deletions src/pythonfinder/models/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,17 +376,15 @@ def which(self, executable) -> PathEntry | None:

def _filter_paths(self, finder) -> Iterator:
for path in self._get_paths():
if path is None:
if not path:
continue
python_versions = finder(path)
if python_versions is not None:
for python in python_versions:
if python is not None:
yield python
python_version = finder(path)
if python_version:
yield python_version

def _get_all_pythons(self, finder) -> Iterator:
for python in self._filter_paths(finder):
if python is not None and python.is_python:
if python:
yield python

def get_pythons(self, finder) -> Iterator:
Expand Down
23 changes: 14 additions & 9 deletions src/pythonfinder/models/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,11 +282,13 @@ def version_sort(path_entry):
return path_entry.as_python.version_sort

unnested = [sub_finder(self.roots[path]) for path in self.roots]
print(f"unnested: {unnested}")
unnested = [
p
for p in unnested
if p is not None and p.is_python and p.as_python is not None
]
print(unnested)
paths = sorted(list(unnested), key=version_sort, reverse=True)
return next(iter(p for p in paths if p is not None), None)

Expand Down Expand Up @@ -390,23 +392,25 @@ def version_tuple(self) -> tuple[int, int, int, bool, bool, bool]:

def matches(
self,
major: int | None = None,
minor: int | None = None,
patch: int | None = None,
pre: bool = False,
dev: bool = False,
arch: str | None = None,
debug: bool = False,
python_name: str | None = None,
) -> bool:
major=None,
minor=None,
patch=None,
pre=False,
dev=False,
arch=None,
debug=False,
python_name=None,
):
result = False
if arch:
own_arch = self.get_architecture()
if arch.isdigit():
arch = f"{arch}bit"

if (
(major is None or self.major == major)
and (minor is None or self.minor == minor)
# Check if patch is None OR self.patch equals patch
and (patch is None or self.patch == patch)
and (pre is None or self.is_prerelease == pre)
and (dev is None or self.is_devrelease == dev)
Expand All @@ -419,6 +423,7 @@ def matches(
)
):
result = True

return result

def as_major(self) -> PythonVersion:
Expand Down

0 comments on commit 627bd73

Please sign in to comment.