Skip to content

@export decorator that adds a function or class to __all__

License

Notifications You must be signed in to change notification settings

jorenham/exports

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Apr 3, 2025
545d75d · Apr 3, 2025

History

76 Commits
Apr 3, 2025
Aug 19, 2024
Apr 3, 2025
Dec 4, 2024
Jul 18, 2023
Aug 19, 2024
Apr 3, 2025
Jul 18, 2023
Apr 3, 2025
Apr 3, 2025
Apr 3, 2025

Repository files navigation

python-exports

The DRY alternative to __all__

exports - PyPI exports - Python Versions exports - license

exports - CI exports - basedmypy exports - basedpyright exports - ruff


Installation

To install the exports package, you can use python-exports on PyPI:

pip install python-exports

Usage

>>> from exports import export

Now you can use it to add to __all__ as

  • function decorator

    >>> @export
    ... def spam():
    ...     ...
  • class decorator:

    >>> @export
    ... class Ham:
    ...     ...
  • by name:

    >>> from functools import reduce as fold
    >>> export('fold')

Behaviour

If the module has no __all__, it is created. Otherwise, __all__ is converted to a list, and the export is appended.

Caveats

Exporting a function or class directly relies on the __name__ attribute, so consider the following example:

>>> def eggs():
...     ...
>>> fake_eggs = eggs

If we want to export fake_eggs, then this will not work:

>>> export(fake_eggs)  # BAD: this will add `'eggs'` to `__all__`

In such cases, use the name instead:

>>> export('fake_eggs')  # GOOD

You'll be safe if you either

  • decorate a function or a class directly with @export,
  • pass the name string when using plain export('...') calls.