Skip to content

Commit

Permalink
Python: new pre-commit actions and pre-commit as GHA (microsoft#6376)
Browse files Browse the repository at this point in the history
### Motivation and Context

<!-- Thank you for your contribution to the semantic-kernel repo!
Please help reviewers and future users, providing the following
information:
  1. Why is this change required?
  2. What problem does it solve?
  3. What scenario does it contribute to?
  4. If it fixes an open issue, please link to the issue here.
-->

- Added static code checking to pre-commit (check-ast and
nbqa-check-ast)
- Added Bandit security checking to pre-commit
- Added pre-commit step to python-lint workflow, if it works, can delete
seperate mypy, ruff and black tests

### Description

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->

### Contribution Checklist

<!-- Before submitting this PR, please make sure: -->

- [x] The code builds clean without any errors or warnings
- [x] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [x] All unit tests pass, and I have added new tests where possible
- [x] I didn't break anyone 😄
  • Loading branch information
eavanvalkenburg authored May 28, 2024
1 parent d8674bd commit 9e99933
Show file tree
Hide file tree
Showing 190 changed files with 2,088 additions and 2,173 deletions.
55 changes: 5 additions & 50 deletions .github/workflows/python-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,67 +7,22 @@ on:
- 'python/**'

jobs:
ruff:
pre-commit:
if: '!cancelled()'
strategy:
fail-fast: false
matrix:
python-version: ["3.10"]
runs-on: ubuntu-latest
timeout-minutes: 5
continue-on-error: true
steps:
- run: echo "/root/.local/bin" >> $GITHUB_PATH
- uses: actions/checkout@v4
- name: Install poetry
run: pipx install poetry
- uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
cache: "poetry"
- name: Install Semantic Kernel
run: cd python && poetry install --no-ansi
- name: Run ruff
run: cd python && poetry run ruff check .
black:
if: '!cancelled()'
strategy:
fail-fast: false
matrix:
python-version: ["3.10"]
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- run: echo "/root/.local/bin" >> $GITHUB_PATH
- uses: actions/checkout@v4
- name: Install poetry
run: pipx install poetry
- uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
cache: "poetry"
- name: Install Semantic Kernel
run: cd python && poetry install --no-ansi
- name: Run black
run: cd python && poetry run black --check .
mypy:
if: '!cancelled()'
strategy:
fail-fast: false
matrix:
python-version: ["3.10"]
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- run: echo "/root/.local/bin" >> $GITHUB_PATH
- uses: actions/checkout@v4
- name: Install poetry
run: pipx install poetry
- uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
cache: "poetry"
- name: Install Semantic Kernel
run: cd python && poetry install --no-ansi
- name: Run mypy
run: cd python && poetry run mypy -p semantic_kernel --config-file=mypy.ini

- name: Install dependencies
run: cd python && poetry install
- uses: pre-commit/[email protected]
6 changes: 5 additions & 1 deletion .github/workflows/python-test-coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ jobs:
python-tests-coverage:
name: Create Test Coverage Messages
runs-on: ${{ matrix.os }}
continue-on-error: true
permissions:
pull-requests: write
contents: read
Expand All @@ -21,14 +20,17 @@ jobs:
os: [ubuntu-latest]
steps:
- name: Wait for unit tests to succeed
continue-on-error: true
uses: lewagon/[email protected]
with:
ref: ${{ github.event.pull_request.head.sha }}
check-name: 'Python Unit Tests (${{ matrix.python-version}}, ${{ matrix.os }})'
repo-token: ${{ secrets.GH_ACTIONS_PR_WRITE }}
wait-interval: 10
allowed-conclusions: success
- uses: actions/checkout@v4
- name: Download coverage
continue-on-error: true
uses: dawidd6/action-download-artifact@v3
with:
name: python-coverage-${{ matrix.os }}-${{ matrix.python-version }}.txt
Expand All @@ -37,6 +39,7 @@ jobs:
search_artifacts: true
if_no_artifact_found: warn
- name: Download pytest
continue-on-error: true
uses: dawidd6/action-download-artifact@v3
with:
name: pytest-${{ matrix.os }}-${{ matrix.python-version }}.xml
Expand All @@ -45,6 +48,7 @@ jobs:
search_artifacts: true
if_no_artifact_found: warn
- name: Pytest coverage comment
continue-on-error: true
id: coverageComment
uses: MishaKav/pytest-coverage-comment@main
with:
Expand Down
32 changes: 26 additions & 6 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,37 @@ repos:
- id: sync_with_poetry
args: [--config=.pre-commit-config.yaml, --db=python/.conf/packages_list.json, python/poetry.lock]
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.0.1
rev: v4.6.0
hooks:
- id: check-toml
files: \.toml$
- id: check-yaml
files: \.yaml$
- id: check-json
files: \.json$
exclude: ^python\/\.vscode\/.*
- id: end-of-file-fixer
files: \.py$
- id: mixed-line-ending
files: \.py$
- repo: https://github.com/psf/black
rev: 24.4.2
- id: debug-statements
files: ^python\/semantic_kernel\/.*\.py$
- id: check-ast
name: Check Valid Python Samples
types: ["python"]
- repo: https://github.com/nbQA-dev/nbQA
rev: 1.8.5
hooks:
- id: black
files: \.py$
- id: nbqa-check-ast
name: Check Valid Python Notebooks
types: ["jupyter"]
- repo: https://github.com/asottile/pyupgrade
rev: v3.15.2
hooks:
- id: pyupgrade
args: [--py310-plus]
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.4.4
rev: v0.4.5
hooks:
- id: ruff
args: [ --fix, --exit-non-zero-on-fix ]
Expand All @@ -36,3 +50,9 @@ repos:
language: system
types: [python]
pass_filenames: false
- repo: https://github.com/PyCQA/bandit
rev: 1.7.8
hooks:
- id: bandit
args: ["-c", "python/pyproject.toml"]
additional_dependencies: [ "bandit[toml]" ]
3 changes: 2 additions & 1 deletion python/.vscode/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
// See https://go.microsoft.com/fwlink/?LinkId=827846
// for the documentation about the extensions.json format
"recommendations": [
"littlefoxteam.vscode-python-test-adapter",
"streetsidesoftware.code-spell-checker",
"ms-python.python",
"charliermarsh.ruff",
"rodolphebarbanneau.python-docstring-highlighter"
]
}
1 change: 0 additions & 1 deletion python/.vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true,
"pythonTestExplorer.testFramework": "pytest",
"[python]": {
"editor.codeActionsOnSave": {
"source.organizeImports": "explicit",
Expand Down
3 changes: 1 addition & 2 deletions python/.vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@
"command": "poetry",
"args": [
"install",
"--extras",
"all"
"--all-extras"
],
"presentation": {
"reveal": "silent",
Expand Down
Loading

0 comments on commit 9e99933

Please sign in to comment.