Skip to content

Commit 75f37d7

Browse files
author
Joeri de Ruiter
committed
Added mypy to scripts and workflow, and some first annotations for celerywyrm
1 parent 9c5b5d0 commit 75f37d7

File tree

10 files changed

+90
-7
lines changed

10 files changed

+90
-7
lines changed

.github/workflows/mypy.yml

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
name: Mypy
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
build:
11+
12+
runs-on: ubuntu-latest
13+
14+
steps:
15+
- uses: actions/checkout@v3
16+
- name: Set up Python 3.9
17+
uses: actions/setup-python@v4
18+
with:
19+
python-version: 3.9
20+
- name: Install Dependencies
21+
run: |
22+
python -m pip install --upgrade pip
23+
pip install -r requirements.txt
24+
- name: Analysing the code with mypy
25+
env:
26+
SECRET_KEY: beepbeep
27+
DEBUG: false
28+
USE_HTTPS: true
29+
DOMAIN: your.domain.here
30+
BOOKWYRM_DATABASE_BACKEND: postgres
31+
MEDIA_ROOT: images/
32+
POSTGRES_PASSWORD: hunter2
33+
POSTGRES_USER: postgres
34+
POSTGRES_DB: github_actions
35+
POSTGRES_HOST: 127.0.0.1
36+
CELERY_BROKER: ""
37+
REDIS_BROKER_PORT: 6379
38+
REDIS_BROKER_PASSWORD: beep
39+
USE_DUMMY_CACHE: true
40+
FLOWER_PORT: 8888
41+
EMAIL_HOST: "smtp.mailgun.org"
42+
EMAIL_PORT: 587
43+
EMAIL_HOST_USER: ""
44+
EMAIL_HOST_PASSWORD: ""
45+
EMAIL_USE_TLS: true
46+
ENABLE_PREVIEW_IMAGES: false
47+
ENABLE_THUMBNAIL_GENERATION: true
48+
HTTP_X_FORWARDED_PROTO: false
49+
run: |
50+
mypy bookwyrm celerywyrm

bookwyrm/telemetry/open_telemetry.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from opentelemetry import trace
22
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
3-
from opentelemetry.sdk.trace import TracerProvider
3+
from opentelemetry.sdk.trace import TracerProvider, Tracer
44
from opentelemetry.sdk.trace.export import BatchSpanProcessor, ConsoleSpanExporter
55

66
from bookwyrm import settings
@@ -16,19 +16,19 @@
1616
)
1717

1818

19-
def instrumentDjango():
19+
def instrumentDjango() -> None:
2020
from opentelemetry.instrumentation.django import DjangoInstrumentor
2121

2222
DjangoInstrumentor().instrument()
2323

2424

25-
def instrumentPostgres():
25+
def instrumentPostgres() -> None:
2626
from opentelemetry.instrumentation.psycopg2 import Psycopg2Instrumentor
2727

2828
Psycopg2Instrumentor().instrument()
2929

3030

31-
def instrumentCelery():
31+
def instrumentCelery() -> None:
3232
from opentelemetry.instrumentation.celery import CeleryInstrumentor
3333
from celery.signals import worker_process_init
3434

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

3939

40-
def tracer():
40+
def tracer() -> Tracer:
4141
return trace.get_tracer(__name__)

bw-dev

+5
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,10 @@ case "$CMD" in
209209
bookwyrm/static/css/bookwyrm.scss bookwyrm/static/css/bookwyrm/**/*.scss --fix \
210210
--config dev-tools/.stylelintrc.js
211211
;;
212+
mypy)
213+
prod_error
214+
runweb mypy celerywyrm bookwyrm
215+
;;
212216
collectstatic_watch)
213217
prod_error
214218
npm run --prefix dev-tools watch:static
@@ -316,6 +320,7 @@ case "$CMD" in
316320
echo " eslint"
317321
echo " stylelint"
318322
echo " formatters"
323+
echo " mypy"
319324
echo " collectstatic_watch"
320325
echo " populate_streams [--stream=<stream name>]"
321326
echo " populate_lists_streams"

celerywyrm/apps.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class CelerywyrmConfig(AppConfig):
66
name = "celerywyrm"
77
verbose_name = "BookWyrm Celery"
88

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

celerywyrm/settings.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
QUERY_TIMEOUT = env.int("CELERY_QUERY_TIMEOUT", env.int("QUERY_TIMEOUT", 30))
77

88
# pylint: disable=line-too-long
9-
REDIS_BROKER_PASSWORD = requests.utils.quote(env("REDIS_BROKER_PASSWORD", ""))
9+
REDIS_BROKER_PASSWORD = requests.compat.quote(env("REDIS_BROKER_PASSWORD", ""))
1010
REDIS_BROKER_HOST = env("REDIS_BROKER_HOST", "redis_broker")
1111
REDIS_BROKER_PORT = env.int("REDIS_BROKER_PORT", 6379)
1212
REDIS_BROKER_DB_INDEX = env.int("REDIS_BROKER_DB_INDEX", 0)

complete_bwdev.fish

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ prettier \
2626
eslint \
2727
stylelint \
2828
formatters \
29+
mypy \
2930
collectstatic_watch \
3031
populate_streams \
3132
populate_lists_streams \

complete_bwdev.sh

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ prettier
2323
eslint
2424
stylelint
2525
formatters
26+
mypy
2627
collectstatic_watch
2728
populate_streams
2829
populate_lists_streams

complete_bwdev.zsh

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ prettier
2525
eslint
2626
stylelint
2727
formatters
28+
mypy
2829
collectstatic_watch
2930
populate_streams
3031
populate_lists_streams

mypy.ini

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[mypy]
2+
plugins = mypy_django_plugin.main
3+
namespace_packages = True
4+
strict = True
5+
6+
[mypy.plugins.django-stubs]
7+
django_settings_module = "bookwyrm.settings"
8+
9+
[mypy-bookwyrm.*]
10+
ignore_errors = True
11+
implicit_reexport = True
12+
13+
[mypy-celerywyrm.*]
14+
ignore_errors = False
15+

requirements.txt

+10
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,13 @@ pytest-env==0.6.2
4343
pytest-xdist==2.3.0
4444
pytidylib==0.3.2
4545
pylint==2.14.0
46+
mypy==1.4.1
47+
celery-types==0.18.0
48+
django-stubs[compatible-mypy]==4.2.3
49+
types-bleach==6.0.0.3
50+
types-dataclasses==0.6.6
51+
types-Markdown==3.4.2.9
52+
types-Pillow==10.0.0.1
53+
types-psycopg2==2.9.21.10
54+
types-python-dateutil==2.8.19.13
55+
types-requests==2.31.0.1

0 commit comments

Comments
 (0)