Skip to content

Commit

Permalink
style: Bumps ruff to 0.1.11 and replaces black (#287)
Browse files Browse the repository at this point in the history
* chore: Bumps ruff to 0.1.11

* docs: Updates README

* ci: Updates CI

* style: Updates ruff config

* docs: Updates makefile

* style: Fixes format

* style: Updates ruff config

* style: Fixes lint & format

* style: Fixes style

* style: Fixes types

* style: Silences ruff on ARG & ANN

* style: Fixes EOF

* style: Updates precommit

* docs: Updates copyright year

* docs: Updates copyright year

* docs: Fixes copyright notice
  • Loading branch information
frgfm authored Jan 7, 2024
1 parent b6a4670 commit af1c020
Show file tree
Hide file tree
Showing 74 changed files with 595 additions and 692 deletions.
14 changes: 4 additions & 10 deletions .github/collect_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,21 +226,15 @@ def get_os(run_lambda):
def get_env_info():
run_lambda = run

if HOLOCRON_AVAILABLE:
holocron_str = holocron.__version__
else:
holocron_str = "N/A"
holocron_str = holocron.__version__ if HOLOCRON_AVAILABLE else "N/A"

if TORCH_AVAILABLE:
torch_str = torch.__version__
cuda_available_str = torch.cuda.is_available()
else:
torch_str = cuda_available_str = "N/A"

if TORCHVISION_AVAILABLE:
torchvision_str = torchvision.__version__
else:
torchvision_str = "N/A"
torchvision_str = torchvision.__version__ if TORCHVISION_AVAILABLE else "N/A"

return SystemEnv(
holocron_version=holocron_str,
Expand Down Expand Up @@ -273,14 +267,14 @@ def get_env_info():

def pretty_str(envinfo):
def replace_nones(dct, replacement="Could not collect"):
for key in dct.keys():
for key in dct:
if dct[key] is not None:
continue
dct[key] = replacement
return dct

def replace_bools(dct, true="Yes", false="No"):
for key in dct.keys():
for key in dct:
if dct[key] is True:
dct[key] = true
elif dct[key] is False:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/builds.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
pip install -e . --upgrade
- name: Import package
run: python -c "import holocron; print(holocron.__version__)"

pypi:
runs-on: ubuntu-latest
steps:
Expand Down
35 changes: 16 additions & 19 deletions .github/workflows/style.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@ jobs:
python: [3.9]
steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v1
- uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python }}
architecture: x64
- name: Run ruff
run: |
pip install ruff==0.1.0
pip install ruff==0.1.11
ruff --version
ruff check --diff .
Expand All @@ -34,8 +33,7 @@ jobs:
python: [3.9]
steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v1
- uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python }}
architecture: x64
Expand All @@ -53,40 +51,39 @@ jobs:
mypy --version
mypy
black:
ruff-format:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest]
python: [3.9]
steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
- uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python }}
architecture: x64
- name: Run black
- name: Run ruff
run: |
pip install "black==23.3.0"
black --version
black --check --diff .
pip install ruff==0.1.11
ruff --version
ruff format --check --diff .
bandit:
precommit-hooks:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest]
python: [3.9]
steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
- uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python }}
architecture: x64
- name: Run bandit
- name: Run ruff
run: |
pip install bandit[toml]
bandit --version
bandit -r . -c pyproject.toml
pip install pre-commit
pre-commit install
pre-commit --version
pre-commit run --all-files
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ jobs:
with:
flags: unittests
fail_ci_if_error: true

api:
runs-on: ${{ matrix.os }}
strategy:
Expand Down
24 changes: 12 additions & 12 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,28 @@ default_language_version:
python: python3.9
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
rev: v4.5.0
hooks:
- id: check-added-large-files
- id: check-ast
- id: check-case-conflict
- id: check-json
- id: check-merge-conflict
- id: check-symlinks
- id: check-toml
- id: check-xml
- id: check-yaml
exclude: .conda
- id: check-toml
- id: check-json
- id: check-added-large-files
- id: end-of-file-fixer
- id: trailing-whitespace
- id: debug-statements
language_version: python3
- id: check-merge-conflict
- id: end-of-file-fixer
- id: no-commit-to-branch
args: ['--branch', 'main']
- repo: https://github.com/psf/black
rev: 23.3.0
hooks:
- id: black
- id: trailing-whitespace
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: 'v0.1.0'
rev: 'v0.1.11'
hooks:
- id: ruff
args:
- --fix
- id: ruff-format
5 changes: 2 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
# this target runs checks on all files
quality:
ruff format --check .
ruff check .
mypy
black --check .
bandit -r . -c pyproject.toml

# this target runs checks on all files and potentially modifies some of them
style:
black .
ruff format .
ruff --fix .

# Run tests for the library
Expand Down
2 changes: 1 addition & 1 deletion api/app/config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2022-2023, François-Guillaume Fernandez.
# Copyright (C) 2022-2024, François-Guillaume Fernandez.

# This program is licensed under the Apache License 2.0.
# See LICENSE or go to <https://www.apache.org/licenses/LICENSE-2.0> for full license details.
Expand Down
2 changes: 1 addition & 1 deletion api/app/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2022-2023, François-Guillaume Fernandez.
# Copyright (C) 2022-2024, François-Guillaume Fernandez.

# This program is licensed under the Apache License 2.0.
# See LICENSE or go to <https://www.apache.org/licenses/LICENSE-2.0> for full license details.
Expand Down
6 changes: 3 additions & 3 deletions api/app/routes/classification.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2022-2023, François-Guillaume Fernandez.
# Copyright (C) 2022-2024, François-Guillaume Fernandez.

