zerver/lib: Use python 3 syntax for typing.

Extracted from a larger commit by tabbott because these changes will
not create significant merge conflicts.
This commit is contained in:
rht
2017-11-05 11:15:10 +01:00
committed by Tim Abbott
parent 561ba33f69
commit 3f4bf2d22f
35 changed files with 388 additions and 573 deletions

View File

@@ -6,24 +6,20 @@ from django.core.exceptions import PermissionDenied
class AbstractEnum(Enum):
'''An enumeration whose members are used strictly for their names.'''
def __new__(cls):
# type: (Type[AbstractEnum]) -> AbstractEnum
def __new__(cls: Type['AbstractEnum']) -> 'AbstractEnum':
obj = object.__new__(cls)
obj._value_ = len(cls.__members__) + 1
return obj
# Override all the `Enum` methods that use `_value_`.
def __repr__(self):
# type: () -> str
def __repr__(self) -> str:
return str(self)
def value(self):
# type: () -> None
def value(self) -> None:
assert False
def __reduce_ex__(self, proto):
# type: (int) -> None
def __reduce_ex__(self, proto: int) -> None:
assert False
class ErrorCode(AbstractEnum):
@@ -69,13 +65,11 @@ class JsonableError(Exception):
code = ErrorCode.NO_SUCH_WIDGET
data_fields = ['widget_name']
def __init__(self, widget_name):
# type: (str) -> None
def __init__(self, widget_name: str) -> None:
self.widget_name = widget_name # type: str
@staticmethod
def msg_format():
# type: () -> str
def msg_format() -> str:
return _("No such widget: {widget_name}")
raise NoSuchWidgetError(widget_name)
@@ -96,8 +90,7 @@ class JsonableError(Exception):
# like 403 or 404.
http_status_code = 400 # type: int
def __init__(self, msg, code=None):
# type: (Text, Optional[ErrorCode]) -> None
def __init__(self, msg: Text, code: Optional[ErrorCode]=None) -> None:
if code is not None:
self.code = code
@@ -105,8 +98,7 @@ class JsonableError(Exception):
self._msg = msg # type: Text
@staticmethod
def msg_format():
# type: () -> Text
def msg_format() -> Text:
'''Override in subclasses. Gets the items in `data_fields` as format args.
This should return (a translation of) a string literal.
@@ -124,29 +116,24 @@ class JsonableError(Exception):
#
@property
def msg(self):
# type: () -> Text
def msg(self) -> Text:
format_data = dict(((f, getattr(self, f)) for f in self.data_fields),
_msg=getattr(self, '_msg', None))
return self.msg_format().format(**format_data)
@property
def data(self):
# type: () -> Dict[str, Any]
def data(self) -> Dict[str, Any]:
return dict(((f, getattr(self, f)) for f in self.data_fields),
code=self.code.name)
def to_json(self):
# type: () -> Dict[str, Any]
def to_json(self) -> Dict[str, Any]:
d = {'result': 'error', 'msg': self.msg}
d.update(self.data)
return d
def __str__(self):
# type: () -> str
def __str__(self) -> str:
return self.msg
class RateLimited(PermissionDenied):
def __init__(self, msg=""):
# type: (str) -> None
def __init__(self, msg: str="") -> None:
super().__init__(msg)