Skip to content

Commit

Permalink
ruff rules added
Browse files Browse the repository at this point in the history
  • Loading branch information
davorrunje committed Apr 5, 2024
1 parent 9a1b568 commit 0f6e04f
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 46 deletions.
4 changes: 2 additions & 2 deletions .devcontainer/cuda-tf/devcontainer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "cuda-tf",
"name": "cuda-11.8",
"image": "mcr.microsoft.com/devcontainers/python:3.9",
// "hostRequirements": {
// "gpu": "optional"
Expand All @@ -22,7 +22,7 @@
"userGid": "1000"
// "upgradePackages": "true"
},
"ghcr.io/devcontainers/features/python:1": {},
// "ghcr.io/devcontainers/features/python:1": {},
// "ghcr.io/devcontainers/features/node:1": "none",
"ghcr.io/devcontainers/features/git:1": {
"version": "latest",
Expand Down
21 changes: 19 additions & 2 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ jobs:
strategy:
matrix:
python-version: ["3.9", "3.10", "3.11"]
tf-version: ["2.10.1", "2.11.1", "2.12.1", "2.13.1", "2.14.1", "2.15.0.post1", "2.16.0rc0"]
tf-version: ["2.10.1", "2.11.1", "2.12.1", "2.13.1", "2.14.1", "2.15.0.post1", "2.16.0"]
fail-fast: false

steps:
Expand Down Expand Up @@ -152,11 +152,28 @@ jobs:
name: coverage-html
path: htmlcov

# https://github.com/marketplace/actions/alls-green#why
pre-commit-check:
runs-on: ubuntu-latest
env:
SKIP: "static-analysis"
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.9"
# - name: Set $PY environment variable
# run: echo "PY=$(python -VV | sha256sum | cut -d' ' -f1)" >> $GITHUB_ENV
# - uses: actions/cache@v4
# with:
# path: ~/.cache/pre-commit
# key: pre-commit|${{ env.PY }}|${{ hashFiles('.pre-commit-config.yaml') }}
- uses: pre-commit/[email protected]

check: # This job does nothing and is only used for the branch protection
if: github.event.pull_request.draft == false

needs:
- pre-commit-check
- static_analysis
- coverage-combine
- test-macos-latest
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,4 @@ repos:
hooks:
- id: detect-secrets
args: ['--baseline', '.secrets.baseline']
exclude: package.lock.json
exclude: "nbs"
2 changes: 2 additions & 0 deletions airt/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""Airt neural network library."""

from .__about__ import __version__

__all__ = ["__version__"]
Expand Down
1 change: 1 addition & 0 deletions airt/keras/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Keras related code."""
4 changes: 4 additions & 0 deletions airt/keras/layers/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
"""Keras layers."""


__all__: list[str] = []
# __all__ = ("MonoDenseLayer",)
31 changes: 18 additions & 13 deletions docs/docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from create_api_docs import create_api_docs
from expand_markdown import expand_markdown
from mkdocs.config import load_config
from update_releases import find_metablock, update_release_notes
from update_releases import _find_metablock, _update_release_notes

IGNORE_DIRS = ("assets", "stylesheets")

Expand All @@ -39,25 +39,25 @@
DEV_SERVER = str(config.get("dev_addr", "0.0.0.0:8008"))


def get_missing_translation(lng: str) -> Path:
def _get_missing_translation(lng: str) -> Path:
return DOCS_DIR / lng / "helpful" / "missing-translation.md"


def get_in_progress(lng: str) -> Path:
def _get_in_progress(lng: str) -> Path:
return DOCS_DIR / lng / "helpful" / "in-progress.md"


app = typer.Typer()


def get_default_title(file: Path) -> str:
def _get_default_title(file: Path) -> str:
title = file.stem.upper().replace("-", " ")
if title == "INDEX":
title = get_default_title(file.parent)
title = _get_default_title(file.parent)
return title


def join_nested(root: Path, path: str) -> Path:
def _join_nested(root: Path, path: str) -> Path:
for i in path.split("/"):
root = root / i
return _touch_file(root)
Expand Down Expand Up @@ -91,6 +91,7 @@ def preview() -> None:

@app.command()
def live(port: Annotated[Optional[str], typer.Argument()] = None) -> None:
"""Serve mkdocs with live reload."""
dev_server = f"0.0.0.0:{port}" if port else DEV_SERVER

typer.echo("Serving mkdocs with live reload")
Expand All @@ -100,18 +101,20 @@ def live(port: Annotated[Optional[str], typer.Argument()] = None) -> None:

@app.command()
def build() -> None:
"""Build the docs."""
_build()


@app.command()
def add(path: Annotated[str, typer.Argument(...)]) -> None:
"""Add a new file to all languages."""
title = ""

exists = []
not_exists = []

for i in LANGUAGES_DIRS:
file = join_nested(i, path)
file = _join_nested(i, path)

if file.exists():
exists.append(i)
Expand All @@ -123,23 +126,24 @@ def add(path: Annotated[str, typer.Argument(...)]) -> None:
else:
not_exists.append(i)
file.write_text(
f"# {title or get_default_title(file)} \n"
"{! " + str(get_in_progress(i.name)) + " !}"
f"# {title or _get_default_title(file)} \n"
"{! " + str(_get_in_progress(i.name)) + " !}"
)
typer.echo(f"{file} - write `in progress`")

if len(exists):
for i in not_exists:
file = i / path
file.write_text(
f"# {title or get_default_title(file)} \n"
"{! " + str(get_missing_translation(i.name)) + " !}"
f"# {title or _get_default_title(file)} \n"
"{! " + str(_get_missing_translation(i.name)) + " !}"
)
typer.echo(f"{file} - write `missing translation`")


@app.command()
def rm(path: Annotated[str, typer.Argument(...)]) -> None:
"""Remove a file from all languages."""
delete = typer.confirm("Are you sure you want to delete files?")
if not delete:
typer.echo("Not deleting")
Expand All @@ -164,6 +168,7 @@ def mv(
path: Annotated[str, typer.Argument(...)],
new_path: Annotated[str, typer.Argument(...)],
) -> None:
"""Move a file to a new path in all languages."""
for i in LANGUAGES_DIRS:
file = i / path
if file.exists():
Expand Down Expand Up @@ -201,7 +206,7 @@ def update_contributing() -> None:

existing_content = CONTRIBUTING_PATH.read_text()

_, content = find_metablock(existing_content.splitlines())
_, content = _find_metablock(existing_content.splitlines())

relative_path = EN_CONTRIBUTING_PATH.relative_to(BASE_DIR.parent)

Expand Down Expand Up @@ -229,7 +234,7 @@ def _build() -> None:
update_contributing()

typer.echo("Updating Release Notes")
update_release_notes(realease_notes_path=EN_DOCS_DIR / "release.md")
_update_release_notes(realease_notes_path=EN_DOCS_DIR / "release.md")

subprocess.run(["mkdocs", "build", "--site-dir", BUILD_DIR], check=True)

Expand Down
19 changes: 14 additions & 5 deletions docs/expand_markdown.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""Expand markdown files with embedded line references."""

import logging
import re
from pathlib import Path
Expand All @@ -11,7 +13,7 @@
app = typer.Typer()


def read_lines_from_file(file_path: Path, lines_spec: Optional[str]) -> str:
def _read_lines_from_file(file_path: Path, lines_spec: Optional[str]) -> str:
"""Read lines from a file.
Args:
Expand Down Expand Up @@ -45,7 +47,7 @@ def read_lines_from_file(file_path: Path, lines_spec: Optional[str]) -> str:
return "".join(selected_lines)


def extract_lines(embedded_line: str) -> str:
def _extract_lines(embedded_line: str) -> str:
to_expand_path_elements = re.search("{!>(.*)!}", embedded_line).group(1).strip() # type: ignore[union-attr]
lines_spec = ""
if "[ln:" in to_expand_path_elements:
Expand All @@ -60,14 +62,21 @@ def extract_lines(embedded_line: str) -> str:
else:
raise ValueError("Couldn't find docs_src directory")

return read_lines_from_file(base_path / to_expand_path_elements, lines_spec)
return _read_lines_from_file(base_path / to_expand_path_elements, lines_spec)


@app.command()
def expand_markdown(
input_markdown_path: Path = typer.Argument(...), # noqa: B008
output_markdown_path: Path = typer.Argument(...), # noqa: B008
) -> None:
"""Expand markdown files with embedded line references.
Args:
input_markdown_path: The path to the input markdown file.
output_markdown_path: The path to the output markdown file.
"""
with input_markdown_path.open() as input_file, output_markdown_path.open(
"w"
) as output_file:
Expand All @@ -77,10 +86,10 @@ def expand_markdown(
# Write the line to the output file
output_file.write(line)
else:
output_file.write(extract_lines(embedded_line=line))
output_file.write(_extract_lines(embedded_line=line))


def remove_lines_between_dashes(file_path: Path) -> None:
def _remove_lines_between_dashes(file_path: Path) -> None:
with file_path.open() as file:
lines = file.readlines()

Expand Down
26 changes: 14 additions & 12 deletions docs/update_releases.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
"""Update the release notes with the latest version and changelog from GitHub releases."""

import re
from collections.abc import Sequence
from pathlib import Path

import requests


def find_metablock(lines: list[str]) -> tuple[list[str], list[str]]:
def _find_metablock(lines: list[str]) -> tuple[list[str], list[str]]:
if lines[0] != "---":
return [], lines

Expand All @@ -17,15 +19,15 @@ def find_metablock(lines: list[str]) -> tuple[list[str], list[str]]:
return lines[:index], lines[index:]


def find_header(lines: list[str]) -> tuple[str, list[str]]:
def _find_header(lines: list[str]) -> tuple[str, list[str]]:
for i in range(len(lines)):
if (line := lines[i]).startswith("#"):
return line, lines[i + 1 :]

return "", lines


def get_github_releases() -> Sequence[tuple[str, str]]:
def _get_github_releases() -> Sequence[tuple[str, str]]:
# Get the latest version from GitHub releases
response = requests.get("https://api.github.com/repos/airtai/airt/releases")
return (
Expand All @@ -35,7 +37,7 @@ def get_github_releases() -> Sequence[tuple[str, str]]:
)


def convert_links_and_usernames(text: str) -> str:
def _convert_links_and_usernames(text: str) -> str:
if "](" not in text:
# Convert HTTP/HTTPS links
text = re.sub(
Expand All @@ -52,29 +54,29 @@ def convert_links_and_usernames(text: str) -> str:
return text


def collect_already_published_versions(text: str) -> list[str]:
def _collect_already_published_versions(text: str) -> list[str]:
data: list[str] = re.findall(r"## (\d.\d.\d.*)", text)
return data


def update_release_notes(realease_notes_path: Path) -> None:
def _update_release_notes(realease_notes_path: Path) -> None:
# Get the changelog from the RELEASE.md file
changelog = realease_notes_path.read_text()

metablockx, lines = find_metablock(changelog.splitlines())
metablockx, lines = _find_metablock(changelog.splitlines())
metablock = "\n".join(metablockx)

header, changelogx = find_header(lines)
header, changelogx = _find_header(lines)
changelog = "\n".join(changelogx)

old_versions = collect_already_published_versions(changelog)
old_versions = _collect_already_published_versions(changelog)

for version, body in filter(
lambda v: v[0] not in old_versions,
get_github_releases(),
_get_github_releases(),
):
body = body.replace("##", "###")
body = convert_links_and_usernames(body)
body = _convert_links_and_usernames(body)
version_changelog = f"## {version}\n\n{body}\n\n"
changelog = version_changelog + changelog

Expand All @@ -93,4 +95,4 @@ def update_release_notes(realease_notes_path: Path) -> None:

if __name__ == "__main__":
base_dir = Path(__file__).resolve().parent
update_release_notes(base_dir / "docs" / "en" / "release.md")
_update_release_notes(base_dir / "docs" / "en" / "release.md")
1 change: 1 addition & 0 deletions examples/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Examples."""
23 changes: 12 additions & 11 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ authors = [

keywords = ["python"]

requires-python = ">=3.9,<3.12"
requires-python = ">=3.9,<3.13"

classifiers = [
"Development Status :: 5 - Production/Stable",
Expand All @@ -24,6 +24,7 @@ classifiers = [
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Operating System :: OS Independent",
"Topic :: Internet",
"Topic :: Software Development :: Libraries :: Application Frameworks",
Expand Down Expand Up @@ -167,20 +168,20 @@ select = [
ignore = [
"E501", # line too long, handled by formatter later
"C901", # too complex
"D418", # Function decorated with `@overload` shouldn't contain a docstring
# "D418", # Function decorated with `@overload` shouldn't contain a docstring

# todo pep8-naming
"N817", # CamelCase `*` imported as acronym `*`
"N815", # Variable `*` in class scope should not be mixedCase
"N803", # Argument name `expandMessageExamples` should be lowercase
# "N817", # CamelCase `*` imported as acronym `*`
# "N815", # Variable `*` in class scope should not be mixedCase
# "N803", # Argument name `expandMessageExamples` should be lowercase

# todo pydocstyle
"D100", # missing docstring in public module
"D102",
"D103",
"D104", # missing docstring in public package
"D105",
"D106", # Missing docstring in public nested class
# "D100", # missing docstring in public module
# "D102",
# "D103",
# "D104", # missing docstring in public package
# "D105",
# "D106", # Missing docstring in public nested class
]

[tool.ruff.lint.isort]
Expand Down

0 comments on commit 0f6e04f

Please sign in to comment.