def get_head_types(pat):
 if isinstance(pat, (pytree.NodePattern, pytree.LeafPattern)):
  return set([pat.type])
 if isinstance(pat, pytree.NegatedPattern):
  if pat.content:
   return get_head_types(pat.content)
  return set([None])
 if isinstance(pat, pytree.WildcardPattern):
  r = set()
  for p in pat.content:
   for x in p:
    r.update(get_head_types(x))
  return r
 raise Exception(("Oh no! I don't understand pattern %s" % pat))

def get_head_types ( pat ) : if isinstance ( pat , ( pytree . NodePattern , pytree . LeafPattern ) ) : return set ( [ pat . type ] ) if isinstance ( pat , pytree . NegatedPattern ) : if pat . content : return get_head_types ( pat . content ) return set ( [ None ] ) if isinstance ( pat , pytree . WildcardPattern ) : r = set ( ) for p in pat . content : for x in p : r . update ( get_head_types ( x ) ) return r raise Exception ( ( "Oh no! I don't understand pattern %s" % pat ) )
accepts a pytree pattern node and returns a set of the pattern types which will match first .