Skip to content

Commit

Permalink
Add type hints and mypy checks (#14)
Browse files Browse the repository at this point in the history
* Add type hints

* Configure mypy checks

* Fix typing issues

* Handle potential ConflictError when configuring uwm
  • Loading branch information
adolfo-ab authored Aug 8, 2024
1 parent 3e6b93e commit 94e457f
Show file tree
Hide file tree
Showing 15 changed files with 312 additions and 157 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@ dist/

# Pycharm
.idea

# mypy
.mypy_cache/
16 changes: 16 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,19 @@ repos:
rev: "v8.18.2"
hooks:
- id: gitleaks

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.11.1
hooks:
- id: mypy
additional_dependencies: ["types-PyYAML", "types-requests"]
args:
- --ignore-missing-imports
- --follow-imports=silent
- --disable-error-code=attr-defined
- --disable-error-code=name-defined
- --disable-error-code=call-arg
- --disable-error-code=arg-type
- --disable-error-code=assignment
- --allow-untyped-defs
- --allow-untyped-calls
6 changes: 6 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ pytest = "^8.2.1"
openshift-python-utilities = "^5.0.51"
setuptools = "^70.0.0"

[tool.mypy]
no_implicit_optional = true
show_error_codes = true
warn_unused_ignores = true
ignore_missing_imports = true

[tool.ruff]
preview = true
line-length = 120
Expand Down
76 changes: 46 additions & 30 deletions trustyai_tests/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
from typing import Any, Generator

import pytest
import yaml
from kubernetes.dynamic import DynamicClient
from kubernetes.dynamic.exceptions import ConflictError
from ocp_resources.configmap import ConfigMap
from ocp_resources.namespace import Namespace
from ocp_resources.resource import get_client
Expand All @@ -17,12 +21,12 @@


@pytest.fixture(scope="session")
def client():
def client() -> DynamicClient:
yield get_client()


@pytest.fixture(autouse=True, scope="session")
def modelmesh_configmap():
def modelmesh_configmap() -> ConfigMap:
operator = is_odh_or_rhoai()
namespace = Namespace(
name="opendatahub" if operator == ODH_OPERATOR else "redhat-ods-applications", ensure_exists=True
Expand All @@ -31,12 +35,12 @@ def modelmesh_configmap():
name="model-serving-config",
namespace=namespace.name,
data={"config.yaml": yaml.dump({"podsPerRuntime": 1})},
):
yield
) as cm:
yield cm


