Skip to content

Commit a0df905

Browse files
authored
Merge pull request #2521 from Urvashi0109/Fix-Type-Declaration-dict
Add support for modern Python typing syntax dict[str, str] in ApiClient.__deserialize
2 parents 4047199 + 87f6633 commit a0df905

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed

kubernetes/client/api_client.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,11 @@ def __deserialize(self, data, klass):
285285
return {k: self.__deserialize(v, sub_kls)
286286
for k, v in six.iteritems(data)}
287287

288+
if klass.startswith('dict['):
289+
sub_kls = re.match(r'dict\[([^,]*),\s*(.*)\]', klass).group(2)
290+
return {k: self.__deserialize(v, sub_kls)
291+
for k, v in six.iteritems(data)}
292+
288293
# convert str to class
289294
if klass in self.NATIVE_TYPES_MAPPING:
290295
klass = self.NATIVE_TYPES_MAPPING[klass]

kubernetes/test/test_api_client.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,33 @@ def test_atexit_closes_threadpool(self):
2525
atexit._run_exitfuncs()
2626
self.assertIsNone(client._pool)
2727

28+
def test_deserialize_dict_syntax_compatibility(self):
29+
"""Test ApiClient.__deserialize supports both
30+
dict(str, str) and dict[str, str] syntax"""
31+
client = kubernetes.client.ApiClient()
32+
33+
# Test data
34+
test_data = {
35+
'key1': 'value1',
36+
'key2': 'value2'
37+
}
38+
39+
# Test legacy syntax: dict(str, str)
40+
result_legacy = client._ApiClient__deserialize(test_data, 'dict(str, str)')
41+
self.assertEqual(result_legacy, test_data)
42+
43+
# Test modern syntax: dict[str, str]
44+
result_modern = client._ApiClient__deserialize(test_data, 'dict[str, str]')
45+
self.assertEqual(result_modern, test_data)
46+
47+
# Test nested dict: dict[str, dict[str, str]]
48+
nested_data = {
49+
'outer1': {'inner1': 'value1', 'inner2': 'value2'},
50+
'outer2': {'inner3': 'value3'}
51+
}
52+
result_nested = client._ApiClient__deserialize(nested_data, 'dict[str, dict[str, str]]')
53+
self.assertEqual(result_nested, nested_data)
54+
2855
def test_rest_proxycare(self):
2956

3057
pool = { 'proxy': urllib3.ProxyManager, 'direct': urllib3.PoolManager }
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
diff --git a/kubernetes/client/api_client.py b/kubernetes/client/api_client.py
2+
index 7ac940fa..c78de369 100644
3+
--- a/kubernetes/client/api_client.py
4+
+++ b/kubernetes/client/api_client.py
5+
@@ -285,6 +285,11 @@ class ApiClient(object):
6+
return {k: self.__deserialize(v, sub_kls)
7+
for k, v in six.iteritems(data)}
8+
9+
+ if klass.startswith('dict['):
10+
+ sub_kls = re.match(r'dict\[([^,]*),\s*(.*)\]', klass).group(2)
11+
+ return {k: self.__deserialize(v, sub_kls)
12+
+ for k, v in six.iteritems(data)}
13+
+
14+
# convert str to class
15+
if klass in self.NATIVE_TYPES_MAPPING:
16+
klass = self.NATIVE_TYPES_MAPPING[klass]

scripts/update-client.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ git apply "${SCRIPT_ROOT}/rest_client_patch.diff"
7878
# once we upgrade to a version of swagger-codegen that includes it (version>= 6.6.0).
7979
# See https://github.com/OpenAPITools/openapi-generator/pull/15283
8080
git apply "${SCRIPT_ROOT}/rest_sni_patch.diff"
81+
# Support dict[str, str] syntax in ApiClient deserializer alongside legacy
82+
# dict(str, str). This enables forward compatibility with modern Python typing
83+
# syntax while maintaining backward compatibility. Users can now convert
84+
# openapi_types for Pydantic integration.
85+
# See https://github.com/kubernetes-client/python/issues/2463
86+
git apply "${SCRIPT_ROOT}/api_client_dict_syntax.diff"
8187
# The following is commented out due to:
8288
# AttributeError: 'RESTResponse' object has no attribute 'headers'
8389
# OpenAPI client generator prior to 6.4.0 uses deprecated urllib3 APIs.

0 commit comments

Comments
 (0)