summaryrefslogtreecommitdiff
path: root/babel/util.py
diff options
context:
space:
mode:
Diffstat (limited to 'babel/util.py')
-rw-r--r--babel/util.py33
1 files changed, 20 insertions, 13 deletions
diff --git a/babel/util.py b/babel/util.py
index 0436b9e..f159c33 100644
--- a/babel/util.py
+++ b/babel/util.py
@@ -7,20 +7,26 @@
:copyright: (c) 2013-2022 by the Babel Team.
:license: BSD, see LICENSE for more details.
"""
+from __future__ import annotations
import codecs
import collections
-from datetime import timedelta, tzinfo
import os
import re
import textwrap
+from collections.abc import Generator, Iterable
+from datetime import datetime as datetime_, timedelta, tzinfo
+from typing import IO, Any, TypeVar
+
import pytz as _pytz
+
from babel import localtime
missing = object()
+_T = TypeVar("_T")
-def distinct(iterable):
+def distinct(iterable: Iterable[_T]) -> Generator[_T, None, None]:
"""Yield all items in an iterable collection that are distinct.
Unlike when using sets for a similar effect, the original ordering of the
@@ -44,7 +50,7 @@ PYTHON_MAGIC_COMMENT_re = re.compile(
br'[ \t\f]* \# .* coding[=:][ \t]*([-\w.]+)', re.VERBOSE)
-def parse_encoding(fp):
+def parse_encoding(fp: IO[bytes]) -> str | None:
"""Deduce the encoding of a source file from magic comment.
It does this in the same way as the `Python interpreter`__
@@ -96,7 +102,7 @@ PYTHON_FUTURE_IMPORT_re = re.compile(
r'from\s+__future__\s+import\s+\(*(.+)\)*')
-def parse_future_flags(fp, encoding='latin-1'):
+def parse_future_flags(fp: IO[bytes], encoding: str = 'latin-1') -> int:
"""Parse the compiler flags by :mod:`__future__` from the given Python
code.
"""
@@ -128,7 +134,7 @@ def parse_future_flags(fp, encoding='latin-1'):
return flags
-def pathmatch(pattern, filename):
+def pathmatch(pattern: str, filename: str) -> bool:
"""Extended pathname pattern matching.
This function is similar to what is provided by the ``fnmatch`` module in
@@ -200,7 +206,7 @@ class TextWrapper(textwrap.TextWrapper):
)
-def wraptext(text, width=70, initial_indent='', subsequent_indent=''):
+def wraptext(text: str, width: int = 70, initial_indent: str = '', subsequent_indent: str = '') -> list[str]:
"""Simple wrapper around the ``textwrap.wrap`` function in the standard
library. This version does not wrap lines on hyphens in words.
@@ -224,25 +230,26 @@ odict = collections.OrderedDict
class FixedOffsetTimezone(tzinfo):
"""Fixed offset in minutes east from UTC."""
- def __init__(self, offset, name=None):
+ def __init__(self, offset: float, name: str | None = None) -> None:
+
self._offset = timedelta(minutes=offset)
if name is None:
name = 'Etc/GMT%+d' % offset
self.zone = name
- def __str__(self):
+ def __str__(self) -> str:
return self.zone
- def __repr__(self):
+ def __repr__(self) -> str:
return f'<FixedOffset "{self.zone}" {self._offset}>'
- def utcoffset(self, dt):
+ def utcoffset(self, dt: datetime_) -> timedelta:
return self._offset
- def tzname(self, dt):
+ def tzname(self, dt: datetime_) -> str:
return self.zone
- def dst(self, dt):
+ def dst(self, dt: datetime_) -> timedelta:
return ZERO
@@ -258,5 +265,5 @@ DSTDIFF = localtime.DSTDIFF
ZERO = localtime.ZERO
-def _cmp(a, b):
+def _cmp(a: Any, b: Any):
return (a > b) - (a < b)