-
Notifications
You must be signed in to change notification settings - Fork 21
Warning Message for users using old versions #701
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
Open
Saga4
wants to merge
10
commits into
main
Choose a base branch
from
mihika_pr_631
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+298
−2
Open
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9dd0708
Update main.py and add version_check.py
mihikap01 6a7188f
Update version.py
mihikap01 8ca1806
Resolve merge conflict in version.py
mihikap01 5b4d4fb
Address review feedback: remove config option, reduce timeout, fix im…
mihikap01 03ee7a4
Merge branch 'main' of https://github.com/codeflash-ai/codeflash
mihikap01 41eda3b
Merge branch 'main' into main
Saga4 f5ee4b3
Merge branch 'main' into main
Saga4 811e469
minor fixes and cleanup
Saga4 46a6de8
Merge branch 'main' into mihika_pr_631
Saga4 69a46d7
Merge branch 'main' into mihika_pr_631
Saga4 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
"""Version checking utilities for codeflash.""" | ||
|
||
from __future__ import annotations | ||
|
||
import time | ||
|
||
import requests | ||
from packaging import version | ||
|
||
from codeflash.cli_cmds.console import console, logger | ||
from codeflash.version import __version__ | ||
|
||
# Simple cache to avoid checking too frequently | ||
_version_cache = {"version": None, "timestamp": 0} | ||
_cache_duration = 3600 # 1 hour cache | ||
|
||
|
||
def get_latest_version_from_pypi() -> str | None: | ||
"""Get the latest version of codeflash from PyPI. | ||
|
||
Returns: | ||
The latest version string from PyPI, or None if the request fails. | ||
|
||
""" | ||
# Check cache first | ||
current_time = time.time() | ||
if _version_cache["version"] is not None and current_time - _version_cache["timestamp"] < _cache_duration: | ||
return _version_cache["version"] | ||
|
||
try: | ||
response = requests.get("https://pypi.org/pypi/codeflash/json", timeout=2) | ||
if response.status_code == 200: | ||
data = response.json() | ||
latest_version = data["info"]["version"] | ||
|
||
# Update cache | ||
_version_cache["version"] = latest_version | ||
_version_cache["timestamp"] = current_time | ||
|
||
return latest_version | ||
logger.debug(f"Failed to fetch version from PyPI: {response.status_code}") | ||
return None # noqa: TRY300 | ||
except requests.RequestException as e: | ||
logger.debug(f"Network error fetching version from PyPI: {e}") | ||
return None | ||
except (KeyError, ValueError) as e: | ||
logger.debug(f"Invalid response format from PyPI: {e}") | ||
return None | ||
except Exception as e: | ||
logger.debug(f"Unexpected error fetching version from PyPI: {e}") | ||
return None | ||
|
||
|
||
def check_for_newer_minor_version() -> None: | ||
"""Check if a newer minor version is available on PyPI and notify the user. | ||
|
||
This function compares the current version with the latest version on PyPI. | ||
If a newer minor version is available, it prints an informational message | ||
suggesting the user upgrade. | ||
""" | ||
latest_version = get_latest_version_from_pypi() | ||
|
||
if not latest_version: | ||
return | ||
|
||
try: | ||
current_parsed = version.parse(__version__) | ||
latest_parsed = version.parse(latest_version) | ||
|
||
# Check if there's a newer minor version available | ||
# We only notify for minor version updates, not patch updates | ||
if latest_parsed.major > current_parsed.major or ( | ||
latest_parsed.major == current_parsed.major and latest_parsed.minor > current_parsed.minor | ||
): | ||
console.print( | ||
f"[bold blue]A newer version of Codeflash is available![/bold blue]\n" | ||
f"Current version: {__version__} | Latest version: {latest_version}\n" | ||
f"Consider upgrading for better quality optimizations.", | ||
style="blue", | ||
) | ||
Comment on lines
+75
to
+80
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that's a lot of info, maybe just a simple one line message would do. also I'm not a big fan of also use the warning log level for that |
||
|
||
except version.InvalidVersion as e: | ||
logger.debug(f"Invalid version format: {e}") | ||
return |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,208 @@ | ||
"""Tests for version checking functionality.""" | ||
|
||
import unittest | ||
from unittest.mock import Mock, patch, MagicMock | ||
from packaging import version | ||
|
||
from codeflash.code_utils.version_check import ( | ||
get_latest_version_from_pypi, | ||
check_for_newer_minor_version, | ||
_version_cache, | ||
_cache_duration | ||
) | ||
|
||
|
||
class TestVersionCheck(unittest.TestCase): | ||
"""Test cases for version checking functionality.""" | ||
|
||
def setUp(self): | ||
"""Reset version cache before each test.""" | ||
_version_cache["version"] = None | ||
_version_cache["timestamp"] = 0 | ||
|
||
def tearDown(self): | ||
"""Clean up after each test.""" | ||
_version_cache["version"] = None | ||
_version_cache["timestamp"] = 0 | ||
|
||
@patch('codeflash.code_utils.version_check.requests.get') | ||
def test_get_latest_version_from_pypi_success(self, mock_get): | ||
"""Test successful version fetch from PyPI.""" | ||
# Mock successful response | ||
mock_response = Mock() | ||
mock_response.status_code = 200 | ||
mock_response.json.return_value = {"info": {"version": "1.2.3"}} | ||
mock_get.return_value = mock_response | ||
|
||
result = get_latest_version_from_pypi() | ||
|
||
self.assertEqual(result, "1.2.3") | ||
mock_get.assert_called_once_with( | ||
"https://pypi.org/pypi/codeflash/json", | ||
timeout=2 | ||
) | ||
|
||
@patch('codeflash.code_utils.version_check.requests.get') | ||
def test_get_latest_version_from_pypi_http_error(self, mock_get): | ||
"""Test handling of HTTP error responses.""" | ||
# Mock HTTP error response | ||
mock_response = Mock() | ||
mock_response.status_code = 404 | ||
mock_get.return_value = mock_response | ||
|
||
result = get_latest_version_from_pypi() | ||
|
||
self.assertIsNone(result) | ||
|
||
@patch('codeflash.code_utils.version_check.requests.get') | ||
def test_get_latest_version_from_pypi_network_error(self, mock_get): | ||
"""Test handling of network errors.""" | ||
# Mock network error | ||
mock_get.side_effect = Exception("Network error") | ||
|
||
result = get_latest_version_from_pypi() | ||
|
||
self.assertIsNone(result) | ||
|
||
@patch('codeflash.code_utils.version_check.requests.get') | ||
def test_get_latest_version_from_pypi_invalid_response(self, mock_get): | ||
"""Test handling of invalid response format.""" | ||
# Mock invalid response format | ||
mock_response = Mock() | ||
mock_response.status_code = 200 | ||
mock_response.json.return_value = {"invalid": "format"} | ||
mock_get.return_value = mock_response | ||
|
||
result = get_latest_version_from_pypi() | ||
|
||
self.assertIsNone(result) | ||
|
||
@patch('codeflash.code_utils.version_check.requests.get') | ||
def test_get_latest_version_from_pypi_caching(self, mock_get): | ||
"""Test that version caching works correctly.""" | ||
# Mock successful response | ||
mock_response = Mock() | ||
mock_response.status_code = 200 | ||
mock_response.json.return_value = {"info": {"version": "1.2.3"}} | ||
mock_get.return_value = mock_response | ||
|
||
# First call should hit the network | ||
result1 = get_latest_version_from_pypi() | ||
self.assertEqual(result1, "1.2.3") | ||
self.assertEqual(mock_get.call_count, 1) | ||
|
||
# Second call should use cache | ||
result2 = get_latest_version_from_pypi() | ||
self.assertEqual(result2, "1.2.3") | ||
self.assertEqual(mock_get.call_count, 1) # Still only 1 call | ||
|
||
@patch('codeflash.code_utils.version_check.requests.get') | ||
def test_get_latest_version_from_pypi_cache_expiry(self, mock_get): | ||
"""Test that cache expires after the specified duration.""" | ||
import time | ||
|
||
# Mock successful response | ||
mock_response = Mock() | ||
mock_response.status_code = 200 | ||
mock_response.json.return_value = {"info": {"version": "1.2.3"}} | ||
mock_get.return_value = mock_response | ||
|
||
# First call | ||
result1 = get_latest_version_from_pypi() | ||
self.assertEqual(result1, "1.2.3") | ||
|
||
# Manually expire the cache | ||
_version_cache["timestamp"] = time.time() - _cache_duration - 1 | ||
|
||
# Second call should hit the network again | ||
result2 = get_latest_version_from_pypi() | ||
self.assertEqual(result2, "1.2.3") | ||
self.assertEqual(mock_get.call_count, 2) | ||
|
||
@patch('codeflash.code_utils.version_check.get_latest_version_from_pypi') | ||
@patch('codeflash.code_utils.version_check.console') | ||
@patch('codeflash.code_utils.version_check.__version__', '1.0.0') | ||
def test_check_for_newer_minor_version_newer_available(self, mock_console, mock_get_version): | ||
"""Test warning message when newer minor version is available.""" | ||
mock_get_version.return_value = "1.1.0" | ||
|
||
check_for_newer_minor_version() | ||
|
||
mock_console.print.assert_called_once() | ||
call_args = mock_console.print.call_args[0][0] | ||
self.assertIn("ℹ️ A newer version of Codeflash is available!", call_args) | ||
self.assertIn("Current version: 1.0.0", call_args) | ||
self.assertIn("Latest version: 1.1.0", call_args) | ||
|
||
@patch('codeflash.code_utils.version_check.get_latest_version_from_pypi') | ||
@patch('codeflash.code_utils.version_check.console') | ||
@patch('codeflash.code_utils.version_check.__version__', '1.0.0') | ||
def test_check_for_newer_minor_version_newer_major_available(self, mock_console, mock_get_version): | ||
"""Test warning message when newer major version is available.""" | ||
mock_get_version.return_value = "2.0.0" | ||
|
||
check_for_newer_minor_version() | ||
|
||
mock_console.print.assert_called_once() | ||
call_args = mock_console.print.call_args[0][0] | ||
self.assertIn("ℹ️ A newer version of Codeflash is available!", call_args) | ||
|
||
@patch('codeflash.code_utils.version_check.get_latest_version_from_pypi') | ||
@patch('codeflash.code_utils.version_check.console') | ||
@patch('codeflash.code_utils.version_check.__version__', '1.1.0') | ||
def test_check_for_newer_minor_version_no_newer_available(self, mock_console, mock_get_version): | ||
"""Test no warning when no newer version is available.""" | ||
mock_get_version.return_value = "1.0.0" | ||
|
||
check_for_newer_minor_version() | ||
|
||
mock_console.print.assert_not_called() | ||
|
||
@patch('codeflash.code_utils.version_check.get_latest_version_from_pypi') | ||
@patch('codeflash.code_utils.version_check.console') | ||
@patch('codeflash.code_utils.version_check.__version__', '1.0.0') | ||
def test_check_for_newer_minor_version_patch_update_ignored(self, mock_console, mock_get_version): | ||
"""Test that patch updates don't trigger warnings.""" | ||
mock_get_version.return_value = "1.0.1" | ||
|
||
check_for_newer_minor_version() | ||
|
||
mock_console.print.assert_not_called() | ||
|
||
@patch('codeflash.code_utils.version_check.get_latest_version_from_pypi') | ||
@patch('codeflash.code_utils.version_check.console') | ||
@patch('codeflash.code_utils.version_check.__version__', '1.0.0') | ||
def test_check_for_newer_minor_version_same_version(self, mock_console, mock_get_version): | ||
"""Test no warning when versions are the same.""" | ||
mock_get_version.return_value = "1.0.0" | ||
|
||
check_for_newer_minor_version() | ||
|
||
mock_console.print.assert_not_called() | ||
|
||
@patch('codeflash.code_utils.version_check.get_latest_version_from_pypi') | ||
@patch('codeflash.code_utils.version_check.console') | ||
@patch('codeflash.code_utils.version_check.__version__', '1.0.0') | ||
def test_check_for_newer_minor_version_no_latest_version(self, mock_console, mock_get_version): | ||
"""Test no warning when latest version cannot be fetched.""" | ||
mock_get_version.return_value = None | ||
|
||
check_for_newer_minor_version() | ||
|
||
mock_console.print.assert_not_called() | ||
|
||
@patch('codeflash.code_utils.version_check.get_latest_version_from_pypi') | ||
@patch('codeflash.code_utils.version_check.console') | ||
@patch('codeflash.code_utils.version_check.__version__', '1.0.0') | ||
def test_check_for_newer_minor_version_invalid_version_format(self, mock_console, mock_get_version): | ||
"""Test handling of invalid version format.""" | ||
mock_get_version.return_value = "invalid-version" | ||
|
||
check_for_newer_minor_version() | ||
|
||
mock_console.print.assert_not_called() | ||
|
||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
personally, I prefer checking
if latest_version < __version__
(major or minor)@Saga4 what do you think ?