{"id":7345,"library":"kubernetes-stubs-elephant-fork","title":"Kubernetes Type Stubs (Elephant Fork)","description":"This library provides type stubs for the official Kubernetes Python API client. It is an actively maintained fork of the original `kubernetes-stubs` package, offering static type checking support for projects using the `kubernetes` client. The current version is 35.0.0.post1, with a frequent release cadence aimed at keeping pace with upstream `kubernetes` client updates.","status":"active","version":"35.0.0.post1","language":"en","source_language":"en","source_url":"https://github.com/lexdene/kubernetes-stubs","tags":["python","kubernetes","type-stubs","typing","mypy"],"install":[{"cmd":"pip install kubernetes-stubs-elephant-fork","lang":"bash","label":"Install package"}],"dependencies":[{"reason":"Provides type hints for the kubernetes client library; essential for its utility, though not a runtime dependency for the stubs package itself.","package":"kubernetes","optional":false}],"imports":[{"note":"Type stubs are automatically picked up by type checkers (e.g., MyPy) for imports from the 'kubernetes' library. You do not import directly from 'kubernetes_stubs_elephant_fork' as it contains no runtime code.","symbol":"client","correct":"from kubernetes import client"},{"note":"Attempting to import from the stub package directly at runtime will result in a ModuleNotFoundError or similar, as it only contains type definitions, not executable code.","wrong":"from kubernetes_stubs_elephant_fork import config","symbol":"config","correct":"from kubernetes import config"}],"quickstart":{"code":"import os\nfrom kubernetes import client, config\nfrom typing import TYPE_CHECKING\n\n# This function will only execute at runtime, not during type checking\nif not TYPE_CHECKING:\n    # Load Kubernetes configuration\n    # For local development, e.g., ~/.kube/config\n    try:\n        config.load_kube_config(context=os.environ.get('KUBE_CONTEXT', ''))\n    except config.config_exception.ConfigException:\n        # Fallback for CI/CD or in-cluster\n        config.load_incluster_config()\n\ndef get_pod_names(namespace: str) -> list[str]:\n    # The type checker uses kubernetes-stubs-elephant-fork here\n    v1: client.CoreV1Api = client.CoreV1Api()\n    \n    # The return type of list_namespaced_pod is typed by the stubs\n    pods_list = v1.list_namespaced_pod(namespace=namespace)\n    \n    names: list[str] = []\n    for pod in pods_list.items:\n        if pod.metadata and pod.metadata.name:\n            names.append(pod.metadata.name)\n    return names\n\nif __name__ == \"__main__\":\n    # Example runtime usage (requires a running Kubernetes cluster/config)\n    try:\n        # Replace 'default' with your target namespace or use env var\n        target_namespace = os.environ.get('KUBE_NAMESPACE', 'default')\n        pod_names = get_pod_names(target_namespace)\n        print(f\"Pods in '{target_namespace}' namespace: {pod_names}\")\n    except Exception as e:\n        print(f\"Could not list pods (is Kubernetes configured?): {e}\")\n\n# To type-check this file:\n# 1. pip install mypy kubernetes kubernetes-stubs-elephant-fork\n# 2. mypy quickstart.py","lang":"python","description":"This quickstart demonstrates how `kubernetes-stubs-elephant-fork` provides type hints for code that uses the `kubernetes` client library. The stubs are automatically picked up by type checkers like MyPy, without requiring direct imports from the stub package. The `if __name__ == \"__main__\"` block provides a runnable example requiring Kubernetes connectivity."},"warnings":[{"fix":"Refer to the `kubernetes-stubs-elephant-fork` GitHub README for the exact `kubernetes` client version range supported by your installed stub version.","message":"The version number of `kubernetes-stubs-elephant-fork` does not directly correspond to the `kubernetes` client version it supports. For example, `kubernetes-stubs-elephant-fork==35.0.0.post1` targets `kubernetes>=28.0.0`. Always consult the stub package's README or changelog to confirm compatibility with your specific `kubernetes` client version to avoid unexpected type errors.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always import symbols from the actual `kubernetes` client library (e.g., `from kubernetes import client`). The stub package is automatically discovered by type checkers.","message":"`kubernetes-stubs-elephant-fork` contains only type stubs and is not meant for direct runtime import or execution. Attempting to `from kubernetes_stubs_elephant_fork import ...` will lead to a `ModuleNotFoundError` or similar runtime error.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Run `pip uninstall kubernetes-stubs` before or after installing `kubernetes-stubs-elephant-fork`.","message":"If migrating from the unmaintained original `kubernetes-stubs` package, it is crucial to uninstall it to prevent potential conflicts or incorrect stub resolution by your type checker.","severity":"gotcha","affected_versions":"Users migrating from `kubernetes-stubs`"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Import symbols from the actual `kubernetes` client library (e.g., `from kubernetes import client`). The stub package is picked up automatically by type checkers for static analysis.","cause":"You are attempting to import symbols directly from the stub package, which contains no runtime code.","error":"ModuleNotFoundError: No module named 'kubernetes_stubs_elephant_fork'"},{"fix":"Ensure `pip install kubernetes-stubs-elephant-fork` is run. Check the stub package's README for compatible `kubernetes` client versions and upgrade/downgrade if necessary. You might also try `mypy --verbose` to debug path issues.","cause":"`kubernetes-stubs-elephant-fork` is not installed, or its version is incompatible with your `kubernetes` client, or MyPy is not correctly configured.","error":"mypy: No library stub file for 'kubernetes.client'"},{"fix":"Uninstall the older or conflicting stub package. Run `pip uninstall kubernetes-stubs` to remove the unmaintained predecessor.","cause":"You likely have both the original `kubernetes-stubs` and `kubernetes-stubs-elephant-fork` packages installed, leading to type checker confusion.","error":"mypy: Conflicting definitions for 'kubernetes.client'"}]}