Skip to main content

Collection of mypy plugins and stubs for kubernetes

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

kubernetes-typed-18.20.0.dev0.tar.gz (186.3 kB view details)

Uploaded Source

Built Distribution

kubernetes_typed-18.20.0.dev0-py3-none-any.whl (723.7 kB view details)

Uploaded Python 3

File details

Details for the file kubernetes-typed-18.20.0.dev0.tar.gz.

File metadata

  • Download URL: kubernetes-typed-18.20.0.dev0.tar.gz
  • Upload date:
  • Size: 186.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.7

File hashes

Hashes for kubernetes-typed-18.20.0.dev0.tar.gz
Algorithm Hash digest
SHA256 423359a4cfd65f200c8192e6862e4fdd58778b26523083ab36515d8273c39eb1
MD5 a383e0dca69157da2fa47d9c4264eff7
BLAKE2b-256 565231418077e04183b74483a84ebd0a252bf1491d4a50ca4c7c7d8c17ee21fe

See more details on using hashes here.

File details

Details for the file kubernetes_typed-18.20.0.dev0-py3-none-any.whl.

File metadata

  • Download URL: kubernetes_typed-18.20.0.dev0-py3-none-any.whl
  • Upload date:
  • Size: 723.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.7

File hashes

Hashes for kubernetes_typed-18.20.0.dev0-py3-none-any.whl
Algorithm Hash digest
SHA256 ecfde7050ed0a0dfca8e89e0be3b17b99f6db05f8c3c5828c3a83dbd0ebcaa64
MD5 0e4d769af07b6517e2da92dc3c0a5fb5
BLAKE2b-256 2ef0a1459ee202186df89d011623d64dbdaca07d01ed0990277b8a4e564c8810

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page