Skip to content
New issue

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

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

Already on GitHub? # to your account

Make token optional in all HfApi methods. #379

Merged
merged 11 commits into from
Oct 6, 2021
Merged

Make token optional in all HfApi methods. #379

merged 11 commits into from
Oct 6, 2021

Conversation

sgugger
Copy link
Contributor

@sgugger sgugger commented Sep 30, 2021

This PR makes the token argument of all HfApi methods optional and default to the saved token. As you can see in the updated docs, the result is quite nice in the sense a user never has to retrieve their token (and thus get some chance of leaking it by mistake).

Since this changes the order of the argument and is thus a breaking change, some internal magic is implemented to detect if the argument at the old place of the token is the token, when the argument at the place of the token is not a valid token, and then do some switch.

@sgugger sgugger requested a review from LysandreJik September 30, 2021 20:07
@sgugger sgugger changed the title [WIP] Make token optional in all HfApi methods. Make token optional in all HfApi methods. Sep 30, 2021
Copy link
Member

@LysandreJik LysandreJik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a very welcome change which should make many things clearer. I think having the token as first parameter in most methods isn't ideal, as it is generally not the key parameter of the method.

Ont nit, however: would it be possible to split the PR in two, with one PR changing the API and the second changing the tests? Modifying both at the same time is a bit dangerous as we try to prevent breaking changes.

@osanseviero, this PR introduces a change in the methods', but Sylvain also introduced mechanisms to be backwards compatible as it would otherwise be very breaking. This will, however, change in a future version with future warnings removed.

Do you know if libraries have an unpinned version of huggingface_hub, and, if so, if they use any of the changed methods here? Namely, create_repo, delete_repo, update_repo_visibility, and upload_file? Thank you.

Comment on lines +331 to +334
if token is None:
raise ValueError(
"You need to pass a valid `token` or login by using `huggingface-cli login`"
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, very nice improvement

Comment on lines 591 to 596
def _is_valid_token(self, token: str):
try:
self.whoami(token=token)
return True
except HTTPError:
return False
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice helper, would it be possible to add a bit of documentation?

Comment on lines +635 to +640
warnings.warn(
"`create_repo` now takes `token` as an optional positional argument. "
"Be sure to adapt your code!",
FutureWarning,
)
token, name = name, token
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect!

@LysandreJik
Copy link
Member

Also cc @stevhliu, could you link which documentation pages should be updated to reflect this change? Thanks a lot

sgugger added a commit that referenced this pull request Oct 4, 2021
@stevhliu
Copy link
Member

stevhliu commented Oct 4, 2021

I think these are the only two pages that will need to be updated:

Let me know if I can help update these :)

@sgugger
Copy link
Contributor Author

sgugger commented Oct 5, 2021

I've already updated those to the new API, normally.

@osanseviero
Copy link
Contributor

Sorry for the delay with my response. Thanks for this PR @sgugger!!

I think there are 4 actionable repos (1 officially supported, 3 non official)

Officially supported libraries

Non official

@LysandreJik
Copy link
Member

Thank you for doing the research @osanseviero!

@Narsil this touches to some of the code in api-inference-community, could you take a quick look at the changes?

Copy link
Member

@LysandreJik LysandreJik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! As seen with @sgugger and @osanseviero, the only library that uses an unpinned version of huggingface_hub and that would potentially break is sentence-transformers cc @nreimers.

The workarounds in create_repo should prevent any breaking change, however.

@LysandreJik LysandreJik merged commit 5a0eb4d into main Oct 6, 2021
@LysandreJik LysandreJik deleted the optional_token branch October 6, 2021 19:18
sgugger added a commit that referenced this pull request Oct 7, 2021
LysandreJik pushed a commit that referenced this pull request Oct 7, 2021
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants