pytest-factoryboy icon indicating copy to clipboard operation
pytest-factoryboy copied to clipboard

UserWarning: UserWarning: Using a <class 'list'> as model type for <ListFactory for <class 'list'>> is discouraged

Open nblock opened this issue 2 years ago • 3 comments

The following simplified example, adjusted from the factoryboy documentation:

import factory
from pytest_factoryboy import register


class User:
    def __init__(self, flags):
        self.flags = flags


@register
class UserFactory(factory.Factory):
    class Meta:
        model = User

    flags = factory.List(["a", "b", "c"])


def test_example(user_factory):
    user = user_factory.build()
    assert user.flags == ["a", "b", "c"]

produces the warning:

UserWarning: Using a <class 'list'> as model type for <ListFactory for <class 'list'>> is discouraged by pytest-factoryboy, as it assumes that the model name is 'list' when using it as SubFactory or RelatedFactory, which is too generic and probably not what you want.
  You can giving an explicit name to the model by using:
  model = named_model(list, "Foo")
    warnings.warn(

and the only way to avoid the warning I could find is this adjusted version:

import factory
from pytest_factoryboy import register, named_model


class User:
    def __init__(self, flags):
        self.flags = flags


class NamedList(factory.ListFactory):
    class Meta:
        model = named_model(list, "NamedList")


@register
class UserFactory(factory.Factory):
    class Meta:
        model = User

    flags = factory.List(["a", "b", "c"], list_factory=NamedList)


def test_example(user_factory):
    user = user_factory.build()
    assert user.flags == ["a", "b", "c"]

Do I miss something obvious or is the list_factory + NamedList boilerplate required to avoid the warning?

pytest-factoryboy 2.5.0

nblock avatar Aug 31 '22 16:08 nblock

hi @nblock

yes, I am afraid using the custom ListFactory subclass is the only way because it's impossible to override the original ListFactory meta's model on runtime. But if you have any idea on how to do that in an elegant and developer-friendly way, please share, and we can together encourage maintainers to add it to the pytest-factoryboy ;)

skarzi avatar Sep 03 '22 12:09 skarzi

I think this is a bug in pytest-factoryboy generation of related fixtures for factory.List.

I'll check this in the next few weeks.

youtux avatar Sep 03 '22 15:09 youtux

Any news and ideas how dismiss this warning with the RelatedFactoryList?

class CategoryFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = Category
        
class ProductFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = Product
      
    category = factory.RelatedFactoryList(CategoryFactory)

I've tried different approaches, but without success

helioascorreia avatar Oct 13 '22 13:10 helioascorreia

@helioascorreia, I can't reproduce the issue with your example. Can you provide a full minimal example that I can just run to reproduce the issue?

youtux avatar Oct 16 '22 12:10 youtux

@nblock I spent some time investigating this, and I agree with @skarzi; I can't think of an elegant way of fixing this, and your solution seems indeed correct and clear.

youtux avatar Oct 16 '22 13:10 youtux