Skip to content

Commit

Permalink
chore: python is sad
Browse files Browse the repository at this point in the history
Signed-off-by: Aaron Pham <29749331+aarnphm@users.noreply.github.com>
  • Loading branch information
aarnphm committed Oct 13, 2022
1 parent ca6dd5f commit b80f95e
Show file tree
Hide file tree
Showing 11 changed files with 27 additions and 49 deletions.
7 changes: 3 additions & 4 deletions src/bentoml/_internal/server/grpc/server.py
Expand Up @@ -10,6 +10,9 @@
from simple_di import inject
from simple_di import Provide

from bentoml.grpc.utils import import_grpc
from bentoml.grpc.utils import import_generated_stubs

from ...utils import LazyLoader
from ...utils import cached_property
from ...utils import resolve_user_filepath
Expand All @@ -27,10 +30,6 @@

from .servicer import Servicer
else:
from bentoml.grpc.utils import import_grpc
from bentoml.grpc.utils import import_generated_stubs
from bentoml._internal.utils import LazyLoader

grpc, aio = import_grpc()
_, services = import_generated_stubs()
health_exception_msg = "'grpcio-health-checking' is required for using health checking endpoints. Install with 'pip install grpcio-health-checking'."
Expand Down
8 changes: 3 additions & 5 deletions src/bentoml/_internal/server/grpc/servicer.py
Expand Up @@ -9,9 +9,12 @@

import anyio

from bentoml.grpc.utils import import_grpc
from bentoml.grpc.utils import grpc_status_code
from bentoml.grpc.utils import validate_proto_fields
from bentoml.grpc.utils import import_generated_stubs

from ...utils import LazyLoader
from ....exceptions import InvalidArgument
from ....exceptions import BentoMLException

Expand All @@ -35,11 +38,6 @@
from ...service.service import Service

else:
from bentoml.grpc.utils import import_grpc
from bentoml.grpc.utils import import_generated_stubs

from ...utils import LazyLoader

