Skip to content

Commit 72b4050

Browse files
melange396minhkhuldshemetov
authored
py client version check fixes and cleanup (#1497)
* move _version_check() call outside class definition * more py client version check cleanup/fixes * version check test update --------- Co-authored-by: minhkhul <[email protected]> Co-authored-by: Dmitry Shemetov <[email protected]> Co-authored-by: minhkhul <[email protected]>
1 parent af96e25 commit 72b4050

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

integrations/client/test_delphi_epidata.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -317,19 +317,15 @@ def raise_for_status(self): pass
317317
def json(self): return json.loads(self.content)
318318
get.reset_mock()
319319
get.return_value = MockJson(b'{"info": {"version": "0.0.1"}}', 200)
320+
320321
Epidata._version_check()
322+
321323
captured = self.capsys.readouterr()
322324
output = captured.err.splitlines()
323325
self.assertEqual(len(output), 1)
324326
self.assertIn("Client version not up to date", output[0])
325327
self.assertIn("\'latest_version\': \'0.0.1\'", output[0])
326328

327-
@patch('delphi.epidata.client.delphi_epidata.Epidata._version_check')
328-
def test_version_check_once(self, version_check):
329-
"""Test that the _version_check() function is only called once on initial module import."""
330-
from delphi.epidata.client.delphi_epidata import Epidata
331-
version_check.assert_not_called()
332-
333329
def test_geo_value(self):
334330
"""test different variants of geo types: single, *, multi."""
335331

src/client/delphi_epidata.py

+19-11
Original file line numberDiff line numberDiff line change
@@ -52,23 +52,24 @@ def log(evt, **kwargs):
5252
kwargs['timestamp'] = time.strftime("%Y-%m-%d %H:%M:%S %z")
5353
return sys.stderr.write(str(kwargs) + "\n")
5454

55-
# Check that this client's version matches the most recent available, runs just once per program execution (on initial module load).
55+
# Check that this client's version matches the most recent available. This
56+
# is intended to run just once per program execution, on initial module load.
57+
# See the bottom of this file for the ultimate call to this method.
5658
@staticmethod
5759
def _version_check():
5860
try:
59-
latest_version = requests.get('https://pypi.org/pypi/delphi-epidata/json').json()['info']['version']
60-
if latest_version != __version__:
61-
Epidata.log(
62-
"Client version not up to date",
63-
client_version=__version__,
64-
latest_version=latest_version
65-
)
61+
request = requests.get('https://pypi.org/pypi/delphi-epidata/json', timeout=5)
62+
latest_version = request.json()['info']['version']
6663
except Exception as e:
6764
Epidata.log("Error getting latest client version", exception=str(e))
65+
return
6866

69-
# Run this once on module load. Use dunder method for Python <= 3.9 compatibility
70-
# https://stackoverflow.com/a/12718272
71-
_version_check.__func__()
67+
if latest_version != __version__:
68+
Epidata.log(
69+
"Client version not up to date",
70+
client_version=__version__,
71+
latest_version=latest_version
72+
)
7273

7374
# Helper function to cast values and/or ranges to strings
7475
@staticmethod
@@ -708,3 +709,10 @@ async def async_make_calls(param_combos):
708709
future = asyncio.ensure_future(async_make_calls(param_list))
709710
responses = loop.run_until_complete(future)
710711
return responses
712+
713+
714+
715+
# This should only run once per program execution, on initial module load,
716+
# as a result of how Python's module system works:
717+
# https://docs.python.org/3/reference/import.html#the-module-cache
718+
Epidata._version_check()

0 commit comments

Comments
 (0)