Added mypy to scripts and workflow, and some first annotations for celerywyrm

This commit is contained in:
Joeri de Ruiter 2023-07-22 13:27:43 +02:00
parent 9c5b5d0ac1
commit 75f37d7361
10 changed files with 90 additions and 7 deletions

50
.github/workflows/mypy.yml vendored Normal file
View file

@ -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

View file

@ -1,6 +1,6 @@
from opentelemetry import trace from opentelemetry import trace
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter 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 opentelemetry.sdk.trace.export import BatchSpanProcessor, ConsoleSpanExporter
from bookwyrm import settings from bookwyrm import settings
@ -16,19 +16,19 @@ elif settings.OTEL_EXPORTER_OTLP_ENDPOINT:
) )
def instrumentDjango(): def instrumentDjango() -> None:
from opentelemetry.instrumentation.django import DjangoInstrumentor from opentelemetry.instrumentation.django import DjangoInstrumentor
DjangoInstrumentor().instrument() DjangoInstrumentor().instrument()
def instrumentPostgres(): def instrumentPostgres() -> None:
from opentelemetry.instrumentation.psycopg2 import Psycopg2Instrumentor from opentelemetry.instrumentation.psycopg2 import Psycopg2Instrumentor
Psycopg2Instrumentor().instrument() Psycopg2Instrumentor().instrument()
def instrumentCelery(): def instrumentCelery() -> None:
from opentelemetry.instrumentation.celery import CeleryInstrumentor from opentelemetry.instrumentation.celery import CeleryInstrumentor
from celery.signals import worker_process_init from celery.signals import worker_process_init
@ -37,5 +37,5 @@ def instrumentCelery():
CeleryInstrumentor().instrument() CeleryInstrumentor().instrument()
def tracer(): def tracer() -> Tracer:
return trace.get_tracer(__name__) return trace.get_tracer(__name__)

5
bw-dev
View file

@ -209,6 +209,10 @@ case "$CMD" in
bookwyrm/static/css/bookwyrm.scss bookwyrm/static/css/bookwyrm/**/*.scss --fix \ bookwyrm/static/css/bookwyrm.scss bookwyrm/static/css/bookwyrm/**/*.scss --fix \
--config dev-tools/.stylelintrc.js --config dev-tools/.stylelintrc.js
;; ;;
mypy)
prod_error
runweb mypy celerywyrm bookwyrm
;;
collectstatic_watch) collectstatic_watch)
prod_error prod_error
npm run --prefix dev-tools watch:static npm run --prefix dev-tools watch:static
@ -316,6 +320,7 @@ case "$CMD" in
echo " eslint" echo " eslint"
echo " stylelint" echo " stylelint"
echo " formatters" echo " formatters"
echo " mypy"
echo " collectstatic_watch" echo " collectstatic_watch"
echo " populate_streams [--stream=<stream name>]" echo " populate_streams [--stream=<stream name>]"
echo " populate_lists_streams" echo " populate_lists_streams"

View file

@ -6,7 +6,7 @@ class CelerywyrmConfig(AppConfig):
name = "celerywyrm" name = "celerywyrm"
verbose_name = "BookWyrm Celery" verbose_name = "BookWyrm Celery"
def ready(self): def ready(self) -> None:
if settings.OTEL_EXPORTER_OTLP_ENDPOINT or settings.OTEL_EXPORTER_CONSOLE: if settings.OTEL_EXPORTER_OTLP_ENDPOINT or settings.OTEL_EXPORTER_CONSOLE:
from bookwyrm.telemetry import open_telemetry from bookwyrm.telemetry import open_telemetry

View file

@ -6,7 +6,7 @@ from bookwyrm.settings import *
QUERY_TIMEOUT = env.int("CELERY_QUERY_TIMEOUT", env.int("QUERY_TIMEOUT", 30)) QUERY_TIMEOUT = env.int("CELERY_QUERY_TIMEOUT", env.int("QUERY_TIMEOUT", 30))
# pylint: disable=line-too-long # 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_HOST = env("REDIS_BROKER_HOST", "redis_broker")
REDIS_BROKER_PORT = env.int("REDIS_BROKER_PORT", 6379) REDIS_BROKER_PORT = env.int("REDIS_BROKER_PORT", 6379)
REDIS_BROKER_DB_INDEX = env.int("REDIS_BROKER_DB_INDEX", 0) REDIS_BROKER_DB_INDEX = env.int("REDIS_BROKER_DB_INDEX", 0)

View file

@ -26,6 +26,7 @@ prettier \
eslint \ eslint \
stylelint \ stylelint \
formatters \ formatters \
mypy \
collectstatic_watch \ collectstatic_watch \
populate_streams \ populate_streams \
populate_lists_streams \ populate_lists_streams \

View file

@ -23,6 +23,7 @@ prettier
eslint eslint
stylelint stylelint
formatters formatters
mypy
collectstatic_watch collectstatic_watch
populate_streams populate_streams
populate_lists_streams populate_lists_streams

View file

@ -25,6 +25,7 @@ prettier
eslint eslint
stylelint stylelint
formatters formatters
mypy
collectstatic_watch collectstatic_watch
populate_streams populate_streams
populate_lists_streams populate_lists_streams

15
mypy.ini Normal file
View file

@ -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

View file

@ -43,3 +43,13 @@ pytest-env==0.6.2
pytest-xdist==2.3.0 pytest-xdist==2.3.0
pytidylib==0.3.2 pytidylib==0.3.2
pylint==2.14.0 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