Skip to content

Commit

Permalink
Switch to Ruff for Python linting
Browse files Browse the repository at this point in the history
Ruff is a new all-in-one Python linter that combines several previous
tools and implements them more quickly. This PR switches to Ruff and
cleans up various parts of the code that Ruff has flagged.
  • Loading branch information
akprasad authored Apr 1, 2023
1 parent 19c09e6 commit d9f32ae
Show file tree
Hide file tree
Showing 41 changed files with 1,778 additions and 1,865 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/basic-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
- name: Install Python3
uses: actions/setup-python@v4
with:
python-version: '3.9.x'
python-version: '3.10.x'
cache: 'pip'
cache-dependency-path: requirements.txt

Expand Down Expand Up @@ -59,7 +59,7 @@ jobs:
- name: Install Python3
uses: actions/setup-python@v4
with:
python-version: '3.9.x'
python-version: '3.10.x'
cache: 'pip'

- name: Install dependencies
Expand Down
23 changes: 4 additions & 19 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -181,29 +181,14 @@ docker-logs:
# Lint commands
# ===============================================

# Check imports in Python code
lint-isort:
@echo "Running Python isort to organize module imports"
@git ls-files '*.py' | xargs isort --check 2>&1

# Check formatting in Python code
lint-black:
@echo "Running Python Black to check formatting"
@git ls-files '*.py' | xargs black 2>&1

# Check Python code complyies with PEP8
lint-flake8:
@echo "Running Python flake8 to conform with PEP8"
@git ls-files '*.py' | xargs flake8 --config=./.flake8 2>&1

# Link checks on Python code
py-lint: py-venv-check lint-black lint-isort lint-flake8
@echo "Python lint completed"
py-lint: py-venv-check
ruff . --fix
black .

# Lint our Python and JavaScript code. Fail on any issues.
lint-check: js-lint py-lint
lint-check: js-lint
black . --diff
@echo 'Lint completed'


# Test, coverage and documentation commands
Expand Down
2 changes: 1 addition & 1 deletion ambuda/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def index(self):
# Abort so that a malicious scraper can't infer that there's an
# interesting page here.
abort(404)
return super(AmbudaIndexView, self).index()
return super().index()


class BaseView(sqla.ModelView):
Expand Down
3 changes: 1 addition & 2 deletions ambuda/auth.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Manages the auth/authentication data flow."""

from http import HTTPStatus
from typing import Optional

from flask import abort, redirect, request, url_for
from flask_login import LoginManager
Expand All @@ -11,7 +10,7 @@
from ambuda.utils.user_mixins import AmbudaAnonymousUser


def _load_user(user_id: int) -> Optional[User]:
def _load_user(user_id: int) -> User | None:
"""Load a user from the database.
Flask-Login uses this function to populate the `current_user` variable.
Expand Down
3 changes: 1 addition & 2 deletions ambuda/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
from sqlalchemy import create_engine, inspect
from sqlalchemy.schema import Column

from ambuda import consts
from ambuda import consts, enums
from ambuda import database as db
from ambuda import enums
from ambuda import queries as q
from ambuda.models.base import Base

