Skip to content

Commit

Permalink
Merge pull request #2916 from jderuiter/mypy-integration
Browse files Browse the repository at this point in the history
mypy integration
  • Loading branch information
mouse-reeve authored Jul 23, 2023
2 parents 9c5b5d0 + 75f37d7 commit 07aca2f
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 7 deletions.
50 changes: 50 additions & 0 deletions .github/workflows/mypy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: Mypy

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Set up Python 3.9
uses: actions/setup-python@v4
with:
python-version: 3.9
- name: Install Dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Analysing the code with mypy
env:
SECRET_KEY: beepbeep
DEBUG: false
USE_HTTPS: true
DOMAIN: your.domain.here
BOOKWYRM_DATABASE_BACKEND: postgres
MEDIA_ROOT: images/
POSTGRES_PASSWORD: hunter2
POSTGRES_USER: postgres
POSTGRES_DB: github_actions
POSTGRES_HOST: 127.0.0.1
CELERY_BROKER: ""
REDIS_BROKER_PORT: 6379
REDIS_BROKER_PASSWORD: beep
USE_DUMMY_CACHE: true
FLOWER_PORT: 8888
EMAIL_HOST: "smtp.mailgun.org"
EMAIL_PORT: 587
EMAIL_HOST_USER: ""
EMAIL_HOST_PASSWORD: ""
EMAIL_USE_TLS: true
ENABLE_PREVIEW_IMAGES: false
ENABLE_THUMBNAIL_GENERATION: true
HTTP_X_FORWARDED_PROTO: false
run: |
mypy bookwyrm celerywyrm
10 changes: 5 additions & 5 deletions bookwyrm/telemetry/open_telemetry.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from opentelemetry import trace
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace import TracerProvider, Tracer
from opentelemetry.sdk.trace.export import BatchSpanProcessor, ConsoleSpanExporter

from bookwyrm import settings
Expand All @@ -16,19 +16,19 @@
)


def instrumentDjango():
def instrumentDjango() -> None:
from opentelemetry.instrumentation.django import DjangoInstrumentor

DjangoInstrumentor().instrument()


def instrumentPostgres():
def instrumentPostgres() -> None:
from opentelemetry.instrumentation.psycopg2 import Psycopg2Instrumentor

Psycopg2Instrumentor().instrument()


def instrumentCelery():
def instrumentCelery() -> None:
from opentelemetry.instrumentation.celery import CeleryInstrumentor
from celery.signals import worker_process_init

Expand All @@ -37,5 +37,5 @@ def init_celery_tracing(*args, **kwargs):
CeleryInstrumentor().instrument()


def tracer():
def tracer() -> Tracer:
return trace.get_tracer(__name__)
5 changes: 5 additions & 0 deletions bw-dev
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,10 @@ case "$CMD" in
bookwyrm/static/css/bookwyrm.scss bookwyrm/static/css/bookwyrm/**/*.scss --fix \
--config dev-tools/.stylelintrc.js
;;
mypy)
prod_error
runweb mypy celerywyrm bookwyrm
;;
collectstatic_watch)
prod_error
npm run --prefix dev-tools watch:static
Expand Down Expand Up @@ -316,6 +320,7 @@ case "$CMD" in
echo " eslint"
echo " stylelint"
echo " formatters"
echo " mypy"
echo " collectstatic_watch"
echo " populate_streams [--stream=<stream name>]"
echo " populate_lists_streams"
Expand Down
2 changes: 1 addition & 1 deletion celerywyrm/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class CelerywyrmConfig(AppConfig):
name = "celerywyrm"
verbose_name = "BookWyrm Celery"

def ready(self):
def ready(self) -> None:
if settings.OTEL_EXPORTER_OTLP_ENDPOINT or settings.OTEL_EXPORTER_CONSOLE:
from bookwyrm.telemetry import open_telemetry

Expand Down
2 changes: 1 addition & 1 deletion celerywyrm/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
QUERY_TIMEOUT = env.int("CELERY_QUERY_TIMEOUT", env.int("QUERY_TIMEOUT", 30))

# pylint: disable=line-too-long
REDIS_BROKER_PASSWORD = requests.utils.quote(env("REDIS_BROKER_PASSWORD", ""))
REDIS_BROKER_PASSWORD = requests.compat.quote(env("REDIS_BROKER_PASSWORD", ""))
REDIS_BROKER_HOST = env("REDIS_BROKER_HOST", "redis_broker")
REDIS_BROKER_PORT = env.int("REDIS_BROKER_PORT", 6379)
REDIS_BROKER_DB_INDEX = env.int("REDIS_BROKER_DB_INDEX", 0)
Expand Down
1 change: 1 addition & 0 deletions complete_bwdev.fish
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ prettier \
eslint \
stylelint \
formatters \
mypy \
collectstatic_watch \
populate_streams \
populate_lists_streams \
Expand Down
1 change: 1 addition & 0 deletions complete_bwdev.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ prettier
eslint
stylelint
formatters
mypy
collectstatic_watch
populate_streams
populate_lists_streams
Expand Down
1 change: 1 addition & 0 deletions complete_bwdev.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ prettier
eslint
stylelint
formatters
mypy
collectstatic_watch
populate_streams
populate_lists_streams
Expand Down
15 changes: 15 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[mypy]
plugins = mypy_django_plugin.main
namespace_packages = True
strict = True

[mypy.plugins.django-stubs]
django_settings_module = "bookwyrm.settings"

[mypy-bookwyrm.*]
ignore_errors = True
implicit_reexport = True

[mypy-celerywyrm.*]
ignore_errors = False

10 changes: 10 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,13 @@ pytest-env==0.6.2
pytest-xdist==2.3.0
pytidylib==0.3.2
pylint==2.14.0
mypy==1.4.1
celery-types==0.18.0
django-stubs[compatible-mypy]==4.2.3
types-bleach==6.0.0.3
types-dataclasses==0.6.6
types-Markdown==3.4.2.9
types-Pillow==10.0.0.1
types-psycopg2==2.9.21.10
types-python-dateutil==2.8.19.13
types-requests==2.31.0.1

0 comments on commit 07aca2f

Please sign in to comment.