peal.global_utils.find_subclasses

peal.global_utils.find_subclasses(base_class, directory)[source]

Import every module under directory and collect subclasses.

Used by the config machinery to find, for example, all ExplainerConfig subclasses so that a config file can be mapped onto its pydantic model by name. Every .py file below directory is imported as a module relative to the project root; anything under a dependencies directory is skipped, since the vendored third-party code is expensive and often not importable. Import errors are printed and the module is skipped. Afterwards, top-level packages whose name starts with directory are imported as well.

Parameters:
  • base_class (type) – The class whose subclasses are wanted (base_class itself matches too, as issubclass is reflexive).

  • directory (str) – Directory to walk.

Returns:

The classes found, possibly with duplicates.

Return type:

list of type