|
| 1 | +# Copyright 2026 The Kubernetes Authors. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from importlib.resources import files |
| 16 | +import unittest |
| 17 | + |
| 18 | +from typing_extensions import assert_type |
| 19 | + |
| 20 | +from kubernetes import client |
| 21 | +from kubernetes.aio import client as aio_client |
| 22 | + |
| 23 | + |
| 24 | +class TestPackageTyping(unittest.TestCase): |
| 25 | + def test_root_package_is_marked_as_typed(self): |
| 26 | + self.assertTrue(files('kubernetes').joinpath('py.typed').is_file()) |
| 27 | + |
| 28 | + def test_synchronous_nested_model_types(self): |
| 29 | + pod = client.V1Pod(status=client.V1PodStatus(container_statuses=[ |
| 30 | + client.V1ContainerStatus( |
| 31 | + image='image', |
| 32 | + image_id='image-id', |
| 33 | + name='container', |
| 34 | + ready=True, |
| 35 | + restart_count=0, |
| 36 | + state=client.V1ContainerState(), |
| 37 | + ), |
| 38 | + ])) |
| 39 | + |
| 40 | + status = assert_type(pod.status, client.V1PodStatus | None) |
| 41 | + assert status is not None |
| 42 | + containers = assert_type( |
| 43 | + status.container_statuses, |
| 44 | + list[client.V1ContainerStatus] | None, |
| 45 | + ) |
| 46 | + assert containers is not None |
| 47 | + state = assert_type( |
| 48 | + containers[0].state, |
| 49 | + client.V1ContainerState | None, |
| 50 | + ) |
| 51 | + self.assertIsInstance(state, client.V1ContainerState) |
| 52 | + |
| 53 | + def test_asynchronous_nested_model_types(self): |
| 54 | + pod = aio_client.V1Pod( |
| 55 | + status=aio_client.V1PodStatus(container_statuses=[ |
| 56 | + aio_client.V1ContainerStatus( |
| 57 | + image='image', |
| 58 | + image_id='image-id', |
| 59 | + name='container', |
| 60 | + ready=True, |
| 61 | + restart_count=0, |
| 62 | + state=aio_client.V1ContainerState(), |
| 63 | + ), |
| 64 | + ]), |
| 65 | + ) |
| 66 | + |
| 67 | + status = assert_type(pod.status, aio_client.V1PodStatus | None) |
| 68 | + assert status is not None |
| 69 | + containers = assert_type( |
| 70 | + status.container_statuses, |
| 71 | + list[aio_client.V1ContainerStatus] | None, |
| 72 | + ) |
| 73 | + assert containers is not None |
| 74 | + state = assert_type( |
| 75 | + containers[0].state, |
| 76 | + aio_client.V1ContainerState | None, |
| 77 | + ) |
| 78 | + self.assertIsInstance(state, aio_client.V1ContainerState) |
0 commit comments