Skip to content

Commit ebc2cf1

Browse files
authored
Breaking change: Change the function name so that IDEs can properly suggest it. (#7)
* Change the function name to be able to be more easily imported (some IDEs actively avoid suggesting importing something named "main") * Wordsmithing. * Wordsmithing #2
1 parent 5b10273 commit ebc2cf1

File tree

3 files changed

+47
-15
lines changed

3 files changed

+47
-15
lines changed

Diff for: README.md

+34-7
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
# @main
1+
# @python_main
22

33
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/python-main)](https://pypi.org/project/python-main/)
44
[![PyPI - Version](https://img.shields.io/pypi/v/python-main)](https://pypi.org/project/python-main/)
55

66

7-
`@main` decorator which runs the tagged function if the current module is being executed as a script.
7+
`@python_main` is a decorator which:
8+
- Automatically calls the function(s) tagged with it, if the current module is being **executed as a script**.
9+
- Does nothing if the current module is being **imported**.
810

9-
No more `if __name__ == "__main__":` all over the place.
11+
It is, essentially, equivalent to the `if __name__ == "__main__":` construct, but as a decorator.
1012

11-
That's it!
13+
That's all it does.
1214

1315
### Installation
1416

@@ -20,14 +22,39 @@ poetry add python-main # ...
2022
### Usage
2123

2224
```python
23-
from python_main import main
25+
from python_main import python_main
2426

2527
A = 10
2628
B = 20
2729

28-
29-
@main
30+
@python_main
3031
def do_print():
3132
"""This will run if this module is executed."""
3233
print(A + B)
3334
```
35+
36+
You can also tag multiple functions with `@python_main` and they will all run if the module is executed, in the order they are defined.
37+
38+
```python
39+
from python_main import python_main
40+
41+
A = 10
42+
B = 20
43+
C = 0
44+
45+
@python_main
46+
def add_a_to_c():
47+
global C
48+
C += A
49+
50+
# ... other functions/ definitions ...
51+
52+
@python_main
53+
def add_b_to_c():
54+
global C
55+
C += B
56+
57+
# At this point:
58+
# - C will be 30 if this module is executed as a script.
59+
# - C will be untouched if this module is imported.
60+
```

Diff for: python_main/__init__.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,16 @@
66
__MAIN_RETURN_TYPE = TypeVar("__MAIN_RETURN_TYPE")
77

88

9-
def main(f: Callable[[], __MAIN_RETURN_TYPE]) -> Callable[[], __MAIN_RETURN_TYPE]:
10-
if getattr(f, __CALLABLE_MODULE_PROP) == __RAN_AS_SCRIPT_MODULE:
11-
f()
12-
return f
9+
def python_main(
10+
main_function_to_be_called: Callable[[], __MAIN_RETURN_TYPE]
11+
) -> Callable[[], __MAIN_RETURN_TYPE]:
12+
if (
13+
getattr(main_function_to_be_called, __CALLABLE_MODULE_PROP)
14+
== __RAN_AS_SCRIPT_MODULE
15+
):
16+
main_function_to_be_called()
17+
return main_function_to_be_called
1318

1419

1520
# Only export the main function
16-
__all__ = ["main"]
21+
__all__ = ["python_main"]

Diff for: tests/test_basic.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import pytest
44

5-
from python_main import main
5+
from python_main import python_main
66

77
EXIT_CODE_RECEIVED = -1
88

@@ -44,7 +44,7 @@ def test_assert_function_actually_gets_called(mock_exit):
4444
__my_main_func.__module__ = "__main__"
4545

4646
# Decorate it
47-
main(__my_main_func)
47+
python_main(__my_main_func)
4848

4949
# Ensure that our main function was able to call mock_exit with the expected value.
5050
global EXIT_CODE_RECEIVED
@@ -60,7 +60,7 @@ def test_assert_function_does_not_get_called(mock_exit):
6060
"""
6161

6262
# Call the function, which is coming from a pytest execution and being imported as a module
63-
function_returned = main(__my_main_func)
63+
function_returned = python_main(__my_main_func)
6464

6565
# Exit code will not have been set.
6666
global EXIT_CODE_RECEIVED

0 commit comments

Comments
 (0)