@pytest.fixture(scope="class")
def model_namespace(client):
def model_namespace(client: DynamicClient) -> Namespace:
with Namespace(
client=client,
name="test-namespace",
Expand All @@ -60,41 +64,51 @@ def model_namespace(client):


@pytest.fixture(scope="class")
def modelmesh_serviceaccount(client, model_namespace):
def modelmesh_serviceaccount(client: DynamicClient, model_namespace: Namespace) -> Any:
with ServiceAccount(client=client, name="modelmesh-serving-sa", namespace=model_namespace.name):
yield


@pytest.fixture(scope="session")
def cluster_monitoring_config(client):
def cluster_monitoring_config(client: DynamicClient) -> ConfigMap:
config_yaml = yaml.dump({"enableUserWorkload": "true"})
with ConfigMap(
name="cluster-monitoring-config",
namespace="openshift-monitoring",
data={"config.yaml": config_yaml},
) as cm:
yield cm
name = "cluster-monitoring-config"
namespace = "openshift-monitoring"
try:
with ConfigMap(
name=name,
namespace=namespace,
data={"config.yaml": config_yaml},
) as cm:
yield cm
except ConflictError:
yield ConfigMap(name=name, namespace=namespace)


@pytest.fixture(scope="session")
def user_workload_monitoring_config(client):
def user_workload_monitoring_config(client: DynamicClient) -> ConfigMap:
config_yaml = yaml.dump({"prometheus": {"logLevel": "debug", "retention": "15d"}})
with ConfigMap(
name="user-workload-monitoring-config",
namespace="openshift-user-workload-monitoring",
data={"config.yaml": config_yaml},
) as cm:
yield cm
name = "user-workload-monitoring-config"
namespace = "openshift-user-workload-monitoring"
try:
with ConfigMap(
name=name,
namespace=namespace,
data={"config.yaml": config_yaml},
) as cm:
yield cm
except ConflictError:
yield ConfigMap(name=name, namespace=namespace)


@pytest.fixture(scope="class")
def trustyai_service(
client,
model_namespace,
modelmesh_serviceaccount,
cluster_monitoring_config,
user_workload_monitoring_config,
):
client: DynamicClient,
model_namespace: Namespace,
modelmesh_serviceaccount: Any,
cluster_monitoring_config: ConfigMap,
user_workload_monitoring_config: ConfigMap,
) -> TrustyAIService:
with TrustyAIService(
client=client,
name=TRUSTYAI_SERVICE,
Expand All @@ -107,7 +121,7 @@ def trustyai_service(


@pytest.fixture(scope="class")
def minio_service(client, model_namespace):
def minio_service(client: DynamicClient, model_namespace: Namespace) -> Generator[MinioService, Any, None]:
with MinioService(
name="minio",
port=9000,
Expand All @@ -119,7 +133,7 @@ def minio_service(client, model_namespace):


@pytest.fixture(scope="class")
def minio_pod(client, model_namespace):
def minio_pod(client: DynamicClient, model_namespace: Namespace) -> Generator[MinioPod, Any, None]:
with MinioPod(
client=client,
name="minio",
Expand All @@ -130,7 +144,7 @@ def minio_pod(client, model_namespace):


@pytest.fixture(scope="class")
def minio_secret(client, model_namespace):
def minio_secret(client: DynamicClient, model_namespace: Namespace) -> Generator[MinioSecret, Any, None]:
with MinioSecret(
client=client,
name=MINIO_DATA_CONNECTION_NAME,
Expand All @@ -146,5 +160,7 @@ def minio_secret(client, model_namespace):


@pytest.fixture(scope="class")
def minio_data_connection(minio_service, minio_pod, minio_secret):
def minio_data_connection(
minio_service: MinioService, minio_pod: MinioPod, minio_secret: MinioSecret
) -> Generator[MinioSecret, Any, None]:
yield minio_secret
21 changes: 11 additions & 10 deletions trustyai_tests/tests/constants.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
TRUSTYAI_SERVICE = "trustyai-service"
TRUSTYAI_SERVICE: str = "trustyai-service"

OPENVINO_MODEL_FORMAT = "openvino_ir"
OPENVINO_MODEL_FORMAT: str = "openvino_ir"

KSERVE_API_GROUP = "serving.kserve.io"
KSERVE_API_GROUP: str = "serving.kserve.io"

TRUSTYAI_API_GROUP = "trustyai.opendatahub.io"
TRUSTYAI_API_GROUP: str = "trustyai.opendatahub.io"

OPENDATAHUB_IO = "opendatahub.io"
OPENDATAHUB_IO: str = "opendatahub.io"

MODEL_DATA_PATH = "./trustyai_tests/model_data"
MODEL_DATA_PATH: str = "./trustyai_tests/model_data"

MINIO_DATA_CONNECTION_NAME = "aws-connection-minio-data-connection"
MINIO_DATA_CONNECTION_NAME: str = "aws-connection-minio-data-connection"

KNATIVE_API_GROUP = "serving.knative.dev"
KNATIVE_API_GROUP: str = "serving.knative.dev"

ODH_OPERATOR = "opendatahub-operator"
RHOAI_OPERATOR = "rhods-operator"
ODH_OPERATOR: str = "opendatahub-operator"

RHOAI_OPERATOR: str = "rhods-operator"
24 changes: 17 additions & 7 deletions trustyai_tests/tests/drift/conftest.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import pytest
from kubernetes.dynamic import DynamicClient
from ocp_resources.inference_service import InferenceService
from ocp_resources.namespace import Namespace
from ocp_resources.serving_runtime import ServingRuntime

from trustyai_tests.tests.constants import KSERVE_API_GROUP
from trustyai_tests.tests.minio import MinioSecret

SKLEARN = "sklearn"
XGBOOST = "xgboost"
MLSERVER = "mlserver"
MLSERVER_RUNTIME_NAME = f"{MLSERVER}-1.x"
MLSERVER_QUAY_IMAGE = "quay.io/aaguirre/mlserver:1.3.2" # TODO: Change it to TrustyAI specific quay
SKLEARN: str = "sklearn"
XGBOOST: str = "xgboost"
MLSERVER: str = "mlserver"
MLSERVER_RUNTIME_NAME: str = f"{MLSERVER}-1.x"
MLSERVER_QUAY_IMAGE: str = "quay.io/aaguirre/mlserver:1.3.2" # TODO: Change it to TrustyAI specific quay


@pytest.fixture(scope="class")
def mlserver_runtime(client, minio_data_connection, model_namespace):
def mlserver_runtime(
client: DynamicClient, minio_data_connection: MinioSecret, model_namespace: Namespace
) -> ServingRuntime:
supported_model_formats = [
{"name": SKLEARN, "version": "0", "autoselect": "true"},
{"name": XGBOOST, "version": "1", "autoselect": "true"},
Expand Down Expand Up @@ -58,7 +63,12 @@ def mlserver_runtime(client, minio_data_connection, model_namespace):


@pytest.fixture(scope="class")
def gaussian_credit_model(client, model_namespace, minio_data_connection, mlserver_runtime):
def gaussian_credit_model(
client: DynamicClient,
model_namespace: Namespace,
minio_data_connection: MinioSecret,
mlserver_runtime: ServingRuntime,
) -> InferenceService:
with InferenceService(
client=client,
name="gaussian-credit-model",
Expand Down
Loading

0 comments on commit 94e457f

Please sign in to comment.