populate_from_public_api

populate_from_public_api#

penzai.treescope.canonical_aliases.populate_from_public_api(module: module, predicate: ~typing.Callable[[~typing.Any, ~penzai.treescope.canonical_aliases.ModuleAttributePath], bool] = <function default_well_known_filter>)[source]#

Populates canonical aliases with all public symbols in a module.

Attempts to walk this module and its submodules to extract well-known symbols. Symbols that already have an alias defined will be ignored.

If the module defines __all__, we assume the symbols in __all__ are the well-known symbols in this module. (See https://docs.python.org/3/reference/simple_stmts.html#the-import-statement)

If the module does not define __all__, we look for all names that do not start with “_”.

We then additionally filter down this set to the set of objects for which the predicate argument returns True.

This function should only be called on modules with a well-defined public API, that exports only functions defined in the module itself. If a module re-exports symbols from another external module (e.g. importing partial from functools), we might otherwise end up using the unrelated module as the “canonical” source of that object. (The prefix_filter function below tries to prevent this if possible when used as a predicate.)

Parameters:
  • module – The module we will collect symbols from.

  • predicate – A filter function to check if an object should be given an alias.