-
Notifications
You must be signed in to change notification settings - Fork 27.3k
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
Fix mock in test_cached_files_are_used_when_internet_is_down
#18804
Fix mock in test_cached_files_are_used_when_internet_is_down
#18804
Conversation
…to have an empty response.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks @Wauplin
The documentation is not available anymore as the PR was closed or merged. |
Thank you, @Wauplin . As my knowledge of The
is the
? And if we don't set |
Hi @ydshieh, I'll try to explain the PR a bit. In the tests, we are defining a Mock object >>> from unittest.mock import Mock
>>> my_mock = Mock()
# the mock we defined
>>> my_mock
<Mock id='140556566369952'>
# `foo` is also a mock we different id
>>> my_mock.foo
<Mock name='mock.foo' id='140556566370816'>
# `foo` can be called as a function and return a new mock with different id
>>> my_mock.foo()
<Mock name='mock.foo()' id='140556566424112'>
# `.foo()` is a mock so you can call anything from it
>>> my_mock.foo().bar
<Mock name='mock.foo().bar' id='140556530612016'>
# now we set a custom return value for `.foo()`
>>> my_mock.foo.return_value = 4
# `.foo` still a mock
>>> my_mock.foo
<Mock name='mock.foo' id='140556566370816'>
# `.foo()` is now a "normal" value, here the integer 4
>>> my_mock.foo()
4
# not a mock so cannot call `.bar` from `4`
>>> my_mock.foo().bar # not a mock so cannot call `.bar` from `4`
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'int' object has no attribute 'bar' The goal of a mock is that almost any manipulation done on it will not fail. It will continue to pass mocked values which in the end you can test. There are a few tweaks you can do on a mock like raising an Error (which is done with the To come back to you initial question, when "requests.request" is patch, it means if in this context So yes, when in with mock.patch("requests.request", return_value=response_mock) as mock_head:
_ = BertConfig.from_pretrained("hf-internal-testing/tiny-random-bert") And finally to be complete, mock objects are also nice because they count how much calls have been made to them. So at the end of test the Hope this help you understand what mocks are doing :) |
@ydshieh I answered to your question in the previous comment but posted it when it was half-written by mistake. Now the explanation is complete :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you @Wauplin, for the fix as well as for unmocking my Mock knowledge 😃
test_cached_files_are_used_when_internet_is_down
What does this PR do?
Fix the CI that is currently breaking because of the 0.9.1 patch release of
huggingface_hub
.Problem is that we are looking at the response from the server when having a HTTPError. In the tests, the response is mocked which makes
response.json()
a mock instead of a dictionary. I now set it to{}
which means an empty response from the server.See slack thread (internal link) for more context.
Expected result
The CI should now pass correctly.