From ed5149b3edf815a0ab6349e39a853978f7c2570b Mon Sep 17 00:00:00 2001 From: Maxwell G Date: Aug 06 2024 21:59:54 +0000 Subject: lint all: adjust type hints - Always convert "reldep" objects to strings instead of returning Package.requires directly. - Use more specific return types in DnfQuery implementation subclass --- diff --git a/pkg_depgraph/dnf.py b/pkg_depgraph/dnf.py index 4fd4fb5..71808dc 100644 --- a/pkg_depgraph/dnf.py +++ b/pkg_depgraph/dnf.py @@ -12,12 +12,11 @@ # . from collections.abc import Collection -from typing import Protocol from fedrq.backends.base import PackageCompat, RepoqueryBase from fedrq.config import RQConfig, get_config -from .protocols import SourcePackageQuery, BinaryPackageQuery, Package +from .protocols import BinaryPackageQuery, SourcePackageQuery class DnfQuery(SourcePackageQuery, BinaryPackageQuery): @@ -26,24 +25,27 @@ class DnfQuery(SourcePackageQuery, BinaryPackageQuery): config: RQConfig = get_config(backend="libdnf5") self._rq: RepoqueryBase = config.get_rq(release) - def get_build_reqs(self, pkg_name: str) -> Collection[object]: + def get_build_reqs(self, pkg_name: str) -> Collection[str]: pkg = self.get_src_pkg(pkg_name) - return pkg.requires + # Convert reldep objects to strings for consistency + return [str(req) for req in pkg.requires] - def get_install_reqs(self, pkg_name: str) -> Collection[object]: + def get_install_reqs(self, pkg_name: str) -> Collection[str]: pkgs = self.get_pkg_names([pkg_name]) - return [req for pkg in pkgs for req in pkg.requires] + return [str(req) for pkg in pkgs for req in pkg.requires] - def get_pkg_names(self, source_pkg_names: Collection[str]) -> Collection[Package]: + # ignore[override] because mypy doesn't understand that the PackageCompat + # ABC matches the Package Protocol + def get_pkg_names( # type: ignore[override] + self, source_pkg_names: Collection[str] + ) -> Collection[PackageCompat]: srpms = self._rq.resolve_pkg_specs(source_pkg_names) srpms.filterm(arch="src") - query = self._rq.get_subpackages( - srpms, - ) + query = self._rq.get_subpackages(srpms) return sorted(pkg for pkg in query) - def get_src_pkg(self, pkg_name: str) -> Package: - srpms = self._rq.query(name=pkg_name, arch="src") + def get_src_pkg(self, pkg_name: str) -> PackageCompat: + srpms = self._rq.query(name=pkg_name, arch="src", latest=1) pkgs = [p for p in srpms] if len(pkgs) != 1: raise LookupError(f"Error: expected unique result, found {pkgs}") diff --git a/pkg_depgraph/protocols.py b/pkg_depgraph/protocols.py index 2f9efbb..571b897 100644 --- a/pkg_depgraph/protocols.py +++ b/pkg_depgraph/protocols.py @@ -31,7 +31,7 @@ class Package(Protocol): class SourcePackageQuery(Protocol): - def get_build_reqs(self, pkg: str) -> Collection[object]: ... + def get_build_reqs(self, pkg: str) -> Collection[str]: ... class BinaryPackageQuery(Protocol): @@ -39,4 +39,4 @@ class BinaryPackageQuery(Protocol): self, source_pkg_names: Collection[str] ) -> Collection[Package]: ... - def get_install_reqs(self, pkg: str) -> Collection[object]: ... + def get_install_reqs(self, pkg: str) -> Collection[str]: ...