Skip to content

Commit

Permalink
fix (#112): correct CustomFields mapping to positional args
Browse files Browse the repository at this point in the history
  • Loading branch information
Lancetnik committed Jul 12, 2024
1 parent c190b28 commit bc98bdc
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
2 changes: 1 addition & 1 deletion fast_depends/__about__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""FastDepends - extracted and cleared from HTTP domain FastAPI Dependency Injection System"""

__version__ = "2.4.5"
__version__ = "2.4.6"
13 changes: 8 additions & 5 deletions fast_depends/core/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,18 +254,21 @@ def _solve(
else:
break

keyword_args: Iterable[str]
if has_args := "args" in self.alias_arguments:
kw["args"] = args
keyword_args = self.keyword_args

else:
keyword_args = set(self.keyword_args + self.positional_args)
for arg in keyword_args - set(self.dependencies.keys()):
if args:
kw[arg], args = args[0], args[1:]
else:
keyword_args = self.keyword_args + self.positional_args

for arg in keyword_args:
if not args:
break

if arg not in self.dependencies:
kw[arg], args = args[0], args[1:]

solved_kw: Dict[str, Any]
solved_kw = yield args, kw, call

Expand Down
17 changes: 17 additions & 0 deletions tests/library/test_custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,20 @@ def sync_catch2(key2: HeaderKey) -> float:

assert sync_catch(headers={"key": 1}) == 1
assert sync_catch2(headers={"key2": 1}) == 1


def test_arguments_mapping():
@inject
def func(
d: int = CustomField(cast=False),
b: int = CustomField(cast=False),
c: int = CustomField(cast=False),
a: int = CustomField(cast=False),
):
assert d == 4
assert b == 2
assert c == 3
assert a == 1

for _ in range(50):
func(4, 2, 3, 1)

0 comments on commit bc98bdc

Please sign in to comment.