Skip to content

Commit e2ca828

Browse files
author
remimd
committed
wip
1 parent d38f0fa commit e2ca828

15 files changed

Lines changed: 129 additions & 30 deletions

File tree

documentation/entrypoint.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ or `injectables`, because everything is not yet fully configured at this stage.
2525

2626
import uvloop
2727
from injection import adefine_scope
28-
from injection.entrypoint import AsyncEntrypoint, Entrypoint, entrypoint_maker
28+
from injection.entrypoint import AsyncEntrypoint, Entrypoint, entrypointmaker
2929
from injection.loaders import PythonModuleLoader
3030

31-
@entrypoint_maker
31+
@entrypointmaker
3232
def entrypoint[**P, T](self: AsyncEntrypoint[P, T]) -> Entrypoint[P, T]:
3333
import src
34-
34+
3535
loader = PythonModuleLoader.from_keywords("# Auto-import")
3636
return (
3737
self.inject()

injection/entrypoint.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from injection import Module, mod
1313
from injection.loaders import PythonModuleLoader
1414

15-
__all__ = ("AsyncEntrypoint", "Entrypoint", "autocall", "entrypoint_maker")
15+
__all__ = ("AsyncEntrypoint", "Entrypoint", "autocall", "entrypointmaker")
1616

1717
type AsyncEntrypoint[**P, T] = Entrypoint[P, Coroutine[Any, Any, T]]
1818
type EntrypointDecorator[**P, T1, T2] = Callable[[Callable[P, T1]], Callable[P, T2]]
@@ -31,7 +31,7 @@ def decorator(wp: Callable[P, T]) -> Callable[P, T]:
3131

3232

3333
@overload
34-
def entrypoint_maker[*Ts, **P, T1, T2](
34+
def entrypointmaker[*Ts, **P, T1, T2](
3535
wrapped: EntrypointSetupMethod[*Ts, P, T1, T2],
3636
/,
3737
*,
@@ -40,7 +40,7 @@ def entrypoint_maker[*Ts, **P, T1, T2](
4040

4141

4242
@overload
43-
def entrypoint_maker[*Ts, **P, T1, T2](
43+
def entrypointmaker[*Ts, **P, T1, T2](
4444
wrapped: None = ...,
4545
/,
4646
*,
@@ -51,7 +51,7 @@ def entrypoint_maker[*Ts, **P, T1, T2](
5151
]: ...
5252

5353

54-
def entrypoint_maker[*Ts, **P, T1, T2](
54+
def entrypointmaker[*Ts, **P, T1, T2](
5555
wrapped: EntrypointSetupMethod[*Ts, P, T1, T2] | None = None,
5656
/,
5757
*,

tests/entrypoint/test_autocall.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from injection.entrypoint import autocall
2+
3+
4+
def test_autocall_with_success():
5+
count = 0
6+
7+
@autocall
8+
def increment() -> None:
9+
nonlocal count
10+
count += 1
11+
12+
assert count == 1
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
from collections.abc import Iterator
2+
from contextlib import contextmanager
3+
4+
from injection import injectable
5+
from injection.entrypoint import Entrypoint
6+
7+
8+
class TestEntrypoint:
9+
def test_async_to_sync_with_success_return_entrypoint(self):
10+
async def async_function() -> int:
11+
return 42
12+
13+
entrypoint = Entrypoint(async_function).async_to_sync()
14+
assert entrypoint() == 42
15+
16+
def test_decorate_with_success_return_entrypoint(self):
17+
enter_count = 0
18+
exit_count = 0
19+
20+
@contextmanager
21+
def decorator() -> Iterator[None]:
22+
nonlocal enter_count, exit_count
23+
enter_count += 1
24+
yield
25+
exit_count += 1
26+
27+
def function():
28+
assert enter_count == exit_count + 1
29+
30+
entrypoint = Entrypoint(function).decorate(decorator())
31+
entrypoint()
32+
assert enter_count == exit_count == 1
33+
34+
def test_inject_with_success_return_entrypoint(self):
35+
@injectable
36+
class Service: ...
37+
38+
def function(service: Service) -> bool:
39+
return isinstance(service, Service)
40+
41+
entrypoint = Entrypoint(function).inject()
42+
assert entrypoint()
43+
44+
def test_setup_with_success_return_entrypoint(self):
45+
count = 0
46+
47+
def increment() -> None:
48+
nonlocal count
49+
count += 1
50+
51+
def function(): ...
52+
53+
entrypoint = Entrypoint(function).setup(increment)
54+
entrypoint()
55+
assert count == 1
56+
57+
def test_async_setup_with_success_return_entrypoint(self):
58+
count = 0
59+
60+
async def increment() -> None:
61+
nonlocal count
62+
count += 1
63+
64+
async def function(): ...
65+
66+
entrypoint = Entrypoint(function).async_setup(increment).async_to_sync()
67+
entrypoint()
68+
assert count == 1
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from injection.entrypoint import Entrypoint, entrypointmaker
2+
3+
4+
def test_entrypointmaker_with_success_return_entrypoint_decorator():
5+
count = 0
6+
7+
def increment() -> None:
8+
nonlocal count
9+
count += 1
10+
11+
@entrypointmaker
12+
def entrypoint[**P, T](self: Entrypoint[P, T]) -> Entrypoint[P, T]:
13+
return self.setup(increment)
14+
15+
@entrypoint
16+
def function(): ...
17+
18+
function()
19+
assert count == 1
File renamed without changes.

0 commit comments

Comments
 (0)