Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix (#112): correct CustomFields mapping to positional args #113

Merged
merged 1 commit into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)