Skip to content

Reference

Decorated functions as entry points.

Example

# file.py
from entrypoint import entrypoint

@entrypoint(__name__)
def main() -> None:
    print("Hello, world!")
>>> import file
>>> # no output
$ python file.py
Hello, world!

MAIN = '__main__' module-attribute

The __main__ name constant.

Main = Nullary[None] module-attribute

The main function that does not take arguments and returns nothing.

EntryPoint

Represents handlers for @entrypoint decorators.

Source code in entrypoint/core.py
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
@final
@frozen()
class EntryPoint:
    """Represents handlers for [`@entrypoint`][entrypoint.core.entrypoint] decorators."""

    name: Optional[str] = None

    def __call__(self, main: M) -> M:
        return self.call(main)

    def call(self, main: M) -> M:
        name = self.name

        if name is None:
            name = get_module(main)

        if is_main(name):
            main()

        return main

entrypoint(name: Optional[str] = None) -> EntryPoint

Defines decorated functions as entry points.

Calls the wrapped function if the module gets run directly.

Instead of applying dark magic, this function expects callers to pass the __name__ variable as an argument, and merely checks it against MAIN when needed.

In case __name__ is not provided, this function will attempt to read the module name from the function given (via __module__).

EntryPoint(name) is created under the hood, and is then used to handle calls.

Parameters:

Name Type Description Default
name Optional[str]

The __name__ of the module.

None

Returns:

Type Description
EntryPoint

The created EntryPoint instance.

Source code in entrypoint/core.py
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
def entrypoint(name: Optional[str] = None) -> EntryPoint:
    """Defines decorated functions as entry points.

    Calls the wrapped function if the module gets run directly.

    Instead of applying dark magic, this function expects
    callers to pass the `__name__` variable as an argument,
    and merely checks it against [`MAIN`][entrypoint.core.MAIN] when needed.

    In case `__name__` is not provided, this function will attempt to read
    the module name from the function given (via `__module__`).

    `EntryPoint(name)` is created under the hood, and is
    then used to handle calls.

    Args:
        name: The `__name__` of the module.

    Returns:
        The created [`EntryPoint`][entrypoint.core.EntryPoint] instance.
    """
    return EntryPoint(name)

is_main(name: str) -> bool

Checks if the name is equal to MAIN.

Parameters:

Name Type Description Default
name str

The name to check.

required

Returns:

Type Description
bool

Whether the name is equal to MAIN.

Source code in entrypoint/core.py
18
19
20
21
22
23
24
25
26
27
def is_main(name: str) -> bool:
    """Checks if the `name` is equal to [`MAIN`][entrypoint.core.MAIN].

    Arguments:
        name: The name to check.

    Returns:
        Whether the `name` is equal to [`MAIN`][entrypoint.core.MAIN].
    """
    return name == MAIN