Factory-Boy - KeyError: 'locale'

991 Views Asked by At

Got following exception from factory-boy during running my pytests:

    def generate(self, params):
>       locale = params.pop('locale')
E       KeyError: 'locale'

How to solve that issue? Everything worked few days ago, my code wasn't changed.

Here is the code example of what :

import factory
from django.db import models

class MyModel(models.Model):
    name = models.CharField(max_length=100)
    price = models.PositiveIntegerField()
    is_cheat = models.BooleanField()

class MyModelFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = MyModel
    name = factory.Faker("name")
    is_cheap = factory.Faker("boolean")
    price = factory.Maybe("is_cheap", yes_declaration=factoryFaker("pyint", max_value=10), no_declaration=factory.Faker("pyint", min_value=100))
1

There are 1 best solutions below

1
On

In faker.py I see that there is no default value in .pop():

def generate(self, params):
    locale = params.pop('locale')  # <-- Issue occurs here
    subfaker = self._get_faker(locale)
    return subfaker.format(self.provider, **params)

But not sure if it should be there. Here is the comment of author of factory-boy with explanation of the situation.

Looks like there is a bug in 3.1.0, downgrading to 3.0.1 should help. At least temporarily, while they haven't fixed that.