Coverage for src\llm_code_lens\version.py: 32%

31 statements  

« prev     ^ index     » next       coverage.py v7.7.0, created at 2025-05-25 12:07 +0300

1""" 

2Version checking module for LLM Code Lens. 

3Checks for newer versions on PyPI and notifies users. 

4""" 

5 

6import requests 

7import time 

8from packaging import version 

9from typing import Optional, Tuple 

10from rich.console import Console 

11 

12# Import the current version 

13from . import __version__ 

14 

15console = Console() 

16 

17def check_for_newer_version() -> None: 

18 """ 

19 Check if a newer version is available on PyPI and print a notification if so. 

20 Designed to fail silently if checks cannot be performed. 

21 """ 

22 try: 

23 current = _get_current_version() 

24 latest, pkg_url = _get_latest_version() 

25 

26 if latest and version.parse(latest) > version.parse(current): 

27 console.print(f"[bold yellow]⚠️ A newer version ({latest}) of LLM Code Lens is available![/]") 

28 console.print(f"[yellow]You're currently using version {current}[/]") 

29 console.print(f"[yellow]Upgrade with: pip install --upgrade llm_code_lens[/]") 

30 console.print(f"[yellow]See what's new: {pkg_url}[/]\n") 

31 except Exception: 

32 # Fail silently - version check should never interrupt main functionality 

33 pass 

34 

35def _get_current_version() -> str: 

36 """Get the current package version.""" 

37 return __version__ 

38 

39def _get_latest_version() -> Tuple[Optional[str], str]: 

40 """ 

41 Fetch the latest version from PyPI. 

42  

43 Returns: 

44 Tuple containing (latest_version, package_url) or (None, default_url) on failure 

45 """ 

46 default_url = "https://pypi.org/project/llm-code-lens/" 

47 

48 try: 

49 # Use a short timeout to prevent hanging 

50 response = requests.get( 

51 "https://pypi.org/pypi/llm-code-lens/json", 

52 timeout=3.0 

53 ) 

54 

55 if response.status_code == 200: 

56 data = response.json() 

57 latest_version = data.get("info", {}).get("version") 

58 return latest_version, default_url 

59 

60 return None, default_url 

61 except (requests.RequestException, ValueError, KeyError): 

62 # Handle network issues, JSON parsing errors, or missing keys 

63 return None, default_url