Skip to content

Commit

Permalink
Replace use of vendored packaging
Browse files Browse the repository at this point in the history
  • Loading branch information
ankona committed May 22, 2024
1 parent fbcc46a commit 31585c1
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 23 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@


[build-system]
requires = ["setuptools", "wheel", "cmake>=3.13"]
requires = ["packaging>=24.0", "setuptools>=70.0", "wheel", "cmake>=3.13"]
build-backend = "setuptools.build_meta"

[tool.black]
Expand Down
3 changes: 2 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ classifiers =
[options]
packages = find:
setup_requires =
setuptools>=39.2
packaging>=24.0
setuptools>=70.0
cmake>=3.13
include_package_data = True
python_requires = >=3.9,<3.12
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ def has_ext_modules(_placeholder):

extras_require = {
"dev": [
"packaging>=24.0",
"black==24.1a1",
"isort>=5.6.4",
"pylint>=2.10.0,<3",
Expand Down
17 changes: 6 additions & 11 deletions smartsim/_core/_install/buildenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,10 @@
# https://setuptools.pypa.io/en/latest/pkg_resources.html

# isort: off
import pkg_resources
from pkg_resources import packaging # type: ignore
from packaging.version import Version, InvalidVersion, parse

# isort: on

Version = packaging.version.Version
InvalidVersion = packaging.version.InvalidVersion
DbEngine = t.Literal["REDIS", "KEYDB"]


Expand Down Expand Up @@ -105,9 +102,7 @@ class Version_(str):

@staticmethod
def _convert_to_version(
vers: t.Union[
str, Iterable[packaging.version.Version], packaging.version.Version
],
vers: t.Union[str, Iterable[Version], Version],
) -> t.Any:
if isinstance(vers, Version):
return vers
Expand All @@ -122,20 +117,20 @@ def _convert_to_version(
def major(self) -> int:
# Version(self).major doesn't work for all Python distributions
# see https://github.com/lebedov/python-pdfbox/issues/28
return int(pkg_resources.parse_version(self).base_version.split(".")[0])
return int(parse(self).base_version.split(".", maxsplit=1)[0])

@property
def minor(self) -> int:
return int(pkg_resources.parse_version(self).base_version.split(".")[1])
return int(parse(self).base_version.split(".", maxsplit=2)[1])

@property
def micro(self) -> int:
return int(pkg_resources.parse_version(self).base_version.split(".")[2])
return int(parse(self).base_version.split(".", maxsplit=3)[2])

@property
def patch(self) -> str:
# return micro with string modifier i.e. 1.2.3+cpu -> 3+cpu
return str(pkg_resources.parse_version(self)).split(".")[2]
return str(parse(self)).split(".")[2]

def __gt__(self, cmp: t.Any) -> bool:
try:
Expand Down
33 changes: 23 additions & 10 deletions tests/install/test_buildenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@


import pytest
from pkg_resources import packaging # type: ignore

# from pkg_resources import packaging # type: ignore
import packaging
from smartsim._core._install.buildenv import Version_

# The tests in this file belong to the group_a group
Expand Down Expand Up @@ -71,19 +71,32 @@ def test_version_equality_ne():

assert v1 != v2


def test_version_bad_input():
# def test_version_bad_input():
"""Test behavior when passing an invalid version string"""
v1 = Version_("abcdefg")
version = Version_("1")
assert version.major == 1
with pytest.raises((IndexError, packaging.version.InvalidVersion)) as ex:
version.minor

# todo: fix behavior to ensure versions are valid.
assert v1
version = Version_("2.")
with pytest.raises((IndexError, packaging.version.InvalidVersion)) as ex:
version.major

version = Version_("3.0.")

with pytest.raises((IndexError, packaging.version.InvalidVersion)) as ex:
version.major

version = Version_("3.1.a")
assert version.major == 3
assert version.minor == 1
with pytest.raises((IndexError, packaging.version.InvalidVersion)) as ex:
version.patch


def test_version_bad_parse_fail():
"""Test behavior when trying to parse with an invalid input string"""
v1 = Version_("abcdefg")

# todo: ensure we can't take invalid input and have this IndexError occur.
with pytest.raises((IndexError, packaging.version.InvalidVersion)) as ex:
_ = v1.minor
version = Version_("abcdefg")
version.minor

0 comments on commit 31585c1

Please sign in to comment.