Skip to content

Commit

Permalink
Give version2tuple() a better type annotation
Browse files Browse the repository at this point in the history
Also, remove superfluous function-level import of `re` (It's already imported
at the top of the file).

Also, prevent the function from crashing when given a string with non-ASCII
Unicode digits.
  • Loading branch information
jwodder committed Dec 8, 2023
1 parent 68f4d1e commit f302d27
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions dandischema/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import re
from typing import Iterator, List

Expand Down Expand Up @@ -43,13 +45,12 @@ def split_camel_case(s: str) -> Iterator[str]:
yield s[last_start:]


def version2tuple(ver: str) -> tuple:
def version2tuple(ver: str) -> tuple[int, int, int]:
"""Convert version to numeric tuple"""
import re

if re.match(r"\d+\.\d+\.\d+$", ver) is None:
if m := re.fullmatch(r"(\d+)\.(\d+)\.(\d+)", ver, flags=re.ASCII):
return (int(m[1]), int(m[2]), int(m[3]))
else:
raise ValueError(r"Version must be well formed: \d+\.\d+\.\d+")
return tuple([int(val) for val in ver.split(".")])


def _ensure_newline(obj: str) -> str:
Expand Down

0 comments on commit f302d27

Please sign in to comment.