Skip to content

Commit

Permalink
safer safe_version (#353)
Browse files Browse the repository at this point in the history
* Update test_importing.py

* Update changelog.rst

* Update importing.py
  • Loading branch information
loriab authored Jan 13, 2025
1 parent 936407e commit 4f19879
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
2 changes: 2 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ Bug Fixes
Misc.
+++++
- (:pr:`342`) Update some docs settings and requirements for newer tools.
- (:pr:`353`) copied in pkg_resources.safe_version code as follow-up to Eric switch to packaging as both nwchem and gamess were now working.
the try_harder_safe_version might be even bettter


0.28.0 / 2024-06-21
Expand Down
14 changes: 11 additions & 3 deletions qcelemental/tests/test_importing.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,14 @@ def test_parse_version():
assert str(v) == "5.3.1"


def test_safe_version():
v = qcel.util.safe_version("5.3.1")
assert v == "5.3.1"
@pytest.mark.parametrize(
"inp,out",
[
("5.3.1", "5.3.1"),
("30 SEP 2023 (R2)", "30.SEP.2023.-R2-"),
("7.0.0+N/A", "7.0.0-N-A"),
],
)
def test_safe_version(inp, out):
v = qcel.util.safe_version(inp)
assert v == out
16 changes: 13 additions & 3 deletions qcelemental/util/importing.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import re
import shutil
import sys
from typing import TYPE_CHECKING, List, Union
Expand Down Expand Up @@ -132,14 +133,23 @@ def which(

def safe_version(version) -> str:
"""
Package resources is a very slow load
Convert an arbitrary string to a standard version string by pkg_resources definition.
"""
return str(parse_version(version))
# from https://github.com/pypa/setuptools/blob/main/pkg_resources/__init__.py
# original function deprecated and never one-to-one replaced
from packaging.version import InvalidVersion, Version

try:
# normalize the version
return str(Version(version))
except InvalidVersion:
version = version.replace(" ", ".")
return re.sub("[^A-Za-z0-9.]+", "-", version)


def parse_version(version) -> "Version":
"""
Package resources is a very slow load
Legitimate version
"""
from packaging.version import parse

Expand Down

0 comments on commit 4f19879

Please sign in to comment.