diff --git a/faker/factory.py b/faker/factory.py index a1e9c06505..074f1a05e3 100644 --- a/faker/factory.py +++ b/faker/factory.py @@ -1,3 +1,4 @@ +import functools import locale as pylocale import logging import sys @@ -64,6 +65,7 @@ def create( return faker @classmethod + @functools.cache def _find_provider_class( cls, provider_path: str, diff --git a/tests/test_factory.py b/tests/test_factory.py index 48c9015ed4..4c226f41a4 100644 --- a/tests/test_factory.py +++ b/tests/test_factory.py @@ -151,6 +151,10 @@ class Provider: def __init__(self, *args, **kwargs): pass + # There's a cache based on the provider name, so when the provider changes behaviour we need + # a new name: + provider_path = f"test_lang_localized_provider_{with_default}" + with patch.multiple( "faker.factory", import_module=MagicMock(return_value=DummyProviderModule()), @@ -167,8 +171,8 @@ def __init__(self, *args, **kwargs): ("ar_EG", with_default), # True if module defines a default locale ] for locale, expected_used in test_cases: - factory = Factory.create(providers=["dummy"], locale=locale) - assert factory.providers[0].__provider__ == "dummy" + factory = Factory.create(providers=[provider_path], locale=locale) + assert factory.providers[0].__provider__ == provider_path from faker.config import DEFAULT_LOCALE print(f"requested locale = {locale} , DEFAULT LOCALE {DEFAULT_LOCALE}")