nodes: 0, module; 1, function_definition; 2, function_name:pick_kwargs; 3, parameters; 4, block; 5, typed_parameter; 6, typed_parameter; 7, typed_default_parameter; 8, expression_statement; 9, expression_statement; 10, expression_statement; 11, if_statement; 12, expression_statement; 13, if_statement; 14, return_statement; 15, identifier:kwargs; 16, type; 17, identifier:required_args; 18, type; 19, identifier:prefix; 20, type; 21, None; 22, comment:"""
      Given a dict of kwargs and a list of required_args, return a dict
      containing only the args in required_args.
  
      If prefix is specified, also search for args that begin with '{prefix}__'.
      Removes prefix in returned dict.
  
      Raises ValueError if both prefixed and unprefixed arg are given in kwargs.
  
      >>> from pprint import pprint as p # Use pprint to sort dict keys
      >>> kwargs = {'a': 1, 'b': 2, 'c': 3, 'x__d': 4}
      >>> p(pick_kwargs(kwargs, ['a', 'd']))
      {'a': 1}
      >>> p(pick_kwargs(kwargs, ['a', 'd'], prefix='x'))
      {'a': 1, 'd': 4}
  
      >>> pick_kwargs({'a': 1, 'x__a': 2}, ['a'], prefix='x')
      Traceback (most recent call last):
      ValueError: Both prefixed and unprefixed args were specified for the
      following parameters: ['a']
      """; 23, assignment; 24, assignment; 25, identifier:prefix; 26, block; 27, assignment; 28, identifier:conflicting_args; 29, block; 30, call; 31, identifier:dict; 32, identifier:list; 33, identifier:str; 34, identifier:picked; 35, dictionary_comprehension; 36, identifier:prefixed; 37, dictionary; 38, expression_statement; 39, expression_statement; 40, identifier:conflicting_args; 41, list_comprehension; 42, raise_statement; 43, attribute; 44, argument_list; 45, pair; 46, for_in_clause; 47, if_clause; 48, assignment; 49, assignment; 50, identifier:k; 51, for_in_clause; 52, if_clause; 53, call; 54, identifier:tz; 55, identifier:merge; 56, identifier:picked; 57, identifier:prefixed; 58, identifier:k; 59, identifier:v; 60, pattern_list; 61, call; 62, comparison_operator:k in required_args; 63, identifier:prefix; 64, binary_operator:prefix + '__'; 65, identifier:prefixed; 66, dictionary_comprehension; 67, identifier:k; 68, identifier:picked; 69, comparison_operator:k in prefixed; 70, identifier:ValueError; 71, argument_list; 72, identifier:k; 73, identifier:v; 74, attribute; 75, argument_list; 76, identifier:k; 77, identifier:required_args; 78, identifier:prefix; 79, string; 80, pair; 81, for_in_clause; 82, if_clause; 83, identifier:k; 84, identifier:prefixed; 85, call; 86, identifier:kwargs; 87, identifier:items; 88, string_content:__; 89, call; 90, identifier:v; 91, pattern_list; 92, call; 93, boolean_operator; 94, attribute; 95, argument_list; 96, identifier:_remove_prefix; 97, argument_list; 98, identifier:k; 99, identifier:v; 100, attribute; 101, argument_list; 102, call; 103, comparison_operator:_remove_prefix(k, prefix) in required_args; 104, concatenated_string; 105, identifier:format; 106, identifier:conflicting_args; 107, identifier:k; 108, identifier:prefix; 109, identifier:kwargs; 110, identifier:items; 111, attribute; 112, argument_list; 113, call; 114, identifier:required_args; 115, string; 116, string; 117, identifier:k; 118, identifier:startswith; 119, identifier:prefix; 120, identifier:_remove_prefix; 121, argument_list; 122, string_content:Both prefixed and unprefixed args were specified; 123, string_content:for the following parameters: {}; 124, identifier:k; 125, identifier:prefix
code: def pick_kwargs(kwargs: dict, required_args: list, prefix: str = None):
      """
      Given a dict of kwargs and a list of required_args, return a dict
      containing only the args in required_args.
  
      If prefix is specified, also search for args that begin with '{prefix}__'.
      Removes prefix in returned dict.
  
      Raises ValueError if both prefixed and unprefixed arg are given in kwargs.
  
      >>> from pprint import pprint as p # Use pprint to sort dict keys
      >>> kwargs = {'a': 1, 'b': 2, 'c': 3, 'x__d': 4}
      >>> p(pick_kwargs(kwargs, ['a', 'd']))
      {'a': 1}
      >>> p(pick_kwargs(kwargs, ['a', 'd'], prefix='x'))
      {'a': 1, 'd': 4}
  
      >>> pick_kwargs({'a': 1, 'x__a': 2}, ['a'], prefix='x')
      Traceback (most recent call last):
      ValueError: Both prefixed and unprefixed args were specified for the
      following parameters: ['a']
      """
      picked = {k: v for k, v in kwargs.items() if k in required_args}
  
      prefixed = {}
      if prefix:
          prefix = prefix + '__'
          prefixed = {
              _remove_prefix(k, prefix): v
              for k, v in kwargs.items()
              if k.startswith(prefix)
              and _remove_prefix(k, prefix) in required_args
          }
  
      conflicting_args = [k for k in picked if k in prefixed]
      if conflicting_args:
          raise ValueError(
              'Both prefixed and unprefixed args were specified '
              'for the following parameters: {}'.format(conflicting_args)
          )
  
      return tz.merge(picked, prefixed)