Expand Down
21 changes: 10 additions & 11 deletions ambuda/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"""

import functools
from typing import Optional

from flask import current_app
from sqlalchemy import create_engine
Expand Down Expand Up @@ -66,7 +65,7 @@ def page_statuses() -> list[db.PageStatus]:
return session.query(db.PageStatus).all()


def text(slug: str) -> Optional[db.Text]:
def text(slug: str) -> db.Text | None:
session = get_session()
return (
session.query(db.Text)
Expand Down Expand Up @@ -99,17 +98,17 @@ def text_meta(slug: str) -> db.Text:
)


def text_section(text_id: int, slug: str) -> Optional[db.TextSection]:
def text_section(text_id: int, slug: str) -> db.TextSection | None:
session = get_session()
return session.query(db.TextSection).filter_by(text_id=text_id, slug=slug).first()


def block(text_id: int, slug: str) -> Optional[db.TextBlock]:
def block(text_id: int, slug: str) -> db.TextBlock | None:
session = get_session()
return session.query(db.TextBlock).filter_by(text_id=text_id, slug=slug).first()


def block_parse(block_id: int) -> Optional[db.BlockParse]:
def block_parse(block_id: int) -> db.BlockParse | None:
session = get_session()
return session.query(db.BlockParse).filter_by(block_id=block_id).first()

Expand Down Expand Up @@ -153,17 +152,17 @@ def projects() -> list[db.Project]:
return session.query(db.Project).all()


def project(slug: str) -> Optional[db.Project]:
def project(slug: str) -> db.Project | None:
session = get_session()
return session.query(db.Project).filter(db.Project.slug == slug).first()


def thread(*, id: int) -> Optional[db.Thread]:
def thread(*, id: int) -> db.Thread | None:
session = get_session()
return session.query(db.Thread).filter_by(id=id).first()


def post(*, id: int) -> Optional[db.Post]:
def post(*, id: int) -> db.Post | None:
session = get_session()
return session.query(db.Post).filter_by(id=id).first()

Expand Down Expand Up @@ -197,7 +196,7 @@ def create_post(*, board_id: int, thread: db.Thread, user_id: int, content: str)
session.commit()


def page(project_id, page_slug: str) -> Optional[db.Page]:
def page(project_id, page_slug: str) -> db.Page | None:
session = get_session()
return (
session.query(db.Page)
Expand All @@ -206,7 +205,7 @@ def page(project_id, page_slug: str) -> Optional[db.Page]:
)


def user(username: str) -> Optional[db.User]:
def user(username: str) -> db.User | None:
session = get_session()
return (
session.query(db.User)
Expand All @@ -233,7 +232,7 @@ def create_user(*, username: str, email: str, raw_password: str) -> db.User:
return user


def blog_post(slug: str) -> Optional[db.BlogPost]:
def blog_post(slug: str) -> db.BlogPost | None:
"""Fetch the given blog post."""
session = get_session()
return session.query(db.BlogPost).filter_by(slug=slug).first()
Expand Down
2 changes: 1 addition & 1 deletion ambuda/scripts/analysis/dcs_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"""

import re
from collections.abc import Iterator
from dataclasses import dataclass
from typing import Iterator

import conllu
from indic_transliteration import sanscript
Expand Down
2 changes: 1 addition & 1 deletion ambuda/scripts/analysis/mahabharata.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""Add the Mahabharata parse data from DCS."""


from collections.abc import Iterator
from pathlib import Path
from typing import Iterator

import ambuda.scripts.analysis.dcs_utils as dcs
from ambuda.scripts.analysis.ramayana import get_kanda_and_sarga, map_and_write
Expand Down
2 changes: 1 addition & 1 deletion ambuda/scripts/analysis/ramayana.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""Add the Ramayana parse data from DCS."""

import xml.etree.ElementTree as ET
from collections.abc import Iterator
from pathlib import Path
from typing import Iterator

from indic_transliteration import sanscript
from sqlalchemy.orm import Session
Expand Down
5 changes: 2 additions & 3 deletions ambuda/scripts/analysis/single_file_text.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""Add parse data from DCS for a simple text."""

import argparse
from collections.abc import Iterator
from pathlib import Path
from typing import Iterator

from sqlalchemy.orm import Session

Expand All @@ -23,8 +23,7 @@ def iter_sections(dcs_text_name):
/ "files"
/ f"{dcs_text_name}-all.conllu"
)
for section in dcs.parse_file(text_path):
yield section
yield from dcs.parse_file(text_path)


def iter_parsed_blocks(dcs_text_name) -> Iterator[tuple[str, str]]:
Expand Down
6 changes: 3 additions & 3 deletions ambuda/seed/dcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
DATA_DIR = PROJECT_DIR / "data" / "ambuda-dcs"


class UpdateException(Exception):
class UpdateError(Exception):
pass


Expand Down Expand Up @@ -80,7 +80,7 @@ def add_parse_data(text_slug: str, path: Path):
with Session(engine) as session:
text = session.query(db.Text).filter_by(slug=text_slug).first()
if not text:
raise UpdateException()
raise UpdateError()

drop_existing_parse_data(session, text.id)

Expand All @@ -102,7 +102,7 @@ def run():
try:
add_parse_data(path.stem, path)
log(f"- Added {path.stem} parse data to the database.")
except UpdateException:
except UpdateError:
log(f"- Skipped {path.stem}.")
skipped.append(path.stem)

Expand Down
2 changes: 1 addition & 1 deletion ambuda/seed/dictionaries/amarakosha.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"""

