Skip to content

Commit 4f19879

Browse files
authored
safer safe_version (#353)
* Update test_importing.py * Update changelog.rst * Update importing.py
1 parent 936407e commit 4f19879

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

docs/changelog.rst

+2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ Bug Fixes
4141
Misc.
4242
+++++
4343
- (:pr:`342`) Update some docs settings and requirements for newer tools.
44+
- (: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.
45+
the try_harder_safe_version might be even bettter
4446

4547

4648
0.28.0 / 2024-06-21

qcelemental/tests/test_importing.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,14 @@ def test_parse_version():
154154
assert str(v) == "5.3.1"
155155

156156

157-
def test_safe_version():
158-
v = qcel.util.safe_version("5.3.1")
159-
assert v == "5.3.1"
157+
@pytest.mark.parametrize(
158+
"inp,out",
159+
[
160+
("5.3.1", "5.3.1"),
161+
("30 SEP 2023 (R2)", "30.SEP.2023.-R2-"),
162+
("7.0.0+N/A", "7.0.0-N-A"),
163+
],
164+
)
165+
def test_safe_version(inp, out):
166+
v = qcel.util.safe_version(inp)
167+
assert v == out

qcelemental/util/importing.py

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import re
23
import shutil
34
import sys
45
from typing import TYPE_CHECKING, List, Union
@@ -132,14 +133,23 @@ def which(
132133

133134
def safe_version(version) -> str:
134135
"""
135-
Package resources is a very slow load
136+
Convert an arbitrary string to a standard version string by pkg_resources definition.
136137
"""
137-
return str(parse_version(version))
138+
# from https://github.com/pypa/setuptools/blob/main/pkg_resources/__init__.py
139+
# original function deprecated and never one-to-one replaced
140+
from packaging.version import InvalidVersion, Version
141+
142+
try:
143+
# normalize the version
144+
return str(Version(version))
145+
except InvalidVersion:
146+
version = version.replace(" ", ".")
147+
return re.sub("[^A-Za-z0-9.]+", "-", version)
138148

139149

140150
def parse_version(version) -> "Version":
141151
"""
142-
Package resources is a very slow load
152+
Legitimate version
143153
"""
144154
from packaging.version import parse
145155

0 commit comments

Comments
 (0)