question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
youtuxcommented, Sep 3, 2022

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.

0reactions
youtuxcommented, Oct 16, 2022

@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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using factory.List resolves in unusable fixtures #67 - GitHub
Setup Here are my models: from typing import List from mypy_extensions import TypedDict class WakatimeCommit(TypedDict): """Class to ...
Read more >
factory-boy create a list of SubFactory for a Factory
So that each time I am calling UserFactory() I am saving and getting unique user named object. In factory-boy I can use factory.SubFactory( ......
Read more >
Why is base-for-all-objects discouraged in C++
Because what would that object have for functionality? In java all the Base class has is a toString, a hashCode & equality and...
Read more >
pytest-factoryboy - PyPI
Model fixture implements an instance of a model created by the factory. Name convention is model's lowercase-underscore class name. import factory from ...
Read more >
SQL statements containing $1 cause errors - Gajus/Slonik
UserWarning : UserWarning: Using a <class 'list'> as model type for <ListFactory for <class 'list'>> is discouraged, 5, 2022-08-31, 2022-10-10.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found