@@ -108,9 +108,8 @@ def is_subparser_field(field: Field) -> bool:
108108
109109
110110class InconsistentArgumentError (RuntimeError ):
111- """
112- Error raised when the number of arguments provided is inconsistent when parsing multiple instances from command line.
113- """
111+ """Error raised when the number of arguments provided is inconsistent when parsing multiple
112+ instances from command line."""
114113
115114 def __init__ (self , * args , ** kwargs ):
116115 super ().__init__ (* args , ** kwargs )
@@ -126,9 +125,8 @@ def camel_case(name):
126125
127126
128127def str2bool (raw_value : str | bool ) -> bool :
129- """
130- Taken from https://stackoverflow.com/questions/15008758/parsing-boolean-values-with-argparse
131- """
128+ """Taken from https://stackoverflow.com/questions/15008758/parsing-boolean-values-with-
129+ argparse."""
132130 if isinstance (raw_value , bool ):
133131 return raw_value
134132 v = raw_value .strip ().lower ()
@@ -208,12 +206,13 @@ def get_item_type(container_type: type[Container[T]]) -> T:
208206def get_argparse_type_for_container (
209207 container_type : type [Container [T ]],
210208) -> type [T ] | Callable [[str ], T ]:
211- """Gets the argparse 'type' option to be used for a given container type.
212- When an annotation is present, the 'type' option of argparse is set to that type.
213- if not, then the default value of 'str' is returned.
209+ """Gets the argparse 'type' option to be used for a given container type. When an annotation is
210+ present, the 'type' option of argparse is set to that type. if not, then the default value of
211+ 'str' is returned.
214212
215213 Arguments:
216- container_type {Type} -- A container type (ideally a typing.Type such as List, Tuple, along with an item annotation: List[str], Tuple[int, int], etc.)
214+ container_type -- A container type (ideally a typing.Type such as List, Tuple, along
215+ with an item annotation: List[str], Tuple[int, int], etc.)
217216
218217 Returns:
219218 typing.Type -- the type that should be used in argparse 'type' argument option.
@@ -413,7 +412,9 @@ def is_dataclass_type_or_typevar(t: type) -> bool:
413412 Returns:
414413 bool: Whether its a dataclass type.
415414 """
416- return dataclasses .is_dataclass (t ) or (is_typevar (t ) and dataclasses .is_dataclass (get_bound (t )))
415+ return dataclasses .is_dataclass (t ) or (
416+ is_typevar (t ) and dataclasses .is_dataclass (get_bound (t ))
417+ )
417418
418419
419420def is_enum (t : type ) -> bool :
@@ -431,7 +432,7 @@ def is_tuple_or_list(t: type) -> bool:
431432
432433
433434def is_union (t : type ) -> bool :
434- """Returns whether or not the given Type annotation is a variant (or subclass) of typing.Union
435+ """Returns whether or not the given Type annotation is a variant (or subclass) of typing.Union.
435436
436437 Args:
437438 t (Type): some type annotation
@@ -453,8 +454,7 @@ def is_union(t: type) -> bool:
453454
454455
455456def is_homogeneous_tuple_type (t : type [tuple ]) -> bool :
456- """Returns whether the given Tuple type is homogeneous: if all items types are the
457- same.
457+ """Returns whether the given Tuple type is homogeneous: if all items types are the same.
458458
459459 This also includes Tuple[<some_type>, ...]
460460
@@ -651,19 +651,22 @@ def _parse(value: str) -> list[Any]:
651651 # if it doesn't work, fall back to the parse_fn.
652652 values = _fallback_parse (value )
653653
654- # we do the default 'argparse' action, which is to add the values to a bigger list of values.
654+ # we do the default 'argparse' action, which is to add the values to a bigger list of
655+ # values.
655656 # result.extend(values)
656657 logger .debug (f"returning values: { values } " )
657658 return values
658659
659660 def _parse_literal (value : str ) -> list [Any ] | Any :
660661 """try to parse the string to a python expression directly.
662+
661663 (useful for nested lists or tuples.)
662664 """
663665 literal = ast .literal_eval (value )
664666 logger .debug (f"Parsed literal: { literal } " )
665667 if not isinstance (literal , (list , tuple )):
666- # we were passed a single-element container, like "--some_list 1", which should give [1].
668+ # we were passed a single-element container, like "--some_list 1", which should give
669+ # [1].
667670 # We therefore return the literal itself, and argparse will append it.
668671 return T (literal )
669672 else :
@@ -723,8 +726,8 @@ def get_nesting_level(possibly_nested_list):
723726
724727
725728def default_value (field : dataclasses .Field ) -> T | _MISSING_TYPE :
726- """Returns the default value of a field in a dataclass, if available.
727- When not available, returns `dataclasses.MISSING`.
729+ """Returns the default value of a field in a dataclass, if available. When not available,
730+ returns `dataclasses.MISSING`.
728731
729732 Args:
730733 field (dataclasses.Field): The dataclasses.Field to get the default value of.
@@ -781,7 +784,6 @@ def keep_keys(d: dict, keys_to_keep: Iterable[str]) -> tuple[dict, dict]:
781784 Tuple[Dict, Dict]
782785 The same dictionary (with all the unwanted keys removed) as well as a
783786 new dict containing only the removed item.
784-
785787 """
786788 d_keys = set (d .keys ()) # save a copy since we will modify the dict.
787789 removed = {}
@@ -792,7 +794,7 @@ def keep_keys(d: dict, keys_to_keep: Iterable[str]) -> tuple[dict, dict]:
792794
793795
794796def compute_identity (size : int = 16 , ** sample ) -> str :
795- """Compute a unique hash out of a dictionary
797+ """Compute a unique hash out of a dictionary.
796798
797799 Parameters
798800 ----------
@@ -801,7 +803,6 @@ def compute_identity(size: int = 16, **sample) -> str:
801803
802804 **sample:
803805 Dictionary to compute the hash from
804-
805806 """
806807 sample_hash = hashlib .sha256 ()
807808
@@ -840,7 +841,7 @@ def zip_dicts(*dicts: dict[K, V]) -> Iterable[tuple[K, tuple[V | None, ...]]]:
840841
841842
842843def dict_union (* dicts : dict [K , V ], recurse : bool = True , dict_factory = dict ) -> dict [K , V ]:
843- """Simple dict union until we use python 3.9
844+ """Simple dict union until we use python 3.9.
844845
845846 If `recurse` is True, also does the union of nested dictionaries.
846847 NOTE: The returned dictionary has keys sorted alphabetically.
@@ -924,7 +925,8 @@ def unflatten(flattened: Mapping[tuple[K, ...], V]) -> PossiblyNestedDict[K, V]:
924925
925926
926927def flatten_join (nested : PossiblyNestedMapping [str , V ], sep : str = "." ) -> dict [str , V ]:
927- """Flatten a dictionary of dictionaries. Joins different nesting levels with `sep` as separator.
928+ """Flatten a dictionary of dictionaries. Joins different nesting levels with `sep` as
929+ separator.
928930
929931 >>> flatten_join({'a': {'b': 2, 'c': 3}, 'c': {'d': 3, 'e': 4}})
930932 {'a.b': 2, 'a.c': 3, 'c.d': 3, 'c.e': 4}
0 commit comments