Skip to content

Commit 363cdc0

Browse files
committed
Refactor locale._TranslationProxy
1 parent 6dbb618 commit 363cdc0

File tree

1 file changed

+50
-54
lines changed

1 file changed

+50
-54
lines changed

sphinx/locale/__init__.py

Lines changed: 50 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,98 +2,94 @@
22

33
import gettext
44
import locale
5-
from collections import UserString, defaultdict
5+
from collections import defaultdict
66
from gettext import NullTranslations
77
from os import path
88
from typing import Any, Callable, Dict, Iterable, List, Optional, Tuple, Union
99

1010

11-
class _TranslationProxy(UserString):
11+
class _TranslationProxy:
1212
"""
1313
Class for proxy strings from gettext translations. This is a helper for the
1414
lazy_* functions from this module.
1515
1616
The proxy implementation attempts to be as complete as possible, so that
1717
the lazy objects should mostly work as expected, for example for sorting.
18-
19-
This inherits from UserString because some docutils versions use UserString
20-
for their Text nodes, which then checks its argument for being either a
21-
basestring or UserString, otherwise calls str() -- not unicode() -- on it.
2218
"""
2319
__slots__ = ('_func', '_args')
2420

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

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

34-
def __init__(self, func: Callable, *args: str) -> None:
30+
def __init__(self, func: 'Callable', *args: str) -> None:
3531
self._func = func
3632
self._args = args
3733

38-
@property
39-
def data(self) -> str: # type: ignore
40-
return self._func(*self._args)
34+
def __str__(self) -> str:
35+
return str(self._func(*self._args))
4136

42-
# replace function from UserString; it instantiates a self.__class__
43-
# for the encoding result
37+
def __dir__(self) -> 'List[str]':
38+
return dir(str)
4439

45-
def encode(self, encoding: str = None, errors: str = None) -> bytes: # type: ignore
46-
if encoding:
47-
if errors:
48-
return self.data.encode(encoding, errors)
49-
else:
50-
return self.data.encode(encoding)
51-
else:
52-
return self.data.encode()
40+
def __getattr__(self, name: str) -> Any:
41+
return getattr(self.__str__(), name)
5342

54-
def __dir__(self) -> List[str]:
55-
return dir(str)
43+
def __getstate__(self) -> 'Tuple[Callable, Tuple[str, ...]]':
44+
return self._func, self._args
5645

57-
def __str__(self) -> str:
58-
return str(self.data)
46+
def __setstate__(self, tup: 'Tuple[Callable, Tuple[str]]') -> None:
47+
self._func, self._args = tup
48+
49+
def __copy__(self) -> '_TranslationProxy':
50+
return _TranslationProxy(self._func, *self._args)
5951

60-
def __add__(self, other: str) -> str: # type: ignore
61-
return self.data + other
52+
def __repr__(self) -> str:
53+
try:
54+
return 'i' + repr(str(self.__str__()))
55+
except Exception:
56+
return f'<{self.__class__.__name__} broken>'
6257

63-
def __radd__(self, other: str) -> str: # type: ignore
64-
return other + self.data
58+
def __add__(self, other: str) -> str:
59+
return self.__str__() + other
6560

66-
def __mod__(self, other: str) -> str: # type: ignore
67-
return self.data % other
61+
def __radd__(self, other: str) -> str:
62+
return other + self.__str__()
6863

69-
def __rmod__(self, other: str) -> str: # type: ignore
70-
return other % self.data
64+
def __mod__(self, other: str) -> str:
65+
return self.__str__() % other
7166

72-
def __mul__(self, other: Any) -> str: # type: ignore
73-
return self.data * other
67+
def __rmod__(self, other: str) -> str:
68+
return other % self.__str__()
7469

75-
def __rmul__(self, other: Any) -> str: # type: ignore
76-
return other * self.data
70+
def __mul__(self, other: Any) -> str:
71+
return self.__str__() * other
7772

78-
def __getattr__(self, name: str) -> Any:
79-
if name == '__members__':
80-
return self.__dir__()
81-
return getattr(self.data, name)
73+
def __rmul__(self, other: Any) -> str:
74+
return other * self.__str__()
8275

83-
def __getstate__(self) -> Tuple[Callable, Tuple[str, ...]]:
84-
return self._func, self._args
76+
def __hash__(self):
77+
return hash(self.__str__())
8578

86-
def __setstate__(self, tup: Tuple[Callable, Tuple[str]]) -> None:
87-
self._func, self._args = tup
79+
def __eq__(self, other):
80+
return self.__str__() == other
8881

89-
def __copy__(self) -> "_TranslationProxy":
90-
return self
82+
def __lt__(self, string):
83+
return self.__str__() < string
9184

92-
def __repr__(self) -> str:
93-
try:
94-
return 'i' + repr(str(self.data))
95-
except Exception:
96-
return '<%s broken>' % self.__class__.__name__
85+
def __contains__(self, char):
86+
return char in self.__str__()
87+
88+
def __len__(self):
89+
return len(self.__str__())
90+
91+
def __getitem__(self, index):
92+
return self.__str__()[index]
9793

9894

9995
translators: Dict[Tuple[str, str], NullTranslations] = defaultdict(NullTranslations)

0 commit comments

Comments
 (0)