1212from collections import defaultdict
1313from getpass import getpass
1414from os import environ , path
15- from typing import Any , Dict , List , Optional , Tuple
15+ from typing import Any , Dict , List , Optional , Tuple , Union
1616from urllib .parse import urlparse
1717
1818import openapi3 .paths
@@ -48,7 +48,9 @@ def parse_boolean(value: str) -> bool:
4848 raise argparse .ArgumentTypeError ("Expected a boolean value" )
4949
5050
51- def parse_dict (value : str ) -> dict :
51+ def parse_dict (
52+ value : str ,
53+ ) -> Union [Dict [str , Any ], "ExplicitEmptyDictValue" , "ExplicitEmptyListValue" ]:
5254 """
5355 A helper function to decode incoming JSON data as python dicts. This is
5456 intended to be passed to the `type=` kwarg for ArgumentParaser.add_argument.
@@ -57,15 +59,28 @@ def parse_dict(value: str) -> dict:
5759 :type value: str
5860
5961 :returns: The dict value of the input.
60- :rtype: dict
62+ :rtype: dict, ExplicitEmptyDictValue, or ExplicitEmptyListValue
6163 """
6264 if not isinstance (value , str ):
6365 raise argparse .ArgumentTypeError ("Expected a JSON string" )
66+
6467 try :
65- return json .loads (value )
68+ result = json .loads (value )
6669 except Exception as e :
6770 raise argparse .ArgumentTypeError ("Expected a JSON string" ) from e
6871
72+ # This is necessary because empty dicts and lists are excluded from requests
73+ # by default, but we still want to support user-defined empty dict
74+ # strings. This is particularly helpful when updating LKE node pool
75+ # labels and taints.
76+ if isinstance (result , dict ) and result == {}:
77+ return ExplicitEmptyDictValue ()
78+
79+ if isinstance (result , list ) and result == []:
80+ return ExplicitEmptyListValue ()
81+
82+ return result
83+
6984
7085TYPES = {
7186 "string" : str ,
@@ -90,6 +105,12 @@ class ExplicitEmptyListValue:
90105 """
91106
92107
108+ class ExplicitEmptyDictValue :
109+ """
110+ A special type used to explicitly pass empty dictionaries to the API.
111+ """
112+
113+
93114def wrap_parse_nullable_value (arg_type : str ) -> TYPES :
94115 """
95116 A helper function to parse `null` as None for nullable CLI args.
0 commit comments