From 9d8d178f100504926711f2b4e226175c0892e182 Mon Sep 17 00:00:00 2001 From: HarmvZ Date: Mon, 29 Jul 2024 12:27:26 +0200 Subject: [PATCH 1/3] Update flake8 in pre-commit --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 38dd9ab..efe1bed 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -23,7 +23,7 @@ repos: - id: black language: python - repo: https://github.com/PyCQA/flake8 - rev: 6.0.0 + rev: 7.1.0 hooks: - id: flake8 language: python From d60e6cab460e893053e6ee855bd5f67ed010578a Mon Sep 17 00:00:00 2001 From: HarmvZ Date: Mon, 29 Jul 2024 12:27:41 +0200 Subject: [PATCH 2/3] Check if current version is most recent --- gcapi/__init__.py | 3 +++ gcapi/check_version.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 gcapi/check_version.py diff --git a/gcapi/__init__.py b/gcapi/__init__.py index 33f3ac0..84b8b34 100644 --- a/gcapi/__init__.py +++ b/gcapi/__init__.py @@ -1,5 +1,8 @@ +from gcapi.check_version import check_version from gcapi.gcapi import AsyncClient, Client +check_version() + __author__ = """James Meakin""" __email__ = "code@jmsmkn.com" __all__ = ["AsyncClient", "Client"] diff --git a/gcapi/check_version.py b/gcapi/check_version.py new file mode 100644 index 0000000..b025231 --- /dev/null +++ b/gcapi/check_version.py @@ -0,0 +1,31 @@ +import warnings +from importlib.metadata import version as get_version + +import httpx +from packaging import version + + +def check_version(): + package_name = "gcapi" + try: + current_version = get_version(package_name) + with httpx.Client() as client: + response = client.get(f"https://pypi.org/pypi/{package_name}/json") + latest_version = response.json()["info"]["version"] + + if version.parse(current_version) < version.parse(latest_version): + warnings.warn( + f"You are using {package_name} version {current_version}. " + f"However, version {latest_version} is available. You should consider" + f" upgrading via `pip install --upgrade {package_name}`", + UserWarning, + stacklevel=0, + ) + except Exception: + # If there's any error in checking the version, we'll silently pass + # This ensures the import process isn't disrupted + pass + + +# Call the function when the package is imported +check_version() From 0bb0354f1922f35e9f210dac8086feb8f09e3b86 Mon Sep 17 00:00:00 2001 From: HarmvZ Date: Mon, 29 Jul 2024 12:28:12 +0200 Subject: [PATCH 3/3] Add tests for version check --- tests/test_check_version.py | 66 +++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 tests/test_check_version.py diff --git a/tests/test_check_version.py b/tests/test_check_version.py new file mode 100644 index 0000000..20677cb --- /dev/null +++ b/tests/test_check_version.py @@ -0,0 +1,66 @@ +import warnings +from unittest.mock import MagicMock, patch + +import httpx +import pytest + +from gcapi import check_version + + +@pytest.fixture +def mock_get_version(): + with patch("gcapi.check_version.get_version") as mock: + yield mock + + +@pytest.fixture +def mock_httpx_client(): + with patch("gcapi.check_version.httpx.Client") as mock: + yield mock + + +@pytest.mark.parametrize( + "current,latest,should_warn", + [ + ("1.0.0", "1.0.1", True), + ("1.0.0", "1.1.0", True), + ("1.0.0", "2.0.0", True), + ("1.0.0", "1.0.0", False), + ("1.0.1", "1.0.0", False), + ("1.1.0", "1.0.0", False), + ("2.0.0", "1.0.0", False), + ], +) +def test_check_version_comparisons( + mock_get_version, mock_httpx_client, current, latest, should_warn +): + mock_get_version.return_value = current + mock_response = MagicMock() + mock_response.json.return_value = {"info": {"version": latest}} + mock_httpx_client.return_value.__enter__.return_value.get.return_value = ( + mock_response + ) + + with warnings.catch_warnings(record=True) as w: + check_version() + + if should_warn: + assert ( + len(w) == 1 + ), f"A warning should be issued for version {current} < {latest}" + assert f"You are using gcapi version {current}" in str(w[0].message) + else: + assert ( + len(w) == 0 + ), f"No warning should be issued for version {current} >= {latest}" + + +def test_check_version_network_error(mock_httpx_client): + mock_httpx_client.return_value.__enter__.return_value.get.side_effect = ( + httpx.RequestError("Network error") + ) + + with warnings.catch_warnings(record=True) as w: + check_version() + + assert len(w) == 0, "No warning should be issued for network errors"