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

Use ruff for linting and code formatting #1019

Closed
wants to merge 10 commits into from
35 changes: 8 additions & 27 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,23 +1,6 @@
LARGE_TEST_REPO_DIR:=tests/test_docs_large
GIT_DESC=$$(git describe)
JUNIT_FLAGS := --capture=sys -o junit_logging=all

.PHONY: lint-black
lint-black: ## check python source code formatting issues, with black
black --check --diff ./

.PHONY: lint-black-apply
lint-black-apply: ## apply black's source code formatting suggestions
black ./

.PHONY: lint-isort
lint-isort: ## check imports are organized, with isort
isort --check --diff ./

.PHONY: lint-isort-apply
lint-isort-apply: ## apply isort's imports organization suggestions
isort ./

MYPY_ARGS := --ignore-missing-imports \
--disallow-incomplete-defs \
--disallow-untyped-defs \
Expand All @@ -26,19 +9,17 @@ MYPY_ARGS := --ignore-missing-imports \
--warn-unused-ignores \
--exclude $(LARGE_TEST_REPO_DIR)/*.py

mypy-host:
.PHONY: lint
lint: ## Check the code for linting, formatting, and typing issues with ruff and mypy
ruff check
ruff format --check
mypy $(MYPY_ARGS) dangerzone

mypy-tests:
mypy $(MYPY_ARGS) tests

mypy: mypy-host mypy-tests ## check type hints with mypy

.PHONY: lint
lint: lint-black lint-isort mypy ## check the code with various linters

.PHONY: lint-apply
format: lint-black-apply lint-isort-apply ## apply all the linter's suggestions
.PHONY: fix
fix: ## apply all the suggestions from ruff
ruff check --fix
ruff format

.PHONY: test
test:
Expand Down
1 change: 0 additions & 1 deletion dangerzone/isolation_provider/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import subprocess
import sys
from abc import ABC, abstractmethod
from pathlib import Path
from typing import IO, Callable, Iterator, Optional

import fitz
Expand Down
2 changes: 1 addition & 1 deletion dangerzone/logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def convert_doc(document: Document) -> None:
ocr_lang,
stdout_callback,
)
except Exception as e:
except Exception:
log.exception(
f"Unexpected error occurred while converting '{document}'"
)
Expand Down
1 change: 0 additions & 1 deletion dev_scripts/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import shutil
import subprocess
import sys
import urllib.request
from datetime import date

DEFAULT_GUI = True
Expand Down
4 changes: 2 additions & 2 deletions dev_scripts/sign-assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ def main():
parser.add_argument(
"--version",
required=True,
help=f"look for assets with this Dangerzone version",
help="look for assets with this Dangerzone version",
)
parser.add_argument(
"dir",
help=f"look for assets in this directory",
help="look for assets in this directory",
)
args = parser.parse_args()
setup_logging()
Expand Down
2 changes: 1 addition & 1 deletion install/windows/build-wxs.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def build_components_xml(root, data):
Guid=subdata["component_guid"],
)
for filename in subdata["files"]:
file_el = ET.SubElement(
ET.SubElement(
component_el, "File", Source=filename, Id="file_" + uuid.uuid4().hex
)

Expand Down
454 changes: 216 additions & 238 deletions poetry.lock

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ pyinstaller = {version = "*", platform = "darwin"}

# Dependencies required for linting the code.
[tool.poetry.group.lint.dependencies]
black = "*"
isort = "*"
click = "*" # Install click so mypy is able to reason about it.
mypy = "*"
ruff = "*"
types-PySide2 = "*"
types-Markdown = "*"
types-requests = "*"
Expand All @@ -60,11 +60,11 @@ pymupdf = "1.24.11" # Last version to support python 3.8 (needed for Ubuntu Foca
[tool.poetry.group.dev.dependencies]
httpx = "^0.27.2"

[tool.isort]
profile = "black"
skip_gitignore = true
# This is necessary due to https://github.com/PyCQA/isort/issues/1835
follow_links = false
[tool.ruff.lint]
select = [
# isort
"I",
]

[build-system]
requires = ["poetry-core>=1.2.0"]
Expand Down
67 changes: 37 additions & 30 deletions tests/gui/test_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,19 @@ def test_order_mime_handers() -> None:
"LibreOffice",
]

with mock.patch(
"subprocess.check_output", return_value=b"libreoffice-draw.desktop"
) as mock_default_mime_hander, mock.patch(
"os.listdir",
side_effect=[
["org.gnome.Evince.desktop"],
["org.pwmt.zathura-pdf-mupdf.desktop"],
["libreoffice-draw.desktop"],
],
) as mock_list, mock.patch(
"dangerzone.gui.logic.DesktopEntry", return_value=mock_desktop
with (
mock.patch(
"subprocess.check_output", return_value=b"libreoffice-draw.desktop"
) as mock_default_mime_hander,
mock.patch(
"os.listdir",
side_effect=[
["org.gnome.Evince.desktop"],
["org.pwmt.zathura-pdf-mupdf.desktop"],
["libreoffice-draw.desktop"],
],
) as mock_list,
mock.patch("dangerzone.gui.logic.DesktopEntry", return_value=mock_desktop),
):
dz = DangerzoneGui(mock_app, dummy)

Expand Down Expand Up @@ -77,18 +79,20 @@ def test_mime_handers_succeeds_no_default_found() -> None:
"LibreOffice",
]

with mock.patch(
"subprocess.check_output",
side_effect=subprocess.CalledProcessError(1, "Oh no, xdg-mime error!)"),
) as mock_default_mime_hander, mock.patch(
"os.listdir",
side_effect=[
["org.gnome.Evince.desktop"],
["org.pwmt.zathura-pdf-mupdf.desktop"],
["libreoffice-draw.desktop"],
],
) as mock_list, mock.patch(
"dangerzone.gui.logic.DesktopEntry", return_value=mock_desktop
with (
mock.patch(
"subprocess.check_output",
side_effect=subprocess.CalledProcessError(1, "Oh no, xdg-mime error!)"),
) as mock_default_mime_hander,
mock.patch(
"os.listdir",
side_effect=[
["org.gnome.Evince.desktop"],
["org.pwmt.zathura-pdf-mupdf.desktop"],
["libreoffice-draw.desktop"],
],
) as mock_list,
mock.patch("dangerzone.gui.logic.DesktopEntry", return_value=mock_desktop),
):
dz = DangerzoneGui(mock_app, dummy)

Expand All @@ -109,13 +113,16 @@ def test_malformed_desktop_entry_is_catched() -> None:
mock_app = mock.MagicMock()
dummy = mock.MagicMock()

with mock.patch("dangerzone.gui.logic.DesktopEntry") as mock_desktop, mock.patch(
"os.listdir",
side_effect=[
["malformed.desktop", "another.desktop"],
[],
[],
],
with (
mock.patch("dangerzone.gui.logic.DesktopEntry") as mock_desktop,
mock.patch(
"os.listdir",
side_effect=[
["malformed.desktop", "another.desktop"],
[],
[],
],
),
):
mock_desktop.side_effect = ParsingError("Oh noes!", "malformed.desktop")
DangerzoneGui(mock_app, dummy)
Expand Down
1 change: 0 additions & 1 deletion tests/gui/test_main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

from pytest import MonkeyPatch, fixture
from pytest_mock import MockerFixture
from pytest_subprocess import FakeProcess
from pytestqt.qtbot import QtBot

from dangerzone.document import Document
Expand Down