From 294f2f12b11822efcf75d546517b6e0dadae459f Mon Sep 17 00:00:00 2001 From: Kalibh Halford Date: Wed, 29 Jan 2025 13:53:41 +0000 Subject: [PATCH] MAINT: Remove returning exceptions Return only bools instead of returning exceptions. Exceptions should be raised and bools can be returned then checked. --- src/features/app_version.py | 8 +++----- src/features/compose_version.py | 10 ++++------ tests/test_app_version.py | 4 ++-- tests/test_compose_version.py | 4 ++-- 4 files changed, 11 insertions(+), 15 deletions(-) diff --git a/src/features/app_version.py b/src/features/app_version.py index 47a8576..14f7a01 100644 --- a/src/features/app_version.py +++ b/src/features/app_version.py @@ -20,7 +20,7 @@ def run(self, path1: Path, path2: Path) -> bool: main_ver = self.get_version(main_content) branch_ver = self.get_version(branch_content) comparison = self.compare(main_ver, branch_ver) - if comparison == RuntimeError: + if not comparison: raise RuntimeError( f"The version in {('/'.join(str(path2).split('/')[4:]))[0:]} has not been updated correctly." ) @@ -51,13 +51,11 @@ def get_version(content: str) -> Version: return Version(content) @staticmethod - def compare(main: Version, branch: Version) -> Union[bool, Type[RuntimeError]]: + def compare(main: Version, branch: Version) -> bool: """ Returns if the branch version is larger than the main version :param main: Version on main :param branch: Version on branch :return: If the version update is correct return true, else return error """ - if branch > main: - return True - return RuntimeError + return branch > main diff --git a/src/features/compose_version.py b/src/features/compose_version.py index 8aa0c23..66ad62a 100644 --- a/src/features/compose_version.py +++ b/src/features/compose_version.py @@ -1,7 +1,7 @@ """Compare Docker compose image version to the version.txt.""" from pathlib import Path -from typing import List, Union, Type +from typing import List from packaging.version import Version @@ -20,7 +20,7 @@ def run(self, app: Path, compose: Path) -> bool: app_ver = Version(app_content) compose_ver = self.get_version(compose_content) comparison = self.compare(app_ver, compose_ver) - if comparison == RuntimeError: + if not comparison: raise RuntimeError( f"The version in {('/'.join(str(compose).split('/')[4:]))[0:]}" f" does not match {('/'.join(str(app).split('/')[4:]))[0:]}." @@ -57,13 +57,11 @@ def get_version(content: List[str]) -> Version: return Version(version_str) @staticmethod - def compare(app: Version, compose: Version) -> Union[bool, Type[RuntimeError]]: + def compare(app: Version, compose: Version) -> bool: """ Returns if the app version and docker compose version are equal. :param app: App version :param compose: Compose version :return: If the version update is correct return true, else return error """ - if app == compose: - return True - return RuntimeError + return app == compose diff --git a/tests/test_app_version.py b/tests/test_app_version.py index 0271afa..fb15d3d 100644 --- a/tests/test_app_version.py +++ b/tests/test_app_version.py @@ -58,10 +58,10 @@ def test_get_version(instance): def test_compare_pass(instance): """Test that the compare returns true for a valid features.app_version""" res = instance.compare(Version("1.0.0"), Version("1.0.1")) - assert res != RuntimeError + assert res def test_compare_fails(instance): """Test that the compare returns an error for an invalid features.app_version""" res = instance.compare(Version("1.0.1"), Version("1.0.0")) - assert res == RuntimeError + assert not res diff --git a/tests/test_compose_version.py b/tests/test_compose_version.py index 774bd8a..c3ea785 100644 --- a/tests/test_compose_version.py +++ b/tests/test_compose_version.py @@ -57,10 +57,10 @@ def test_get_version(instance): def test_compare_pass(instance): """Test that the compare returns true for a valid features.compose_version""" res = instance.compare(Version("1.0.0"), Version("1.0.0")) - assert res != RuntimeError + assert res def test_compare_fails(instance): """Test that the compare returns an error for an invalid features.compose_version""" res = instance.compare(Version("1.0.1"), Version("1.0.0")) - assert res == RuntimeError + assert not res