Skip to content

Commit

Permalink
style: update ruff settings (#162)
Browse files Browse the repository at this point in the history
* chore: add config for `toml` formatter
* chore: remove unnecessary `shebang` in python files
* chore(deps): add `pyright` dev-dependency
* ci(pre-commit): update `poetry-check` hook args
* ci(pre-commit): update `ruff-pre-commit` hook to `0.2.0`
* docs: split docstring in `cli` function
* fix: adjust typing
* style: update `ruff` settings
* test: add `get_font_env` test
  • Loading branch information
DeadNews authored Feb 3, 2024
1 parent f7f57f3 commit ca58de3
Show file tree
Hide file tree
Showing 18 changed files with 89 additions and 38 deletions.
5 changes: 2 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ repos:
- id: checkmake

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.13
rev: v0.2.0
hooks:
- id: ruff-format
- id: ruff
Expand All @@ -58,5 +58,4 @@ repos:
rev: 1.7.1
hooks:
- id: poetry-check
- id: poetry-lock
args: [--check]
args: [--lock]
2 changes: 2 additions & 0 deletions .taplo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[formatting]
allowed_blank_lines = 1
3 changes: 2 additions & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"recommendations": [
"charliermarsh.ruff",
"ms-python.mypy-type-checker",
"ms-python.python"
"ms-python.python",
"tamasfe.even-better-toml"
]
}
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pc-install:
update-latest:
poetry up --latest

checks: pc-run lint
checks: pc-run install lint

pc-run:
pre-commit run -a
Expand All @@ -21,3 +21,6 @@ lint:

test:
poetry run poe test

pyright:
poetry run poe pyright
50 changes: 49 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 11 additions & 13 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ python-dotenv = "^1.0.0"
[tool.poetry.group.lint.dependencies]
mypy = "^1.8.0"
poethepoet = "^0.24.4"
pyright = "^1.1.349"
ruff = "^0.1.13"

[tool.poetry.group.test.dependencies]
Expand All @@ -44,6 +45,7 @@ style = "semver"

[tool.poe.tasks]
mypy = "mypy ."
pyright = "pyright src"
ruff = "ruff check ."
ruff-fmt = "ruff format ."
lint.sequence = ["ruff", "ruff-fmt", "mypy"]
Expand Down Expand Up @@ -91,28 +93,24 @@ ignore = [
"COM812", # Trailing comma missing
"D203", # 1 blank line required before class docstring
"D212", # Multi-line docstring summary should start at the first line
"E501", # Line too long
"EXE001", # Shebang is present but file is not executable
"FBT001", # Boolean positional arg in function definition
"FBT002", # Boolean default value in function definition
"ISC001", # Checks for implicitly concatenated strings on a single line.
"ISC001", # Checks for implicitly concatenated strings on a single line
"PLR0913", # Too many arguments to function call
#
"S603", # Calling subprocess.Popen with shell=False
]

[tool.ruff.per-file-ignores]
[tool.ruff.lint.per-file-ignores]
"__init__.py" = ["F401"]
"tests/*" = ["ANN", "D", "PLC1901", "PLR2004", "S"]
"tests/*" = ["ANN", "D", "E501", "PLC1901", "PLR2004", "S"]

[tool.ruff.flake8-comprehensions]
allow-dict-calls-with-keyword-arguments = false

[tool.ruff.flake8-tidy-imports]
[tool.ruff.lint.flake8-tidy-imports]
ban-relative-imports = "all"

[tool.ruff.pydocstyle]
convention = "google"

[tool.ruff.pycodestyle]
[tool.ruff.lint.pycodestyle]
max-doc-length = 129
max-line-length = 129

[tool.ruff.lint.pydocstyle]
convention = "google"
1 change: 0 additions & 1 deletion src/images_upload_cli/__init__.py
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/usr/bin/env python
"""Public accessible objects of that module."""

from images_upload_cli.upload import HOSTINGS, UPLOAD
Expand Down
1 change: 0 additions & 1 deletion src/images_upload_cli/__main__.py
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/usr/bin/env python
"""Entrypoint for cli, enables execution with `python -m images_upload_cli`."""
from images_upload_cli.cli import cli

