Skip to content

Commit

Permalink
Deprecation for handling Depends, now only callable in _inject_depend…
Browse files Browse the repository at this point in the history
…encies
  • Loading branch information
igorbenav committed Sep 15, 2024
1 parent d911dfb commit 6819c5d
Showing 1 changed file with 8 additions and 33 deletions.
41 changes: 8 additions & 33 deletions fastcrud/endpoint/helper.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import inspect
from typing import Optional, Union, Annotated, Sequence, Callable, TypeVar, Any
import warnings

from pydantic import BaseModel, Field
from pydantic.functional_validators import field_validator
Expand Down Expand Up @@ -127,42 +126,18 @@ def _extract_unique_columns(
return unique_columns


def _temporary_dependency_handling(
funcs: Optional[Sequence[Callable]] = None,
) -> Union[Sequence[params.Depends], None]: # pragma: no cover
"""
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(
"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 [
func if isinstance(func, params.Depends) else Depends(func)
for func in funcs
]
return None


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

if funcs is not None:
return [Depends(func) for func in funcs]

return None
if funcs is None:
return None

for func in funcs:
if not callable(func):
raise TypeError(f"All dependencies must be callable. Got {type(func)} instead.")
return [Depends(func) for func in funcs]


def _apply_model_pk(**pkeys: dict[str, type]):
Expand Down

0 comments on commit 6819c5d

Please sign in to comment.