neighthan / auto-argparse

Automatically create argparse parsers.
MIT License
2 stars 1 forks source link

Improve code for stripping off outer types #4

Open neighthan opened 4 years ago

neighthan commented 4 years ago

When we're handling things like Optional (Union[T, None]) or List we essentially strip off one typing layer, possibly storing some information about it, and then proceed to get the parser arguments from the inner types. It would be nicer to wrap this logic into separate functions so we could do something like

anno = param.annotation
kwargs = {}
for preprocess_func in [strip_optional, strip_sequence]:
    anno, kwargs = preprocess_func(anno, kwargs)
# the rest of the logic for inner types
neighthan commented 4 years ago

While we're at it, we might as well just put the logic for the inner types into a little function as well. I think it might look clearer then, though, to have it linear instead of using a loop:

anno, kwargs = strip_optional(anno, kwargs)
anno, kwargs = strip_sequence(anno, kwargs)
anno, kwargs = inner_logic(anno, kwargs) # make a real name for this
neighthan commented 3 years ago

Take a look at https://docs.python.org/3/whatsnew/3.8.html#typing; the new typing.get_origin and typing.get_args may be helpful to use.