Skip to content

Commit

Permalink
Add files, scripts and actions for linting and static analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
kumaranvpl committed Sep 27, 2023
1 parent c36a706 commit 9f2668c
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 5 deletions.
23 changes: 22 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,29 @@ name: CI
on: [workflow_dispatch, pull_request, push]

jobs:
build:
static_analysis:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v4
with:
python-version: 3.8
- name: Install Dependencies and library
shell: bash
run: |
set -ux
python -m pip install --upgrade pip
pip install -e ".[lint]"
- name: Run mypy
shell: bash
run: mypy app

- name: Run bandit
shell: bash
run: bandit -c pyproject.toml -r app

test:
needs: [static_analysis]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ venv.bak/
.dmypy.json
dmypy.json

# ruff
.ruff_cache/

# Pyre type checker
.pyre/

Expand Down
6 changes: 5 additions & 1 deletion app/application.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
from pydantic import BaseModel, Field
from faststream import FastStream, Logger
from faststream.kafka import KafkaBroker
from pydantic import BaseModel, Field


class Name(BaseModel):
name: str = Field(..., description="Name of the person")


class Greeting(BaseModel):
greeting: str = Field(..., description="Greeting message")


broker = KafkaBroker("localhost:9092")
app = FastStream(broker)

to_greetings = broker.publisher("greetings")


@broker.subscriber("names")
async def on_names(msg: Name, logger: Logger) -> None:
result = f"hello {msg.name}"
Expand Down
64 changes: 63 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,68 @@ dependencies = [
]

[project.optional-dependencies]
dev = [
lint = [
"mypy==1.5.1",
"black==23.9.1",
"isort>=5",
"ruff==0.0.291",
"pyupgrade-directories",
"bandit==1.7.5",
]

testing = [
"faststream[kafka, testing]>=0.1.3",
]

dev = ["app[lint,testing]"]

[tool.mypy]
strict = true
python_version = "3.8"
ignore_missing_imports = true
install_types = true
non_interactive = true

# from https://blog.wolt.com/engineering/2021/09/30/professional-grade-mypy-configuration/
disallow_untyped_defs = true
no_implicit_optional = true
check_untyped_defs = true
warn_return_any = true
show_error_codes = true
warn_unused_ignores = false

disallow_incomplete_defs = true
disallow_untyped_decorators = true
disallow_any_unimported = false

[tool.isort]
profile = "black"
known_first_party = ["app"]

[tool.black]
line-length = 88
target-version = ['py38']
include = '\.pyi?$'

[tool.ruff]
fix = true
line-length = 88
target-version = "py38"
select = [
"E", # pycodestyle errors
"W", # pycodestyle warnings
"F", # pyflakes
"I", # isort
"C", # flake8-comprehensions
"B", # flake8-bugbear
"Q", # flake8-quotes
]
ignore = [
"E501", # line too long, handled by black
"C901", # too complex
]

[tool.ruff.pydocstyle]
convention = "google"

[tool.bandit]
13 changes: 13 additions & 0 deletions scripts/lint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

echo "Running pyup_dirs..."
pyup_dirs --py38-plus --recursive app tests

echo "Running ruff..."
ruff app tests --fix

echo "Running isort..."
isort app tests

echo "Running black..."
black app tests
Empty file modified scripts/start_kafka_broker_locally.sh
100644 → 100755
Empty file.
8 changes: 8 additions & 0 deletions scripts/static-analysis.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash
set -e

echo "Running mypy..."
mypy app

echo "Running bandit..."
bandit -c pyproject.toml -r app
Empty file modified scripts/stop_kafka_broker_locally.sh
100644 → 100755
Empty file.
Empty file modified scripts/subscribe_to_kafka_broker_locally.sh
100644 → 100755
Empty file.
3 changes: 1 addition & 2 deletions tests/test_application.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import pytest

from faststream.kafka import TestKafkaBroker

from app.application import Name, Greeting, broker, on_names
from app.application import Greeting, Name, broker, on_names


@broker.subscriber("greetings")
Expand Down

0 comments on commit 9f2668c

Please sign in to comment.