Skip to content

Commit

Permalink
MNT enable preview on black (#849)
Browse files Browse the repository at this point in the history
* MNT enable preview on black

* add py37 compat
  • Loading branch information
adrinjalali committed Apr 26, 2022
1 parent e61f39b commit 04c38fc
Show file tree
Hide file tree
Showing 17 changed files with 163 additions and 89 deletions.
4 changes: 4 additions & 0 deletions pyproject.toml
@@ -0,0 +1,4 @@
[tool.black]
line-length = 88
target_version = ['py37', 'py38', 'py39', 'py310']
preview = true
9 changes: 7 additions & 2 deletions setup.py
Expand Up @@ -51,10 +51,15 @@ def get_version() -> str:
version=get_version(),
author="Hugging Face, Inc.",
author_email="julien@huggingface.co",
description="Client library to download and publish models on the huggingface.co hub",
description=(
"Client library to download and publish models on the huggingface.co hub"
),
long_description=open("README.md", "r", encoding="utf-8").read(),
long_description_content_type="text/markdown",
keywords="model-hub machine-learning models natural-language-processing deep-learning pytorch pretrained-models",
keywords=(
"model-hub machine-learning models natural-language-processing deep-learning"
" pytorch pretrained-models"
),
license="Apache",
url="https://github.com/huggingface/huggingface_hub",
package_dir={"": "src"},
Expand Down
3 changes: 2 additions & 1 deletion src/huggingface_hub/commands/lfs.py
Expand Up @@ -88,7 +88,8 @@ def run(self):
cwd=local_path,
)
subprocess.run(
f"git config lfs.customtransfer.multipart.args {LFS_MULTIPART_UPLOAD_COMMAND}".split(),
"git config lfs.customtransfer.multipart.args"
f" {LFS_MULTIPART_UPLOAD_COMMAND}".split(),
check=True,
cwd=local_path,
)
Expand Down
32 changes: 23 additions & 9 deletions src/huggingface_hub/commands/user.py
Expand Up @@ -46,7 +46,10 @@ def register_subcommand(parser: ArgumentParser):
# new system: git-based repo system
repo_parser = parser.add_parser(
"repo",
help="{create, ls-files} Commands to interact with your huggingface.co repos.",
help=(
"{create, ls-files} Commands to interact with your huggingface.co"
" repos."
),
)
repo_subparsers = repo_parser.add_subparsers(
help="huggingface.co repos related commands"
Expand All @@ -57,20 +60,29 @@ def register_subcommand(parser: ArgumentParser):
repo_create_parser.add_argument(
"name",
type=str,
help="Name for your repo. Will be namespaced under your username to build the repo id.",
help=(
"Name for your repo. Will be namespaced under your username to build"
" the repo id."
),
)
repo_create_parser.add_argument(
"--type",
type=str,
help='Optional: repo_type: set to "dataset" or "space" if creating a dataset or space, default is model.',
help=(
'Optional: repo_type: set to "dataset" or "space" if creating a dataset'
" or space, default is model."
),
)
repo_create_parser.add_argument(
"--organization", type=str, help="Optional: organization namespace."
)
repo_create_parser.add_argument(
"--space_sdk",
type=str,
help='Optional: Hugging Face Spaces SDK type. Required when --type is set to "space".',
help=(
"Optional: Hugging Face Spaces SDK type. Required when --type is set to"
' "space".'
),
choices=SPACES_SDK_TYPES,
)
repo_create_parser.add_argument(
Expand Down Expand Up @@ -306,8 +318,8 @@ def notebook_login():
from IPython.display import clear_output, display
except ImportError:
raise ImportError(
"The `notebook_login` function can only be used in a notebook (Jupyter or Colab) and you need the "
"`ipywdidgets` module: `pip install ipywidgets`."
"The `notebook_login` function can only be used in a notebook (Jupyter or"
" Colab) and you need the `ipywdidgets` module: `pip install ipywidgets`."
)

box_layout = widgets.Layout(
Expand Down Expand Up @@ -392,8 +404,10 @@ def _login(hf_api, username=None, password=None, token=None):
if "store" not in helpers:
print(
ANSI.red(
"Authenticated through git-credential store but this isn't the helper defined on your machine.\nYou "
"might have to re-authenticate when pushing to the Hugging Face Hub. Run the following command in your "
"terminal in case you want to set this credential helper as the default\n\ngit config --global credential.helper store"
"Authenticated through git-credential store but this isn't the helper"
" defined on your machine.\nYou might have to re-authenticate when"
" pushing to the Hugging Face Hub. Run the following command in your"
" terminal in case you want to set this credential helper as the"
" default\n\ngit config --global credential.helper store"
)
)
20 changes: 12 additions & 8 deletions src/huggingface_hub/file_download.py
Expand Up @@ -346,7 +346,8 @@ def _request_with_retry(
raise err
else:
logger.info(
f"{method} request to {url} timed out, retrying... [{tries/max_retries}]"
f"{method} request to {url} timed out, retrying..."
f" [{tries/max_retries}]"
)
sleep_time = min(
max_wait_time, base_wait_time * 2 ** (tries - 1)
Expand Down Expand Up @@ -493,7 +494,8 @@ def cached_download(
token = HfFolder.get_token()
if token is None:
raise EnvironmentError(
"You specified use_auth_token=True, but a huggingface token was not found."
"You specified use_auth_token=True, but a huggingface token was not"
" found."
)
headers["authorization"] = f"Bearer {token}"

Expand All @@ -516,7 +518,8 @@ def cached_download(
# If we don't have any of those, raise an error.
if etag is None:
raise OSError(
"Distant resource does not have an ETag, we won't be able to reliably ensure reproducibility."
"Distant resource does not have an ETag, we won't be able to"
" reliably ensure reproducibility."
)
# In case of a redirect,
# save an extra redirect on the request.get call,
Expand Down Expand Up @@ -568,14 +571,15 @@ def cached_download(
# Notify the user about that
if local_files_only:
raise ValueError(
"Cannot find the requested files in the cached path and outgoing traffic has been"
" disabled. To enable model look-ups and downloads online, set 'local_files_only'"
" to False."
"Cannot find the requested files in the cached path and"
" outgoing traffic has been disabled. To enable model look-ups"
" and downloads online, set 'local_files_only' to False."
)
else:
raise ValueError(
"Connection error, and we cannot find the requested files in the cached path."
" Please try again or make sure your Internet connection is on."
"Connection error, and we cannot find the requested files in"
" the cached path. Please try again or make sure your Internet"
" connection is on."
)

# From now on, etag is not None.
Expand Down
33 changes: 19 additions & 14 deletions src/huggingface_hub/hf_api.py
Expand Up @@ -671,8 +671,8 @@ def _validate_or_retrieve_token(
if self._is_valid_token(name):
# TODO(0.6) REMOVE
warnings.warn(
f"`{function_name}` now takes `token` as an optional positional argument. "
"Be sure to adapt your code!",
f"`{function_name}` now takes `token` as an optional positional"
" argument. Be sure to adapt your code!",
FutureWarning,
)
token, name = name, token
Expand Down Expand Up @@ -899,7 +899,8 @@ def list_models(
if emissions_thresholds is not None:
if cardData is None:
raise ValueError(
"`emissions_thresholds` were passed without setting `cardData=True`."
"`emissions_thresholds` were passed without setting"
" `cardData=True`."
)
else:
return _filter_emissions(res, *emissions_thresholds)
Expand Down Expand Up @@ -1458,8 +1459,8 @@ def create_repo(
if repo_type == "space":
if space_sdk is None:
raise ValueError(
"No space_sdk provided. `create_repo` expects space_sdk to be one of "
f"{SPACES_SDK_TYPES} when repo_type is 'space'`"
"No space_sdk provided. `create_repo` expects space_sdk to be one"
f" of {SPACES_SDK_TYPES} when repo_type is 'space'`"
)
if space_sdk not in SPACES_SDK_TYPES:
raise ValueError(
Expand Down Expand Up @@ -1689,12 +1690,14 @@ def move_repo(

if len(from_id.split("/")) != 2:
raise ValueError(
f"Invalid repo_id: {from_id}. It should have a namespace (:namespace:/:repo_name:)"
f"Invalid repo_id: {from_id}. It should have a namespace"
" (:namespace:/:repo_name:)"
)

if len(to_id.split("/")) != 2:
raise ValueError(
f"Invalid repo_id: {to_id}. It should have a namespace (:namespace:/:repo_name:)"
f"Invalid repo_id: {to_id}. It should have a namespace"
" (:namespace:/:repo_name:)"
)

json = {"fromRepo": from_id, "toRepo": to_id, "type": repo_type}
Expand All @@ -1710,13 +1713,15 @@ def move_repo(
except HTTPError as e:
if r.text:
raise HTTPError(
f"{r.status_code} Error Message: {r.text}. For additional documentation "
"please see https://hf.co/docs/hub/main#how-can-i-rename-or-transfer-a-repo."
f"{r.status_code} Error Message: {r.text}. For additional"
" documentation please see"
" https://hf.co/docs/hub/main#how-can-i-rename-or-transfer-a-repo."
) from e
else:
raise e
logger.info(
"Accepted transfer request. You will get an email once this is successfully completed."
"Accepted transfer request. You will get an email once this is successfully"
" completed."
)

@_deprecate_positional_args
Expand Down Expand Up @@ -1808,8 +1813,8 @@ def upload_file(
except ValueError: # if token is invalid or organization token
if self._is_valid_token(path_or_fileobj):
warnings.warn(
"`upload_file` now takes `token` as an optional positional argument. "
"Be sure to adapt your code!",
"`upload_file` now takes `token` as an optional positional"
" argument. Be sure to adapt your code!",
FutureWarning,
)
token, path_or_fileobj, path_in_repo, repo_id = (
Expand All @@ -1829,8 +1834,8 @@ def upload_file(
elif not isinstance(path_or_fileobj, (RawIOBase, BufferedIOBase, bytes)):
# ^^ Test from: https://stackoverflow.com/questions/44584829/how-to-determine-if-file-is-opened-in-binary-or-text-mode
raise ValueError(
"path_or_fileobj must be either an instance of str or BinaryIO. "
"If you passed a fileobj, make sure you've opened the file in binary mode."
"path_or_fileobj must be either an instance of str or BinaryIO. If you"
" passed a fileobj, make sure you've opened the file in binary mode."
)

if repo_type in REPO_TYPES_URL_PREFIXES:
Expand Down
7 changes: 4 additions & 3 deletions src/huggingface_hub/hub_mixin.py
Expand Up @@ -276,9 +276,10 @@ def push_to_hub(
token = HfFolder.get_token()
if token is None:
raise ValueError(
"You must login to the Hugging Face hub on this computer by typing `huggingface-cli login` and "
"entering your credentials to use `use_auth_token=True`. Alternatively, you can pass your own "
"token as the `use_auth_token` argument."
"You must login to the Hugging Face hub on this computer by typing"
" `huggingface-cli login` and entering your credentials to use"
" `use_auth_token=True`. Alternatively, you can pass your own token"
" as the `use_auth_token` argument."
)
elif isinstance(use_auth_token, str):
token = use_auth_token
Expand Down
7 changes: 5 additions & 2 deletions src/huggingface_hub/inference_api.py
Expand Up @@ -113,15 +113,18 @@ def __init__(
model_info = HfApi().model_info(repo_id=repo_id, token=token)
if not model_info.pipeline_tag and not task:
raise ValueError(
"Task not specified in the repository. Please add it to the model card using pipeline_tag (https://huggingface.co/docs#how-is-a-models-type-of-inference-api-and-widget-determined)"
"Task not specified in the repository. Please add it to the model card"
" using pipeline_tag"
" (https://huggingface.co/docs#how-is-a-models-type-of-inference-api-and-widget-determined)"
)

if task and task != model_info.pipeline_tag:
if task not in ALL_TASKS:
raise ValueError(f"Invalid task {task}. Make sure it's valid.")

logger.warning(
"You're using a different task than the one specified in the repository. Be sure to know what you're doing :)"
"You're using a different task than the one specified in the"
" repository. Be sure to know what you're doing :)"
)
self.task = task
else:
Expand Down
10 changes: 6 additions & 4 deletions src/huggingface_hub/keras_mixin.py
Expand Up @@ -195,7 +195,8 @@ def save_pretrained_keras(
if config:
if not isinstance(config, dict):
raise RuntimeError(
f"Provided config to save_pretrained_keras should be a dict. Got: '{type(config)}'"
"Provided config to save_pretrained_keras should be a dict. Got:"
f" '{type(config)}'"
)
path = os.path.join(save_directory, CONFIG_NAME)
with open(path, "w") as f:
Expand Down Expand Up @@ -373,9 +374,10 @@ def push_to_hub_keras(

if token is None:
raise ValueError(
"You must login to the Hugging Face hub on this computer by typing `huggingface-cli login` and "
"entering your credentials to use `use_auth_token=True`. Alternatively, you can pass your own "
"token as the `use_auth_token` argument."
"You must login to the Hugging Face hub on this computer by typing"
" `huggingface-cli login` and entering your credentials to use"
" `use_auth_token=True`. Alternatively, you can pass your own token as the"
" `use_auth_token` argument."
)

if repo_path_or_name is None:
Expand Down

0 comments on commit 04c38fc

Please sign in to comment.