Expand Down
4 changes: 1 addition & 3 deletions src/images_upload_cli/cli.py
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/usr/bin/env python
"""Entrypoint for cli."""

import asyncio
Expand Down Expand Up @@ -41,9 +40,8 @@ def cli(
notify: bool,
clipboard: bool,
) -> None:
"""Upload images via APIs."""
"""
Upload images via APIs.
Args:
images (tuple[Path]): A tuple of `Path` objects representing the paths to the images to be uploaded.
hosting (str): The hosting service to use for image upload.
Expand Down
1 change: 0 additions & 1 deletion src/images_upload_cli/upload.py
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/usr/bin/env python
"""Upload callables."""

from collections.abc import Callable
Expand Down
6 changes: 3 additions & 3 deletions src/images_upload_cli/util.py
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/usr/bin/env python
"""Utils."""

from io import BytesIO
Expand Down Expand Up @@ -79,7 +78,8 @@ def get_img_ext(img: bytes) -> str:
str: The extension of the image file.
"""
with BytesIO(img) as f:
return Image.open(f).format.lower()
ext = Image.open(f).format
return "" if ext is None else ext.lower()


def get_font(size: int = 14) -> ImageFont.FreeTypeFont:
Expand All @@ -93,7 +93,7 @@ def get_font(size: int = 14) -> ImageFont.FreeTypeFont:
An instance of the `ImageFont.FreeTypeFont` class representing the font for captions.
"""
if font_name := getenv("CAPTION_FONT"):
return ImageFont.truetype(font_name, size=size) # pragma: no cover
return ImageFont.truetype(font_name, size=size)

return get_default_font(size)

Expand Down
1 change: 0 additions & 1 deletion tests/__init__.py
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
#!/usr/bin/env python
1 change: 0 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/usr/bin/env python
"""Shared fixtures."""

from pathlib import Path
Expand Down
1 change: 0 additions & 1 deletion tests/mock.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/usr/bin/env python
"""Mock data."""


Expand Down
7 changes: 3 additions & 4 deletions tests/test_bm.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env python
import pytest
from click.testing import CliRunner
from click.testing import CliRunner, Result
from dotenv import load_dotenv
from images_upload_cli.__main__ import cli
from pytest_benchmark.fixture import BenchmarkFixture
Expand Down Expand Up @@ -28,7 +27,7 @@ def test_bm_cli_online(benchmark: BenchmarkFixture, runner: CliRunner):
"""

@benchmark
def result():
def result() -> Result:
"""Measure the execution time of the cli function."""
args = [
"-h",
Expand Down Expand Up @@ -64,7 +63,7 @@ def test_bm_cli(benchmark: BenchmarkFixture, runner: CliRunner, httpx_mock: HTTP
load_dotenv(dotenv_path="tests/data/.env.sample")

@benchmark
def result():
def result() -> Result:
"""Measure the execution time of the cli function."""
args = [
"-h",
Expand Down
1 change: 0 additions & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/usr/bin/env python
import pytest
from click.testing import CliRunner
from images_upload_cli.__main__ import cli
Expand Down
1 change: 0 additions & 1 deletion tests/test_upload.py
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/usr/bin/env python
import pytest
from dotenv import load_dotenv
from httpx import AsyncClient
Expand Down
13 changes: 12 additions & 1 deletion tests/test_util.py
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env python
from os import environ
from platform import system

import pytest
from images_upload_cli.util import GetEnvError, get_env, get_font, get_img_ext, human_size
Expand Down Expand Up @@ -42,6 +42,17 @@ def test_get_font() -> None:
assert isinstance(get_font(), ImageFont.FreeTypeFont)


def test_get_font_env() -> None:
if system() == "Linux":
environ["CAPTION_FONT"] = "DejaVuSerif"
elif system() == "Darwin":
environ["CAPTION_FONT"] = "Helvetica"
elif system() == "Windows":
environ["CAPTION_FONT"] = "arial"

assert isinstance(get_font(), ImageFont.FreeTypeFont)


def test_get_env() -> None:
environ["TEST_KEY_1"] = "test"
assert get_env("TEST_KEY_1") == "test"
Expand Down

0 comments on commit ca58de3

Please sign in to comment.