Skip to content

Commit 4f36e8f

Browse files
authored
Migrate to ruff 0.16
1 parent ad32ba0 commit 4f36e8f

31 files changed

Lines changed: 557 additions & 518 deletions

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Simply apply the decorators and the package takes care of the rest.
2020
```python
2121
from injection import injectable, inject, singleton
2222

23+
2324
@singleton
2425
class Printer:
2526
def __init__(self):
@@ -29,6 +30,7 @@ class Printer:
2930
self.history.append(message)
3031
print(message)
3132

33+
3234
@injectable
3335
class Service:
3436
def __init__(self, printer: Printer):
@@ -37,10 +39,12 @@ class Service:
3739
def hello(self):
3840
self.printer.print("Hello world!")
3941

42+
4043
@inject
4144
def main(service: Service):
4245
service.hello()
4346

47+
4448
if __name__ == "__main__":
4549
main()
4650
```

docs/guides/imports.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@ For more control over which modules get imported, use `PythonModuleLoader` with
3232
from injection.loaders import PythonModuleLoader
3333
from src import adapters, services
3434

35+
3536
def predicate(module_name: str) -> bool:
3637
# Only import modules containing "impl" in their name
3738
return "impl" in module_name
3839

40+
3941
PythonModuleLoader(predicate).load(adapters, services)
4042
```
4143

@@ -67,7 +69,7 @@ This last approach is particularly useful for explicitly marking which modules s
6769
```python
6870
# auto-import
6971

72+
7073
@injectable(on=AbstractDependency)
71-
class Dependency(AbstractDependency):
72-
...
74+
class Dependency(AbstractDependency): ...
7375
```

docs/guides/main-functions.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ from injection import adefine_scope
1212
from injection.entrypoint import AsyncEntrypoint, Entrypoint, entrypointmaker
1313
from injection.loaders import PythonModuleLoader
1414

15+
1516
@entrypointmaker
1617
def entrypoint[**P, T](self: AsyncEntrypoint[P, T]) -> Entrypoint[P, T]:
1718
import src
@@ -32,6 +33,7 @@ async def main(dependency: Dependency):
3233
# All setup is automatically applied
3334
...
3435

36+
3537
if __name__ == "__main__":
3638
main()
3739
```
@@ -60,6 +62,7 @@ from injection.loaders import ProfileLoader, PythonModuleLoader
6062

6163
profile_loader = ProfileLoader(...)
6264

65+
6366
@entrypointmaker(profile_loader=profile_loader)
6467
def entrypoint[**P, T](self: Entrypoint[P, T]) -> Entrypoint[P, T]:
6568
import src
@@ -84,27 +87,26 @@ from injection.entrypoint import Entrypoint, entrypointmaker
8487
from injection.loaders import PythonModuleLoader
8588
from os import getenv
8689

90+
8791
@dataclass
8892
class Config:
8993
profile: Profile
9094

95+
9196
@constant
9297
def _config_factory() -> Config:
9398
profile = Profile(getenv("PROFILE", "development"))
9499
return Config(profile)
95100

101+
96102
@entrypointmaker(profile_loader=profile_loader)
97103
def entrypoint[**P, T](self: Entrypoint[P, T], config: Config) -> Entrypoint[P, T]:
98104
import src
99-
105+
100106
profile = config.profile # Use config to determine profile
101107
suffixes = self.profile_loader.required_module_names(profile)
102108
module_loader = PythonModuleLoader.endswith(*suffixes)
103-
return (
104-
self.inject()
105-
.load_profile(profile)
106-
.load_modules(module_loader, src)
107-
)
109+
return self.inject().load_profile(profile).load_modules(module_loader, src)
108110
```
109111

110112
In this example, `config` is resolved from the default module and used to dynamically load the appropriate profile.

docs/guides/profiles.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ Storing your profile names in a `StrEnum` is recommended for type safety and con
1111
from enum import StrEnum
1212
from injection import mod
1313

14+
1415
class Profile(StrEnum):
1516
DEV = "development"
1617
PROD = "production"
1718
STAGING = "staging"
1819

20+
1921
# Get a module for a specific profile
2022
dev_module = mod(Profile.DEV)
2123
```
@@ -44,10 +46,12 @@ For more complex scenarios where profiles share common subsets of dependencies,
4446
```python
4547
from injection.loaders import ProfileLoader
4648

47-
profile_loader = ProfileLoader({
48-
Profile.DEV: [SubProfile.STUB],
49-
Profile.TEST: [SubProfile.STUB],
50-
})
49+
profile_loader = ProfileLoader(
50+
{
51+
Profile.DEV: [SubProfile.STUB],
52+
Profile.TEST: [SubProfile.STUB],
53+
}
54+
)
5155

5256
profile_loader.load(Profile.DEV)
5357
```

docs/guides/register-dependencies.md

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ A new instance is created every time the dependency is resolved.
1010
```python
1111
from injection import injectable
1212

13+
1314
@injectable
14-
class Dependency:
15-
...
15+
class Dependency: ...
1616
```
1717

1818
## Singleton
@@ -21,9 +21,9 @@ A single instance is created and shared across the entire application.
2121
```python
2222
from injection import singleton
2323

24+
2425
@singleton
25-
class Dependency:
26-
...
26+
class Dependency: ...
2727
```
2828

2929
## Constant
@@ -33,10 +33,12 @@ Register a pre-existing value as a dependency.
3333
from dataclasses import dataclass
3434
from injection import set_constant
3535

