Skip to content

Commit c4dbd00

Browse files
sanggustipre-commit-ci[bot]deependujha
authored
feat: add support to disable external version checks (Lightning-AI#737)
* Feat: Add mechanism to disable external version checks (PyPI API calls) for air-gapped environments Fixes Lightning-AI#734 * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Add changelog * Fix: Adjusted by comments and request * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix: remove redundant of `_LITDATA_DISABLE_VERSION` at `_check_version_and_prompt_upgrade` * Update src/litdata/helpers.py by merging condition of disable version check and parsing current version Co-authored-by: Deependu <deependujha21@gmail.com> * Update src/litdata/CHANGELOG.md Co-authored-by: Deependu <deependujha21@gmail.com> * Feat: use monkeypatch for setting environment variable * Update src/litdata/CHANGELOG.md * Update src/litdata/constants.py * update * update --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Deependu <deependujha21@gmail.com>
1 parent 359bbf1 commit c4dbd00

File tree

4 files changed

+52
-1
lines changed

4 files changed

+52
-1
lines changed

src/litdata/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
1111
### Added
1212

1313
- Introduced `CHANGELOG.md` to track changes across releases ([#733](https://github.com/lightning-ai/litdata/pull/733))
14+
- Add environment variable `LITDATA_DISABLE_VERSION_CHECK` to disable PyPI version check ([#737](https://github.com/Lightning-AI/litData/pull/737))
1415

1516
### Changed
1617

src/litdata/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555

5656
_MAX_WAIT_TIME = int(os.getenv("MAX_WAIT_TIME", "120"))
5757
_FORCE_DOWNLOAD_TIME = int(os.getenv("FORCE_DOWNLOAD_TIME", "30"))
58+
_LITDATA_DISABLE_VERSION_CHECK = int(os.getenv("LITDATA_DISABLE_VERSION_CHECK", "0"))
5859

5960
# DON'T CHANGE ORDER
6061
_TORCH_DTYPES_MAPPING = {

src/litdata/helpers.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import requests
66
from packaging import version as packaging_version
77

8+
from litdata.constants import _LITDATA_DISABLE_VERSION_CHECK
9+
810

911
class WarningCache(set):
1012
"""Cache for warnings."""
@@ -28,7 +30,7 @@ def _get_newer_version(curr_version: str) -> Optional[str]:
2830
Returning the newest version if different from the current or ``None`` otherwise.
2931
3032
"""
31-
if packaging_version.parse(curr_version).is_prerelease:
33+
if _LITDATA_DISABLE_VERSION_CHECK == 1 or packaging_version.parse(curr_version).is_prerelease:
3234
return None
3335
try:
3436
response = requests.get(f"https://pypi.org/pypi/{__package_name__}/json", timeout=30)

tests/test_helper.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import warnings
2+
from unittest.mock import Mock, patch
3+
4+
import pytest
5+
6+
from litdata.helpers import _check_version_and_prompt_upgrade, _get_newer_version
7+
8+
9+
@pytest.mark.parametrize("disable_version_check", [1, 0, None])
10+
def test_get_newer_version_respects_env_flag(monkeypatch, disable_version_check):
11+
"""Verify that _get_newer_version respects LITDATA_DISABLE_VERSION_CHECK and skips requests when disabled."""
12+
if disable_version_check is not None:
13+
monkeypatch.setattr("litdata.helpers._LITDATA_DISABLE_VERSION_CHECK", disable_version_check)
14+
15+
# Mock requests.get
16+
mock_get = Mock()
17+
mock_get.return_value.json.return_value = {
18+
"releases": {"0.2.50": [], "2.51.0": []},
19+
"info": {"version": "2.51.0", "yanked": False},
20+
}
21+
22+
monkeypatch.setattr("litdata.helpers.requests.get", mock_get)
23+
24+
# Clear cached function results
25+
_get_newer_version.cache_clear()
26+
27+
result = _get_newer_version("0.2.50")
28+
29+
if disable_version_check:
30+
assert result is None
31+
mock_get.assert_not_called()
32+
else:
33+
assert result == "2.51.0"
34+
mock_get.assert_called_once_with("https://pypi.org/pypi/litdata/json", timeout=30)
35+
36+
37+
@patch("litdata.helpers._get_newer_version")
38+
def test_check_version_default_behavior_warning(mock_get_newer, monkeypatch):
39+
"""Test default behavior: calls _get_newer_version and warns if newer version exists."""
40+
mock_get_newer.return_value = "0.2.58"
41+
42+
with warnings.catch_warnings(record=True) as w:
43+
warnings.simplefilter("always")
44+
_check_version_and_prompt_upgrade("0.2.50")
45+
assert len(w) == 1
46+
assert f"A newer version of litdata is available ({mock_get_newer.return_value})" in str(w[0].message)
47+
mock_get_newer.assert_called_once_with("0.2.50")

0 commit comments

Comments
 (0)