def _expand_scope(scope):
    """Returns a scope and all subscopes

    Arguments:
      scope (str): the scope to expand

    Returns:
      expanded scope (set): set of all scope's subscopes including the scope itself
    """

    # remove filter, save for later
    scope_name, sep, filter_ = scope.partition('!')

    # expand scope and subscopes
    expanded_scope_names = set()

    def _add_subscopes(scope_name):
        expanded_scope_names.add(scope_name)
        if scope_definitions[scope_name].get('subscopes'):
            for subscope in scope_definitions[scope_name].get('subscopes'):
                _add_subscopes(subscope)

    _add_subscopes(scope_name)

    # reapply !filter
    if filter_:
        expanded_scopes = {
            f"{scope_name}!{filter_}"
            for scope_name in expanded_scope_names
            # server scopes have some cross-resource subscopes
            # where the !server filter doesn't make sense,
            # e.g. read:servers -> read:users:name
            if not (filter_.startswith("server") and scope_name.startswith("read:user"))
        }
    else:
        expanded_scopes = expanded_scope_names

    # return immutable frozenset because the result is cached
    return frozenset(expanded_scopes)
Returns a scope and all subscopes

Arguments:
scope (str): the scope to expand

Returns:
expanded scope (set): set of all scope's subscopes including the scope itself
jupyterhub/scopes.py