Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ lint:
tools/verify_headers.py qiskit_ibm_runtime test

mypy:
mypy --module qiskit_ibm_runtime
mypy --module qiskit_ibm_runtime --package test

style:
black --check qiskit_ibm_runtime setup.py test docs/tutorials program_source
Expand Down
5 changes: 5 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
source = qiskit_ibm_runtime

[mypy]
exclude = test/unit/test_jupyter.py
python_version = 3.7
namespace_packages = True
ignore_missing_imports = True
Expand All @@ -15,3 +16,7 @@ strict_optional = False
show_none_errors = False
show_error_codes = True
no_site_packages = True

[mypy-test.*]
disallow_untyped_calls = False
disallow_untyped_defs = False
4 changes: 2 additions & 2 deletions test/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import os
from dataclasses import dataclass
from functools import wraps
from typing import Optional, List, Any
from typing import Callable, Optional, List, Any
from unittest import SkipTest

from qiskit_ibm_runtime import IBMRuntimeService
Expand Down Expand Up @@ -71,7 +71,7 @@ def _wrapper(self, *args, **kwargs):
def integration_test_setup(
supported_auth: Optional[List[str]] = None,
init_service: Optional[bool] = True,
):
) -> Callable:
"""Returns a decorator for integration test initialization.

Args:
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/test_tutorials.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
]


def _is_supported(auth: str, tutorial_filename: str):
def _is_supported(auth: str, tutorial_filename: str) -> bool:
"""Not all tutorials work for all auth types. Check if the given tutorial is supported by the
targeted environment."""
allowlist = (
Expand Down
27 changes: 18 additions & 9 deletions test/ibm_test_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
import unittest
from contextlib import suppress
from collections import defaultdict
from typing import DefaultDict, Dict

from qiskit_ibm_runtime import QISKIT_IBM_RUNTIME_LOGGER_NAME
from qiskit_ibm_runtime.exceptions import IBMNotAuthorizedError
from qiskit_ibm_runtime import IBMRuntimeService

from .utils import setup_test_logging
from .decorators import IntegrationTestDependencies, integration_test_setup
Expand All @@ -31,6 +33,11 @@
class IBMTestCase(unittest.TestCase):
"""Custom TestCase for use with qiskit-ibm-runtime."""

log: logging.Logger
dependencies: IntegrationTestDependencies
service: IBMRuntimeService
program_ids: Dict[str, str]

@classmethod
def setUpClass(cls):
super().setUpClass()
Expand Down Expand Up @@ -68,7 +75,7 @@ class IBMIntegrationTestCase(IBMTestCase):

@classmethod
@integration_test_setup()
def setUpClass(cls, dependencies: IntegrationTestDependencies):
def setUpClass(cls, dependencies: IntegrationTestDependencies) -> None:
"""Initial class level setup."""
# pylint: disable=arguments-differ
super().setUpClass()
Expand All @@ -78,8 +85,8 @@ def setUpClass(cls, dependencies: IntegrationTestDependencies):
def setUp(self) -> None:
"""Test level setup."""
super().setUp()
self.to_delete = defaultdict(list)
self.to_cancel = defaultdict(list)
self.to_delete: DefaultDict = defaultdict(list)
self.to_cancel: DefaultDict = defaultdict(list)

def tearDown(self) -> None:
"""Test level teardown."""
Expand All @@ -99,12 +106,12 @@ def tearDown(self) -> None:

def _upload_program(
self,
service,
name=None,
max_execution_time=300,
data=None,
service: IBMRuntimeService,
name: str = None,
max_execution_time: int = 300,
data: str = None,
is_public: bool = False,
):
) -> str:
"""Upload a new program."""
name = name or PROGRAM_PREFIX
data = data or RUNTIME_PROGRAM
Expand Down Expand Up @@ -138,7 +145,9 @@ def tearDownClass(cls) -> None:
service = cls.service
service.delete_program(cls.program_ids[service.auth])
cls.log.debug(
"Deleted %s program %s", service.auth, cls.program_ids[service.auth]
"Deleted %s program %s",
service.auth,
cls.program_ids[service.auth],
)

@classmethod
Expand Down
14 changes: 7 additions & 7 deletions test/integration/test_auth_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,49 +25,49 @@ class TestAuthClient(IBMTestCase):
"""Tests for the AuthClient."""

@integration_test_setup(supported_auth=["legacy"], init_service=False)
def test_valid_login(self, dependencies: IntegrationTestDependencies):
def test_valid_login(self, dependencies: IntegrationTestDependencies) -> None:
"""Test valid authentication."""
client = self._init_auth_client(dependencies.token, dependencies.url)
self.assertTrue(client.access_token)

@integration_test_setup(supported_auth=["legacy"], init_service=False)
def test_url_404(self, dependencies: IntegrationTestDependencies):
def test_url_404(self, dependencies: IntegrationTestDependencies) -> None:
"""Test login against a 404 URL"""
url_404 = re.sub(r"/api.*$", "/api/TEST_404", dependencies.url)
with self.assertRaises(ApiError):
_ = self._init_auth_client(dependencies.token, url_404)

@integration_test_setup(supported_auth=["legacy"], init_service=False)
def test_invalid_token(self, dependencies: IntegrationTestDependencies):
def test_invalid_token(self, dependencies: IntegrationTestDependencies) -> None:
"""Test login using invalid token."""
qe_token = "INVALID_TOKEN"
with self.assertRaises(ApiError):
_ = self._init_auth_client(qe_token, dependencies.url)

@integration_test_setup(supported_auth=["legacy"], init_service=False)
def test_url_unreachable(self, dependencies: IntegrationTestDependencies):
def test_url_unreachable(self, dependencies: IntegrationTestDependencies) -> None:
"""Test login against an invalid (malformed) URL."""
qe_url = "INVALID_URL"
with self.assertRaises(ApiError):
_ = self._init_auth_client(dependencies.token, qe_url)

@integration_test_setup(supported_auth=["legacy"], init_service=False)
def test_api_version(self, dependencies: IntegrationTestDependencies):
def test_api_version(self, dependencies: IntegrationTestDependencies) -> None:
"""Check the version of the QX API."""
client = self._init_auth_client(dependencies.token, dependencies.url)
version = client.api_version()
self.assertIsNotNone(version)

@integration_test_setup(supported_auth=["legacy"], init_service=False)
def test_user_urls(self, dependencies: IntegrationTestDependencies):
def test_user_urls(self, dependencies: IntegrationTestDependencies) -> None:
"""Check the user urls of the QX API."""
client = self._init_auth_client(dependencies.token, dependencies.url)
user_urls = client.user_urls()
self.assertIsNotNone(user_urls)
self.assertTrue("http" in user_urls and "ws" in user_urls)

@integration_test_setup(supported_auth=["legacy"], init_service=False)
def test_user_hubs(self, dependencies: IntegrationTestDependencies):
def test_user_hubs(self, dependencies: IntegrationTestDependencies) -> None:
"""Check the user hubs of the QX API."""
client = self._init_auth_client(dependencies.token, dependencies.url)
user_hubs = client.user_hubs()
Expand Down
6 changes: 3 additions & 3 deletions test/integration/test_backend_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

"""Test deserializing server data."""

from typing import Any, Dict, Optional
from typing import Any, Dict, Set, Optional

import dateutil.parser

Expand Down Expand Up @@ -88,7 +88,7 @@ def test_backend_properties(self, service):

def _verify_data(
self, data: Dict, good_keys: tuple, good_key_prefixes: Optional[tuple] = None
):
) -> None:
"""Verify that the input data does not contain serialized objects.

