Skip to content
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

Remove black formatter to use only ruff #1783

Merged
merged 1 commit into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/workflows/python-quality.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ jobs:
run: |
pip install --upgrade pip
pip install .[dev]
- run: black --check tests src contrib
- run: ruff tests src contrib
- run: python utils/check_contrib_list.py
- run: python utils/check_static_imports.py
Expand Down
6 changes: 1 addition & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ repos:
- id: trailing-whitespace
- id: check-case-conflict
- id: check-merge-conflict
- repo: https://github.com/psf/black
rev: 23.1.0
hooks:
- id: black
- repo: https://github.com/charliermarsh/ruff-pre-commit # https://github.com/charliermarsh/ruff#usage
rev: 'v0.0.243'
rev: 'v0.1.3'
hooks:
- id: ruff
5 changes: 2 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,8 @@ Follow these steps to start contributing:

7. Format your code.

`hugginface_hub` relies on [`black`](https://github.com/psf/black) and [`ruff`](https://github.com/astral-sh/ruff)
to format its source code consistently. You can apply automatic style corrections and code verifications
with the following command:
`hugginface_hub` relies on [`ruff`](https://github.com/astral-sh/ruff) to format its source code consistently. You
can apply automatic style corrections and code verifications with the following command:

```bash
$ make style
Expand Down
4 changes: 1 addition & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@ check_dirs := contrib src tests utils setup.py


quality:
black --check $(check_dirs)
ruff $(check_dirs)
mypy src
python utils/check_contrib_list.py
python utils/check_static_imports.py
python utils/generate_async_inference_client.py

style:
black $(check_dirs)
ruff $(check_dirs) --fix
ruff --fix $(check_dirs)
python utils/check_contrib_list.py --update
python utils/check_static_imports.py --update
python utils/generate_async_inference_client.py --update
Expand Down
29 changes: 22 additions & 7 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
[tool.black]
line-length = 119
target_version = ['py37', 'py38', 'py39', 'py310']
preview = true

[tool.mypy]
ignore_missing_imports = true
no_implicit_optional = true
Expand All @@ -14,11 +9,31 @@ plugins = [
[tool.ruff]
# Ignored rules:
# "E501" -> line length violation
# "F821" -> undefined named in type annotation (e.g. Literal["something"])
ignore = ["E501", "F821"]
ignore = ["E501"]
select = ["E", "F", "I", "W"]
line-length = 119
exclude = [
".eggs",
".git",
".git-rewrite",
".hg",
".mypy_cache",
".nox",
".pants.d",
".pytype",
".ruff_cache",
".svn",
".tox",
".venv",
".venv*",
"__pypackages__",
"_build",
"build",
"dist",
"venv",
]

[tool.ruff.isort]
lines-after-imports = 2
known-first-party = ["huggingface_hub"]

3 changes: 1 addition & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ def get_version() -> str:
]

extras["quality"] = [
"black==23.7",
"ruff>=0.0.241",
"ruff>=0.1.3",
"mypy==1.5.1",
]

Expand Down
16 changes: 9 additions & 7 deletions src/huggingface_hub/_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@

logger = logging.get_logger(__name__)

_HF_LOGO_ASCII = """
_| _| _| _| _|_|_| _|_|_| _|_|_| _| _| _|_|_| _|_|_|_| _|_| _|_|_| _|_|_|_|
_| _| _| _| _| _| _| _|_| _| _| _| _| _| _| _|
_|_|_|_| _| _| _| _|_| _| _|_| _| _| _| _| _| _|_| _|_|_| _|_|_|_| _| _|_|_|
_| _| _| _| _| _| _| _| _| _| _|_| _| _| _| _| _| _| _|
_| _| _|_| _|_|_| _|_|_| _|_|_| _| _| _|_|_| _| _| _| _|_|_| _|_|_|_|
"""


def login(
token: Optional[str] = None,
Expand Down Expand Up @@ -144,13 +152,7 @@ def interpreter_login(new_session: bool = True, write_permission: bool = False)
print("User is already logged in.")
return

print("""
_| _| _| _| _|_|_| _|_|_| _|_|_| _| _| _|_|_| _|_|_|_| _|_| _|_|_| _|_|_|_|
_| _| _| _| _| _| _| _|_| _| _| _| _| _| _| _|
_|_|_|_| _| _| _| _|_| _| _|_| _| _| _| _| _| _|_| _|_|_| _|_|_|_| _| _|_|_|
_| _| _| _| _| _| _| _| _| _| _|_| _| _| _| _| _| _| _|
_| _| _|_| _|_|_| _|_|_| _|_|_| _| _| _|_|_| _| _| _| _|_|_| _|_|_|_|
""")
print(_HF_LOGO_ASCII)
if HfFolder.get_token() is not None:
print(
" A token is already saved on your machine. Run `huggingface-cli"
Expand Down
9 changes: 3 additions & 6 deletions src/huggingface_hub/file_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -1606,12 +1606,9 @@ def get_hf_file_metadata(
# Return
return HfFileMetadata(
commit_hash=r.headers.get(HUGGINGFACE_HEADER_X_REPO_COMMIT),
etag=_normalize_etag(
# We favor a custom header indicating the etag of the linked resource, and
# we fallback to the regular etag header.
r.headers.get(HUGGINGFACE_HEADER_X_LINKED_ETAG)
or r.headers.get("ETag")
),
# We favor a custom header indicating the etag of the linked resource, and
# we fallback to the regular etag header.
etag=_normalize_etag(r.headers.get(HUGGINGFACE_HEADER_X_LINKED_ETAG) or r.headers.get("ETag")),
# Either from response headers (if redirected) or defaults to request url
# Do not use directly `url`, as `_request_wrapper` might have followed relative
# redirects.
Expand Down
14 changes: 8 additions & 6 deletions src/huggingface_hub/hf_file_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from ._commit_api import CommitOperationCopy, CommitOperationDelete
from .constants import DEFAULT_REVISION, ENDPOINT, REPO_TYPE_MODEL, REPO_TYPES_MAPPING, REPO_TYPES_URL_PREFIXES
from .file_download import hf_hub_url
from .hf_api import HfApi
from .utils import (
EntryNotFoundError,
Expand Down Expand Up @@ -45,10 +46,8 @@ class HfFileSystemResolvedPath:
path_in_repo: str

def unresolve(self) -> str:
return (
f"{REPO_TYPES_URL_PREFIXES.get(self.repo_type, '') + self.repo_id}@{safe_revision(self.revision)}/{self.path_in_repo}"
.rstrip("/")
)
repo_path = REPO_TYPES_URL_PREFIXES.get(self.repo_type, "") + self.repo_id
return f"{repo_path}@{safe_revision(self.revision)}/{self.path_in_repo}".rstrip("/")


class HfFileSystem(fsspec.AbstractFileSystem):
Expand Down Expand Up @@ -421,8 +420,11 @@ def _fetch_range(self, start: int, end: int) -> bytes:
"range": f"bytes={start}-{end - 1}",
**self.fs._api._build_hf_headers(),
}
url = (
f"{self.fs.endpoint}/{REPO_TYPES_URL_PREFIXES.get(self.resolved_path.repo_type, '') + self.resolved_path.repo_id}/resolve/{safe_quote(self.resolved_path.revision)}/{safe_quote(self.resolved_path.path_in_repo)}"
url = hf_hub_url(
repo_id=self.resolved_path.repo_id,
revision=self.resolved_path.revision,
filename=self.resolved_path.path_in_repo,
endpoint=self.fs.endpoint,
)
r = http_backoff("GET", url, headers=headers)
hf_raise_for_status(r)
Expand Down
8 changes: 2 additions & 6 deletions utils/_legacy_check_future_compatible_signatures.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
from pathlib import Path
from typing import Callable, NoReturn

import black
from ruff.__main__ import find_ruff_bin

from huggingface_hub.hf_api import HfApi
Expand Down Expand Up @@ -173,17 +172,14 @@ def generate_hf_api_module() -> str:
# Generate code with stubs
generated_code = STUBS_SECTION_TEMPLATE_REGEX.sub(STUBS_SECTION_TEMPLATE.format(stubs=all_stubs_source), raw_code)

# Format (black+ruff)
# Format (ruff)
return format_generated_code(generated_code)


def format_generated_code(code: str) -> str:
"""
Format some code with black+ruff. Cannot be done "on the fly" so we first save the code in a temporary file.
Format some code with ruff. Cannot be done "on the fly" so we first save the code in a temporary file.
"""
# Format with black
code = black.format_file_contents(code, fast=False, mode=black.FileMode(line_length=119))

# Format with ruff
with tempfile.TemporaryDirectory() as tmpdir:
filepath = Path(tmpdir) / "__init__.py"
Comment on lines -184 to 185

Choose a reason for hiding this comment

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

The call to ruff without arguments will not format but check.
See #27144 where we heavily rely on the formatting of the # Copied from

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch!
Fixed this in #1824.

Expand Down