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

Laurent Peuch cortex at worlddomination.be
Tue Feb 25 23:39:11 CET 2020


On Mon, Feb 17, 2020 at 10:54:59AM +0100, Frank Bessou wrote:
> 
> 
> On 14/02/2020 15:59, Laurent Peuch wrote:
> ># HG changeset patch
> ># User Laurent Peuch <cortex at worlddomination.be>
> ># Date 1579114581 -3600
> >#      Wed Jan 15 19:56:21 2020 +0100
> ># Node ID 7ae886a6eede12cbdb23f8d3e25136eb396c5726
> ># Parent  89d379f5a59bb5cb9307a4155454ac450bdbfdd7
> ># Available At https://hg.logilab.org/users/lpeuch/yams
> >#              hg pull https://hg.logilab.org/users/lpeuch/yams -r 7ae886a6eede
> >[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
> >  __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: Dict = None,
> >+                 i18nvalues: Iterable = None) -> None:
> 
> 
> I saw you used Optional[T] for arguments that can be done. Here you only use
> T. I think we should choose a single way of writing optional arguments for
> better consistency.

Very likely mistakes from me, I've been doing several iterators on
those changeset and very likely made small mistakes like those.

> 
> >          # 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, _: Any) -> None:
> 
> Why not typing "_" as Callable[[str], str] ?

Oh, that's a leftother from pytype (or a mistake from me) I think.
Will fix it :)

-- 

Laurent Peuch -- Bram



More information about the cubicweb-devel mailing list