From 86d1768b5bc56695992586bc3609859ad425a018 Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Thu, 19 Jan 2023 20:22:57 +0100 Subject: replace some more :type ...: with type hints --- fietsboek/__init__.py | 6 +++--- fietsboek/config.py | 8 +++----- fietsboek/jinja2.py | 21 ++++++--------------- 3 files changed, 12 insertions(+), 23 deletions(-) diff --git a/fietsboek/__init__.py b/fietsboek/__init__.py index fed720b..423e901 100644 --- a/fietsboek/__init__.py +++ b/fietsboek/__init__.py @@ -14,12 +14,14 @@ Content ------- """ from pathlib import Path +from typing import Optional import importlib_metadata import redis from pyramid.config import Configurator from pyramid.csrf import CookieCSRFStoragePolicy from pyramid.i18n import default_locale_negotiator +from pyramid.request import Request from pyramid.session import SignedCookieSessionFactory from . import config as mod_config @@ -31,7 +33,7 @@ from .security import SecurityPolicy __VERSION__ = importlib_metadata.version("fietsboek") -def locale_negotiator(request): +def locale_negotiator(request: Request) -> Optional[str]: """Negotiates the right locale to use. This tries the following: @@ -43,9 +45,7 @@ def locale_negotiator(request): .. _Accept-Language: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Language :param request: The request for which to get the language. - :type request: pyramid.request.Request :return: The determined locale, or ``None`` if the default should be used. - :rtype: str """ locale = default_locale_negotiator(request) if locale: diff --git a/fietsboek/config.py b/fietsboek/config.py index 54faf39..a5a87a8 100644 --- a/fietsboek/config.py +++ b/fietsboek/config.py @@ -242,13 +242,11 @@ class Config(BaseModel): return hasher.hexdigest() -def parse(config): +def parse(config: dict) -> Config: """Parses the configuration into a :class:`Config`. :param config: The configuration dict to parse. - :type config: dict :return: The parsed (and validated) configuration. - :rtype: Config :raises ValidationError: When the configuration is malformed. """ config = config.copy() @@ -280,7 +278,7 @@ def parse(config): # Now we can parse the main config try: - config = Config.parse_obj(config) + parsed_config = Config.parse_obj(config) except pydantic.ValidationError as validation_error: errors.append(("configuration", validation_error)) @@ -294,7 +292,7 @@ def parse(config): for key in keys: LOGGER.warning("Unknown configuration key: %r", key) - return config + return parsed_config def _field_name(field): diff --git a/fietsboek/jinja2.py b/fietsboek/jinja2.py index 71982e0..a3ca7d8 100644 --- a/fietsboek/jinja2.py +++ b/fietsboek/jinja2.py @@ -5,21 +5,20 @@ import json import jinja2 from babel.dates import format_datetime from babel.numbers import format_decimal +from jinja2.runtime import Context from markupsafe import Markup +from pyramid.request import Request @jinja2.pass_context -def filter_format_decimal(ctx, value): +def filter_format_decimal(ctx: Context, value: float) -> str: """Format a decimal number according to the locale. This uses the right thousands grouping and the right decimal separator. :param ctx: The jinja context, passed automatically. - :type ctx: jinja2.runtime.Context :param value: The value to format. - :type value: float :return: The formatted decimal. - :rtype: str """ request = ctx.get("request") locale = request.localizer.locale_name @@ -27,15 +26,12 @@ def filter_format_decimal(ctx, value): @jinja2.pass_context -def filter_format_datetime(ctx, value): +def filter_format_datetime(ctx: Context, value: datetime.datetime) -> str: """Format a datetime according to the locale. :param ctx: The jinja context, passed automatically. - :type ctx: jinja2.runtime.Context :param value: The value to format. - :type value: datetime.datetime :return: The formatted date. - :rtype: str """ request = ctx.get("request") locale = request.localizer.locale_name @@ -43,7 +39,7 @@ def filter_format_datetime(ctx, value): @jinja2.pass_context -def filter_local_datetime(ctx, value): +def filter_local_datetime(ctx: Context, value: datetime.datetime) -> Markup: """Format a UTC datetime to show in the user's local timezone. This is done by embedding the UTC timestamp in the page, such that we can @@ -53,11 +49,8 @@ def filter_local_datetime(ctx, value): represent the time in the user's timezone, but in UTC. :param ctx: The jinja context, passed automatically. - :type ctx: jinja2.runtime.Context :param value: The value to format. - :type value: datetime.datetime :return: The formatted date. - :rtype: Markup """ # If we get a naive object in, we assume that we got it from the database # and we have to treat it like a UTC-aware object. This happens when we @@ -79,16 +72,14 @@ def filter_local_datetime(ctx, value): ) -def global_embed_tile_layers(request): +def global_embed_tile_layers(request: Request) -> Markup: """Renders the available tile servers for the current user, as a JSON object. The returned value is wrapped as a :class:`~markupsafe.Markup` so that it won't get escaped by jinja. :param request: The Pyramid request. - :type request: pyramid.request.Request :return: The available tile servers. - :rtype: markupsafe.Markup """ # pylint: disable=import-outside-toplevel,cyclic-import from .views import tileproxy -- cgit v1.2.3