Skip to content

Commit

Permalink
Merge pull request #28 from geoadmin/feat-pb-1056-tighten-mypy
Browse files Browse the repository at this point in the history
PB-1056 Tighten type checking rules
  • Loading branch information
msom authored Oct 10, 2024
2 parents fa123ac + ed8bc3b commit 60f04a7
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 17 deletions.
4 changes: 3 additions & 1 deletion app/config/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
from ninja import NinjaAPI
from provider.api import router as provider_router

from django.http.request import HttpRequest

root = NinjaAPI()
api = NinjaAPI()


@root.get('/checker')
def checker(request):
def checker(request: HttpRequest) -> dict[str, bool | str]:
return {"success": True, "message": " OK"}


Expand Down
2 changes: 1 addition & 1 deletion app/config/settings_prod.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


# Read configuration from file
def get_logging_config():
def get_logging_config() -> dict[str, object]:
'''Read logging configuration
Read and parse the yaml logging configuration file passed in the environment variable
LOGGING_CFG and return it as dictionary
Expand Down
4 changes: 2 additions & 2 deletions app/distributions/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@


@admin.register(Attribution)
class AttributionAdmin(admin.ModelAdmin):
class AttributionAdmin(admin.ModelAdmin): # type:ignore[type-arg]
'''Admin View for Attribution'''


@admin.register(Dataset)
class DatasetAdmin(admin.ModelAdmin):
class DatasetAdmin(admin.ModelAdmin): # type:ignore[type-arg]
'''Admin View for Dataset'''
14 changes: 10 additions & 4 deletions app/distributions/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from utils.language import get_language
from utils.language import get_translation

from django.db.models import QuerySet
from django.http import HttpRequest
from django.shortcuts import get_object_or_404

Expand Down Expand Up @@ -45,7 +46,11 @@ def attribution_to_response(model: Attribution, lang: LanguageCode) -> Attributi


@router.get("attributions/{attribution_id}", response={200: AttributionSchema}, exclude_none=True)
def attribution(request: HttpRequest, attribution_id: int, lang: LanguageCode | None = None):
def attribution(
request: HttpRequest,
attribution_id: int,
lang: LanguageCode | None = None
) -> AttributionSchema:
"""
Get the attribution with the given ID, return translatable fields in the given language.
Expand Down Expand Up @@ -98,7 +103,8 @@ def attribution(request: HttpRequest, attribution_id: int, lang: LanguageCode |


@router.get("attributions", response={200: AttributionListSchema}, exclude_none=True)
def attributions(request: HttpRequest, lang: LanguageCode | None = None):
def attributions(request: HttpRequest,
lang: LanguageCode | None = None) -> dict[str, list[AttributionSchema]]:
"""
Get all attributions, return translatable fields in the given language.
Expand All @@ -113,7 +119,7 @@ def attributions(request: HttpRequest, lang: LanguageCode | None = None):


@router.get("datasets/{dataset_id}", response={200: DatasetSchema}, exclude_none=True)
def dataset(request: HttpRequest, dataset_id: int):
def dataset(request: HttpRequest, dataset_id: int) -> Dataset:
"""
Get the dataset with the given ID.
"""
Expand All @@ -122,7 +128,7 @@ def dataset(request: HttpRequest, dataset_id: int):


@router.get("datasets", response={200: DatasetListSchema}, exclude_none=True)
def datasets(request: HttpRequest):
def datasets(request: HttpRequest) -> dict[str, QuerySet[Dataset]]:
"""
Get all datasets.
Expand Down
2 changes: 1 addition & 1 deletion app/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import sys


def main():
def main() -> None:
"""Run administrative tasks."""
# default to the prod settings. Can be overwritten with .env
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings_prod')
Expand Down
2 changes: 1 addition & 1 deletion app/provider/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@


@admin.register(Provider)
class ProviderAdmin(admin.ModelAdmin):
class ProviderAdmin(admin.ModelAdmin): # type:ignore[type-arg]
'''Admin View for Provider'''
6 changes: 4 additions & 2 deletions app/provider/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ def provider_to_response(model: Provider, lang: LanguageCode) -> ProviderSchema:


@router.get("/{provider_id}", response={200: ProviderSchema}, exclude_none=True)
def provider(request: HttpRequest, provider_id: int, lang: LanguageCode | None = None):
def provider(
request: HttpRequest, provider_id: int, lang: LanguageCode | None = None
) -> ProviderSchema:
"""
Get the provider with the given ID, return translatable fields in the given language.
Expand Down Expand Up @@ -93,7 +95,7 @@ def provider(request: HttpRequest, provider_id: int, lang: LanguageCode | None =


@router.get("/", response={200: ProviderListSchema}, exclude_none=True)
def providers(request: HttpRequest, lang: LanguageCode | None = None):
def providers(request: HttpRequest, lang: LanguageCode | None = None) -> ProviderListSchema:
"""
Get all providers, return translatable fields in the given language.
Expand Down
13 changes: 8 additions & 5 deletions app/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
import os

from gunicorn.app.base import BaseApplication

from gunicorn.config import Config
from django.core.handlers.wsgi import WSGIHandler
from django.core.wsgi import get_wsgi_application

# Here we cannot uses `from django.conf import settings` because it breaks the `make gunicornserver`
Expand All @@ -42,20 +43,22 @@

class StandaloneApplication(BaseApplication): # pylint: disable=abstract-method

def __init__(self, app, options=None): # pylint: disable=redefined-outer-name
cfg: Config

def __init__(self, app: WSGIHandler, options: dict[str, object] | None = None) -> None: # pylint: disable=redefined-outer-name
self.options = options or {}
self.application = app
super().__init__()

def load_config(self):
def load_config(self) -> None:
config = {
key: value for key,
value in self.options.items() if key in self.cfg.settings and value is not None
}
for key, value in config.items():
self.cfg.set(key.lower(), value)

def load(self):
def load(self) -> WSGIHandler:
return self.application


Expand All @@ -72,4 +75,4 @@ def load(self):
'timeout': 60,
'logconfig_dict': get_logging_config()
}
StandaloneApplication(application, options).run()
StandaloneApplication(application, options).run() # type:ignore[no-untyped-call]
17 changes: 17 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,23 @@ mypy_path = ./app,./app/stubs
plugins =
mypy_django_plugin.main
explicit_package_bases = True
exclude = ^.*[/\\]test_.*

# add some additional checks, but not quite strict
strict = True
implicit_reexport = True
warn_unreachable = True
untyped_calls_exclude = gunicorn

[mypy.plugins.django-stubs]
django_settings_module = "config.settings_dev"

# relax rules for stubs
[mypy-environ.*]
disallow_untyped_defs = False
disallow_incomplete_defs = False

[mypy-gunicorn.*]
disallow_untyped_defs = False
disallow_incomplete_defs = False
ignore_missing_imports = True

0 comments on commit 60f04a7

Please sign in to comment.