Skip to content

Commit

Permalink
First step towards pypa#524
Browse files Browse the repository at this point in the history
  • Loading branch information
Sylvain MARIE committed Jun 17, 2021
1 parent b845168 commit fbadf5b
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/setuptools_scm/__init__.py
Expand Up @@ -157,6 +157,7 @@ def get_version(
parse=None,
git_describe_command=None,
dist_name=None,
version_cls=None, # or name it version_factory ?
):
"""
If supplied, relative_to should be a file from which root may
Expand Down
2 changes: 2 additions & 0 deletions src/setuptools_scm/config.py
Expand Up @@ -66,6 +66,7 @@ def __init__(
parse=None,
git_describe_command=None,
dist_name=None,
version_cls=None,
):
# TODO:
self._relative_to = relative_to
Expand All @@ -83,6 +84,7 @@ def __init__(
self.tag_regex = tag_regex
self.git_describe_command = git_describe_command
self.dist_name = dist_name
self.version_cls = version_cls

@property
def fallback_root(self):
Expand Down
10 changes: 8 additions & 2 deletions src/setuptools_scm/version.py
Expand Up @@ -98,7 +98,11 @@ def tag_to_version(tag, config=None):
)
)

if VERSION_CLASS is not None:
# use custom version class if provided
if config.version_cls is not None:
version = config.version_cls(version)
trace("version", repr(version))
elif VERSION_CLASS is not None:
version = pkg_parse_version(version)
trace("version", repr(version))

Expand Down Expand Up @@ -187,7 +191,9 @@ def format_next_version(self, guess_next, fmt="{guessed}.dev{distance}", **kw):
def _parse_tag(tag, preformatted, config):
if preformatted:
return tag
if VERSION_CLASS is None or not isinstance(tag, VERSION_CLASS):
# use custom version class if provided
version_cls = config.version_cls or VERSION_CLASS
if version_cls is None or not isinstance(tag, version_cls):
tag = tag_to_version(tag, config)
return tag

Expand Down
14 changes: 14 additions & 0 deletions testing/test_basic_api.py
Expand Up @@ -123,3 +123,17 @@ def parse(root):

with pytest.raises(TypeError):
setuptools_scm.get_version(parse=parse)


# def test_custom_version_cls(monkeypatch):
#
# monkeypatch.setenv(setuptools_scm.PRETEND_KEY, "1.0.1")
#
# class MyVersion:
# def __init__(self, tag_str: str):
# # todo
# pass
#
# # TODO unfortunately with PRETEND_KEY the preformatted flag becomes True which bypasses our class.
# # which other mechanism would be ok to use ?
# assert setuptools_scm.get_version(version_cls=MyVersion) == "1"
16 changes: 16 additions & 0 deletions testing/test_version.py
Expand Up @@ -170,3 +170,19 @@ def test_version_bump_bad():
):

guess_next_version(tag_version="2.0.0-alpha.5-PMC")


def test_custom_version_cls():
"""Test that we can pass our own version class instead of pkg_resources"""

class MyVersion:
def __init__(self, tag_str: str):
self.tag = tag_str

def __repr__(self):
return "Custom %s" % self.tag

scm_version = meta("1.0.0-foo", config=Configuration(version_cls=MyVersion))

assert isinstance(scm_version.tag, MyVersion)
assert repr(scm_version.tag) == "Custom 1.0.0-foo"

0 comments on commit fbadf5b

Please sign in to comment.