Skip to content

Commit

Permalink
Update docs to use new syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
maldoinc committed Sep 12, 2023
1 parent 86109e0 commit 9f34831
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 18 deletions.
4 changes: 2 additions & 2 deletions docs/pages/interfaces.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ which concrete class should be resolved.

```python
def home(
engine: Engine = wire(qualifier="electric"),
combustion: Engine = wire(qualifier="combustion"),
engine: Annotated[Engine, Wire(qualifier="electric")],
combustion: Annotated[Engine, Wire(qualifier="combustion")],
):
...
```
4 changes: 2 additions & 2 deletions docs/pages/parameters.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ To inject a parameter by name simply call `wire(param="param_name")`.

```python
@container.autowire
def do_something_cool(cache_dir: str = wire(param="cache_dir")) -> None:
def do_something_cool(cache_dir: Annotated[str, Wire(param="cache_dir")]) -> None:
...
```

Expand All @@ -39,7 +39,7 @@ at once and concatenate their values together or simply format the value of a si

```python
@container.autowire
def do_something_cool(logs_cache_dir: str = wire(expr="${cache_dir}/${env}/logs")) -> None:
def do_something_cool(logs_cache_dir: Annotated[str, Wire(expr="${cache_dir}/${env}/logs")]) -> None:
...
```

Expand Down
23 changes: 13 additions & 10 deletions docs/pages/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,20 @@ from wireup import container
# Parameters serve as configuration for services.
# Think of a database url or environment name.
container.params.update({
"db.connection_str": os.environ.get("DATABASE_URL") # (1)!
"db.connection_str": os.environ.get("DATABASE_URL") # (1)!
"cache_dir": gettempdir(),
"env": os.environ.get("ENV", "dev")
})


# Constructor injection is supported for regular classes as well as dataclasses.
@container.register # (2)!
# Constructor injection is supported for regular classes as well as dataclasses
@container.register # (2)!
class DbService:
# Inject a parameter by name
connection_str: str = wire(param="db.connection_str"),
connection_str: Annotated[str, Wire(param="db.connection_str")],
# Or by interpolating multiple parameters into a string
cache_dir: str = wire(expr="${cache_dir}/${env}/db"),
cache_dir: Annotated[str, Wire(expr="${cache_dir}/${env}/db")],

# resulting in a more compact syntax.
@container.register
@dataclass
class UserRepository:
Expand All @@ -41,17 +40,21 @@ class UserRepository:

```python
@app.route("/greet/<str:name>")
@container.autowire # (2)!
@container.autowire # (1)!
# Classes are automatically injected based on annotated type.
# Parameters will be located based on the hint given in their default value.
# Unknown arguments will not be processed.
def greet(name: str, user_repository: UserRepository, env: str = wire(param="env")): # (1)!
def greet(
name: str,
user_repository: UserRepository,
env: Annotated[str, Wire(param="env")]
):
...
```

1. We know that this will be used in conjunction with many other libraries, so WireUp will not throw on unknown
1. Decorate all methods where the library must perform injection.
We know that this will be used in conjunction with many other libraries, so WireUp will not throw on unknown
parameters in order to let other decorators to do their job.
2. Decorate all methods where the library must perform injection.

**Installation**

Expand Down
8 changes: 4 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ for services and a simple web view called "greet".
**1. Register dependencies**

```python
from wireup import container
from wireup import container, Wire

# Parameters serve as configuration for services.
# Think of a database url or environment name.
Expand All @@ -32,9 +32,9 @@ container.params.update({
@container.register
class DbService:
# Inject a parameter by name
connection_str: str = wire(param="db.connection_str"),
connection_str: Annotated[str, Wire(param="db.connection_str")],
# Or by interpolating multiple parameters into a string
cache_dir: str = wire(expr="${cache_dir}/${env}/db"),
cache_dir: Annotated[str, Wire(expr="${cache_dir}/${env}/db")],


@container.register
Expand All @@ -51,7 +51,7 @@ class UserRepository:
# Decorate all targets where the library must perform injection,such as views in a web app.
# Classes are automatically injected based on annotated type.
# Parameters will be located based on the hint given in their default value.
def greet(name: str, user_repository: UserRepository, env: str = wire(param="env")):
def greet(name: str, user_repository: UserRepository, env: Annotated[str, Wire(param="env")]):
...
```

Expand Down
3 changes: 3 additions & 0 deletions test/test_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,3 +431,6 @@ def do_thing(self):

def test_get_returns_same_container_proxy(self):
self.assertEqual(self.container.get(RandomService), self.container.get(RandomService))



0 comments on commit 9f34831

Please sign in to comment.