Typing Stubs for grpcio-health-checking

1.0.0.20260408 · active · verified Tue Apr 14

This package provides static type checking stubs for the `grpcio-health-checking` library, enabling tools like MyPy to validate usage of `grpc_health.v1` modules. It is part of the `typeshed` project, which supplies type hints for many popular Python packages. The current version is 1.0.0.20260408, with releases tied to updates of the underlying runtime library.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use the type hints provided by `types-grpcio-health-checking` in a `grpcio-health-checking` service implementation. It highlights the correct import paths and how type annotations enhance code clarity and enable static analysis. Note that the `types-` package is for type checking, not for runtime execution; the actual runtime code requires `grpcio` and `grpcio-health-checking`.

import grpc
from grpc_health.v1 import health_pb2
from grpc_health.v1.health import HealthServicer

# This example demonstrates using type hints provided by types-grpcio-health-checking.
# For this code to run, 'grpcio' and 'grpcio-health-checking' must also be installed.
# The 'types-' package primarily assists static analysis tools like MyPy.

class MyHealthServicer(HealthServicer):
    """
    An example gRPC Health Servicer implementation. The type hints for
    `request` and the return value are validated by type checkers (like mypy)
    thanks to the 'types-grpcio-health-checking' package providing stubs
    for 'grpcio-health-checking'.
    """
    def Check(self,
              request: health_pb2.HealthCheckRequest,
              context: grpc.ServicerContext) -> health_pb2.HealthCheckResponse:
        """
        Implements the Check method of the gRPC Health Service.
        """
        print(f"Received health check for service: {request.service}")
        # In a real application, you'd check actual service status here.
        if request.service == "myservice":
            return health_pb2.HealthCheckResponse(status=health_pb2.HealthCheckResponse.ServingStatus.SERVING)
        return health_pb2.HealthCheckResponse(status=health_pb2.HealthCheckResponse.ServingStatus.NOT_SERVING)

    def Watch(self,
              request: health_pb2.HealthCheckRequest,
              context: grpc.ServicerContext) -> health_pb2.HealthCheckResponse:
        """
        Implements the Watch method (streaming). For simplicity,
        this example just yields a single response.
        """
        yield health_pb2.HealthCheckResponse(status=health_pb2.HealthCheckResponse.ServingStatus.SERVING)

# To verify type-checking with MyPy:
# 1. Ensure you have 'grpcio', 'grpcio-health-checking', 'types-grpcio-health-checking', and 'mypy' installed.
# 2. Save this code as 'health_types_example.py'.
# 3. Run: mypy health_types_example.py
# If 'types-grpcio-health-checking' were missing, mypy would report errors for `HealthServicer` and other type hints.

view raw JSON →