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

add ignore option to get_version and add shortcut #39

Merged
merged 5 commits into from
Jan 29, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
40 changes: 37 additions & 3 deletions dunamai/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -978,6 +978,7 @@ def get_version(
first_choice: Callable[[], Optional[Version]] = None,
third_choice: Callable[[], Optional[Version]] = None,
fallback: Version = Version("0.0.0"),
ignore: Union[str, Version] = "0",
marnikow marked this conversation as resolved.
Show resolved Hide resolved
) -> Version:
"""
Check pkg_resources info or a fallback function to determine the version.
Expand All @@ -990,29 +991,62 @@ def get_version(
:param third_choice: Callback to determine a version if the installed
package cannot be found by name.
:param fallback: If no other matches found, use this version.
:param ignore: Ignore this version if it is found.
mtkennerly marked this conversation as resolved.
Show resolved Hide resolved
"""
if isinstance(ignore, str):
ignore = Version(ignore)

if first_choice:
first_ver = first_choice()
if first_ver:
if first_ver and first_ver != ignore:
return first_ver

try:
import importlib.metadata as ilm
except ImportError:
import importlib_metadata as ilm # type: ignore
try:
return Version(ilm.version(name))
ilm_version = Version(ilm.version(name))
if ilm_version != ignore:
return ilm_version
except ilm.PackageNotFoundError:
pass

if third_choice:
third_ver = third_choice()
if third_ver:
if third_ver and third_ver != ignore:
return third_ver

return fallback


def get_version_from_vcs(
marnikow marked this conversation as resolved.
Show resolved Hide resolved
name: str,
first_choice: Callable[[], Optional[Version]] = None,
third_choice: Callable[[], Optional[Version]] = Version.from_any_vcs,
fallback: Version = Version("0.0.0"),
ignore: Union[str, Version] = "0",
) -> Version:
"""
A shortcut call on the `get_version` function with the third choice any vcs.

Check pkg_resources info or a fallback function to determine the version.
This is intended as a convenient default for setting your `__version__` if
you do not want to include a generated version statically during packaging.

:param name: Installed package name.
:param first_choice: Callback to determine a version before checking
to see if the named package is installed.
:param third_choice: Callback to determine a version if the installed
package cannot be found by name.
:param fallback: If no other matches found, use this version.
:param ignore: Ignore this version if it is found.
"""
return get_version(
name, first_choice=first_choice, third_choice=third_choice, fallback=fallback, ignore=ignore
)


def serialize_pep440(
base: str,
stage: str = None,
Expand Down
41 changes: 41 additions & 0 deletions tests/unit/test_dunamai.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,47 @@ def test__get_version__fallback() -> None:
assert get_version("dunamai_nonexistent_test") == Version("0.0.0")


def test__get_version__from_name__ignore() -> None:
assert get_version(
"dunamai", ignore=pkg_resources.get_distribution("dunamai").version, fallback=Version("2")
) == Version("2")
assert get_version(
"dunamai",
ignore=Version(pkg_resources.get_distribution("dunamai").version),
fallback=Version("2"),
) == Version("2")


def test__get_version__first_choice__ignore() -> None:
assert get_version(
"dunamai_nonexistent_test",
first_choice=lambda: Version("1"),
ignore="1",
fallback=Version("2"),
) == Version("2")
assert get_version(
"dunamai_nonexistent_test",
first_choice=lambda: Version("1"),
ignore=Version("1"),
fallback=Version("2"),
) == Version("2")


def test__get_version__third_choice__ignore() -> None:
assert get_version(
"dunamai_nonexistent_test",
third_choice=lambda: Version("3"),
ignore="3",
fallback=Version("2"),
) == Version("2")
assert get_version(
"dunamai_nonexistent_test",
third_choice=lambda: Version("3"),
ignore=Version("3"),
fallback=Version("2"),
) == Version("2")


def test__version__from_any_vcs(tmp_path) -> None:
with chdir(tmp_path):
with pytest.raises(RuntimeError):
Expand Down