UserWarning: UserWarning: Using a <class 'list'> as model type for <ListFactory for <class 'list'>> is discouraged
See original GitHub issueThe 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:
- Created a year ago
- Comments:5 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
@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.