|
20 | 20 | import warnings
|
21 | 21 | from collections import OrderedDict, abc
|
22 | 22 | from difflib import get_close_matches
|
23 |
| -from importlib.metadata import requires |
| 23 | +from importlib.metadata import requires, version |
24 | 24 | from typing import (
|
25 | 25 | TYPE_CHECKING,
|
26 | 26 | Any,
|
@@ -1095,23 +1095,23 @@ def has_c() -> bool:
|
1095 | 1095 | return False
|
1096 | 1096 |
|
1097 | 1097 |
|
1098 |
| -class Version(tuple): |
| 1098 | +class Version(tuple[int]): |
1099 | 1099 | """A class that can be used to compare version strings."""
|
1100 | 1100 |
|
1101 |
| - def __new__(cls, *version): |
| 1101 | + def __new__(cls, *version: int) -> Version: |
1102 | 1102 | padded_version = cls._padded(version, 4)
|
1103 | 1103 | return super().__new__(cls, tuple(padded_version))
|
1104 | 1104 |
|
1105 | 1105 | @classmethod
|
1106 |
| - def _padded(cls, iter, length, padding=0): |
| 1106 | + def _padded(cls, iter: Any, length: int, padding: int = 0) -> list[int]: |
1107 | 1107 | as_list = list(iter)
|
1108 | 1108 | if len(as_list) < length:
|
1109 | 1109 | for _ in range(length - len(as_list)):
|
1110 | 1110 | as_list.append(padding)
|
1111 | 1111 | return as_list
|
1112 | 1112 |
|
1113 | 1113 | @classmethod
|
1114 |
| - def from_string(cls, version_string): |
| 1114 | + def from_string(cls, version_string: str) -> Version: |
1115 | 1115 | mod = 0
|
1116 | 1116 | bump_patch_level = False
|
1117 | 1117 | if version_string.endswith("+"):
|
@@ -1147,28 +1147,33 @@ def from_string(cls, version_string):
|
1147 | 1147 | return Version(*version)
|
1148 | 1148 |
|
1149 | 1149 | @classmethod
|
1150 |
| - def from_version_array(cls, version_array): |
| 1150 | + def from_version_array(cls, version_array: Any) -> Version: |
1151 | 1151 | version = list(version_array)
|
1152 | 1152 | if version[-1] < 0:
|
1153 | 1153 | version[-1] = -1
|
1154 | 1154 | version = cls._padded(version, 3)
|
1155 | 1155 | return Version(*version)
|
1156 | 1156 |
|
1157 |
| - def at_least(self, *other_version): |
| 1157 | + def at_least(self, *other_version: Any) -> bool: |
1158 | 1158 | return self >= Version(*other_version)
|
1159 | 1159 |
|
1160 |
| - def __str__(self): |
| 1160 | + def __str__(self) -> str: |
1161 | 1161 | return ".".join(map(str, self))
|
1162 | 1162 |
|
1163 | 1163 |
|
1164 |
| -def check_for_min_version(package_version: str, package_name: str) -> tuple[str, bool]: |
1165 |
| - package_version = Version.from_string(package_version) |
| 1164 | +def check_for_min_version(package_name: str) -> tuple[str, str, bool]: |
| 1165 | + """Test whether an installed package is of the desired version.""" |
| 1166 | + package_version_str = version(package_name) |
| 1167 | + package_version = Version.from_string(package_version_str) |
1166 | 1168 | # Dependency is expected to be in one of the forms:
|
1167 | 1169 | # "pymongocrypt<2.0.0,>=1.13.0; extra == 'encryption'"
|
1168 | 1170 | # 'dnspython<3.0.0,>=1.16.0'
|
1169 | 1171 | #
|
1170 |
| - requirement = [i for i in requires("pymongo") if i.startswith(package_name)][0] # noqa: RUF015 |
| 1172 | + requirements = requires("pymongo") |
| 1173 | + assert requirements is not None |
| 1174 | + requirement = [i for i in requirements if i.startswith(package_name)][0] # noqa: RUF015 |
1171 | 1175 | if ";" in requirement:
|
1172 | 1176 | requirement = requirement.split(";")[0]
|
1173 | 1177 | required_version = requirement[requirement.find(">=") + 2 :]
|
1174 |
| - return required_version, package_version >= Version.from_string(required_version) |
| 1178 | + is_valid = package_version >= Version.from_string(required_version) |
| 1179 | + return package_version_str, required_version, is_valid |
0 commit comments