Skip to content

Commit cbd7ab7

Browse files
committed
cleanup and typing
1 parent ebf9ee3 commit cbd7ab7

File tree

4 files changed

+21
-20
lines changed

4 files changed

+21
-20
lines changed

pymongo/asynchronous/srv_resolver.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import ipaddress
1919
import random
20-
from importlib.metadata import version
2120
from typing import TYPE_CHECKING, Any, Optional, Union
2221

2322
from pymongo.common import CONNECT_TIMEOUT, check_for_min_version
@@ -33,8 +32,7 @@ def _have_dnspython() -> bool:
3332
try:
3433
import dns # noqa: F401
3534

36-
dns_version = version("dnspython")
37-
required_version, is_valid = check_for_min_version(dns_version, "dnspython")
35+
dns_version, required_version, is_valid = check_for_min_version("dnspython")
3836
if not is_valid:
3937
raise RuntimeError(
4038
f"pymongo requires dnspython>={required_version}, "

pymongo/common.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import warnings
2121
from collections import OrderedDict, abc
2222
from difflib import get_close_matches
23-
from importlib.metadata import requires
23+
from importlib.metadata import requires, version
2424
from typing import (
2525
TYPE_CHECKING,
2626
Any,
@@ -1095,23 +1095,23 @@ def has_c() -> bool:
10951095
return False
10961096

10971097

1098-
class Version(tuple):
1098+
class Version(tuple[int]):
10991099
"""A class that can be used to compare version strings."""
11001100

1101-
def __new__(cls, *version):
1101+
def __new__(cls, *version: int) -> Version:
11021102
padded_version = cls._padded(version, 4)
11031103
return super().__new__(cls, tuple(padded_version))
11041104

11051105
@classmethod
1106-
def _padded(cls, iter, length, padding=0):
1106+
def _padded(cls, iter: Any, length: int, padding: int = 0) -> list[int]:
11071107
as_list = list(iter)
11081108
if len(as_list) < length:
11091109
for _ in range(length - len(as_list)):
11101110
as_list.append(padding)
11111111
return as_list
11121112

11131113
@classmethod
1114-
def from_string(cls, version_string):
1114+
def from_string(cls, version_string: str) -> Version:
11151115
mod = 0
11161116
bump_patch_level = False
11171117
if version_string.endswith("+"):
@@ -1147,28 +1147,33 @@ def from_string(cls, version_string):
11471147
return Version(*version)
11481148

11491149
@classmethod
1150-
def from_version_array(cls, version_array):
1150+
def from_version_array(cls, version_array: Any) -> Version:
11511151
version = list(version_array)
11521152
if version[-1] < 0:
11531153
version[-1] = -1
11541154
version = cls._padded(version, 3)
11551155
return Version(*version)
11561156

1157-
def at_least(self, *other_version):
1157+
def at_least(self, *other_version: Any) -> bool:
11581158
return self >= Version(*other_version)
11591159

1160-
def __str__(self):
1160+
def __str__(self) -> str:
11611161
return ".".join(map(str, self))
11621162

11631163

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)
11661168
# Dependency is expected to be in one of the forms:
11671169
# "pymongocrypt<2.0.0,>=1.13.0; extra == 'encryption'"
11681170
# 'dnspython<3.0.0,>=1.16.0'
11691171
#
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
11711175
if ";" in requirement:
11721176
requirement = requirement.split(";")[0]
11731177
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

pymongo/encryption_options.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from pymongo.uri_parser_shared import _parse_kms_tls_options
2424

2525
try:
26-
from pymongocrypt import __version__ as pymongocrypt_version # type:ignore[import-untyped]
26+
import pymongocrypt # type:ignore[import-untyped] # noqa: F401
2727

2828
# Check for pymongocrypt>=1.10.
2929
from pymongocrypt import synchronous as _ # noqa: F401
@@ -42,7 +42,7 @@
4242

4343
def check_min_pymongocrypt() -> None:
4444
"""Raise an appropriate error if the min pymongocrypt is not installed."""
45-
required_version, is_valid = check_for_min_version(pymongocrypt_version, "pymongocrypt")
45+
pymongocrypt_version, required_version, is_valid = check_for_min_version("pymongocrypt")
4646
if not is_valid:
4747
raise ConfigurationError(
4848
f"client side encryption requires pymongocrypt>={required_version}, "

pymongo/synchronous/srv_resolver.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import ipaddress
1919
import random
20-
from importlib.metadata import version
2120
from typing import TYPE_CHECKING, Any, Optional, Union
2221

2322
from pymongo.common import CONNECT_TIMEOUT, check_for_min_version
@@ -33,8 +32,7 @@ def _have_dnspython() -> bool:
3332
try:
3433
import dns # noqa: F401
3534

36-
dns_version = version("dnspython")
37-
required_version, is_valid = check_for_min_version(dns_version, "dnspython")
35+
dns_version, required_version, is_valid = check_for_min_version("dnspython")
3836
if not is_valid:
3937
raise RuntimeError(
4038
f"pymongo requires dnspython>={required_version}, "

0 commit comments

Comments
 (0)