Skip to content

Commit

Permalink
chores: reduce redudant typevar, add support for python3.13
Browse files Browse the repository at this point in the history
  • Loading branch information
raceychan committed Nov 22, 2024
1 parent 440369b commit 8232a9f
Show file tree
Hide file tree
Showing 12 changed files with 22 additions and 33 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.9", "3.10", "3.11", "3.12"]
python-version: ["3.9", "3.10", "3.11", "3.12", "3.13"]
steps:
- name: Checkout
uses: actions/checkout@v4
Expand Down
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

## Install

ididi requires python >= 3.9

```bash
pip install ididi
```
Expand Down Expand Up @@ -60,11 +62,9 @@ NOTE:
2. async resource in a sync dependent is not supported, but sync resource in a async dependent is supported.

```python
from ididi import DependencyGraph
import ididi

dg = DependencyGraph()

@dg.node
async def get_db(client: Client) -> ty.AsyncGenerator[DataBase, None]:
db = DataBase(client)
assert client.is_opened
Expand All @@ -74,13 +74,14 @@ async def get_db(client: Client) -> ty.AsyncGenerator[DataBase, None]:
finally:
await db.close()

@dg.entry
@ididi.entry
async def main(db: DataBase, sql: str) -> ty.Any:
res = await db.execute(sql)
return res

assert await main(sql="select money from bank")
```

> [!NOTE]
> **`DependencyGraph.node` accepts a wide arrange of types, such as dependent class, sync/async facotry, sync/async resource factory, with typing support.**
Expand Down
3 changes: 1 addition & 2 deletions ididi/_ds.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
from ._type_resolve import get_bases
from .node import DependentNode
from .utils.param_utils import MISSING, Maybe

T = ty.TypeVar("T")
from .utils.typing_utils import T

GraphNodes = dict[type[T], DependentNode[T]]
"""
Expand Down
6 changes: 1 addition & 5 deletions ididi/_itypes.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import inspect
import typing as ty

from typing_extensions import ParamSpec

R = ty.TypeVar("R")
P = ParamSpec("P")
T = ty.TypeVar("T")
from .utils.typing_utils import P, R, T

EMPTY_SIGNATURE = inspect.Signature()
INSPECT_EMPTY = inspect.Signature.empty
Expand Down
4 changes: 2 additions & 2 deletions ididi/_type_resolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@

import typing_extensions as tye

from ._itypes import AsyncClosable, Closable, T
from ._itypes import AsyncClosable, Closable
from .errors import (
ForwardReferenceNotFoundError,
GenericDependencyNotSupportedError,
MissingReturnTypeError,
)
from .utils.typing_utils import eval_type, get_full_typed_signature, is_builtin_type
from .utils.typing_utils import T, eval_type, get_full_typed_signature, is_builtin_type

SyncResource = ty.Union[ty.ContextManager[ty.Any], Closable]
AsyncResource = ty.Union[ty.AsyncContextManager[ty.Any], AsyncClosable]
Expand Down
6 changes: 1 addition & 5 deletions ididi/api.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import typing as ty

import typing_extensions as tye

from .graph import DependencyGraph as DependencyGraph

T = ty.TypeVar("T")
P = tye.ParamSpec("P")
from .utils.typing_utils import P, T


def entry(
Expand Down
9 changes: 4 additions & 5 deletions ididi/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,9 @@
)
from .node import DependentNode, LazyDependent
from .utils.param_utils import MISSING, Maybe, is_provided
from .utils.typing_utils import P, T

T = ty.TypeVar("T")
Stack = ty.TypeVar("Stack", ExitStack, AsyncExitStack)
P = tye.ParamSpec("P")


class AbstractScope(ty.Generic[Stack]):
Expand Down Expand Up @@ -472,7 +471,7 @@ def _resolve_concrete_node(
return node

def replace_node(
self, old_node: DependentNode[ty.Any], new_node: DependentNode[ty.Any]
self, old_node: DependentNode[T], new_node: DependentNode[T]
) -> None:
"""
Replace an existing node with a new node.
Expand Down Expand Up @@ -527,7 +526,7 @@ def static_resolve(
self,
dependent: ty.Union[type, ty.Callable[P, T]],
node_config: Maybe[NodeConfig] = MISSING,
) -> DependentNode[ty.Any]:
) -> DependentNode[T]:
"""
Resolve a dependency without building its instance.
Args:
Expand All @@ -542,7 +541,7 @@ def static_resolve(
def dfs(
dependent_factory: ty.Union[type, ty.Callable[P, T]],
node_config: Maybe[NodeConfig],
) -> DependentNode[ty.Any]:
) -> DependentNode[T]:

if is_function(dependent_factory):
sig = get_typed_signature(dependent_factory)
Expand Down
7 changes: 1 addition & 6 deletions ididi/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
from functools import lru_cache
from inspect import Parameter

import typing_extensions as tye

from ._itypes import (
EMPTY_SIGNATURE,
INSPECT_EMPTY,
Expand All @@ -33,10 +31,7 @@
ProtocolFacotryNotProvidedError,
)
from .utils.param_utils import MISSING, Maybe, is_provided
from .utils.typing_utils import get_factory_sig_from_cls

T = ty.TypeVar("T")
P = tye.ParamSpec("P")
from .utils.typing_utils import P, T, get_factory_sig_from_cls


class Dependent(ty.Generic[T]):
Expand Down
2 changes: 1 addition & 1 deletion ididi/utils/param_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import typing_extensions as tye

T = ty.TypeVar("T")
from .typing_utils import T


class _Missed:
Expand Down
3 changes: 3 additions & 0 deletions ididi/utils/typing_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
import typing as ty
from typing import _eval_type as ty_eval_type # type: ignore


import typing_extensions as tye

T = ty.TypeVar("T")
R = ty.TypeVar("R")
P = tye.ParamSpec("P")

PrimitiveBuiltins = type[ty.Union[int, float, complex, str, bool, bytes, bytearray]]
ContainerBuiltins = type[
Expand Down
3 changes: 1 addition & 2 deletions ididi/visual.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import typing as ty

from .utils.typing_utils import T
from graphviz import Digraph

from .graph import DependencyGraph
from .node import DependentNode as DependentNode

T = ty.TypeVar("T")


class Visualizer:
def __init__(
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ classifiers = [
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",
]

[project.optional-dependencies]
Expand Down

0 comments on commit 8232a9f

Please sign in to comment.