Skip to content

Commit

Permalink
remove fuzzy tag
Browse files Browse the repository at this point in the history
  • Loading branch information
dhpitt committed Feb 23, 2024
1 parent 647cad4 commit b1e9fee
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
12 changes: 8 additions & 4 deletions src/configmypy/argparse_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,12 @@ def read_conf(self, config=None, fuzzy=False, **additional_config):
----------
config : dict, default is None
if not None, a dict config to update
fuzzy: bool, default is False
if True, all values, regardless of type,
infer_types: bool, default is False
if True, uses custom type handling
where all values, regardless of type,
may take the value None.
If false, uses default argparse
typecasting
additional_config : dict
Returns
Expand All @@ -64,8 +67,9 @@ def read_conf(self, config=None, fuzzy=False, **additional_config):

parser = argparse.ArgumentParser(description='Read the config from the commandline.')
for key, value in iter_nested_dict_flat(config, return_intermediate_keys=self.overwrite_nested_config):
# smartly infer types if
inferencer = TypeInferencer(orig_type=type(value), fuzzy=fuzzy, strict=(not self.infer_types))
# smartly infer types if infer_types is turned on
# otherwise force default typecasting
inferencer = TypeInferencer(orig_type=type(value), strict=(not self.infer_types))

parser.add_argument(f'--{key}', type=inferencer, default=value)

Expand Down
30 changes: 15 additions & 15 deletions src/configmypy/type_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import Callable


def infer_boolean(var, fuzzy: bool=False, strict: bool=True):
def infer_boolean(var, strict: bool=True):
"""
accept argparse inputs of string, correctly
convert into bools. Without this behavior,
Expand All @@ -18,14 +18,14 @@ def infer_boolean(var, fuzzy: bool=False, strict: bool=True):
return True
elif var.lower() == 'false':
return False
elif fuzzy and var.lower() == 'None':
elif var.lower() == 'None':
return None
elif strict:
raise ArgumentTypeError()
else:
return str(var)

def infer_numeric(var, fuzzy: bool=False, strict: bool=True):
def infer_numeric(var, strict: bool=True):
# int if possible -> float -> NoneType -> Err
if var.isnumeric():
return int(var)
Expand All @@ -34,27 +34,30 @@ def infer_numeric(var, fuzzy: bool=False, strict: bool=True):
if len(decimal_left_right) == 2:
if sum([x.isnumeric() for x in decimal_left_right]) == 2: # True, True otherwise False
return float(var)
elif fuzzy and var.lower() == 'none':
elif var.lower() == 'none':
return None
elif strict:
raise ArgumentTypeError()
else:
return str(var)

def infer_str(var, fuzzy: bool=False, strict:bool=True):
def infer_str(var, strict:bool=True):
"""
infer string values and handle fuzzy None and bool types
infer string values and handle fuzzy None and bool types (optional)
var: str input
fuzzy:
strict: whether to allow "fuzzy" None types
"""
if fuzzy:
return infer_boolean(var,fuzzy=True,strict=False)
if strict:
return str(var)
elif var.lower() == 'None':
return None
else:
return str(var)


class TypeInferencer(object):
def __init__(self, orig_type: Callable, strict: bool=True, fuzzy: bool=False):
def __init__(self, orig_type: Callable, strict: bool=True):
"""
TypeInferencer mediates between argparse
and ArgparseConfig
Expand All @@ -68,15 +71,12 @@ def __init__(self, orig_type: Callable, strict: bool=True, fuzzy: bool=False):
ex. if argparseConfig takes a boolean for arg 'x',
passing --x False into the command line will return a value
of True, since argparse works with strings and bool('False') = True.
fuzzy: bool, default False
if True, accept values of None regardless of type.
strict: bool, default True
if True, raise ArgumentTypeError when the value cannot be cast to
the allowed types. Otherwise default to str.
"""
self.orig_type = orig_type
self.fuzzy = fuzzy
self.strict = strict

def __call__(self, var):
Expand All @@ -86,9 +86,9 @@ def __call__(self, var):
"""
if self.orig_type == bool:
return infer_boolean(var, self.fuzzy, self.strict)
return infer_boolean(var, self.strict)
elif self.orig_type == float or self.orig_type == int:
return infer_numeric(var, self.fuzzy, self.strict)
return infer_numeric(var, self.strict)
else:
if self.strict:
return infer_str(var)
Expand Down

0 comments on commit b1e9fee

Please sign in to comment.