Skip to content

Commit

Permalink
Fixed client version compatibility check (#148)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Anton Sergeyev <anton.sergeyev@meetiqm.com>
  • Loading branch information
antonsergeyev and antonsergeyev authored Nov 19, 2024
1 parent 487d6ca commit 51c875f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 13 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
Changelog
=========

Version 20.5
============

* Fixed client version compatibility check. `#148 <https://github.com/iqm-finland/iqm-client/pull/148>`_

Version 20.4
============
* ``active_reset_cycles`` added to ``CircuitCompilationOptions`` (in 20.2 it was only added to ``RunRequest`` making it
Expand Down
25 changes: 15 additions & 10 deletions src/iqm/iqm_client/iqm_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -936,14 +936,19 @@ def _check_versions(self) -> Optional[str]:
timeout=REQUESTS_TIMEOUT,
)
if versions_response.status_code == 200:
compatible_versions = versions_response.json()['iqm-client']
min_version = parse(compatible_versions['min'])
max_version = parse(compatible_versions['max'])
client_version = parse(version('iqm-client'))
if client_version < min_version or client_version >= max_version:
return (
f'Your IQM Client version {client_version} was built for a different version of IQM Server. '
f'You might encounter issues. For the best experience, consider using a version '
f'of IQM Client that satisfies {min_version} <= iqm-client < {max_version}.'
)
try:
libraries = versions_response.json()
compatible_versions = libraries.get('iqm-client', libraries.get('iqm_client'))
min_version = parse(compatible_versions['min'])
max_version = parse(compatible_versions['max'])
client_version = parse(version('iqm-client'))
if client_version < min_version or client_version >= max_version:
return (
f'Your IQM Client version {client_version} was built for a different version of IQM Server. '
f'You might encounter issues. For the best experience, consider using a version '
f'of IQM Client that satisfies {min_version} <= iqm-client < {max_version}.'
)
except Exception: # pylint: disable=broad-except
return 'Could not verify IQM client library compatibility. You might encounter issues.'

return None
28 changes: 25 additions & 3 deletions tests/test_iqm_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1041,8 +1041,14 @@ def test_get_dynamic_quantum_architecture_not_found(base_url, sample_client):
unstub()


@pytest.mark.parametrize('server_version_diff', [0, 1])
def test_check_versions(base_url, server_version_diff, recwarn):
@pytest.mark.parametrize(
'iqm_client_name,server_version_diff',
[
('iqm_client', 0),
('iqm-client', 1),
],
)
def test_check_versions_success(base_url, iqm_client_name, server_version_diff, recwarn):
"""Test that a warning about version incompatibility is shown when initializing client with incompatible server."""
client_version = parse(version('iqm-client'))
min_version = f'{client_version.major + server_version_diff}.0'
Expand All @@ -1051,7 +1057,7 @@ def test_check_versions(base_url, server_version_diff, recwarn):
MockJsonResponse(
200,
{
'iqm-client': {
iqm_client_name: {
'min': min_version,
'max': max_version,
}
Expand All @@ -1072,3 +1078,19 @@ def test_check_versions(base_url, server_version_diff, recwarn):
):
IQMClient(base_url)
unstub()


def test_check_versions_bad_response(base_url):
"""Test that unexpected response from /client-libraries endpoint does not break the client."""
when(requests).get(f'{base_url}/info/client-libraries', headers=ANY, timeout=ANY).thenReturn(
MockJsonResponse(
200,
{'iqm_client': 'invalid-payload'},
)
)
with pytest.warns(
UserWarning,
match=re.escape('Could not verify IQM client library compatibility. You might encounter issues.'),
):
IQMClient(base_url)
unstub()

0 comments on commit 51c875f

Please sign in to comment.