def explicit_package_contents_filter()

in tools/tensorflow_docs/api_generator/public_api.py [0:0]


def explicit_package_contents_filter(path: Sequence[str], parent: Any,
                                     children: Children) -> Children:
  """Filter modules to only include explicit contents.

  This function returns the children explicitly included by this module, meaning
  that it will exclude:

  *   Modules in a package not explicitly imported by the package (submodules
      are implicitly injected into their parent's namespace).
  *   Modules imported by a module that is not a package.

  This filter is useful if you explicitly define your API in the packages of
  your library, but do not expliticly define that API in the `__all__` variable
  of each module. The purpose is to make it easier to maintain that API.

  Note: This filter does work with wildcard imports, however it is generally not
  recommended to use wildcard imports.

  Args:
    path: A tuple of names forming the path to the object.
    parent: The parent object.
    children: A list of (name, value) tuples describing the attributes of the
      patent.

  Returns:
    A filtered list of children `(name, value)` pairs.
  """
  del path  # Unused
  is_parent_module = inspect.ismodule(parent)
  is_parent_package = is_parent_module and hasattr(parent, '__path__')
  if is_parent_package:
    imported_symbols = _get_imported_symbols(parent)
  filtered_children = []
  for child in children:
    name, obj = child
    if inspect.ismodule(obj):
      # Do not include modules in a package not explicitly imported by the
      # package.
      if is_parent_package and name not in imported_symbols:
        continue
      # Do not include modules imported by a module that is not a package.
      if is_parent_module and not is_parent_package:
        continue
    filtered_children.append(child)
  return filtered_children