From d32510861de95e4f05fed8d07601d04879a48663 Mon Sep 17 00:00:00 2001 From: Pastukhov Nikita Date: Thu, 6 Jun 2024 20:33:39 +0300 Subject: [PATCH] chore: bump dependencies (#100) --- docs/docs/tutorial/annotated.md | 2 +- fast_depends/core/build.py | 3 +- fast_depends/core/model.py | 4 +- fast_depends/use.py | 3 +- fast_depends/utils.py | 2 +- pyproject.toml | 1 + requirements.dev.txt | 4 +- scripts/lint.sh | 2 +- tests/async/test_config.py | 3 +- tests/library/test_custom.py | 15 +++++ tests/sync/test_config.py | 3 +- tests/test_params.py | 12 ++-- tests/test_schema.py | 102 +++++++++++++++----------------- 13 files changed, 82 insertions(+), 74 deletions(-) diff --git a/docs/docs/tutorial/annotated.md b/docs/docs/tutorial/annotated.md index 2e12a00..64207ae 100644 --- a/docs/docs/tutorial/annotated.md +++ b/docs/docs/tutorial/annotated.md @@ -134,7 +134,7 @@ func(user_id=1) # correct calling !!! tip "" I really recommend *do not use* `Annotated` as a positional argument - The best way to avoid all missunderstanding between you and Python - use `pydantic.Field` with `Annotated` everywhere + The best way to avoid all misunderstanding between you and Python - use `pydantic.Field` with `Annotated` everywhere Like in the following example diff --git a/fast_depends/core/build.py b/fast_depends/core/build.py index 0f86602..6cd5e37 100644 --- a/fast_depends/core/build.py +++ b/fast_depends/core/build.py @@ -1,4 +1,5 @@ import inspect +from copy import deepcopy from typing import ( Any, Awaitable, @@ -97,7 +98,7 @@ def build_call_model( if isinstance(next_custom, Depends): dep = next_custom elif isinstance(next_custom, CustomField): - custom = next_custom + custom = deepcopy(next_custom) else: # pragma: no cover raise AssertionError("unreachable") diff --git a/fast_depends/core/model.py b/fast_depends/core/model.py index 5ae4e65..3403351 100644 --- a/fast_depends/core/model.py +++ b/fast_depends/core/model.py @@ -115,7 +115,7 @@ def flat_dependencies( flat: Dict[ Callable[..., Any], Tuple[ - "CallModel[..., Any]", + CallModel[..., Any], Tuple[Callable[..., Any], ...], ], ] = {} @@ -179,7 +179,7 @@ def __init__( self.extra_dependencies = extra_dependencies or () self.custom_fields = custom_fields or {} - sorted_dep: List["CallModel[..., Any]"] = [] + sorted_dep: List[CallModel[..., Any]] = [] flat = self.flat_dependencies for calls in flat.values(): _sort_dep(sorted_dep, calls, flat) diff --git a/fast_depends/use.py b/fast_depends/use.py index 52e71c3..972a9c6 100644 --- a/fast_depends/use.py +++ b/fast_depends/use.py @@ -6,6 +6,7 @@ Callable, Iterator, Optional, + Protocol, Sequence, TypeVar, Union, @@ -13,7 +14,7 @@ overload, ) -from typing_extensions import ParamSpec, Protocol +from typing_extensions import ParamSpec from fast_depends._compat import ConfigDict from fast_depends.core import CallModel, build_call_model diff --git a/fast_depends/utils.py b/fast_depends/utils.py index 1c914d7..d734125 100644 --- a/fast_depends/utils.py +++ b/fast_depends/utils.py @@ -108,7 +108,7 @@ def get_typed_signature(call: Callable[..., Any]) -> Tuple[inspect.Signature, An def collect_outer_stack_locals() -> Dict[str, Any]: frame = inspect.currentframe() - frames: List["FrameType"] = [] + frames: List[FrameType] = [] while frame is not None: if "fast_depends" not in frame.f_code.co_filename: frames.append(frame) diff --git a/pyproject.toml b/pyproject.toml index 798f4af..84629ef 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -43,6 +43,7 @@ dynamic = ["version"] dependencies = [ "pydantic>=1.7.4,!=1.8,!=1.8.1,<3.0.0", "anyio>=3.0.0,<5.0.0", + "typing-extensions<4.12.1; python_version < '3.9'", ] [project.urls] diff --git a/requirements.dev.txt b/requirements.dev.txt index 969771c..8a6f8d7 100644 --- a/requirements.dev.txt +++ b/requirements.dev.txt @@ -4,5 +4,5 @@ -r requirements.test.txt mypy >=1.8.0 -ruff ==0.4.4 -codespell ==2.2.6 \ No newline at end of file +ruff ==0.4.8 +codespell ==2.3.0 \ No newline at end of file diff --git a/scripts/lint.sh b/scripts/lint.sh index 5700638..46ef1eb 100644 --- a/scripts/lint.sh +++ b/scripts/lint.sh @@ -2,7 +2,7 @@ echo "Running mypy..." mypy fast_depends echo "Running ruff linter (isort, flake, pyupgrade, etc. replacement)..." -ruff fast_depends tests --fix +ruff check fast_depends tests --fix echo "Running ruff formatter (black replacement)..." ruff format tests diff --git a/tests/async/test_config.py b/tests/async/test_config.py index 25bfd4b..f8f6a5c 100644 --- a/tests/async/test_config.py +++ b/tests/async/test_config.py @@ -10,8 +10,7 @@ async def dep(a: str): @inject(pydantic_config={"str_max_length" if PYDANTIC_V2 else "max_anystr_length": 1}) -async def limited_str(a=Depends(dep)): - ... +async def limited_str(a=Depends(dep)): ... @inject() diff --git a/tests/library/test_custom.py b/tests/library/test_custom.py index 79096b8..4044df9 100644 --- a/tests/library/test_custom.py +++ b/tests/library/test_custom.py @@ -168,3 +168,18 @@ def sync_catch(key: logging.Logger = Header(cast=False)): # noqa: B008 return key assert sync_catch(headers={"key": 1}) == 1 + + +def test_reusable_annotated() -> None: + HeaderKey = Annotated[float, Header(cast=False)] + + @inject + def sync_catch(key: HeaderKey) -> float: + return key + + @inject + def sync_catch2(key2: HeaderKey) -> float: + return key2 + + assert sync_catch(headers={"key": 1}) == 1 + assert sync_catch2(headers={"key2": 1}) == 1 diff --git a/tests/sync/test_config.py b/tests/sync/test_config.py index eba08a3..c47c1c9 100644 --- a/tests/sync/test_config.py +++ b/tests/sync/test_config.py @@ -10,8 +10,7 @@ def dep(a: str): @inject(pydantic_config={"str_max_length" if PYDANTIC_V2 else "max_anystr_length": 1}) -def limited_str(a=Depends(dep)): - ... +def limited_str(a=Depends(dep)): ... @inject() diff --git a/tests/test_params.py b/tests/test_params.py index 9e428c3..cf87943 100644 --- a/tests/test_params.py +++ b/tests/test_params.py @@ -4,20 +4,16 @@ def test_params(): - def func1(m): - ... + def func1(m): ... def func2(c, b=Depends(func1), d=CustomField()): # noqa: B008 ... - def func3(b): - ... + def func3(b): ... - def main(a, b, m=Depends(func2), k=Depends(func3)): - ... + def main(a, b, m=Depends(func2), k=Depends(func3)): ... - def extra_func(n): - ... + def extra_func(n): ... model = build_call_model(main, extra_dependencies=(Depends(extra_func),)) diff --git a/tests/test_schema.py b/tests/test_schema.py index ff685ee..0b64651 100644 --- a/tests/test_schema.py +++ b/tests/test_schema.py @@ -11,8 +11,8 @@ REF_KEY = "$defs" if PYDANTIC_V2 else "definitions" -def test_base(): - def handler(): +def test_base() -> None: + def handler() -> None: pass schema = get_schema(build_call_model(handler)) @@ -20,8 +20,8 @@ def handler(): class TestNoType: - def test_no_type(self): - def handler(a): + def test_no_type(self) -> None: + def handler(a) -> None: pass schema = get_schema(build_call_model(handler)) @@ -32,15 +32,15 @@ def handler(a): "type": "object", }, schema - def test_no_type_embeded(self): - def handler(a): + def test_no_type_embeded(self) -> None: + def handler(a) -> None: pass schema = get_schema(build_call_model(handler), embed=True) assert schema == {"title": "A"}, schema - def test_no_type_with_default(self): - def handler(a=None): + def test_no_type_with_default(self) -> None: + def handler(a=None) -> None: pass schema = get_schema(build_call_model(handler)) @@ -50,8 +50,8 @@ def handler(a=None): "type": "object", }, schema - def test_no_type_with_default_and_embed(self): - def handler(a=None): + def test_no_type_with_default_and_embed(self) -> None: + def handler(a=None) -> None: pass schema = get_schema(build_call_model(handler), embed=True) @@ -59,8 +59,8 @@ def handler(a=None): class TestOneArg: - def test_one_arg(self): - def handler(a: int): + def test_one_arg(self) -> None: + def handler(a: int) -> None: pass schema = get_schema(build_call_model(handler)) @@ -71,15 +71,15 @@ def handler(a: int): "type": "object", }, schema - def test_one_arg_with_embed(self): - def handler(a: int): + def test_one_arg_with_embed(self) -> None: + def handler(a: int) -> None: pass schema = get_schema(build_call_model(handler), embed=True) assert schema == {"title": "A", "type": "integer"}, schema - def test_one_arg_with_optional(self): - def handler(a: Optional[int]): + def test_one_arg_with_optional(self) -> None: + def handler(a: Optional[int]) -> None: pass schema = get_schema(build_call_model(handler)) @@ -95,8 +95,8 @@ def handler(a: Optional[int]): "type": "object", }, schema - def test_one_arg_with_default(self): - def handler(a: int = 0): + def test_one_arg_with_default(self) -> None: + def handler(a: int = 0) -> None: pass schema = get_schema(build_call_model(handler)) @@ -106,8 +106,8 @@ def handler(a: int = 0): "type": "object", }, schema - def test_one_arg_with_default_and_embed(self): - def handler(a: int = 0): + def test_one_arg_with_default_and_embed(self) -> None: + def handler(a: int = 0) -> None: pass schema = get_schema(build_call_model(handler), embed=True) @@ -115,11 +115,11 @@ def handler(a: int = 0): class TestOneArgWithModel: - def test_base(self): + def test_base(self) -> None: class Model(BaseModel): a: int - def handler(a: Model): + def handler(a: Model) -> None: pass schema = get_schema(build_call_model(handler)) @@ -138,11 +138,11 @@ def handler(a: Model): "type": "object", }, schema - def test_resolved_model(self): + def test_resolved_model(self) -> None: class Model(BaseModel): a: int - def handler(a: Model): + def handler(a: Model) -> None: pass schema = get_schema(build_call_model(handler), resolve_refs=True) @@ -160,11 +160,11 @@ def handler(a: Model): "type": "object", }, schema - def test_optional_model(self): + def test_optional_model(self) -> None: class Model(BaseModel): a: int - def handler(a: Optional[Model] = None): + def handler(a: Optional[Model] = None) -> None: pass schema = get_schema(build_call_model(handler), resolve_refs=True) @@ -197,11 +197,11 @@ def handler(a: Optional[Model] = None): "type": "object", }, schema - def test_optional_embeded_model(self): + def test_optional_embeded_model(self) -> None: class Model(BaseModel): a: int - def handler(a: Optional[Model]): + def handler(a: Optional[Model]) -> None: pass schema = get_schema(build_call_model(handler), resolve_refs=True, embed=True) @@ -226,14 +226,14 @@ def handler(a: Optional[Model]): } ), schema - def test_nested_resolved_model(self): + def test_nested_resolved_model(self) -> None: class Model2(BaseModel): a: int class Model(BaseModel): a: Model2 - def handler(a: Model): + def handler(a: Model) -> None: pass schema = get_schema(build_call_model(handler), resolve_refs=True) @@ -258,11 +258,11 @@ def handler(a: Model): "type": "object", }, schema - def test_embeded_model(self): + def test_embeded_model(self) -> None: class Model(BaseModel): a: int - def handler(a: Model): + def handler(a: Model) -> None: pass schema = get_schema(build_call_model(handler), resolve_refs=True, embed=True) @@ -273,14 +273,14 @@ def handler(a: Model): "type": "object", }, schema - def test_embeded_resolved_model(self): + def test_embeded_resolved_model(self) -> None: class Model2(BaseModel): a: int class Model(BaseModel): a: Model2 - def handler(a: Model): + def handler(a: Model) -> None: pass schema = get_schema(build_call_model(handler), resolve_refs=True, embed=True) @@ -300,8 +300,8 @@ def handler(a: Model): class TestMultiArgs: - def test_base(self): - def handler(a, b): + def test_base(self) -> None: + def handler(a, b) -> None: pass schema = get_schema(build_call_model(handler)) @@ -312,8 +312,8 @@ def handler(a, b): "type": "object", }, schema - def test_types_and_default(self): - def handler(a: str, b: int = 0): + def test_types_and_default(self) -> None: + def handler(a: str, b: int = 0) -> None: pass schema = get_schema(build_call_model(handler)) @@ -327,8 +327,8 @@ def handler(a: str, b: int = 0): "type": "object", }, schema - def test_ignores_embed(self): - def handler(a: str, b: int = 0): + def test_ignores_embed(self) -> None: + def handler(a: str, b: int = 0) -> None: pass schema = get_schema(build_call_model(handler), embed=True) @@ -342,11 +342,11 @@ def handler(a: str, b: int = 0): "type": "object", }, schema - def test_model(self): + def test_model(self) -> None: class Model(BaseModel): a: int = Field(0, description="description") - def handler(a: str, b: Model): + def handler(a: str, b: Model) -> None: pass schema = get_schema(build_call_model(handler)) @@ -374,11 +374,11 @@ def handler(a: str, b: Model): "type": "object", }, schema - def test_resolved_model(self): + def test_resolved_model(self) -> None: class Model(BaseModel): a: int = Field(0, description="description") - def handler(a: str, b: Model): + def handler(a: str, b: Model) -> None: pass schema = get_schema(build_call_model(handler), resolve_refs=True) @@ -404,27 +404,23 @@ def handler(a: str, b: Model): }, schema -def test_depends(): +def test_depends() -> None: class Model(BaseModel): a: int = Field(0, description="description") class CustomClass: pass - def dep4(d: Model): - ... + def dep4(d: Model) -> None: ... - def dep2(c: float = Field(..., title="best")): - ... + def dep2(c: float = Field(..., title="best")) -> None: ... - def dep(a: float = 1, d: CustomClass = Depends(dep2)): - ... + def dep(a: float = 1, d: CustomClass = Depends(dep2)) -> None: ... def handler( b: str = Field(""), a=Depends(dep), - ): - ... + ) -> None: ... schema = get_schema( build_call_model(handler, extra_dependencies=(Depends(dep4),)),