[pypy-commit] pypy rpython-sprint: Cleanup rpython/annotator/signature.py

ariava pypy.commits at gmail.com
Sun Mar 18 11:11:56 EDT 2018


Author: Arianna Avanzini <avanzini.arianna at gmail.com>
Branch: rpython-sprint
Changeset: r93989:294fd4ce722f
Date: 2018-03-18 14:47 +0100
http://bitbucket.org/pypy/pypy/changeset/294fd4ce722f/

Log:	Cleanup rpython/annotator/signature.py

	- Use 'isinstance()' instead of 'is' in _annotation_key() to account
	for user-defined subclasses of list and dict.
	- Cleanup assert messages when validating annotations and convert them
	to exceptions.
	- Remove intermediate variables used just once before return.
	- Cleanup comments.

diff --git a/rpython/annotator/signature.py b/rpython/annotator/signature.py
--- a/rpython/annotator/signature.py
+++ b/rpython/annotator/signature.py
@@ -14,16 +14,16 @@
 
 def _annotation_key(t):
     from rpython.rtyper import extregistry
-    if type(t) is list:
+    if isinstance(t, list):
         assert len(t) == 1
         return ('list', _annotation_key(t[0]))
-    elif type(t) is dict:
+    elif isinstance(t, dict):
         assert len(t.keys()) == 1
         return ('dict', _annotation_key(t.items()[0]))
     elif isinstance(t, tuple):
         return tuple([_annotation_key(i) for i in t])
     elif extregistry.is_registered(t):
-        # XXX should it really be always different?
+        # XXX do we want to do something in this case?
         return t
     return t
 
@@ -38,24 +38,37 @@
             return t
     return _compute_annotation(t, bookkeeper)
 
+
+def _validate_annotation_size(t):
+    try:
+        _ = iter(t)
+    except TypeError:  # if it's not an iterable, just return
+        return t
+    if len(t) == 0:
+        raise ValueError("Cannot handle empty %s in args enforcing", type(t))
+    if isinstance(t, tuple) or len(t) == 1:  # we accept tuples with len > 1 because
+        return t                             # tuple items are all of same type
+    raise TypeError("Cannot specify multiple types in a %s (try using tuple)", type(t))
+
+
 def _compute_annotation(t, bookkeeper=None):
     from rpython.rtyper.lltypesystem import lltype
     from rpython.rtyper.llannotation import lltype_to_annotation
+    _validate_annotation_size(t)
     if isinstance(t, SomeObject):
         return t
     elif isinstance(t, lltype.LowLevelType):
         return lltype_to_annotation(t)
     elif isinstance(t, list):
-        assert len(t) == 1, "We do not support type joining in list"
-        listdef = ListDef(bookkeeper, annotation(t[0]), mutated=True, resized=True)
-        return SomeList(listdef)
+        return SomeList(
+                ListDef(bookkeeper, annotation(t[0]),
+                        mutated=True, resized=True))
     elif isinstance(t, tuple):
         return SomeTuple(tuple([annotation(i) for i in t]))
     elif isinstance(t, dict):
-        assert len(t) == 1, "We do not support type joining in dict"
-        result = SomeDict(DictDef(bookkeeper, annotation(t.keys()[0]),
-                                annotation(t.values()[0])))
-        return result
+        return SomeDict(
+                DictDef(bookkeeper,
+                        annotation(t.keys()[0]), annotation(t.values()[0])))
     elif type(t) is types.NoneType:
         return s_None
     elif extregistry.is_registered(t):
@@ -84,13 +97,12 @@
     elif t is types.NoneType:
         return s_None
     elif bookkeeper and extregistry.is_registered_type(t):
-        entry = extregistry.lookup_type(t)
-        return entry.compute_annotation_bk(bookkeeper)
+        return (extregistry.lookup_type(t)
+                .compute_annotation_bk(bookkeeper))
     elif t is type:
         return SomeType()
     elif bookkeeper and not hasattr(t, '_freeze_'):
-        classdef = bookkeeper.getuniqueclassdef(t)
-        return SomeInstance(classdef)
+        return SomeInstance(bookkeeper.getuniqueclassdef(t))
     else:
         raise AssertionError("annotationoftype(%r)" % (t,))
 


More information about the pypy-commit mailing list