import re
from typing import Iterator
from collections.abc import Iterator

import click
from indic_transliteration import sanscript
Expand Down
2 changes: 1 addition & 1 deletion ambuda/seed/dictionaries/apte.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def _make_compounds(first_word, groups):
for group in groups:
child = group[0]
# Case 1: simple and well-formed
if re.fullmatch("\w+", child.text):
if re.fullmatch(r"\w+", child.text):
samasa = sandhi_utils.apply(first_word, child.text)
group[0].text = first_word + "\u2014" + group[0].text
yield samasa, group
Expand Down
2 changes: 1 addition & 1 deletion ambuda/seed/dictionaries/apte_sanskrit_hindi.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"""

import xml.etree.ElementTree as ET
from typing import Iterator
from collections.abc import Iterator

import click
from indic_transliteration import sanscript
Expand Down
8 changes: 4 additions & 4 deletions ambuda/seed/dictionaries/shabdartha_kaustubha.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"""

import re
from typing import Iterator
from collections.abc import Iterator

import click
from indic_transliteration import sanscript
Expand All @@ -41,9 +41,9 @@ def create_entries(key: str, body: str) -> Iterator[tuple[str, str]]:
body = re.sub(r"\[(.*)\]", r"<lb/><b>\1</b>", body)

# Per Vishvas, '|' divides headwords.
for key in key.split("|"):
key = standardize_key(key)
yield key, f"<s>{body}</s>"
for k in key.split("|"):
k = standardize_key(k)
yield k, f"<s>{body}</s>"


def sak_generator(dict_blob: str):
Expand Down
2 changes: 1 addition & 1 deletion ambuda/seed/lookup/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ def run():
"Error: Failed to create page statuses, "
"create roles, and creat bot user."
f"Error: {ex}"
)
) from ex
6 changes: 4 additions & 2 deletions ambuda/seed/lookup/create_bot_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
def _create_bot_user(session):
try:
password = os.environ["AMBUDA_BOT_PASSWORD"]
except KeyError:
raise ValueError("Please set the AMBUDA_BOT_PASSWORD environment variable.")
except KeyError as e:
raise ValueError(
"Please set the AMBUDA_BOT_PASSWORD environment variable."
) from e

user = db.User(username=consts.BOT_USERNAME, email="[email protected]")
user.set_password(password)
Expand Down
2 changes: 1 addition & 1 deletion ambuda/seed/texts/gretil.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def run():
for spec in ALLOW:
add_document(engine, spec)
except Exception as ex:
raise Exception("Error: Failed to get latest from GRETIL. " f"Error: {ex}")
raise Exception("Error: Failed to get latest from GRETIL.") from ex

log("Done.")

Expand Down
2 changes: 1 addition & 1 deletion ambuda/seed/utils/itihasa_utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/env python3
"""Database utility functions."""

from collections.abc import Iterator
from dataclasses import dataclass
from pathlib import Path
from typing import Iterator

from dotenv import load_dotenv
from indic_transliteration import sanscript
Expand Down
9 changes: 5 additions & 4 deletions ambuda/tasks/ocr.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Background tasks for proofing projects."""

from typing import Optional

from celery import group
from celery.result import GroupResult
Expand Down Expand Up @@ -53,8 +52,10 @@ def _run_ocr_for_page_inner(
version=0,
author_id=bot_user.id,
)
except Exception:
raise ValueError(f'OCR failed for page "{project.slug}/{page.slug}".')
except Exception as e:
raise ValueError(
f'OCR failed for page "{project.slug}/{page.slug}".'
) from e


@app.task(bind=True)
Expand All @@ -75,7 +76,7 @@ def run_ocr_for_page(
def run_ocr_for_project(
app_env: str,
project: db.Project,
) -> Optional[GroupResult]:
) -> GroupResult | None:
"""Create a `group` task to run OCR on a project.
Usage:
Expand Down
2 changes: 1 addition & 1 deletion ambuda/utils/auth.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from collections.abc import Callable
from functools import wraps
from typing import Callable

from flask import current_app
from flask_login import current_user
Expand Down
Loading

0 comments on commit d9f32ae

Please sign in to comment.