Skip to content

Commit

Permalink
Update type hints for formatter functions
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelhwilliams committed Jul 16, 2024
1 parent 25ac435 commit e3167d6
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 17 deletions.
6 changes: 4 additions & 2 deletions flask_admin/_types.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from typing import Union, Sequence, Dict, Callable
from typing import Union, Sequence, Dict, Callable, Any

import sqlalchemy
from markupsafe import Markup

T_COLUMN_LIST = Sequence[Union[str, sqlalchemy.Column]]
T_FORMATTERS = Dict[type, Callable] # todo: Make this tighter
T_FORMATTER = Callable[[Any, Any, Any], Union[str, Markup]]
T_FORMATTERS = Dict[type, T_FORMATTER]
2 changes: 1 addition & 1 deletion flask_admin/contrib/geoa/typefmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from sqlalchemy import func


def geom_formatter(view, value, name):
def geom_formatter(view, value, name) -> str:
kwargs = {
"data-role": "leaflet",
"disabled": "disabled",
Expand Down
6 changes: 4 additions & 2 deletions flask_admin/contrib/mongoengine/typefmt.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Union

from markupsafe import Markup, escape

from mongoengine.base import BaseList
Expand All @@ -8,7 +10,7 @@
from . import helpers


def grid_formatter(view, value, name):
def grid_formatter(view, value, name) -> Union[str, Markup]:
if not value.grid_id:
return ''

Expand All @@ -26,7 +28,7 @@ def grid_formatter(view, value, name):
})


def grid_image_formatter(view, value, name):
def grid_image_formatter(view, value, name) -> Union[str, Markup]:
if not value.grid_id:
return ''

Expand Down
12 changes: 6 additions & 6 deletions flask_admin/contrib/sqla/typefmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from sqlalchemy.orm.collections import InstrumentedList


def choice_formatter(view, choice, name):
def choice_formatter(view, choice, name) -> str:
"""
Return label of selected choice
see https://sqlalchemy-utils.readthedocs.io/
Expand All @@ -16,7 +16,7 @@ def choice_formatter(view, choice, name):
return choice.value


def arrow_formatter(view, arrow_time, name):
def arrow_formatter(view, arrow_time, name) -> str:
"""
Return human-friendly string of the time relative to now.
see https://arrow.readthedocs.io/
Expand All @@ -27,7 +27,7 @@ def arrow_formatter(view, arrow_time, name):
return arrow_time.humanize()


def arrow_export_formatter(view, arrow_time, name):
def arrow_export_formatter(view, arrow_time, name) -> str:
"""
Return string representation of Arrow object
see https://arrow.readthedocs.io/
Expand All @@ -47,13 +47,13 @@ def arrow_export_formatter(view, arrow_time, name):
})
try:
from sqlalchemy_utils import Choice
DEFAULT_FORMATTERS[Choice] = choice_formatter # type: ignore[assignment]
DEFAULT_FORMATTERS[Choice] = choice_formatter
except ImportError:
pass

try:
from arrow import Arrow
DEFAULT_FORMATTERS[Arrow] = arrow_formatter # type: ignore[assignment]
EXPORT_FORMATTERS[Arrow] = arrow_export_formatter # type: ignore[assignment]
DEFAULT_FORMATTERS[Arrow] = arrow_formatter
EXPORT_FORMATTERS[Arrow] = arrow_export_formatter
except ImportError:
pass
13 changes: 7 additions & 6 deletions flask_admin/model/typefmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from markupsafe import Markup
from flask_admin._compat import text_type
from flask_admin._types import T_FORMATTERS


def null_formatter(view, value, name):
Expand Down Expand Up @@ -38,7 +39,7 @@ def bool_formatter(view, value, name):
return Markup('<span class="fa %s glyphicon glyphicon-%s icon-%s" title="%s"></span>' % (fa, glyph, glyph, label))


def list_formatter(view, values, name):
def list_formatter(view, values, name) -> str:
"""
Return string with comma separated values
Expand All @@ -48,7 +49,7 @@ def list_formatter(view, values, name):
return u', '.join(text_type(v) for v in values)


def enum_formatter(view, value, name):
def enum_formatter(view, value, name) -> str:
"""
Return the name of the enumerated member.
Expand All @@ -58,7 +59,7 @@ def enum_formatter(view, value, name):
return value.name


def dict_formatter(view, value, name):
def dict_formatter(view, value, name) -> str:
"""
Removes unicode entities when displaying dict as string. Also unescapes
non-ASCII characters stored in the JSON.
Expand All @@ -69,20 +70,20 @@ def dict_formatter(view, value, name):
return json.dumps(value, ensure_ascii=False)


BASE_FORMATTERS = {
BASE_FORMATTERS: T_FORMATTERS = {
type(None): empty_formatter,
bool: bool_formatter,
list: list_formatter,
dict: dict_formatter,
}

EXPORT_FORMATTERS = {
EXPORT_FORMATTERS: T_FORMATTERS = {
type(None): empty_formatter,
list: list_formatter,
dict: dict_formatter,
}

DETAIL_FORMATTERS = {
DETAIL_FORMATTERS: T_FORMATTERS = {
type(None): empty_formatter,
list: list_formatter,
dict: dict_formatter,
Expand Down

0 comments on commit e3167d6

Please sign in to comment.