From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by finch.gentoo.org (Postfix) with ESMTPS id 2E18A15801B for ; Sat, 29 Jul 2023 03:57:54 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 5D430E0ACF; Sat, 29 Jul 2023 03:57:53 +0000 (UTC) Received: from smtp.gentoo.org (woodpecker.gentoo.org [140.211.166.183]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id 3EED1E0ACF for ; Sat, 29 Jul 2023 03:57:53 +0000 (UTC) Received: from oystercatcher.gentoo.org (oystercatcher.gentoo.org [148.251.78.52]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id 2F2B1340976 for ; Sat, 29 Jul 2023 03:57:52 +0000 (UTC) Received: from localhost.localdomain (localhost [IPv6:::1]) by oystercatcher.gentoo.org (Postfix) with ESMTP id 534D1E78 for ; Sat, 29 Jul 2023 03:57:50 +0000 (UTC) From: "Sam James" To: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: 8bit Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Sam James" Message-ID: <1690603065.003a8d8ab9aaf4a6d4d0548b1ee46f79af3caf78.sam@gentoo> Subject: [gentoo-commits] proj/portage:master commit in: lib/portage/ X-VCS-Repository: proj/portage X-VCS-Files: lib/portage/versions.py X-VCS-Directories: lib/portage/ X-VCS-Committer: sam X-VCS-Committer-Name: Sam James X-VCS-Revision: 003a8d8ab9aaf4a6d4d0548b1ee46f79af3caf78 X-VCS-Branch: master Date: Sat, 29 Jul 2023 03:57:50 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply X-Archives-Salt: ff6204e5-e771-4948-8330-3e87392be1d6 X-Archives-Hash: 97f351204eeaf98d0a148be945ae2213 commit: 003a8d8ab9aaf4a6d4d0548b1ee46f79af3caf78 Author: Andrew Udvare gmail com> AuthorDate: Sun Jul 16 21:42:37 2023 +0000 Commit: Sam James gentoo org> CommitDate: Sat Jul 29 03:57:45 2023 +0000 URL: https://gitweb.gentoo.org/proj/portage.git/commit/?id=003a8d8a versions: add basic typing Signed-off-by: Andrew Udvare gmail.com> Signed-off-by: Sam James gentoo.org> lib/portage/versions.py | 88 +++++++++++++++++++++++++++---------------------- 1 file changed, 48 insertions(+), 40 deletions(-) diff --git a/lib/portage/versions.py b/lib/portage/versions.py index f3a224eec..4b97c6124 100644 --- a/lib/portage/versions.py +++ b/lib/portage/versions.py @@ -16,8 +16,10 @@ __all__ = [ ] import re +import typing import warnings from functools import lru_cache +from typing import Any, Dict, List, Optional, Sequence, Tuple, Union import portage @@ -28,7 +30,7 @@ portage.proxy.lazyimport.lazyimport( "portage.util:cmp_sort_key", ) from portage import _unicode_decode -from portage.eapi import _get_eapi_attrs +from portage.eapi import _eapi_attrs, _get_eapi_attrs from portage.exception import InvalidData from portage.localization import _ @@ -76,7 +78,7 @@ endversion_keys = ["pre", "p", "alpha", "beta", "rc"] _slot_re_cache = {} -def _get_slot_re(eapi_attrs): +def _get_slot_re(eapi_attrs: _eapi_attrs) -> typing.re.Pattern: cache_key = eapi_attrs.slot_operator slot_re = _slot_re_cache.get(cache_key) if slot_re is not None: @@ -96,7 +98,7 @@ def _get_slot_re(eapi_attrs): _pv_re = None -def _get_pv_re(eapi_attrs): +def _get_pv_re(eapi_attrs: _eapi_attrs) -> typing.re.Pattern: global _pv_re if _pv_re is not None: return _pv_re @@ -106,7 +108,7 @@ def _get_pv_re(eapi_attrs): return _pv_re -def ververify(myver, silent=1): +def ververify(myver: str, silent: int = 1) -> bool: if ver_regexp.match(myver): return True if not silent: @@ -115,7 +117,7 @@ def ververify(myver, silent=1): @lru_cache(1024) -def vercmp(ver1, ver2, silent=1): +def vercmp(ver1: str, ver2: str, silent: int = 1) -> Optional[int]: """ Compare two versions Example usage: @@ -262,7 +264,7 @@ def vercmp(ver1, ver2, silent=1): return rval -def pkgcmp(pkg1, pkg2): +def pkgcmp(pkg1: Tuple[str, str, str], pkg2: Tuple[str, str, str]) -> Optional[int]: """ Compare 2 package versions created in pkgsplit format. @@ -289,7 +291,7 @@ def pkgcmp(pkg1, pkg2): return vercmp("-".join(pkg1[1:]), "-".join(pkg2[1:])) -def _pkgsplit(mypkg, eapi=None): +def _pkgsplit(mypkg: str, eapi: Any = None) -> Optional[Tuple[str, str, str]]: """ @param mypkg: pv @return: @@ -317,7 +319,11 @@ _missing_cat = "null" @lru_cache(10240) -def catpkgsplit(mydata, silent=1, eapi=None): +def catpkgsplit( + mydata: Union[str, "_pkg_str"], + silent: int = 1, + eapi: Any = None, +) -> Optional[Tuple[str, ...]]: """ Takes a Category/Package-Version-Rev and returns a list of each. @@ -368,33 +374,33 @@ class _pkg_str(str): def __new__( cls, - cpv, - metadata=None, - settings=None, - eapi=None, - repo=None, - slot=None, - build_time=None, - build_id=None, - file_size=None, - mtime=None, - db=None, + cpv: str, + metadata: Optional[Dict[str, Any]] = None, + settings: Any = None, + eapi: Any = None, + repo: Optional[str] = None, + slot: Optional[str] = None, + build_time: Optional[int] = None, + build_id: Optional[str] = None, + file_size: Optional[int] = None, + mtime: Optional[int] = None, + db: Any = None, ): return str.__new__(cls, cpv) def __init__( self, - cpv, - metadata=None, - settings=None, - eapi=None, - repo=None, - slot=None, - build_time=None, - build_id=None, - file_size=None, - mtime=None, - db=None, + cpv: str, + metadata: Optional[Dict[str, Any]] = None, + settings: Any = None, + eapi: Any = None, + repo: Optional[str] = None, + slot: Optional[str] = None, + build_time: Optional[int] = None, + build_id: Optional[str] = None, + file_size: Optional[int] = None, + mtime: Optional[int] = None, + db: Any = None, ): if not isinstance(cpv, str): # Avoid TypeError from str.__init__ with PyPy. @@ -456,13 +462,13 @@ class _pkg_str(str): repo = _unknown_repo self.__dict__["repo"] = repo - def __setattr__(self, name, value): + def __setattr__(self, name: str, value: Any) -> None: raise AttributeError( "_pkg_str instances are immutable", self.__class__, name, value ) @staticmethod - def _long(var, default): + def _long(var: Any, default: int) -> int: if var is not None: try: var = int(var) @@ -474,7 +480,7 @@ class _pkg_str(str): return var @property - def stable(self): + def stable(self) -> bool: try: return self._stable except AttributeError: @@ -492,7 +498,9 @@ class _pkg_str(str): return stable -def pkgsplit(mypkg, silent=1, eapi=None): +def pkgsplit( + mypkg: str, silent: int = 1, eapi: Any = None +) -> Optional[Tuple[str, str, str]]: """ @param mypkg: either a pv or cpv @return: @@ -509,7 +517,7 @@ def pkgsplit(mypkg, silent=1, eapi=None): return (cat + "/" + pn, ver, rev) -def cpv_getkey(mycpv, eapi=None): +def cpv_getkey(mycpv: Union[_pkg_str, str], eapi: Any = None) -> Optional[str]: """Calls catpkgsplit on a cpv and returns only the cp.""" try: return mycpv.cp @@ -535,7 +543,7 @@ def cpv_getkey(mycpv, eapi=None): return mysplit[0] -def cpv_getversion(mycpv, eapi=None): +def cpv_getversion(mycpv: Union[str, _pkg_str], eapi: Any = None) -> Optional[str]: """Returns the v (including revision) from an cpv.""" try: return mycpv.version @@ -547,7 +555,7 @@ def cpv_getversion(mycpv, eapi=None): return mycpv[len(cp + "-") :] -def cpv_sort_key(eapi=None): +def cpv_sort_key(eapi: Any = None) -> Any: """ Create an object for sorting cpvs, to be used as the 'key' parameter in places like list.sort() or sorted(). This calls catpkgsplit() once for @@ -562,7 +570,7 @@ def cpv_sort_key(eapi=None): split_cache = {} - def cmp_cpv(cpv1, cpv2): + def cmp_cpv(cpv1: Any, cpv2: Any) -> int: split1 = split_cache.get(cpv1, False) if split1 is False: split1 = None @@ -595,11 +603,11 @@ def cpv_sort_key(eapi=None): return cmp_sort_key(cmp_cpv) -def catsplit(mydep): +def catsplit(mydep: str) -> List[str]: return mydep.split("/", 1) -def best(mymatches, eapi=None): +def best(mymatches: Sequence[Any], eapi: Any = None) -> Any: """Accepts None arguments; assumes matches are valid.""" if not mymatches: return ""