diff --git a/src/transformers/testing_utils.py b/src/transformers/testing_utils.py index 3188160ddd..80fc45f818 100644 --- a/src/transformers/testing_utils.py +++ b/src/transformers/testing_utils.py @@ -34,6 +34,7 @@ from typing import Iterator, List, Optional, Union from unittest import mock import huggingface_hub +import requests from transformers import logging as transformers_logging @@ -1688,12 +1689,16 @@ class RequestCounter: self.head_request_count = 0 self.get_request_count = 0 self.other_request_count = 0 - self.old_request = huggingface_hub.file_download.requests.request - huggingface_hub.file_download.requests.request = self.new_request + + # Mock `get_session` to count HTTP calls. + self.old_get_session = huggingface_hub.utils._http.get_session + self.session = requests.Session() + self.session.request = self.new_request + huggingface_hub.utils._http.get_session = lambda: self.session return self def __exit__(self, *args, **kwargs): - huggingface_hub.file_download.requests.request = self.old_request + huggingface_hub.utils._http.get_session = self.old_get_session def new_request(self, method, **kwargs): if method == "GET": @@ -1703,7 +1708,7 @@ class RequestCounter: else: self.other_request_count += 1 - return self.old_request(method=method, **kwargs) + return requests.request(method=method, **kwargs) def is_flaky(max_attempts: int = 5, wait_before_retry: Optional[float] = None, description: Optional[str] = None): diff --git a/tests/test_configuration_common.py b/tests/test_configuration_common.py index d2172491f9..1d249c3b52 100644 --- a/tests/test_configuration_common.py +++ b/tests/test_configuration_common.py @@ -357,7 +357,7 @@ class ConfigTestUtils(unittest.TestCase): _ = BertConfig.from_pretrained("hf-internal-testing/tiny-random-bert") # Under the mock environment we get a 500 error when trying to reach the model. - with mock.patch("requests.request", return_value=response_mock) as mock_head: + with mock.patch("requests.Session.request", return_value=response_mock) as mock_head: _ = BertConfig.from_pretrained("hf-internal-testing/tiny-random-bert") # This check we did call the fake head request mock_head.assert_called() diff --git a/tests/test_feature_extraction_common.py b/tests/test_feature_extraction_common.py index f26709e2d9..6ffec66913 100644 --- a/tests/test_feature_extraction_common.py +++ b/tests/test_feature_extraction_common.py @@ -83,7 +83,7 @@ class FeatureExtractorUtilTester(unittest.TestCase): # Download this model to make sure it's in the cache. _ = Wav2Vec2FeatureExtractor.from_pretrained("hf-internal-testing/tiny-random-wav2vec2") # Under the mock environment we get a 500 error when trying to reach the model. - with mock.patch("requests.request", return_value=response_mock) as mock_head: + with mock.patch("requests.Session.request", return_value=response_mock) as mock_head: _ = Wav2Vec2FeatureExtractor.from_pretrained("hf-internal-testing/tiny-random-wav2vec2") # This check we did call the fake head request mock_head.assert_called() diff --git a/tests/test_image_processing_common.py b/tests/test_image_processing_common.py index 12cdef7a47..15a6688fbb 100644 --- a/tests/test_image_processing_common.py +++ b/tests/test_image_processing_common.py @@ -215,7 +215,7 @@ class ImageProcessorUtilTester(unittest.TestCase): # Download this model to make sure it's in the cache. _ = ViTImageProcessor.from_pretrained("hf-internal-testing/tiny-random-vit") # Under the mock environment we get a 500 error when trying to reach the model. - with mock.patch("requests.request", return_value=response_mock) as mock_head: + with mock.patch("requests.Session.request", return_value=response_mock) as mock_head: _ = ViTImageProcessor.from_pretrained("hf-internal-testing/tiny-random-vit") # This check we did call the fake head request mock_head.assert_called() diff --git a/tests/test_modeling_common.py b/tests/test_modeling_common.py index 113ee8f1c1..43e06e9067 100755 --- a/tests/test_modeling_common.py +++ b/tests/test_modeling_common.py @@ -3348,7 +3348,7 @@ class ModelUtilsTest(TestCasePlus): _ = BertModel.from_pretrained("hf-internal-testing/tiny-random-bert") # Under the mock environment we get a 500 error when trying to reach the model. - with mock.patch("requests.request", return_value=response_mock) as mock_head: + with mock.patch("requests.Session.request", return_value=response_mock) as mock_head: _ = BertModel.from_pretrained("hf-internal-testing/tiny-random-bert") # This check we did call the fake head request mock_head.assert_called() diff --git a/tests/test_modeling_tf_common.py b/tests/test_modeling_tf_common.py index 207f729525..f3878913a4 100644 --- a/tests/test_modeling_tf_common.py +++ b/tests/test_modeling_tf_common.py @@ -2016,7 +2016,7 @@ class UtilsFunctionsTest(unittest.TestCase): _ = TFBertModel.from_pretrained("hf-internal-testing/tiny-random-bert") # Under the mock environment we get a 500 error when trying to reach the model. - with mock.patch("requests.request", return_value=response_mock) as mock_head: + with mock.patch("requests.Session.request", return_value=response_mock) as mock_head: _ = TFBertModel.from_pretrained("hf-internal-testing/tiny-random-bert") # This check we did call the fake head request mock_head.assert_called() diff --git a/tests/test_tokenization_common.py b/tests/test_tokenization_common.py index 836f66a14a..84f6886db6 100644 --- a/tests/test_tokenization_common.py +++ b/tests/test_tokenization_common.py @@ -3954,7 +3954,7 @@ class TokenizerUtilTester(unittest.TestCase): _ = BertTokenizer.from_pretrained("hf-internal-testing/tiny-random-bert") # Under the mock environment we get a 500 error when trying to reach the tokenizer. - with mock.patch("requests.request", return_value=response_mock) as mock_head: + with mock.patch("requests.Session.request", return_value=response_mock) as mock_head: _ = BertTokenizer.from_pretrained("hf-internal-testing/tiny-random-bert") # This check we did call the fake head request mock_head.assert_called() @@ -3972,7 +3972,7 @@ class TokenizerUtilTester(unittest.TestCase): _ = GPT2TokenizerFast.from_pretrained("gpt2") # Under the mock environment we get a 500 error when trying to reach the tokenizer. - with mock.patch("requests.request", return_value=response_mock) as mock_head: + with mock.patch("requests.Session.request", return_value=response_mock) as mock_head: _ = GPT2TokenizerFast.from_pretrained("gpt2") # This check we did call the fake head request mock_head.assert_called() diff --git a/tests/utils/test_hub_utils.py b/tests/utils/test_hub_utils.py index 272ea26fa3..540847d667 100644 --- a/tests/utils/test_hub_utils.py +++ b/tests/utils/test_hub_utils.py @@ -89,7 +89,7 @@ class GetFromCacheTests(unittest.TestCase): response_mock.json.return_value = {} # Under the mock environment we get a 500 error when trying to reach the tokenizer. - with mock.patch("requests.request", return_value=response_mock) as mock_head: + with mock.patch("requests.Session.request", return_value=response_mock) as mock_head: path = cached_file(RANDOM_BERT, "conf", _raise_exceptions_for_connection_errors=False) self.assertIsNone(path) # This check we did call the fake head request