Skip to content

Commit

Permalink
add support for iterable types for depth 1
Browse files Browse the repository at this point in the history
  • Loading branch information
dhpitt committed Mar 7, 2024
1 parent b1e9fee commit c492b58
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/configmypy/type_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,26 @@ def infer_str(var, strict:bool=True):
return None
else:
return str(var)


def infer_iterable(var, iterable_type: Callable, strict: bool=True):
"""
infer value of an iterable with expected type List[type] or Tuple[type]
var: str
input to argparse
iterable_type: Callable
function to apply on contents of iterable
"""
# unpack outer list, apply recursively, return list or tuple of inner
if var[0] == "[" and var[-1] == "]":
return infer_iterable(var[1:-1], iterable_type, strict)
elif var[0] == "(" and var [-1] == ")":
return tuple(infer_iterable(var[1:-1], iterable_type, strict))
else:
iterable_entries = var.split(",")
return [iterable_type(x,strict) for x in iterable_entries]



class TypeInferencer(object):
def __init__(self, orig_type: Callable, strict: bool=True):
Expand Down

0 comments on commit c492b58

Please sign in to comment.