Args:
Expand All @@ -97,7 +97,7 @@ def _verify_data(
good_key_prefixes: A list of known prefixes for keys that look like
serialized objects.
"""
suspect_keys = set()
suspect_keys: Set[Any] = set()
_find_potential_encoded(data, "", suspect_keys)
# Remove known good keys from suspect keys.
for gkey in good_keys:
Expand Down
6 changes: 3 additions & 3 deletions test/integration/test_basic_server_paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ class TestBasicServerPaths(IBMTestCase):

@classmethod
@integration_test_setup(supported_auth=["legacy"])
def setUpClass(cls, dependencies: IntegrationTestDependencies):
def setUpClass(cls, dependencies: IntegrationTestDependencies) -> None:
# pylint: disable=arguments-differ
super().setUpClass()
cls.service = dependencies.service
cls.hgps = list(dependencies.service._hgps.keys())
cls.service = dependencies.service # type: ignore
cls.hgps = list(dependencies.service._hgps.keys()) # type: ignore

def _require_2_hgps(self):
if len(self.hgps) < 2:
Expand Down
30 changes: 18 additions & 12 deletions test/integration/test_proxies.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def tearDown(self):
@integration_test_setup(supported_auth=["cloud"])
def test_proxies_cloud_runtime_client(
self, dependencies: IntegrationTestDependencies
):
) -> None:
"""Should reach the proxy using RuntimeClient."""
# pylint: disable=unused-argument
params = dependencies.service._client_params
Expand All @@ -73,7 +73,7 @@ def test_proxies_cloud_runtime_client(
@integration_test_setup(supported_auth=["legacy"], init_service=False)
def test_proxies_legacy_runtime_client(
self, dependencies: IntegrationTestDependencies
):
) -> None:
"""Should reach the proxy using RuntimeClient."""
service = IBMRuntimeService(
auth="legacy",
Expand All @@ -95,7 +95,9 @@ def test_proxies_legacy_runtime_client(
self.assertIn(api_line, proxy_output)

@integration_test_setup(supported_auth=["legacy"], init_service=False)
def test_proxies_account_client(self, dependencies: IntegrationTestDependencies):
def test_proxies_account_client(
self, dependencies: IntegrationTestDependencies
) -> None:
"""Should reach the proxy using AccountClient."""
service = IBMRuntimeService(
auth="legacy",
Expand All @@ -117,7 +119,9 @@ def test_proxies_account_client(self, dependencies: IntegrationTestDependencies)
self.assertIn(api_line, proxy_output)

@integration_test_setup(supported_auth=["legacy"], init_service=False)
def test_proxies_authclient(self, dependencies: IntegrationTestDependencies):
def test_proxies_authclient(
self, dependencies: IntegrationTestDependencies
) -> None:
"""Should reach the proxy using AuthClient."""
pproxy_desired_access_log_line_ = pproxy_desired_access_log_line(
dependencies.url
Expand All @@ -138,7 +142,9 @@ def test_proxies_authclient(self, dependencies: IntegrationTestDependencies):
)

@integration_test_setup(supported_auth=["legacy"], init_service=False)
def test_proxies_versionclient(self, dependencies: IntegrationTestDependencies):
def test_proxies_versionclient(
self, dependencies: IntegrationTestDependencies
) -> None:
"""Should reach the proxy using IBMVersionFinder."""
pproxy_desired_access_log_line_ = pproxy_desired_access_log_line(
dependencies.url
Expand All @@ -156,7 +162,7 @@ def test_proxies_versionclient(self, dependencies: IntegrationTestDependencies):
@integration_test_setup(supported_auth=["legacy"], init_service=False)
def test_invalid_proxy_port_runtime_client(
self, dependencies: IntegrationTestDependencies
):
) -> None:
"""Should raise RequestApiError with ProxyError using RuntimeClient."""
params = ClientParameters(
auth_type="legacy",
Expand All @@ -172,7 +178,7 @@ def test_invalid_proxy_port_runtime_client(
@integration_test_setup(supported_auth=["legacy"], init_service=False)
def test_invalid_proxy_port_authclient(
self, dependencies: IntegrationTestDependencies
):
) -> None:
"""Should raise RequestApiError with ProxyError using AuthClient."""
params = ClientParameters(
auth_type="legacy",
Expand All @@ -188,7 +194,7 @@ def test_invalid_proxy_port_authclient(
@integration_test_setup(supported_auth=["legacy"], init_service=False)
def test_invalid_proxy_port_versionclient(
self, dependencies: IntegrationTestDependencies
):
) -> None:
"""Should raise RequestApiError with ProxyError using VersionClient."""
with self.assertRaises(RequestsApiError) as context_manager:
version_finder = VersionClient(
Expand All @@ -201,7 +207,7 @@ def test_invalid_proxy_port_versionclient(
@integration_test_setup(supported_auth=["legacy"], init_service=False)
def test_invalid_proxy_address_runtime_client(
self, dependencies: IntegrationTestDependencies
):
) -> None:
"""Should raise RequestApiError with ProxyError using RuntimeClient."""
params = ClientParameters(
auth_type="legacy",
Expand All @@ -218,7 +224,7 @@ def test_invalid_proxy_address_runtime_client(
@integration_test_setup(supported_auth=["legacy"], init_service=False)
def test_invalid_proxy_address_authclient(
self, dependencies: IntegrationTestDependencies
):
) -> None:
"""Should raise RequestApiError with ProxyError using AuthClient."""
params = ClientParameters(
auth_type="legacy",
Expand All @@ -234,7 +240,7 @@ def test_invalid_proxy_address_authclient(
@integration_test_setup(supported_auth=["legacy"], init_service=False)
def test_invalid_proxy_address_versionclient(
self, dependencies: IntegrationTestDependencies
):
) -> None:
"""Should raise RequestApiError with ProxyError using VersionClient."""
with self.assertRaises(RequestsApiError) as context_manager:
version_finder = VersionClient(
Expand All @@ -245,7 +251,7 @@ def test_invalid_proxy_address_versionclient(
self.assertIsInstance(context_manager.exception.__cause__, ProxyError)

@integration_test_setup(supported_auth=["legacy"], init_service=False)
def test_proxy_urls(self, dependencies: IntegrationTestDependencies):
def test_proxy_urls(self, dependencies: IntegrationTestDependencies) -> None:
"""Test different forms of the proxy urls."""
test_urls = [
"http://{}:{}".format(ADDRESS, PORT),
Expand Down
8 changes: 7 additions & 1 deletion test/program.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import uuid
import copy
from qiskit_ibm_runtime import IBMRuntimeService


DEFAULT_DATA = "def main() {}"
Expand Down Expand Up @@ -50,7 +51,12 @@
}


def upload_program(service, name=None, max_execution_time=300, is_public: bool = False):
def upload_program(
service: IBMRuntimeService,
name: str = None,
max_execution_time: int = 300,
is_public: bool = False,
) -> str:
"""Upload a new program."""
name = name or uuid.uuid4().hex
data = DEFAULT_DATA
Expand Down
2 changes: 1 addition & 1 deletion test/unit/mock/fake_account_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def __init__(
config["backend_name"] = f"backend{idx}"
self._backends.append(FakeApiBackend(config, status))

def list_backends(self, *args, **kwargs) -> List[Dict[str, Any]]:
def list_backends(self) -> List[Dict[str, Any]]:
"""Return backends available for this provider."""
# pylint: disable=unused-argument
return [back.configuration.copy() for back in self._backends]
Expand Down
2 changes: 1 addition & 1 deletion test/unit/mock/fake_legacy_auth_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def __init__(self, *args, **kwargs):
"""Initialize a auth runtime client."""
pass

def user_urls(self) -> Dict[str, str]:
def user_urls(self) -> Dict[str, Union[str, Dict]]:
"""Retrieve the API URLs from the authentication service.

Returns:
Expand Down
Loading