Skip to content

Commit

Permalink
fix: make sure anyio is optional (#278)
Browse files Browse the repository at this point in the history
* fix: make sure `anyio` is optional

* fix: missing `UNSET` when not using `msgspec`
  • Loading branch information
cofin authored Nov 2, 2024
1 parent bc4d4e6 commit eb0196b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
3 changes: 2 additions & 1 deletion advanced_alchemy/service/_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
from advanced_alchemy.repository.typing import ModelT, OrderingPair
from advanced_alchemy.service._util import ResultConverter
from advanced_alchemy.service.typing import (
UNSET, # pyright: ignore[reportAttributeAccessIssue]
BulkModelDictT,
ModelDictListT,
ModelDictT,
Expand Down Expand Up @@ -330,6 +329,8 @@ async def to_model(
)

if is_msgspec_model(data):
from msgspec import UNSET

return model_from_dict(
model=self.repository.model_type,
**{f: val for f in data.__struct_fields__ if (val := getattr(data, f, None)) != UNSET},
Expand Down
3 changes: 2 additions & 1 deletion advanced_alchemy/service/_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
from advanced_alchemy.repository.typing import ModelT, OrderingPair
from advanced_alchemy.service._util import ResultConverter
from advanced_alchemy.service.typing import (
UNSET, # pyright: ignore[reportAttributeAccessIssue]
BulkModelDictT,
ModelDictListT,
ModelDictT,
Expand Down Expand Up @@ -331,6 +330,8 @@ def to_model(
)

if is_msgspec_model(data):
from msgspec import UNSET

return model_from_dict(
model=self.repository.model_type,
**{f: val for f in data.__struct_fields__ if (val := getattr(data, f, None)) != UNSET},
Expand Down
19 changes: 15 additions & 4 deletions advanced_alchemy/utils/fixtures.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
from __future__ import annotations

from pathlib import Path
from typing import Any

from anyio import Path as AsyncPath
from typing import TYPE_CHECKING, Any

from advanced_alchemy._serialization import decode_json
from advanced_alchemy.exceptions import MissingDependencyError

if TYPE_CHECKING:
from pathlib import Path

from anyio import Path as AsyncPath

__all__ = ("open_fixture", "open_fixture_async")

Expand All @@ -23,6 +26,8 @@ def open_fixture(fixtures_path: Path | AsyncPath, fixture_name: str) -> Any:
Returns:
Any: The parsed JSON data
"""
from pathlib import Path

fixture = Path(fixtures_path / f"{fixture_name}.json")
if fixture.exists():
with fixture.open(mode="r", encoding="utf-8") as f:
Expand All @@ -45,6 +50,12 @@ async def open_fixture_async(fixtures_path: Path | AsyncPath, fixture_name: str)
Returns:
Any: The parsed JSON data
"""
try:
from anyio import Path as AsyncPath
except ImportError as exc:
msg = "The `anyio` library is required to use this function. Please install it with `pip install anyio`."
raise MissingDependencyError(msg) from exc

fixture = AsyncPath(fixtures_path / f"{fixture_name}.json")
if await fixture.exists():
async with await fixture.open(mode="r", encoding="utf-8") as f:
Expand Down

0 comments on commit eb0196b

Please sign in to comment.