Skip to content

Commit

Permalink
Restore annotations in sphinx.locale to objects
Browse files Browse the repository at this point in the history
  • Loading branch information
AA-Turner committed Sep 28, 2022
1 parent 0466fa8 commit 5ed06f5
Showing 1 changed file with 21 additions and 25 deletions.
46 changes: 21 additions & 25 deletions sphinx/locale/__init__.py
Expand Up @@ -3,11 +3,7 @@
import locale
from gettext import NullTranslations, translation
from os import path

if False:
# NoQA
from collections.abc import Callable, Iterable
from typing import Any, Dict, List, Optional, Tuple, Union
from typing import Any, Callable, Dict, Iterable, List, Optional, Tuple, Union


class _TranslationProxy:
Expand All @@ -20,32 +16,32 @@ class _TranslationProxy:
"""
__slots__ = ('_func', '_args')

def __new__(cls, func: 'Callable', *args: str) -> '_TranslationProxy':
def __new__(cls, func: Callable, *args: str) -> '_TranslationProxy':
if not args:
# not called with "function" and "arguments", but a plain string
return str(func) # type: ignore[return-value]
return object.__new__(cls)

def __getnewargs__(self) -> 'Tuple[str]':
def __getnewargs__(self) -> Tuple[str]:
return (self._func,) + self._args # type: ignore

def __init__(self, func: 'Callable', *args: str) -> None:
def __init__(self, func: Callable, *args: str) -> None:
self._func = func
self._args = args

def __str__(self) -> str:
return str(self._func(*self._args))

def __dir__(self) -> 'List[str]':
def __dir__(self) -> List[str]:
return dir(str)

def __getattr__(self, name: str) -> 'Any':
def __getattr__(self, name: str) -> Any:
return getattr(self.__str__(), name)

def __getstate__(self) -> 'Tuple[Callable, Tuple[str, ...]]':
def __getstate__(self) -> Tuple[Callable, Tuple[str, ...]]:
return self._func, self._args

def __setstate__(self, tup: 'Tuple[Callable, Tuple[str]]') -> None:
def __setstate__(self, tup: Tuple[Callable, Tuple[str]]) -> None:
self._func, self._args = tup

def __copy__(self) -> '_TranslationProxy':
Expand All @@ -69,10 +65,10 @@ def __mod__(self, other: str) -> str:
def __rmod__(self, other: str) -> str:
return other % self.__str__()

def __mul__(self, other: 'Any') -> str:
def __mul__(self, other: Any) -> str:
return self.__str__() * other

def __rmul__(self, other: 'Any') -> str:
def __rmul__(self, other: Any) -> str:
return other * self.__str__()

def __hash__(self):
Expand All @@ -94,15 +90,15 @@ def __getitem__(self, index):
return self.__str__()[index]


translators: 'Dict[Tuple[str, str], NullTranslations]' = {}
translators: Dict[Tuple[str, str], NullTranslations] = {}


def init(
locale_dirs: 'List[Optional[str]]',
language: 'Optional[str]',
locale_dirs: List[Optional[str]],
language: Optional[str],
catalog: str = 'sphinx',
namespace: str = 'general',
) -> 'Tuple[NullTranslations, bool]':
) -> Tuple[NullTranslations, bool]:
"""Look for message catalogs in `locale_dirs` and *ensure* that there is at
least a NullTranslations catalog set in `translators`. If called multiple
times or if several ``.mo`` files are found, their contents are merged
Expand All @@ -118,7 +114,7 @@ def init(

if language and '_' in language:
# for language having country code (like "de_AT")
languages: 'Optional[List[str]]' = [language, language.split('_')[0]]
languages: Optional[List[str]] = [language, language.split('_')[0]]
elif language:
languages = [language]
else:
Expand All @@ -143,7 +139,7 @@ def init(
return translator, has_translation


def setlocale(category: int, value: 'Union[str, Iterable[str], None]' = None) -> None:
def setlocale(category: int, value: Union[str, Iterable[str], None] = None) -> None:
"""Update locale settings.
This does not throw any exception even if update fails.
Expand All @@ -166,7 +162,7 @@ def setlocale(category: int, value: 'Union[str, Iterable[str], None]' = None) ->
def init_console(
locale_dir: str = path.abspath(path.dirname(__file__)), # NoQA: B008
catalog: str = 'sphinx',
) -> 'Tuple[NullTranslations, bool]':
) -> Tuple[NullTranslations, bool]:
"""Initialize locale for console.
.. versionadded:: 1.8
Expand Down Expand Up @@ -197,7 +193,7 @@ def _lazy_translate(catalog: str, namespace: str, message: str) -> str:
return translator.gettext(message)


def get_translation(catalog: str, namespace: str = 'general') -> 'Callable[[str], str]':
def get_translation(catalog: str, namespace: str = 'general') -> Callable[[str], str]:
"""Get a translation function based on the *catalog* and *namespace*.
The extension can use this API to translate the messages on the
Expand All @@ -222,7 +218,7 @@ def setup(app):
.. versionadded:: 1.8
"""
def gettext(message: str, *args: 'Any') -> str:
def gettext(message: str, *args: Any) -> str:
if not is_translator_registered(catalog, namespace):
# not initialized yet
return _TranslationProxy(_lazy_translate, catalog, namespace, message) # type: ignore # NOQA
Expand Down Expand Up @@ -260,7 +256,7 @@ def gettext(message: str, *args: 'Any') -> str:
}

# Moved to sphinx.directives.other (will be overridden later)
versionlabels: 'Dict[str, str]' = {}
versionlabels: Dict[str, str] = {}

# Moved to sphinx.domains.python (will be overridden later)
pairindextypes: 'Dict[str, str]' = {}
pairindextypes: Dict[str, str] = {}

0 comments on commit 5ed06f5

Please sign in to comment.