{"id":2560,"library":"kubernetes-stubs","title":"Kubernetes Type Stubs","description":"Type stubs for the Kubernetes Python API client. This library provides static type checking for code interacting with the `kubernetes` client library. The version numbers of `kubernetes-stubs` generally track the upstream `kubernetes` client library. As of version 22.6.0.post1, it provides type definitions mirroring the API of the corresponding Kubernetes client version. Contributions and bug fixes are welcomed.","status":"active","version":"22.6.0.post1","language":"en","source_language":"en","source_url":"https://github.com/MaterializeInc/kubernetes-stubs","tags":["kubernetes","typing","stubs","mypy","type-checking"],"install":[{"cmd":"pip install kubernetes-stubs","lang":"bash","label":"Install kubernetes-stubs"}],"dependencies":[{"reason":"Provides the runtime Kubernetes Python client API that these stubs type-check.","package":"kubernetes","optional":false}],"imports":[{"symbol":"client","correct":"from kubernetes import client"},{"symbol":"config","correct":"from kubernetes import config"}],"quickstart":{"code":"import os\nfrom kubernetes import client, config\n\ntry:\n    # Try to load kubeconfig from default location (e.g., ~/.kube/config)\n    config.load_kube_config(config_file=os.environ.get('KUBECONFIG'))\nexcept config.config_exception.ConfigException:\n    # Fallback to in-cluster config if KUBECONFIG is not set or file not found\n    config.load_incluster_config()\n\nv1 = client.CoreV1Api()\nprint(\"Listing pods with their IPs:\")\nret = v1.list_pod_for_all_namespaces(watch=False)\nfor i in ret.items:\n    print(f\"{i.status.pod_ip}\\t{i.metadata.namespace}\\t{i.metadata.name}\")","lang":"python","description":"This quickstart demonstrates how to initialize the Kubernetes Python client and list all pods across all namespaces, benefiting from the type stubs for static analysis. It attempts to load configuration from a kubeconfig file (respecting KUBECONFIG environment variable) or falls back to in-cluster configuration."},"warnings":[{"fix":"Check the project's GitHub for latest releases or explore community-maintained forks like `kubernetes-stubs-elephant-fork` for newer Kubernetes client versions.","message":"The `kubernetes-stubs` library can sometimes lag behind new releases of the official `kubernetes` client library, leading to missing type definitions for the latest API versions or newly added fields. Consider using forks like `kubernetes-stubs-elephant-fork` if you need more up-to-date stubs.","severity":"gotcha","affected_versions":"<=22.6.0.post1"},{"fix":"For advanced type-checking needs, especially with Custom Resources, investigate `kubernetes-typed` and its `mypy` plugin for enhanced capabilities.","message":"The generated stubs may not always be complete or entirely accurate for all edge cases or dynamically generated Kubernetes resources (like Custom Resource Definitions). For more comprehensive type checking for CRDs or client API functions, consider using `kubernetes-typed` with its `mypy` plugin.","severity":"gotcha","affected_versions":"All"},{"fix":"Regularly consult the official Kubernetes API documentation for deprecation notices and update your code to use current API versions and fields.","message":"The underlying Kubernetes API frequently deprecates older API versions and fields. While `kubernetes-stubs` tracks the Python client, using deprecated APIs in your code can still lead to runtime warnings or failures in a Kubernetes cluster, even if the stubs permit them. Always refer to the official Kubernetes API documentation for deprecation policies.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-10T00:00:00.000Z","next_check":"2026-07-09T00:00:00.000Z"}