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: set as default type for factory only if model is defined #479

Merged
merged 3 commits into from
Jan 16, 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 polyfactory/factories/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ def __init_subclass__(cls, *args: Any, **kwargs: Any) -> None: # noqa: C901
if random_seed is not None:
cls.seed_random(random_seed)

if cls.__set_as_default_factory_for_type__:
if cls.__set_as_default_factory_for_type__ and hasattr(cls, "__model__"):
BaseFactory._factory_type_mapping[cls.__model__] = cls

@classmethod
Expand Down
20 changes: 20 additions & 0 deletions tests/test_factory_configuration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from typing import Any, Type

from typing_extensions import TypeGuard

from polyfactory.factories.base import BaseFactory, T


def test_setting_set_as_default_factory_for_type_on_base_factory() -> None:
"""Setting the value to `True` shouldn't raise exception when initializing."""

class CustomBaseFactory(BaseFactory[T]):
__is_base_factory__ = True
__set_as_default_factory_for_type__ = True

@classmethod
def is_supported_type(cls, value: Any) -> TypeGuard[Type[T]]:
# Set this as false since this factory will be injected into the
# list of base factories, but this obviously shouldn't be ran
# for any of the types.
return False
Loading