Skip to content

Annotate utils/uuid.py and utils/text.py #1514

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions kombu/utils/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@


from difflib import SequenceMatcher
from typing import Iterator, Optional, Sequence, Tuple, Union

from kombu import version_info_t

Expand All @@ -16,8 +17,7 @@ def escape_regex(p, white=''):
for c in p)


def fmatch_iter(needle, haystack, min_ratio=0.6):
# type: (str, Sequence[str], float) -> Iterator[Tuple[float, str]]
def fmatch_iter(needle: str, haystack: Sequence[str], min_ratio: float = 0.6) -> Iterator[Tuple[float, str]]:
"""Fuzzy match: iteratively.

Yields:
Expand All @@ -29,19 +29,17 @@ def fmatch_iter(needle, haystack, min_ratio=0.6):
yield ratio, key


def fmatch_best(needle, haystack, min_ratio=0.6):
# type: (str, Sequence[str], float) -> str
def fmatch_best(needle: str, haystack: Sequence[str], min_ratio: float = 0.6) -> Optional[str]:
"""Fuzzy match - Find best match (scalar)."""
try:
return sorted(
fmatch_iter(needle, haystack, min_ratio), reverse=True,
)[0][1]
except IndexError:
pass
return None


def version_string_as_tuple(s):
# type: (str) -> version_info_t
def version_string_as_tuple(s: str) -> version_info_t:
"""Convert version string to version info tuple."""
v = _unpack_version(*s.split('.'))
# X.Y.3a1 -> (X, Y, 3, 'a1')
Expand All @@ -53,13 +51,17 @@ def version_string_as_tuple(s):
return v


def _unpack_version(major, minor=0, micro=0, releaselevel='', serial=''):
# type: (int, int, int, str, str) -> version_info_t
def _unpack_version(
major: str,
minor: Union[str, int] = 0,
micro: Union[str, int] = 0,
releaselevel: str = '',
serial: str = ''
) -> version_info_t:
return version_info_t(int(major), int(minor), micro, releaselevel, serial)


def _splitmicro(micro, releaselevel='', serial=''):
# type: (int, str, str) -> Tuple[int, str, str]
def _splitmicro(micro: str, releaselevel: str = '', serial: str = '') -> Tuple[int, str, str]:
for index, char in enumerate(micro):
if not char.isdigit():
break
Expand Down
6 changes: 3 additions & 3 deletions kombu/utils/uuid.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"""UUID utilities."""
from typing import Callable
from uuid import UUID, uuid4

from uuid import uuid4


def uuid(_uuid=uuid4):
def uuid(_uuid: Callable[[], UUID] = uuid4) -> str:
"""Generate unique id in UUID4 format.

See Also:
Expand Down
4 changes: 4 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@ disallow_untyped_defs = True
ignore_missing_imports = True
files =
kombu/utils/time.py,
kombu/utils/uuid.py,
t/unit/utils/test_uuid.py,
kombu/utils/text.py,
kombu/asynchronous/semaphore.py


[pep257]
ignore = D102,D104,D203,D105,D213

Expand Down
4 changes: 2 additions & 2 deletions t/unit/utils/test_uuid.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

class test_UUID:

def test_uuid4(self):
def test_uuid4(self) -> None:
assert uuid() != uuid()

def test_uuid(self):
def test_uuid(self) -> None:
i1 = uuid()
i2 = uuid()
assert isinstance(i1, str)
Expand Down