diff --git a/faker/providers/python/__init__.py b/faker/providers/python/__init__.py index 8f9ce43a81..669efdad24 100644 --- a/faker/providers/python/__init__.py +++ b/faker/providers/python/__init__.py @@ -1,4 +1,3 @@ -import logging import math import string import sys @@ -17,8 +16,6 @@ TypesSpec = Union[List[Type], Tuple[Type, ...]] TEnum = TypeVar("TEnum", bound=Enum) -logger = logging.getLogger(__name__) - class EmptyEnumException(BaseFakerException): pass @@ -476,8 +473,10 @@ def pydict( nb_elements = self.randomize_nb_elements(nb_elements, min=1) if nb_elements > words_list_count: - logger.warning( - f"Number of nb_elements is greater than the number of words in the list. {words_list_count} words will be used." + warnings.warn( + f"Number of nb_elements is greater than the number of words in the list." + f" {words_list_count} words will be used.", + RuntimeWarning, ) nb_elements = words_list_count diff --git a/tests/providers/test_python.py b/tests/providers/test_python.py index 477af304bb..4b89ebc377 100644 --- a/tests/providers/test_python.py +++ b/tests/providers/test_python.py @@ -1,10 +1,9 @@ import decimal -import logging import sys import unittest import warnings -from typing import Iterable, Optional, Union +from typing import Iterable, Optional, Type, Union from unittest.mock import patch import pytest @@ -14,8 +13,20 @@ @pytest.mark.parametrize("object_type", (None, bool, str, float, int, tuple, set, list, Iterable, dict)) def test_pyobject( - object_type: Optional[Union[bool, str, float, int, tuple, set, list, Iterable, dict]], -): + object_type: Optional[ + Union[ + Type[bool], + Type[str], + Type[float], + Type[int], + Type[tuple], + Type[set], + Type[list], + Type[Iterable], + Type[dict], + ] + ], +) -> None: random_object = Faker().pyobject(object_type=object_type) if object_type is None: assert random_object is None @@ -319,15 +330,12 @@ def test_pydict_with_invalid_number_of_nb_elements(self): nb_elements = 10000 words_list_count = len(self.fake.get_words_list()) - - logger = logging.getLogger("faker.providers.python") - - with patch.object(logger, "warning") as mock_warn: + warning_msg = ( + f"Number of nb_elements is greater than the number of words in the list." + f" {words_list_count} words will be used." + ) + with pytest.warns(RuntimeWarning, match=warning_msg): result = self.fake.pydict(nb_elements=nb_elements) - - mock_warn.assert_called_once_with( - f"Number of nb_elements is greater than the number of words in the list. {words_list_count} words will be used." - ) self.assertEqual(len(result), words_list_count)