Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Faker class generate method with default locale #790

Closed
Ramon5 opened this issue Oct 7, 2020 · 3 comments
Closed

Faker class generate method with default locale #790

Ramon5 opened this issue Oct 7, 2020 · 3 comments

Comments

@Ramon5
Copy link

Ramon5 commented Oct 7, 2020

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
Copy link
Member

rbarrois commented Oct 7, 2020

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
Copy link

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

rbarrois added a commit that referenced this issue Dec 23, 2020
Unrolling the extra context for a declaration might depend on the
declaration's internals; that's typically the case with factory.Maybe:
the inner declarations might depend on the actual declaration used.

This adds `evaluate_pre` and a `evaluate_post` entrypoints to
declarations, more readable with regard to which build phase they are
used in.

Each of those will perform unrolling before calling the semi-public
actual function entrypoint (self.evaluate() for evaluate_pre,
self.call() for evaluate_post).

As a side effect, this fixes the issues with factory.Faker() when called
inside a factory.Maybe().

Closes #785 #786 #787 #788 #790 #796.
rbarrois added a commit that referenced this issue Dec 23, 2020
Unrolling the extra context for a declaration might depend on the
declaration's internals; that's typically the case with factory.Maybe:
the inner declarations might depend on the actual declaration used.

This adds `evaluate_pre` and a `evaluate_post` entrypoints to
declarations, more readable with regard to which build phase they are
used in.

Each of those will perform unrolling before calling the semi-public
actual function entrypoint (self.evaluate() for evaluate_pre,
self.call() for evaluate_post).

As a side effect, this fixes the issues with factory.Faker() when called
inside a factory.Maybe().

Closes #785 #786 #787 #788 #790 #796.
@rbarrois
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants