diff --git a/src/pythonfinder/models/mixins.py b/src/pythonfinder/models/mixins.py index 9fdabc6..90d34b7 100644 --- a/src/pythonfinder/models/mixins.py +++ b/src/pythonfinder/models/mixins.py @@ -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) @@ -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): diff --git a/src/pythonfinder/models/path.py b/src/pythonfinder/models/path.py index 90254e5..3165d59 100644 --- a/src/pythonfinder/models/path.py +++ b/src/pythonfinder/models/path.py @@ -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: diff --git a/src/pythonfinder/models/python.py b/src/pythonfinder/models/python.py index 40f0d39..ef42448 100644 --- a/src/pythonfinder/models/python.py +++ b/src/pythonfinder/models/python.py @@ -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) @@ -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) @@ -419,6 +423,7 @@ def matches( ) ): result = True + return result def as_major(self) -> PythonVersion: