Skip to content

Commit

Permalink
improve types
Browse files Browse the repository at this point in the history
format_time now accepts datetime.time
  • Loading branch information
alex-oleshkevich committed Jan 9, 2023
1 parent 5c09d80 commit 7826fcb
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[tool.poetry]
name = "starlette_babel"
description = "Locale support for Starlette"
version = "0.2.3"
version = "0.2.4"
authors = ["Alex Oleshkevich <[email protected]>"]
license = "MIT"
readme = "README.md"
Expand Down
18 changes: 16 additions & 2 deletions starlette_babel/formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,28 @@ def format_datetime(
return dates.format_datetime(dt, format=format, locale=parse_locale(locale))


def format_date(date: datetime.datetime, format: _DateTimeFormats = "medium", locale: str | None = None) -> str:
def format_date(
date: datetime.date | datetime.datetime, format: _DateTimeFormats = "medium", locale: str | None = None
) -> str:
return dates.format_date(date, format=format, locale=parse_locale(locale))


def format_time(
time: datetime.datetime, format: _DateTimeFormats = "medium", rebase: bool = True, locale: str | None = None
time: datetime.time | datetime.datetime,
format: _DateTimeFormats = "medium",
rebase: bool = True,
locale: str | None = None,
) -> str:
if rebase:
if isinstance(time, datetime.time):
time = datetime.datetime.utcnow().replace(
hour=time.hour,
minute=time.minute,
second=time.second,
microsecond=time.microsecond,
tzinfo=time.tzinfo,
fold=time.fold,
)
time = to_user_timezone(time)
return dates.format_time(time, format=format, locale=parse_locale(locale))

Expand Down
14 changes: 13 additions & 1 deletion starlette_babel/translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,19 @@ def translate(self) -> str:
return typing.cast(str, value)


gettext_lazy = typing.cast(typing.Callable[[str], str], LazyString)
class _GettextLazy(typing.Protocol): # pragma: nocover
def __call__(
self,
msgid: str,
msgid_plural: str | None = None,
count: int | None = None,
domain: str = "messages",
translator: Translator | None = None,
) -> str:
...


gettext_lazy = typing.cast(_GettextLazy, LazyString)


def gettext(
Expand Down
6 changes: 6 additions & 0 deletions tests/test_formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ def test_format_time(
assert format_time(christmas, "long", False) == "12:30:59 UTC"
assert format_time(christmas, "full", False) == "12:30:59, Універсальны каардынаваны час"

naive_time = datetime.time(12, 30, 59)
assert format_time(naive_time, "medium", False) == "12:30:59"


def test_format_time_rebases_timezone(
bel_tz: typing.Generator[None, None, None], bel_locale: typing.Generator[None, None, None]
Expand All @@ -92,6 +95,9 @@ def test_format_time_rebases_timezone(
assert format_time(christmas, "long", True) == "15:30:59 +0300"
assert format_time(christmas, "full", True) == "15:30:59, Маскоўскі стандартны час"

naive_time = datetime.time(12, 30, 59)
assert format_time(naive_time, "medium", True) == "15:30:59"


def test_format_timedelta(
bel_tz: typing.Generator[None, None, None], bel_locale: typing.Generator[None, None, None]
Expand Down

0 comments on commit 7826fcb

Please sign in to comment.