36+
3637
@dataclass(frozen=True)
3738
class Settings:
3839
api_key: str
3940

41+
4042
settings = set_constant(Settings("<secret_api_key>"))
4143
```
4244

@@ -53,9 +55,9 @@ For lazy constants, use the `@constant` decorator:
5355
```python
5456
from injection import constant
5557

58+
5659
@constant
57-
class LazySettings:
58-
...
60+
class LazySettings: ...
5961
```
6062

6163
## Factories
@@ -66,8 +68,9 @@ _Make sure not to forget the return type annotation._
6668
```python
6769
from injection import injectable
6870

69-
class Dependency:
70-
...
71+
72+
class Dependency: ...
73+
7174

7275
@injectable
7376
def _dependency_factory() -> Dependency:
@@ -85,12 +88,12 @@ Register an implementation for an abstract class or protocol.
8588
from injection import injectable
8689
from abc import ABC
8790

88-
class AbstractDependency(ABC):
89-
...
91+
92+
class AbstractDependency(ABC): ...
93+
9094

9195
@injectable(on=AbstractDependency)
92-
class Dependency(AbstractDependency):
93-
...
96+
class Dependency(AbstractDependency): ...
9497
```
9598

9699
## Scoped
@@ -99,9 +102,9 @@ A single instance is created per scope. Using a `StrEnum` for scope names is rec
99102
```python
100103
from injection import scoped
101104

105+
102106
@scoped("<scope_name>")
103-
class Dependency:
104-
...
107+
class Dependency: ...
105108
```
106109

107110
## Scoped with context manager
@@ -114,12 +117,12 @@ Scoped dependencies can be registered using generator functions (sync or async)
114117
from collections.abc import Iterator
115118
from injection import scoped
116119

120+
117121
class Dependency:
118-
def open(self):
119-
...
122+
def open(self): ...
123+
124+
def close(self): ...
120125

121-
def close(self):
122-
...
123126

124127
@scoped("<scope_name>")
125128
def dependency_factory() -> Iterator[Dependency]:
@@ -137,7 +140,7 @@ Register a dependency for a specific profile. Using a `StrEnum` for profile name
137140
```python
138141
from injection import mod
139142

143+
140144
@mod("<profile_name>").injectable
141-
class Dependency:
142-
...
145+
class Dependency: ...
143146
```

docs/guides/resolve-dependencies.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ The `@inject` decorator automatically resolves function parameters based on thei
66
```python
77
from injection import inject
88

9+
910
@inject
10-
def function(dependency: Dependency):
11-
...
11+
def function(dependency: Dependency): ...
12+
1213

1314
function() # You can now call `function` without arguments
1415
```
@@ -23,17 +24,17 @@ When `@inject` is applied to an async function, it can resolve dependencies that
2324
Static type checkers like mypy will complain about missing arguments when calling injected functions:
2425
```python
2526
@inject
26-
def function(dependency: Dependency):
27-
...
27+
def function(dependency: Dependency): ...
28+
2829

2930
function() # ❌ mypy error: Missing positional argument "dependency" in call to "function" [call-arg]
3031
```
3132

3233
To fix this, provide a default value for injected parameters:
3334
```python
3435
@inject
35-
def function(dependency: Dependency = NotImplemented):
36-
...
36+
def function(dependency: Dependency = NotImplemented): ...
37+
3738

3839
function() # ✅ OK
3940
```
@@ -47,6 +48,7 @@ The `@asfunction` decorator provides an alternative to `@inject` for cases where
4748
from injection import asfunction
4849
from typing import NamedTuple
4950

51+
5052
@asfunction
5153
class Function(NamedTuple):
5254
dependency: Dependency
@@ -55,6 +57,7 @@ class Function(NamedTuple):
5557
# Use self.dependency here
5658
...
5759

60+
5861
# Call with only the runtime parameters
5962
Function("foo", "bar", "baz")
6063
```
@@ -126,9 +129,10 @@ The async version of `get_lazy_instance` returns an awaitable instead of an inve
126129
```python
127130
from injection import LazyInstance
128131

132+
129133
class Class:
130134
dependency = LazyInstance(Dependency)
131-
135+
132136
def do_something(self):
133137
self.dependency.some_method() # Resolved on every access
134138
```

docs/guides/scopes.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,14 @@ from injection import MappedScope
5252

5353
type Locale = str
5454

55+
5556
@dataclass
5657
class Bindings:
5758
locale: Locale
58-
59+
5960
scope = MappedScope("<scope_name>")
6061

62+
6163
with Bindings("fr_FR").scope.define():
6264
# Dependencies can now access the locale binding
6365
...

docs/index.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Simply apply the decorators and the package takes care of the rest.
3737
```python
3838
from injection import injectable, inject, singleton
3939

40+
4041
@singleton
4142
class Printer:
4243
def __init__(self):
@@ -46,6 +47,7 @@ class Printer:
4647
self.history.append(message)
4748
print(message)
4849

50+
4951
@injectable
5052
class Service:
5153
def __init__(self, printer: Printer):
@@ -54,10 +56,12 @@ class Service:
5456
def hello(self):
5557
self.printer.print("Hello world!")
5658

59+
5760
@inject
5861
def main(service: Service):
5962
service.hello()
6063

64+
6165
if __name__ == "__main__":
6266
main()
6367
```

0 commit comments

Comments
 (0)