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

ci: add action for python linting and formatting #12

Merged
merged 4 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# SPDX-License-Identifier: Apache-2.0

name: Lint, Format, and MyPy

on:
push:
branches:
- "main"
- "release-**"
paths:
- '**.py'
- 'pyproject.toml'
- 'requirements*.txt'
- 'tox.ini'
- 'scripts/*.sh'
- '.github/**'
pull_request:
branches:
- "main"
- "release-**"
paths:
- '**.py'
- 'pyproject.toml'
- 'requirements*.txt'
- 'tox.ini'
- 'scripts/*.sh'
- '.github/**'

env:
PYTHON_VERSION: 3.11

jobs:
lint:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
# https://github.com/actions/checkout/issues/249
fetch-depth: 0
submodules: true

- name: Setup Python 3.11
uses: actions/setup-python@v5
with:
python-version: 3.11
cache: pip
cache-dependency-path: |
**/pyproject.toml
**/requirements*.txt

- name: Install dependencies
id: deps
run: |
python -m pip install --upgrade pip
python -m pip install tox

- name: Run Ruff check
run: |
tox -e ruff -- check

- name: Run linting
if: ${{ !cancelled() && (steps.deps.outcome == 'success') }}
run: |
echo "::add-matcher::.github/workflows/matchers/pylint.json"
tox -e lint

- name: Run mypy type checks
if: ${{ !cancelled() && (steps.deps.outcome == 'success') }}
run: |
tox -e mypy
33 changes: 33 additions & 0 deletions .github/workflows/matchers/pylint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"problemMatcher": [
{
"owner": "pylint-error",
"severity": "error",
"pattern": [
{
"regexp": "^(.+):(\\d+):(\\d+):\\s(([EF]\\d{4}):\\s.+)$",
"file": 1,
"line": 2,
"column": 3,
"message": 4,
"code": 5
}
]
},
{
"owner": "pylint-warning",
"severity": "warning",
"pattern": [
{
"regexp": "^(.+):(\\d+):(\\d+):\\s(([CRW]\\d{4}):\\s.+)$",
"file": 1,
"line": 2,
"column": 3,
"message": 4,
"code": 5
}
]
}
]
}

File renamed without changes.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,5 @@ spellcheck-sort: .spellcheck-en-custom.txt ## Sort spellcheck directory
sort -d -f -o $< $<

.PHONY: verify
verify: check-tox ## Run linting and formatting checks via tox
tox p -e ruff,fastlint,spellcheck
verify: check-tox ## Run linting, typing, and formatting checks via tox
tox p -e fastlint,mypy,ruff
11 changes: 10 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ source = "https://github.com/instructlab/sdg"
issues = "https://github.com/instructlab/sdg/issues"

[tool.setuptools_scm]
version_file = "src/sdg/_version.py"
version_file = "src/instructlab_sdg/_version.py"
# do not include +gREV local version, required for Test PyPI upload
local_scheme = "no-local-version"

Expand Down Expand Up @@ -103,3 +103,12 @@ from-first = true
# import-heading-firstparty=First Party
# import-heading-localfolder=Local
known-local-folder = ["tuning"]

[tool.mypy]
disable_error_code = ["import-not-found", "import-untyped"]
exclude = [
"^src/instructlab_sdg/generate_data\\.py$",
"^src/instructlab_sdg/utils\\.py$",
]
# honor excludes by not following there through imports
follow_imports = "silent"
2 changes: 1 addition & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# SPDX-License-Identifier: Apache-2.0

# TODO: Uncomment below line once requirements.txt is created
# -r requirements.txt
-r requirements.txt

pre-commit>=3.0.4,<4.0
pylint>=2.16.2,<4.0
Expand Down
9 changes: 9 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# SPDX-License-Identifier: Apache-2.0
click>=8.1.7,<9.0.0
httpx>=0.25.0,<1.0.0
jinja2
openai>=1.13.3,<2.0.0
rouge_score
tqdm>=4.66.2,<5.0.0
# TODO: remove 'instructlab' once https://github.com/instructlab/sdg/issues/6 is resolved
instructlab
1 change: 1 addition & 0 deletions src/instructlab_sdg/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
# First Party
from instructlab_sdg.generate_data import generate_data
27 changes: 15 additions & 12 deletions src/instructlab_sdg/generate_data.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
# SPDX-License-Identifier: Apache-2.0

# Standard
from datetime import datetime
from functools import partial
from pathlib import Path
from typing import Optional
import json
import multiprocessing
import os
import random
import re
import string
import time
from datetime import datetime
from functools import partial
from pathlib import Path
from typing import Optional

import click
import tqdm
# instructlab - All of these need to go away - issue #6
from instructlab.config import (DEFAULT_MULTIPROCESSING_START_METHOD,
get_model_family)
from instructlab.utils import (chunk_document, max_seed_example_tokens,
num_chars_from_tokens, read_taxonomy)
# Third Party
# instructlab - All of these need to go away - issue #6
from instructlab.config import DEFAULT_MULTIPROCESSING_START_METHOD, get_model_family
from instructlab.utils import (
chunk_document,
max_seed_example_tokens,
num_chars_from_tokens,
read_taxonomy,
)
from jinja2 import Template
from rouge_score import rouge_scorer
import click
import tqdm

# Local
# First Party
from instructlab_sdg import utils

DEFAULT_PROMPT_TEMPLATE_MERLINITE = """\
Expand Down
6 changes: 3 additions & 3 deletions src/instructlab_sdg/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# SPDX-License-Identifier: Apache-2.0

# Standard
from typing import Optional, Sequence, Union
import copy
import dataclasses
import io
Expand All @@ -9,14 +10,13 @@
import math
import os
import sys
from typing import Optional, Sequence, Union

import httpx
# Third Party
# instructlab - TODO these need to go away, issue #6
from instructlab.config import DEFAULT_API_KEY, DEFAULT_MODEL_OLD
from instructlab.utils import get_sysprompt
# Third Party
from openai import OpenAI, OpenAIError
import httpx

StrOrOpenAIObject = Union[str, object]

Expand Down
12 changes: 11 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[tox]
# py3-unit runs unit tests with 'python3'
# py311-unit runs the same tests with 'python3.11'
envlist = ruff, lint, spellcheck
envlist = ruff, lint, mypy, spellcheck
minversion = 4.4

# format, check, and linting targets don't build and install the project to
Expand Down Expand Up @@ -49,3 +49,13 @@ commands =
sh -c 'command -v aspell || (echo "aspell is not installed. Please install it." && exit 1)'
{envpython} -m pyspelling --config {toxinidir}/.spellcheck.yml --spellchecker aspell
allowlist_externals = sh

[testenv:mypy]
description = Python type checking with mypy
deps =
mypy>=1.10.0,<2.0
types-tqdm
types-PyYAML
pytest
commands =
mypy src