pb, services = import_generated_stubs()
grpc, aio = import_grpc()
health = LazyLoader(
Expand Down
3 changes: 1 addition & 2 deletions src/bentoml/_internal/service/service.py
Expand Up @@ -12,6 +12,7 @@
from ..tag import Tag
from ..models import Model
from ..runner import Runner
from ...grpc.utils import import_grpc
from ..bento.bento import get_default_svc_readme
from .inference_api import InferenceAPI
from ..io_descriptors import IODescriptor
Expand All @@ -27,8 +28,6 @@
from ..server.grpc.servicer import Servicer
from .openapi.specification import OpenAPISpecification
else:
from bentoml.grpc.utils import import_grpc

grpc, _ = import_grpc()

logger = logging.getLogger(__name__)
Expand Down
5 changes: 2 additions & 3 deletions src/bentoml/grpc/interceptors/access.py
Expand Up @@ -6,9 +6,11 @@
from timeit import default_timer
from typing import TYPE_CHECKING

from bentoml.grpc.utils import import_grpc
from bentoml.grpc.utils import to_http_status
from bentoml.grpc.utils import wrap_rpc_handler
from bentoml.grpc.utils import GRPC_CONTENT_TYPE
from bentoml.grpc.utils import import_generated_stubs

if TYPE_CHECKING:
import grpc
Expand All @@ -23,9 +25,6 @@
from bentoml.grpc.types import BentoServicerContext
from bentoml.grpc.v1alpha1 import service_pb2 as pb
else:
from bentoml.grpc.utils import import_grpc
from bentoml.grpc.utils import import_generated_stubs

pb, _ = import_generated_stubs()
grpc, aio = import_grpc()

Expand Down
3 changes: 1 addition & 2 deletions src/bentoml/grpc/interceptors/opentelemetry.py
Expand Up @@ -16,6 +16,7 @@
from opentelemetry.trace.status import StatusCode
from opentelemetry.semconv.trace import SpanAttributes

from bentoml.grpc.utils import import_grpc
from bentoml.grpc.utils import wrap_rpc_handler
from bentoml.grpc.utils import GRPC_CONTENT_TYPE
from bentoml.grpc.utils import parse_method_name
Expand All @@ -38,8 +39,6 @@
from bentoml.grpc.types import HandlerCallDetails
from bentoml.grpc.types import BentoServicerContext
else:
from bentoml.grpc.utils import import_grpc

grpc, aio = import_grpc()

logger = logging.getLogger(__name__)
Expand Down
5 changes: 2 additions & 3 deletions src/bentoml/grpc/interceptors/prometheus.py
Expand Up @@ -10,8 +10,10 @@
from simple_di import inject
from simple_di import Provide

from bentoml.grpc.utils import import_grpc
from bentoml.grpc.utils import to_http_status
from bentoml.grpc.utils import wrap_rpc_handler
from bentoml.grpc.utils import import_generated_stubs
from bentoml._internal.context import component_context
from bentoml._internal.configuration.containers import BentoMLContainer

Expand All @@ -30,9 +32,6 @@
from bentoml.grpc.v1alpha1 import service_pb2 as pb
from bentoml._internal.server.metrics.prometheus import PrometheusClient
else:
from bentoml.grpc.utils import import_grpc
from bentoml.grpc.utils import import_generated_stubs

pb, _ = import_generated_stubs()
grpc, aio = import_grpc()

Expand Down
14 changes: 2 additions & 12 deletions src/bentoml/grpc/utils/__init__.py
Expand Up @@ -8,9 +8,10 @@
from dataclasses import dataclass

from bentoml.exceptions import InvalidArgument
from bentoml.grpc.utils._import_hook import import_grpc
from bentoml.grpc.utils._import_hook import import_generated_stubs

if TYPE_CHECKING:
import types
from enum import Enum

import grpc
Expand All @@ -22,18 +23,7 @@
from bentoml.grpc.v1alpha1 import service_pb2 as pb
from bentoml._internal.io_descriptors import IODescriptor

# We need this here so that __all__ is detected due to lazy import
def import_generated_stubs(
version: str = "v1alpha1",
) -> tuple[types.ModuleType, types.ModuleType]:
...

def import_grpc() -> tuple[types.ModuleType, types.ModuleType]:
...

else:
from bentoml.grpc.utils._import_hook import import_grpc
from bentoml.grpc.utils._import_hook import import_generated_stubs

pb, _ = import_generated_stubs()
grpc, _ = import_grpc()
Expand Down
5 changes: 2 additions & 3 deletions src/bentoml/testing/grpc/__init__.py
Expand Up @@ -7,6 +7,8 @@
from contextlib import asynccontextmanager

from bentoml.exceptions import BentoMLException
from bentoml.grpc.utils import import_grpc
from bentoml.grpc.utils import import_generated_stubs
from bentoml._internal.utils import LazyLoader
from bentoml._internal.utils import reserve_free_port
from bentoml._internal.utils import cached_contextmanager
Expand All @@ -26,9 +28,6 @@
from bentoml.grpc.v1alpha1 import service_pb2 as pb
from bentoml.grpc.v1alpha1 import service_test_pb2_grpc as services_test
else:
from bentoml.grpc.utils import import_grpc
from bentoml.grpc.utils import import_generated_stubs

pb, _ = import_generated_stubs()
_, services_test = import_generated_stubs(file="service_test.proto")
grpc, aio = import_grpc() # pylint: disable=E1111
Expand Down
13 changes: 5 additions & 8 deletions tests/e2e/bento_server_grpc/tests/test_descriptors.py
@@ -1,4 +1,3 @@
# pylint: disable=assignment-from-no-return,used-before-assignment
from __future__ import annotations

import io
Expand All @@ -9,6 +8,8 @@

import pytest

from bentoml.grpc.utils import import_grpc
from bentoml.grpc.utils import import_generated_stubs
from bentoml.testing.grpc import create_channel
from bentoml.testing.grpc import async_client_call
from bentoml.testing.grpc import randomize_pb_ndarray
Expand All @@ -20,18 +21,14 @@
import numpy as np
import pandas as pd
import PIL.Image as PILImage
from grpc import aio
from google.protobuf import struct_pb2
from google.protobuf import wrappers_pb2

from bentoml._internal import external_typing as ext
from bentoml.grpc.v1alpha1 import service_pb2 as pb
else:
from bentoml.grpc.utils import import_grpc
from bentoml.grpc.utils import import_generated_stubs

pb, _ = import_generated_stubs()
grpc, aio = import_grpc()
grpc, _ = import_grpc()
wrappers_pb2 = LazyLoader("wrappers_pb2", globals(), "google.protobuf.wrappers_pb2")
struct_pb2 = LazyLoader("struct_pb2", globals(), "google.protobuf.struct_pb2")
np = LazyLoader("np", globals(), "numpy")
Expand Down Expand Up @@ -134,8 +131,8 @@ async def test_json(host: str):
await async_client_call(
"echo_json",
channel=channel,
data={"json": struct_pb2.Value(string_value="hi")},
assert_data=lambda resp: resp.json.string_value == "hi",
data={"json": struct_pb2.Value(string_value='"hi"')},
assert_data=lambda resp: resp.json.string_value == '"hi"',
)
await async_client_call(
"echo_json",
Expand Down
7 changes: 3 additions & 4 deletions tests/unit/grpc/interceptors/test_access.py
Expand Up @@ -8,11 +8,14 @@

import pytest

from bentoml.grpc.utils import import_grpc
from bentoml.grpc.utils import wrap_rpc_handler
from bentoml.grpc.utils import import_generated_stubs
from bentoml.testing.grpc import create_channel
from bentoml.testing.grpc import async_client_call
from bentoml.testing.grpc import create_bento_servicer
from bentoml.testing.grpc import make_standalone_server
from bentoml._internal.utils import LazyLoader
from bentoml.grpc.interceptors.access import AccessLogServerInterceptor
from bentoml.grpc.interceptors.opentelemetry import AsyncOpenTelemetryServerInterceptor

Expand All @@ -33,10 +36,6 @@
from bentoml.grpc.v1alpha1 import service_test_pb2 as pb_test
from bentoml.grpc.v1alpha1 import service_test_pb2_grpc as services_test
else:
from bentoml.grpc.utils import import_grpc
from bentoml.grpc.utils import import_generated_stubs
from bentoml._internal.utils import LazyLoader

_, services = import_generated_stubs()
pb_test, services_test = import_generated_stubs(file="service_test.proto")
grpc, aio = import_grpc()
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/grpc/interceptors/test_prometheus.py
Expand Up @@ -9,10 +9,13 @@

import pytest

from bentoml.grpc.utils import import_grpc
from bentoml.grpc.utils import import_generated_stubs
from bentoml.testing.grpc import create_channel
from bentoml.testing.grpc import async_client_call
from bentoml.testing.grpc import create_bento_servicer
from bentoml.testing.grpc import make_standalone_server
from bentoml._internal.utils import LazyLoader
from bentoml.grpc.interceptors.prometheus import PrometheusServerInterceptor
from bentoml._internal.configuration.containers import BentoMLContainer

Expand All @@ -24,9 +27,6 @@
from bentoml.grpc.v1alpha1 import service_pb2_grpc as services
from bentoml.grpc.v1alpha1 import service_test_pb2 as pb_test
else:
from bentoml.grpc.utils import import_grpc
from bentoml.grpc.utils import import_generated_stubs
from bentoml._internal.utils import LazyLoader

_, services = import_generated_stubs()
pb_test, _ = import_generated_stubs(file="service_test.proto")
Expand Down

0 comments on commit b80f95e

Please sign in to comment.