Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specify that gettext functions require a LiteralString; other fixes #461

Merged
merged 1 commit into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ repos:
hooks:
- id: reformat-gherkin
files: \.feature$
- repo: https://github.com/shellcheck-py/shellcheck-py
rev: v0.9.0.6
hooks:
- id: shellcheck
args:
- --external-sources
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ ignore_missing_imports = true
show_error_codes = true
warn_unreachable = true
warn_unused_ignores = true
warn_redundant_casts = false
check_untyped_defs = false
warn_redundant_casts = true
check_untyped_defs = true

[tool.pytest.ini_options]
pythonpath = 'src'
Expand Down
7 changes: 4 additions & 3 deletions src/baseframe/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Baseframe init."""

from typing import Callable, cast
from typing import cast

from flask_babel import gettext, lazy_gettext

Expand All @@ -9,15 +9,16 @@
from .blueprint import * # NOQA
from .deprecated import * # NOQA
from .extensions import * # NOQA
from .extensions import GetTextProtocol
from .filters import * # NOQA
from .utils import * # NOQA
from .views import * # NOQA

from . import forms # isort:skip

# Pretend these return str, not Any or LazyString
_ = cast(Callable[..., str], gettext)
__ = cast(Callable[..., str], lazy_gettext)
_ = cast(GetTextProtocol, gettext)
__ = cast(GetTextProtocol, lazy_gettext)

# TODO: baseframe_js and baseframe_css are defined in deprecated.py
# and pending removal after an audit of all apps
Expand Down
17 changes: 15 additions & 2 deletions src/baseframe/extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import os.path
import typing as t
import typing_extensions as te
from datetime import tzinfo
from typing import cast

Expand Down Expand Up @@ -37,6 +38,18 @@
]


class GetTextProtocol(te.Protocol):
"""
Callable protocol for gettext and lazy_gettext.

Specify that the first parameter must always be a literal string, not a variable,
and that the return type is a string (though actually a LazyString).
"""

def __call__(self, string: te.LiteralString, **variables) -> str:
...


DEFAULT_LOCALE = 'en'

networkbar_cache = Cache(with_jinja2_ext=False)
Expand All @@ -52,8 +65,8 @@
baseframe_translations = Domain(
os.path.join(os.path.dirname(__file__), 'translations'), domain='baseframe'
)
_ = cast(t.Callable[..., str], baseframe_translations.gettext)
__ = cast(t.Callable[..., str], baseframe_translations.lazy_gettext)
_ = cast(GetTextProtocol, baseframe_translations.gettext)
__ = cast(GetTextProtocol, baseframe_translations.lazy_gettext)


def get_user_locale() -> str:
Expand Down
6 changes: 4 additions & 2 deletions src/baseframe/forms/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,10 @@
}

InvalidUrlPatterns = t.Iterable[t.Tuple[t.Iterable[t.Any], str]]
AllowedListInit = t.Union[t.Iterable[str], t.Callable[[], t.Iterable[str]], None]
AllowedList = t.Union[t.Iterable[str], None]
AllowedListInit = t.Optional[
t.Union[t.Iterable[str], t.Callable[[], t.Optional[t.Iterable[str]]]]
]
AllowedList = t.Optional[t.Iterable[str]]


def is_empty(value: t.Any) -> bool:
Expand Down