Skip to main content
mypy logo

kubernetes-typed

Build status Checked with mypy

mypy plugin to dynamically define types for Kubernetes objects.

Features

Installation

Install with pip:

pip install kubernetes-typed

Versioning

This package follows kubernetes client versioning approach. MAJOR.MINOR parts of version will match client version for which stubs were generated, and PATCH version will be stub or plugin specific updates.

Custom Resource Definitions

Add type checks for Custom Resource Definition spec given its definition in yaml file .

  1. Configure mypy to use crd_typed plugin:
[mypy]

plugins = crd_typed.plugin
  1. Import CustomResource
from crd_type import CustomResource
  1. Annotate your variables:
resource: CustomResource["relative/path/to/crd.yaml"]

You can get type definition for different parts of crd:

  • Get TypeDict definition for custom resource body:

    from crd_type import CustomResource
    
    resource: CustomResource["relative/path/to/crd.yaml"]
    
  • Get definition only for resource spec:

    from crd_type import CustomResource
    
    resource: CustomResource["relative/path/to/crd.yaml", "spec"]
    
  • Get definition for nested spec item, if that item is type object or array:

    from crd_type import CustomResource
    
    resource: CustomResource["relative/path/to/crd.yaml", "spec", "some_property"]
    
  • Get definition for array item, if that is array of objects, via items key:

    from crd_type import CustomResource
    
    resource: CustomResource["relative/path/to/crd.yaml", "spec", "some_array_of_objects", "items"]
    

Limitations

  • CRDs that use additionalProperties are not supported.
  • CRDs can define multiple versions, currently only first one will be used
  • Custom attributes like x-kubernetes-int-or-string, x-kubernetes-embedded-resource, are not supported

Kubernetes Python Client types

This package provides basic type stubs for kubernetes python client out of the box.

To enable full type checking for classes use provided kubernetes_typed plugin. This plugin requires kubernetes, you can require it during installation like this:

pip install kubernetes-typed[client]

Configure mypy to use it and it will automatically type check classes from kubernetes.client:

[mypy]

plugins = kubernetes_typed.plugin

Kubernetes Python Client Models Dict Types

If you want to type check resource dicts instead of classes, you can use generated TypedDicts provided by this package.

To do this for any model class in kubernetes.client append its name with Dict, and import it from kubernetes_type.client

For example:

kubernetes.client.V1Pod -> kubernetes_typed.client.V1PodDict

from kubernetes.client.api import core_v1_api

from kubernetes_typed.client import V1PodDict

api_instance = core_v1_api.CoreV1Api()

pod_manifest: V1PodDict = {
    "apiVersion": "v1",
    "kind": "Pod",
    "metadata": {"name": "test-pod"},
    "spec": {
        "containers": [
            {
                "image": "nginx",
                "name": "nginx",
            },
        ],
    },
}

api_instance.create_namespaced_pod(body=pod_manifest, namespace='default')

Limitations

  • Kubernetes client api functions are currently not covered by stubs, so you might get Call to untyped function errors. Check mypy config doc on how to disable separate warnings.

Release files for kubernetes-typed 18.20.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for kubernetes-typed 18.20.2
File Size Uploaded
kubernetes_typed-18.20.2.tar.gz 199.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for kubernetes-typed 18.20.2
File Interpreter ABI Platform
kubernetes_typed-18.20.2-py3-none-any.whl Python 3 none any Details

Total release size: 957.0 kB

Release files / kubernetes_typed-18.20.2.tar.gz

Download URL kubernetes_typed-18.20.2.tar.gz
Size 199.1 kB
Tags Source
SHA-256 checksum
How to use checksums
5c2fcf5de2b3192133a4cba9d0d8f7187e4ccc7f7ad6d3a17dfe67ea5b90b101
BLAKE2b-256 checksum
How to use checksums
374c0f49075f2ca5f4bd2c5ec05f0ca37c1649076a3bd93ae13f593f95fb31ba
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.1.1 CPython/3.12.5

Release files / kubernetes_typed-18.20.2-py3-none-any.whl

Download URL kubernetes_typed-18.20.2-py3-none-any.whl
Size 757.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
dff59feb21c092a83ac5b0e65b9cc5f9d00caa95833e0c486692dedfc1276a13
BLAKE2b-256 checksum
How to use checksums
dad5d13cf0211d950b19b89683e65829b4da9c8b2eed0b71afec564f240e3bcb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.1.1 CPython/3.12.5
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page