Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Enhancement] Update digit_version function #865

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 51 additions & 11 deletions mmpose/__init__.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,59 @@
import warnings

import mmcv
from mmcv import digit_version, parse_version_info
from mmcv import parse_version_info
from packaging.version import parse

from .version import __version__, short_version

mmcv_minimum_version = '1.3.8'
mmcv_maximum_version = '1.4.0'
MMCV_MIN = '1.3.8'
MMCV_MAX = '1.5.0'


def digit_version(version_str: str, length: int = 4):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Importing is good enough, and do not need to copy the implementation.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean importing from mmcv? I think the point of this PR is to avoid import this function from mmcv. Because digit_version from mmcv depends on mmcv version itself, like a circular dependency.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually my thinking its the reverse. We will use the implementation of digit_version in mmcv, so that we will get automatic bug fixes.

"""Convert a version string into a tuple of integers.

This method is usually used for comparing two versions. For pre-release
versions: alpha < beta < rc.
Args:
version_str (str): The version string.
length (int): The maximum number of version levels. Default: 4.
Returns:
tuple[int]: The version info in digits (integers).
"""
version = parse(version_str)
assert version.release, f'failed to parse version {version_str}'
release = list(version.release)
release = release[:length]
if len(release) < length:
release = release + [0] * (length - len(release))
if version.is_prerelease:
mapping = {'a': -3, 'b': -2, 'rc': -1}
val = -4
# version.pre can be None
if version.pre:
if version.pre[0] not in mapping:
warnings.warn(f'unknown prerelease version {version.pre[0]}, '
'version checking may go wrong')
else:
val = mapping[version.pre[0]]
release.extend([val, version.pre[-1]])
else:
release.extend([val, 0])

elif version.is_postrelease:
release.extend([1, version.post])
else:
release.extend([0, 0])
return tuple(release)


mmcv_version = digit_version(mmcv.__version__)
version_info = parse_version_info(__version__)

assert digit_version(mmcv_minimum_version) <= mmcv_version, \
f'MMCV=={mmcv.__version__} is used but incompatible. ' \
f'Please install mmcv>={mmcv_minimum_version}.'

assert digit_version(mmcv_maximum_version) > mmcv_version, \
f'MMCV=={mmcv.__version__} is used but incompatible. ' \
f'Please install mmcv<{mmcv_maximum_version}.'
assert (mmcv_version >= digit_version(MMCV_MIN)
and mmcv_version <= digit_version(MMCV_MAX)), \
f'MMCV=={mmcv.__version__} is used but incompatible. ' \
f'Please install mmcv>={MMCV_MIN}, <={MMCV_MAX}.'

__all__ = ['__version__', 'short_version', 'version_info']
__all__ = ['__version__', 'short_version', 'version_info', 'digit_version']
1 change: 1 addition & 0 deletions requirements/runtime.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ matplotlib
munkres
numpy
opencv-python
packaging
pillow
scipy
torchvision
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ line_length = 79
multi_line_output = 0
known_standard_library = pkg_resources,setuptools
known_first_party = mmpose
known_third_party = PIL,cv2,h5py,json_tricks,matplotlib,mmcv,munkres,numpy,poseval,pytest,scipy,seaborn,spacepy,titlecase,torch,torchvision,trimesh,xmltodict,xtcocotools
known_third_party = PIL,cv2,h5py,json_tricks,matplotlib,mmcv,munkres,numpy,packaging,poseval,pytest,scipy,seaborn,spacepy,titlecase,torch,torchvision,trimesh,xmltodict,xtcocotools
no_lines_before = STDLIB,LOCALFOLDER
default_section = THIRDPARTY
20 changes: 20 additions & 0 deletions tests/test_version.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,28 @@
import mmpose
from mmpose import digit_version


def test_version():
version = mmpose.__version__
assert isinstance(version, str)
assert isinstance(mmpose.short_version, str)
assert mmpose.short_version in version


def test_digit_version():
assert digit_version('0.2.16') == (0, 2, 16, 0, 0, 0)
assert digit_version('1.2.3') == (1, 2, 3, 0, 0, 0)
assert digit_version('1.2.3rc0') == (1, 2, 3, 0, -1, 0)
assert digit_version('1.2.3rc1') == (1, 2, 3, 0, -1, 1)
assert digit_version('1.0rc0') == (1, 0, 0, 0, -1, 0)
assert digit_version('1.0') == digit_version('1.0.0')
assert digit_version('1.5.0+cuda90_cudnn7.6.3_lms') == digit_version('1.5')
assert digit_version('1.0.0dev') < digit_version('1.0.0a')
assert digit_version('1.0.0a') < digit_version('1.0.0a1')
assert digit_version('1.0.0a') < digit_version('1.0.0b')
assert digit_version('1.0.0b') < digit_version('1.0.0rc')
assert digit_version('1.0.0rc1') < digit_version('1.0.0')
assert digit_version('1.0.0') < digit_version('1.0.0post')
assert digit_version('1.0.0post') < digit_version('1.0.0post1')
assert digit_version('v1') == (1, 0, 0, 0, 0, 0)
assert digit_version('v1.1.5') == (1, 1, 5, 0, 0, 0)