[PATCH 06 of 24 yams V2] [mypy] add typing in _exceptions.py

Laurent Peuch cortex at worlddomination.be
Wed Mar 4 15:17:42 CET 2020


# HG changeset patch
# User Laurent Peuch <cortex at worlddomination.be>
# Date 1579114581 -3600
#      Wed Jan 15 19:56:21 2020 +0100
# Node ID 25e6d96e27a50f719498174bfe4a6b4125afb93a
# Parent  fcf88607a18da9fa279022bab4c60c4e43b6a8bc
# Available At https://hg.logilab.org/users/lpeuch/yams
#              hg pull https://hg.logilab.org/users/lpeuch/yams -r 25e6d96e27a5
# EXP-Topic type_annotations
[mypy] add typing in _exceptions.py

diff --git a/yams/_exceptions.py b/yams/_exceptions.py
--- a/yams/_exceptions.py
+++ b/yams/_exceptions.py
@@ -16,6 +16,7 @@
 # You should have received a copy of the GNU Lesser General Public License along
 # with yams. If not, see <http://www.gnu.org/licenses/>.
 """YAMS exception classes"""
+from typing import Any, Generator, Tuple, Optional, Dict, Iterable, Callable
 
 __docformat__ = "restructuredtext en"
 
@@ -23,16 +24,19 @@
 class SchemaError(Exception):
     """base class for schema exceptions"""
 
-    def __str__(self):
+    def __unicode__(self) -> str:
+        raise NotImplementedError()
+
+    def __str__(self) -> str:
         return self.__unicode__()
 
 
 class UnknownType(SchemaError):
     """using an unknown entity type"""
 
-    msg = 'Unknown type %s'
+    msg: str = 'Unknown type %s'
 
-    def __unicode__(self):
+    def __unicode__(self) -> str:
         return self.msg % self.args
 
 
@@ -45,16 +49,16 @@ class BadSchemaDefinition(SchemaError):
     * line is the actual line in text form
     """
 
-    msg = '%s line %s: %s'
+    msg: str = '%s line %s: %s'
 
-    def __get_filename(self):
+    @property
+    def filename(self) -> Optional[str]:
         if len(self.args) > 1:
             return self.args[0]
         else:
             return None
-    filename = property(__get_filename)
 
-    def __unicode__(self):
+    def __unicode__(self) -> str:
         msgs = []
         args_offset = 0
         if self.filename is not None:
@@ -89,7 +93,8 @@ class ValidationError(SchemaError):
     Translation will be done **in-place** by calling :meth:`translate`.
     """
 
-    def __init__(self, entity, errors, msgargs=None, i18nvalues=None):
+    def __init__(self, entity, errors: Dict, msgargs: Optional[Dict] = None,
+                 i18nvalues: Optional[Iterable] = None) -> None:
         # set args so ValidationError are serializable through pyro
         SchemaError.__init__(self, entity, errors)
         self.entity = entity
@@ -99,18 +104,22 @@ class ValidationError(SchemaError):
         self.i18nvalues = i18nvalues
         self._translated = False
 
-    def __unicode__(self):
+    def __unicode__(self) -> str:
         if self._translated:
-            errors = self.errors
+            errors_dict = self.errors
         else:
-            errors = dict(self._translated_errors(str))
-        if len(errors) == 1:
-            attr, error = next(iter(errors.items()))
+            errors_dict = dict(self._translated_errors(str))
+
+        if len(errors_dict) == 1:
+            attr, error = next(iter(errors_dict.items()))
+
             return u'%s (%s): %s' % (self.entity, attr, error)
-        errors = '\n'.join('* %s: %s' % (k, v) for k, v in errors.items())
+
+        errors = '\n'.join('* %s: %s' % (k, v) for k, v in errors_dict.items())
+
         return u'%s:\n%s' % (self.entity, errors)
 
-    def translate(self, _):
+    def translate(self, _: Callable[[str], str]) -> None:
         """Translate and interpolate messsages in the errors dictionary, using
         the given translation function.
 
@@ -127,7 +136,7 @@ class ValidationError(SchemaError):
                     self.msgargs[key] = _(self.msgargs[key])
         self.errors = dict(self._translated_errors(_))
 
-    def _translated_errors(self, _):
+    def _translated_errors(self, _: Any) -> Generator[Tuple[str, str], None, None]:
         for key, msg in self.errors.items():
             msg = _(msg)
             if key is not None:



More information about the cubicweb-devel mailing list