Skip to content

Commit

Permalink
Add dependency on packaging for version parsing (#341)
Browse files Browse the repository at this point in the history
* test version parsing

* switch from pkg_resources to packaging.version
  • Loading branch information
berquist authored Jul 25, 2024
1 parent b4c3bd3 commit 859c083
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ numpy = [
{ version = ">=1.26.1,<2.0", python = ">=3.9,<3.10" },
{ version = ">=1.26.1", python = ">=3.9,<3.13" },
]
packaging = [
{ version = ">=24.0", python = ">=3.7,<3.8" },
{ version = ">=24.1", python = ">=3.8" },
]
# qcel is compatible with most any numpy, v1 or v2, but numpy v2 only works with pint >=0.24, which is only available for py >=3.10
python = "^3.7"
pint = [
Expand Down
10 changes: 10 additions & 0 deletions qcelemental/tests/test_importing.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,13 @@ def test_which_f_raisemsg():
qcel.util.which("evills", raise_error=True, raise_msg="Install `evills`.")

assert str(e.value).endswith("Command 'evills' not found in envvar PATH. Install `evills`.")


def test_parse_version():
v = qcel.util.parse_version("5.3.1")
assert str(v) == "5.3.1"


def test_safe_version():
v = qcel.util.safe_version("5.3.1")
assert v == "5.3.1"
18 changes: 9 additions & 9 deletions qcelemental/util/importing.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import os
import shutil
import sys
from typing import List, Union
from typing import TYPE_CHECKING, List, Union

if TYPE_CHECKING:
from packaging.version import Version


def which_import(
Expand Down Expand Up @@ -127,20 +130,17 @@ def which(
return ans


def safe_version(*args, **kwargs) -> str:
def safe_version(version) -> str:
"""
Package resources is a very slow load
"""
import pkg_resources

version = pkg_resources.safe_version(*args, **kwargs)
return version
return str(parse_version(version))


def parse_version(*args, **kwargs):
def parse_version(version) -> "Version":
"""
Package resources is a very slow load
"""
import pkg_resources
from packaging.version import parse

return pkg_resources.parse_version(*args, **kwargs)
return parse(version)

0 comments on commit 859c083

Please sign in to comment.