Skip to content

Commit

Permalink
deprecation warning added to depends
Browse files Browse the repository at this point in the history
  • Loading branch information
igorbenav committed May 10, 2024
1 parent 2e3bdd1 commit 989eb6d
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 30 deletions.
8 changes: 2 additions & 6 deletions fastcrud/crud/fast_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,15 +215,11 @@ def _parse_filters(
filters.append(column != value)
elif op == "in":
if not isinstance(value, (tuple, list, set)):
raise ValueError(
"in filter must be tuple, list or set"
)
raise ValueError("in filter must be tuple, list or set")
filters.append(column.in_(value))
elif op == "not_in":
if not isinstance(value, (tuple, list, set)):
raise ValueError(
"in filter must be tuple, list or set"
)
raise ValueError("in filter must be tuple, list or set")
filters.append(column.not_in(value))
else:
column = getattr(model, key, None)
Expand Down
40 changes: 34 additions & 6 deletions fastcrud/endpoint/helper.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from typing import Optional, Union, Annotated, Sequence, Callable
import warnings

from pydantic import BaseModel, Field
from pydantic.functional_validators import field_validator
from fastapi import Depends, params
Expand Down Expand Up @@ -80,13 +82,39 @@ def _extract_unique_columns(
return unique_columns


def _temporary_dependency_handling(
funcs: Optional[Sequence[Callable]] = None,
) -> Union[Sequence[params.Depends], None]:
"""
Checks if any function in the provided sequence is an instance of params.Depends.
Issues a deprecation warning once if such instances are found, and returns the sequence if any params.Depends are found.
Args:
funcs: Optional sequence of callables or params.Depends instances.
"""
if funcs is not None:
if any(isinstance(func, params.Depends) for func in funcs):
warnings.warn(

Check warning on line 97 in fastcrud/endpoint/helper.py

View check run for this annotation

Codecov / codecov/patch

fastcrud/endpoint/helper.py#L97

Added line #L97 was not covered by tests
"Passing a function wrapped in `Depends` directly to dependency handlers is deprecated and will be removed in version 0.15.0.",
DeprecationWarning,
stacklevel=2,
)
return [

Check warning on line 102 in fastcrud/endpoint/helper.py

View check run for this annotation

Codecov / codecov/patch

fastcrud/endpoint/helper.py#L102

Added line #L102 was not covered by tests
func if isinstance(func, params.Depends) else Depends(func)
for func in funcs
]
return None


def _inject_dependencies(
funcs: Optional[Sequence[Callable]] = None,
) -> Sequence[params.Depends]:
) -> Optional[Sequence[params.Depends]]:
"""Wraps a list of functions in FastAPI's Depends."""
dependencies = []
temp_handling = _temporary_dependency_handling(funcs)
if temp_handling is not None:
return temp_handling

Check warning on line 115 in fastcrud/endpoint/helper.py

View check run for this annotation

Codecov / codecov/patch

fastcrud/endpoint/helper.py#L115

Added line #L115 was not covered by tests

if funcs is not None:
for func in funcs:
dependency = Depends(func)
dependencies.append(dependency)
return dependencies
return [Depends(func) for func in funcs]

return None
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "fastcrud"
version = "0.12.0"
version = "0.12.1"
description = "FastCRUD is a Python package for FastAPI, offering robust async CRUD operations and flexible endpoint creation utilities."
authors = ["Igor Benav <igor.magalhaes.r@gmail.com>"]
license = "MIT"
Expand Down
22 changes: 8 additions & 14 deletions tests/sqlalchemy/core/test_parse_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,33 +26,27 @@ async def test_parse_filters_contained_in(test_model):
fast_crud = FastCRUD(test_model)
filters = fast_crud._parse_filters(category_id__in=[1, 2])
assert len(filters) == 1
assert str(
filters[0]
) == "test.category_id IN (__[POSTCOMPILE_category_id_1])"
assert str(filters[0]) == "test.category_id IN (__[POSTCOMPILE_category_id_1])"


@pytest.mark.asyncio
async def test_parse_filters_not_contained_in(test_model):
fast_crud = FastCRUD(test_model)
filters = fast_crud._parse_filters(category_id__not_in=[1, 2])
assert len(filters) == 1
assert str(
filters[0]
) == "(test.category_id NOT IN (__[POSTCOMPILE_category_id_1]))"
assert (
str(filters[0]) == "(test.category_id NOT IN (__[POSTCOMPILE_category_id_1]))"
)


@pytest.mark.asyncio
@pytest.mark.parametrize(
'type', ('in', 'not_in')
)
async def test_parse_filters_contained_in_raises_exception(
test_model, type: str
):
@pytest.mark.parametrize("type", ("in", "not_in"))
async def test_parse_filters_contained_in_raises_exception(test_model, type: str):
fast_crud = FastCRUD(test_model)
with pytest.raises(ValueError) as exc:
if type == 'in':
if type == "in":
fast_crud._parse_filters(category_id__in=1)
elif type == 'not_in':
elif type == "not_in":
fast_crud._parse_filters(category_id__not_in=1)
assert str(exc.value) == "in filter must be tuple, list or set"

Expand Down
4 changes: 1 addition & 3 deletions tests/sqlmodel/core/test_parse_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ async def test_parse_filters_contained_in(test_model):
fast_crud = FastCRUD(test_model)
filters = fast_crud._parse_filters(category_id__in=[1, 2])
assert len(filters) == 1
assert str(
filters[0]
) == "test.category_id IN (__[POSTCOMPILE_category_id_1])"
assert str(filters[0]) == "test.category_id IN (__[POSTCOMPILE_category_id_1])"


@pytest.mark.asyncio
Expand Down

0 comments on commit 989eb6d

Please sign in to comment.