Skip to content

Commit

Permalink
Merge pull request #3 from airtai/2-include-static-checks
Browse files Browse the repository at this point in the history
Add files, scripts and actions for linting and static analysis
  • Loading branch information
davorrunje authored Sep 27, 2023
2 parents c36a706 + a78caa1 commit b54b74a
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 6 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 ".[static-analysis]"
- 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
8 changes: 6 additions & 2 deletions app/application.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
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")

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

[project.optional-dependencies]
dev = [
lint = [
"black==23.9.1",
"ruff==0.0.291",
"pyupgrade-directories",
]

static-analysis = [
"types-PyYAML",
"types-setuptools",
"types-ujson",
"mypy==1.5.1",
"bandit==1.7.5",
]

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

dev = ["app[lint,static-analysis,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 = true

disallow_incomplete_defs = true
disallow_untyped_decorators = true
disallow_any_unimported = false

[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]
10 changes: 10 additions & 0 deletions scripts/lint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

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

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

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 b54b74a

Please sign in to comment.