From 84d313682cd79fd5b097393a1c613e9151520204 Mon Sep 17 00:00:00 2001 From: Marnik De Bont Date: Thu, 27 Jan 2022 14:37:28 +0100 Subject: [PATCH 1/5] feat: add ignore option to get_version --- dunamai/__init__.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/dunamai/__init__.py b/dunamai/__init__.py index 31689c8..9401d0f 100644 --- a/dunamai/__init__.py +++ b/dunamai/__init__.py @@ -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", ) -> Version: """ Check pkg_resources info or a fallback function to determine the version. @@ -990,10 +991,15 @@ 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. """ + + 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: @@ -1001,13 +1007,15 @@ def get_version( 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 From daa1b1f006deea09881fea09f7e6bc0ea2acee02 Mon Sep 17 00:00:00 2001 From: Marnik De Bont Date: Thu, 27 Jan 2022 14:38:02 +0100 Subject: [PATCH 2/5] test: test ignore option to get_version --- tests/unit/test_dunamai.py | 41 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/tests/unit/test_dunamai.py b/tests/unit/test_dunamai.py index bde5eaf..8b69e04 100644 --- a/tests/unit/test_dunamai.py +++ b/tests/unit/test_dunamai.py @@ -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): From b226b03cf5d86c953303dcfadc4e25bda933ce2a Mon Sep 17 00:00:00 2001 From: Marnik De Bont Date: Thu, 27 Jan 2022 14:58:31 +0100 Subject: [PATCH 3/5] feat: add a shortcut call on the `get_version` function for vcs --- dunamai/__init__.py | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/dunamai/__init__.py b/dunamai/__init__.py index 9401d0f..f06f9be 100644 --- a/dunamai/__init__.py +++ b/dunamai/__init__.py @@ -993,7 +993,6 @@ def get_version( :param fallback: If no other matches found, use this version. :param ignore: Ignore this version if it is found. """ - if isinstance(ignore, str): ignore = Version(ignore) @@ -1021,6 +1020,33 @@ def get_version( return fallback +def get_version_from_vcs( + 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, From 55e3682392b9fbe22bc9f7acc8ac2b1d6e770c9f Mon Sep 17 00:00:00 2001 From: Marnik De Bont Date: Fri, 28 Jan 2022 01:03:04 +0100 Subject: [PATCH 4/5] Apply recommendations --- dunamai/__init__.py | 44 ++++++++++---------------------------- tests/unit/test_dunamai.py | 12 +++++------ 2 files changed, 17 insertions(+), 39 deletions(-) diff --git a/dunamai/__init__.py b/dunamai/__init__.py index f06f9be..d922830 100644 --- a/dunamai/__init__.py +++ b/dunamai/__init__.py @@ -978,7 +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", + ignore: Sequence[Union[str, Version]] = None ) -> Version: """ Check pkg_resources info or a fallback function to determine the version. @@ -993,12 +993,17 @@ def get_version( :param fallback: If no other matches found, use this version. :param ignore: Ignore this version if it is found. """ - if isinstance(ignore, str): - ignore = Version(ignore) + ignore_versions = [] # type: List[Version] + if ignore: + for i in ignore: + if isinstance(i, str): + ignore_versions.append(Version(i)) + else: + ignore_versions.append(i) if first_choice: first_ver = first_choice() - if first_ver and first_ver != ignore: + if first_ver and first_ver not in ignore_versions: return first_ver try: @@ -1007,46 +1012,19 @@ def get_version( import importlib_metadata as ilm # type: ignore try: ilm_version = Version(ilm.version(name)) - if ilm_version != ignore: + if ilm_version not in ignore_versions: return ilm_version except ilm.PackageNotFoundError: pass if third_choice: third_ver = third_choice() - if third_ver and third_ver != ignore: + if third_ver and third_ver not in ignore_versions: return third_ver return fallback -def get_version_from_vcs( - 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, diff --git a/tests/unit/test_dunamai.py b/tests/unit/test_dunamai.py index 8b69e04..9ad4a35 100644 --- a/tests/unit/test_dunamai.py +++ b/tests/unit/test_dunamai.py @@ -444,11 +444,11 @@ def test__get_version__fallback() -> None: def test__get_version__from_name__ignore() -> None: assert get_version( - "dunamai", ignore=pkg_resources.get_distribution("dunamai").version, fallback=Version("2") + "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), + ignore=[Version(pkg_resources.get_distribution("dunamai").version)], fallback=Version("2"), ) == Version("2") @@ -457,13 +457,13 @@ def test__get_version__first_choice__ignore() -> None: assert get_version( "dunamai_nonexistent_test", first_choice=lambda: Version("1"), - ignore="1", + ignore=["1"], fallback=Version("2"), ) == Version("2") assert get_version( "dunamai_nonexistent_test", first_choice=lambda: Version("1"), - ignore=Version("1"), + ignore=[Version("1")], fallback=Version("2"), ) == Version("2") @@ -472,13 +472,13 @@ def test__get_version__third_choice__ignore() -> None: assert get_version( "dunamai_nonexistent_test", third_choice=lambda: Version("3"), - ignore="3", + ignore=["3"], fallback=Version("2"), ) == Version("2") assert get_version( "dunamai_nonexistent_test", third_choice=lambda: Version("3"), - ignore=Version("3"), + ignore=[Version("3")], fallback=Version("2"), ) == Version("2") From aa46470e8ffbe28522723f293bb8b4ae182e640d Mon Sep 17 00:00:00 2001 From: Marnik De Bont Date: Sat, 29 Jan 2022 14:10:33 +0100 Subject: [PATCH 5/5] Make ignore in get_version only accept versions and write special comparison logic. --- dunamai/__init__.py | 59 ++++++++++++++++++++------ tests/unit/test_dunamai.py | 84 +++++++++++++++++++++++--------------- 2 files changed, 97 insertions(+), 46 deletions(-) diff --git a/dunamai/__init__.py b/dunamai/__init__.py index d922830..8476e0a 100644 --- a/dunamai/__init__.py +++ b/dunamai/__init__.py @@ -374,6 +374,37 @@ def __eq__(self, other: Any) -> bool: and self.epoch == other.epoch ) + def equals_ignored_version(self, ignored_version: "Version") -> bool: + """ + Compare this version to another version but ignore None values in the ignored version. + :param ignored_version: The version to compare to. + :return: True if this version equals the ignored version. + """ + if self.base != ignored_version.base: + return False + if ignored_version.stage is not None: + if self.stage != ignored_version.stage: + return False + if ignored_version.revision is not None: + if self.revision != ignored_version.revision: + return False + if ignored_version.distance is not None: + if self.distance != ignored_version.distance: + return False + if ignored_version.commit is not None: + if self.commit != ignored_version.commit: + return False + if ignored_version.dirty is not None: + if self.dirty != ignored_version.dirty: + return False + if ignored_version.tagged_metadata is not None: + if self.tagged_metadata != ignored_version.tagged_metadata: + return False + if ignored_version.epoch is not None: + if self.epoch != ignored_version.epoch: + return False + return True + def __lt__(self, other: Any) -> bool: if not isinstance(other, Version): raise TypeError( @@ -978,7 +1009,7 @@ def get_version( first_choice: Callable[[], Optional[Version]] = None, third_choice: Callable[[], Optional[Version]] = None, fallback: Version = Version("0.0.0"), - ignore: Sequence[Union[str, Version]] = None + ignore: Sequence[Version] = None, ) -> Version: """ Check pkg_resources info or a fallback function to determine the version. @@ -991,19 +1022,13 @@ 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. + :param ignore: Ignore a found version if it is part of this list. When + comparing the found version to an ignored one, fields with None in the ignored + version are not taken into account. """ - ignore_versions = [] # type: List[Version] - if ignore: - for i in ignore: - if isinstance(i, str): - ignore_versions.append(Version(i)) - else: - ignore_versions.append(i) - if first_choice: first_ver = first_choice() - if first_ver and first_ver not in ignore_versions: + if first_ver and not _version_in_list_of_ignored_versions(first_ver, ignore): return first_ver try: @@ -1012,14 +1037,14 @@ def get_version( import importlib_metadata as ilm # type: ignore try: ilm_version = Version(ilm.version(name)) - if ilm_version not in ignore_versions: + if not _version_in_list_of_ignored_versions(ilm_version, ignore): return ilm_version except ilm.PackageNotFoundError: pass if third_choice: third_ver = third_choice() - if third_ver and third_ver not in ignore_versions: + if third_ver and not _version_in_list_of_ignored_versions(third_ver, ignore): return third_ver return fallback @@ -1161,4 +1186,12 @@ def _parse_git_timestamp_iso_strict(raw: str) -> dt.datetime: return dt.datetime.strptime(compat, "%Y-%m-%dT%H:%M:%S%z") +def _version_in_list_of_ignored_versions(version: Version, ignore: Optional[Sequence[Version]]): + if ignore: + for ignored_version in ignore: + if version.equals_ignored_version(ignored_version): + return True + return False + + __version__ = get_version("dunamai").serialize() diff --git a/tests/unit/test_dunamai.py b/tests/unit/test_dunamai.py index 9ad4a35..26024a4 100644 --- a/tests/unit/test_dunamai.py +++ b/tests/unit/test_dunamai.py @@ -1,5 +1,5 @@ import os -import pkg_resources +import pkg_resources # type: ignore import re from contextlib import contextmanager from pathlib import Path @@ -443,44 +443,62 @@ def test__get_version__fallback() -> None: 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") + 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") + assert ( + get_version( + "dunamai_nonexistent_test", + first_choice=lambda: Version("1"), + ignore=[Version("1")], + fallback=Version("2"), + ) + == Version("2") + ) + + +def test__get_version__first_choice__ignore__with_commit() -> None: + assert ( + get_version( + "dunamai_nonexistent_test", + first_choice=lambda: Version("1", commit="aaaa"), + ignore=[Version("1")], + fallback=Version("2"), + ) + == Version("2") + ) + + +def test__get_version__first_choice__ignore__without_commit() -> None: + assert ( + get_version( + "dunamai_nonexistent_test", + first_choice=lambda: Version("1"), + ignore=[Version("1", commit="aaaa")], + fallback=Version("2"), + ) + == Version("1") + ) 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") + 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: