@@ -2054,17 +2054,23 @@ def _check_and_log_subprocess(command, logger, **kwargs):
20542054 return proc .stdout
20552055
20562056
2057- def _check_isinstance (types , ** kwargs ):
2057+ # In the following _check_foo functions, the first parameter starts with an
2058+ # underscore because it is intended to be positional-only (e.g., so that
2059+ # `_check_isinstance([...], types=foo)` doesn't fail.
2060+
2061+
2062+ def _check_isinstance (_types , ** kwargs ):
20582063 """
20592064 For each *key, value* pair in *kwargs*, check that *value* is an instance
2060- of one of *types *; if not, raise an appropriate TypeError.
2065+ of one of *_types *; if not, raise an appropriate TypeError.
20612066
2062- As a special case, a ``None`` entry in *types * is treated as NoneType.
2067+ As a special case, a ``None`` entry in *_types * is treated as NoneType.
20632068
20642069 Examples
20652070 --------
20662071 >>> cbook._check_isinstance((SomeClass, None), arg=arg)
20672072 """
2073+ types = _types
20682074 if isinstance (types , type ) or types is None :
20692075 types = (types ,)
20702076 none_allowed = None in types
@@ -2088,17 +2094,40 @@ def type_name(tp):
20882094 type_name (type (v ))))
20892095
20902096
2091- def _check_in_list (values , ** kwargs ):
2097+ def _check_in_list (_values , ** kwargs ):
20922098 """
2093- For each *key, value* pair in *kwargs*, check that *value* is in *values *;
2099+ For each *key, value* pair in *kwargs*, check that *value* is in *_values *;
20942100 if not, raise an appropriate ValueError.
20952101
20962102 Examples
20972103 --------
20982104 >>> cbook._check_in_list(["foo", "bar"], arg=arg, other_arg=other_arg)
20992105 """
2106+ values = _values
21002107 for k , v in kwargs .items ():
21012108 if v not in values :
21022109 raise ValueError (
21032110 "{!r} is not a valid value for {}; supported values are {}"
21042111 .format (v , k , ', ' .join (map (repr , values ))))
2112+
2113+
2114+ def _check_getitem (_mapping , ** kwargs ):
2115+ """
2116+ *kwargs* must consist of a single *key, value* pair. If *key* is in
2117+ *_mapping*, return ``_mapping[value]``; else, raise an appropriate
2118+ ValueError.
2119+
2120+ Examples
2121+ --------
2122+ >>> cbook._check_getitem({"foo": "bar"}, arg=arg)
2123+ """
2124+ mapping = _mapping
2125+ if len (kwargs ) != 1 :
2126+ raise ValueError ("_check_getitem takes a single keyword argument" )
2127+ (k , v ), = kwargs .items ()
2128+ try :
2129+ return mapping [v ]
2130+ except KeyError :
2131+ raise ValueError (
2132+ "{!r} is not a valid value for {}; supported values are {}"
2133+ .format (v , k , ', ' .join (map (repr , mapping )))) from None
0 commit comments