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

feat: add recursion guard #468

Merged
merged 13 commits into from
Jan 16, 2024
Prev Previous commit
Next Next commit
fix: avoid 3.10+ set syntax
  • Loading branch information
adhtruong committed Jan 2, 2024
commit 1ff9dc921c4200aea09b087acc73e7ba061f8b63
11 changes: 7 additions & 4 deletions polyfactory/factories/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import copy
from abc import ABC, abstractmethod
from collections import Counter, abc, deque
from contextlib import suppress
@@ -98,8 +99,10 @@ class BuildContext(TypedDict):


def _get_build_context(build_context: BuildContext | None) -> BuildContext:
build_context = build_context or {"seen_models": set()}
return build_context.copy()
if build_context is None:
return {"seen_models": set()}

return copy.deepcopy(build_context)


class BaseFactory(ABC, Generic[T]):
@@ -857,7 +860,7 @@ def process_kwargs(cls, build_context: BuildContext | None = None, **kwargs: Any

"""
build_context = _get_build_context(build_context)
build_context["seen_models"] = build_context["seen_models"] | {cls.__model__}
build_context["seen_models"].add(cls.__model__)

result: dict[str, Any] = {**kwargs}
generate_post: dict[str, PostGenerated] = {}
@@ -910,7 +913,7 @@ def process_kwargs_coverage(

"""
build_context = _get_build_context(build_context)
build_context["seen_models"] = build_context["seen_models"] | {cls.__model__}
build_context["seen_models"].add(cls.__model__)

result: dict[str, Any] = {**kwargs}
generate_post: dict[str, PostGenerated] = {}