# This program is licensed under the Apache License 2.0.
# See LICENSE or go to <https://www.apache.org/licenses/LICENSE-2.0> for full license details.
Expand All @@ -12,8 +12,8 @@
router = APIRouter()


@router.post("/", response_model=ClsCandidate, status_code=status.HTTP_200_OK, summary="Perform image classification")
async def classify(file: UploadFile = File(...)):
@router.post("/", status_code=status.HTTP_200_OK, summary="Perform image classification")
async def classify(file: UploadFile = File(...)) -> ClsCandidate:
"""Runs holocron vision model to analyze the input image"""
probs = classify_image(decode_image(file.file.read()))

Expand Down
2 changes: 1 addition & 1 deletion api/app/schemas.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2022-2023, François-Guillaume Fernandez.
# Copyright (C) 2022-2024, François-Guillaume Fernandez.

# This program is licensed under the Apache License 2.0.
# See LICENSE or go to <https://www.apache.org/licenses/LICENSE-2.0> for full license details.
Expand Down
2 changes: 1 addition & 1 deletion api/app/vision.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2022-2023, François-Guillaume Fernandez.
# Copyright (C) 2022-2024, François-Guillaume Fernandez.

# This program is licensed under the Apache License 2.0.
# See LICENSE or go to <https://www.apache.org/licenses/LICENSE-2.0> for full license details.
Expand Down
2 changes: 1 addition & 1 deletion api/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ services:
- "traefik.http.routers.api.service=backend@docker"
- "traefik.http.routers.api.tls={}"
- "traefik.http.services.backend.loadbalancer.server.port=8050"

traefik:
image: traefik:v2.9.6
volumes:
Expand Down
2 changes: 1 addition & 1 deletion demo/app.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2022-2023, François-Guillaume Fernandez.
# Copyright (C) 2022-2024, François-Guillaume Fernandez.

# This program is licensed under the Apache License 2.0.
# See LICENSE or go to <https://www.apache.org/licenses/LICENSE-2.0> for full license details.
Expand Down
18 changes: 9 additions & 9 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2019-2023, François-Guillaume Fernandez.
# Copyright (C) 2019-2024, François-Guillaume Fernandez.

# This program is licensed under the Apache License 2.0.
# See LICENSE or go to <https://www.apache.org/licenses/LICENSE-2.0> for full license details.
Expand All @@ -22,7 +22,7 @@

from tabulate import tabulate

sys.path.insert(0, Path().resolve().parent.parent)
sys.path.insert(0, Path().cwd().parent.parent)


import holocron
Expand Down Expand Up @@ -217,10 +217,12 @@ def inject_checkpoint_metadata(app, what, name, obj, options, lines):

# Loading Meta
meta = field.value.meta
table.append(("url", f"`link <{meta.url}>`__"))
table.append(("sha256", meta.sha256[:16]))
table.append(("size", f"{meta.size / 1024**2:.1f}MB"))
table.append(("num_params", f"{meta.num_params / 1e6:.1f}M"))
table.extend((
("url", f"`link <{meta.url}>`__"),
("sha256", meta.sha256[:16]),
("size", f"{meta.size / 1024 ** 2:.1f}MB"),
("num_params", f"{meta.num_params / 1000000.0:.1f}M"),
))
# Wrap the text
max_visible = 3
v = meta.categories
Expand Down Expand Up @@ -311,9 +313,7 @@ def add_ga_javascript(app, pagename, templatename, context, doctree):
gtag('js', new Date());
gtag('config', '{0}');
</script>
""".format(
app.config.googleanalytics_id
)
""".format(app.config.googleanalytics_id)
context["metatags"] = metatags


Expand Down
2 changes: 1 addition & 1 deletion holocron/models/checkpoints.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2023, François-Guillaume Fernandez.
# Copyright (C) 2023-2024, François-Guillaume Fernandez.

# This program is licensed under the Apache License 2.0.
# See LICENSE or go to <https://www.apache.org/licenses/LICENSE-2.0> for full license details.
Expand Down
26 changes: 12 additions & 14 deletions holocron/models/classification/convnext.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2022-2023, François-Guillaume Fernandez.
# Copyright (C) 2022-2024, François-Guillaume Fernandez.

# This program is licensed under the Apache License 2.0.
# See LICENSE or go to <https://www.apache.org/licenses/LICENSE-2.0> for full license details.
Expand Down Expand Up @@ -46,7 +46,7 @@ class LayerScale(nn.Module):

def __init__(self, chans: int, scale: float = 1e-6) -> None:
super().__init__()
self.register_parameter("weight", nn.Parameter(scale * torch.ones(chans)))
self.register_parameter("weight", nn.Parameter(scale * torch.ones(chans))) # type: ignore[arg-type]

def forward(self, x: torch.Tensor) -> torch.Tensor:
return x * self.weight.reshape(1, -1, *((1,) * (x.ndim - 2)))
Expand Down Expand Up @@ -168,19 +168,17 @@ def __init__(
block_idx += _num_blocks

super().__init__(
OrderedDict(
[
("features", nn.Sequential(*_layers)),
("pool", GlobalAvgPool2d(flatten=True)),
(
"head",
nn.Sequential(
nn.LayerNorm(planes[-1], eps=1e-6),
nn.Linear(planes[-1], num_classes),
),
OrderedDict([
("features", nn.Sequential(*_layers)),
("pool", GlobalAvgPool2d(flatten=True)),
(
"head",
nn.Sequential(
nn.LayerNorm(planes[-1], eps=1e-6),
nn.Linear(planes[-1], num_classes),
),
]
)
),
])
)

# Init all layers
Expand Down
Loading

0 comments on commit af1c020

Please sign in to comment.