FactoryBoy / factory_boy

A test fixtures replacement for Python
https://factoryboy.readthedocs.io/
MIT License
3.49k stars 392 forks source link

Faker class generate method with default locale #790

Closed Ramon5 closed 3 years ago

Ramon5 commented 3 years ago

The problem

I have a old version in my project, then using Faker class, i generate faker name with Faker("name") but in newer versions i have using Faker("name").generate(), thats problem is, when i using the generate method without params, the 'locale' key error is getted

Proposed solution

If the developer does not provide a dictionary with a locale, a default locale is provided.

import locale as local_language

class Faker(declarations.ParameteredDeclaration):

        ...

        def generate(self, params: dict = None):
             locale = params.pop('locale', None) or {'locale': local_language.getlocale()[0]}
             subfaker = self._get_faker(locale)
             return subfaker.format(self.provider, **params)

Extra notes

factory_boy version 3.1.0

rbarrois commented 3 years ago

Hi! Thanks for the report.

There is indeed something wrong with the latest release, but I haven't found the time to look into it — see #788 #787 #786 #785…

However, please note that the factory.Faker(...).generate() method is private.

As described here, the params dict should have been properly prepared before reaching the .generate() function; but that fails in specific cases — with factory.Maybe and class Params.

tomasgarzon commented 3 years ago

Any update here? when do you plan to move this to pypi package? (kwargs.pop('locale') keyerror)

rbarrois commented 3 years ago

This has been fixed in #828; a new release should be made in the next couple of days :)