Skip to content

Improve code for stripping off outer types #4

Open

Description

@neighthan

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

Activity

  1. neighthan commented on Jun 26, 2020

    @neighthan
    OwnerAuthor

    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
  2. neighthan commented on Jan 25, 2021

    @neighthan
    OwnerAuthor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions