[PATCH 07 of 24 yams V2] [mypy] add typing in serialize.py

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


# HG changeset patch
# User Laurent Peuch <cortex at worlddomination.be>
# Date 1579116480 -3600
#      Wed Jan 15 20:28:00 2020 +0100
# Node ID 4c000e0d0baf31b6611e6869ab31c102e977772a
# Parent  25e6d96e27a50f719498174bfe4a6b4125afb93a
# Available At https://hg.logilab.org/users/lpeuch/yams
#              hg pull https://hg.logilab.org/users/lpeuch/yams -r 4c000e0d0baf
# EXP-Topic type_annotations
[mypy] add typing in serialize.py

diff --git a/yams/serialize.py b/yams/serialize.py
--- a/yams/serialize.py
+++ b/yams/serialize.py
@@ -1,15 +1,21 @@
 import logging
 from io import StringIO
+from typing import Dict
 from logilab.common.graph import ordered_nodes
 
+from yams.types import _SchemaType
 
-def serialize_to_python(s):
+
+def serialize_to_python(schema: _SchemaType) -> str:
     out = StringIO()
     w = out.write
     w(u'from yams.buildobjs import *\n\n')
-    graph = {}
-    for entity in s.entities():
+
+    graph: Dict = {}
+
+    for entity in schema.entities():
         graph.setdefault(entity, [])
+
     for e in reversed(ordered_nodes(graph)):
         if not e.final:
             if e._specialized_type:
@@ -24,7 +30,8 @@ def serialize_to_python(s):
             else:
                 w(u'    pass\n')
             w(u'\n')
-    for r in s.relations():
+
+    for r in schema.relations():
         if not r.final:
             if r.subjects() and r.objects():
                 w(u'class %s(RelationDefinition):\n' % r.type)
@@ -36,4 +43,5 @@ def serialize_to_python(s):
             else:
                 logging.warning(
                     'relation definition %s missing subject/object' % r.type)
+
     return out.getvalue()
diff --git a/yams/types.py b/yams/types.py
--- a/yams/types.py
+++ b/yams/types.py
@@ -26,6 +26,8 @@ if TYPE_CHECKING:
     from yams import schema, buildobjs
 
     _RdefType = Union[schema.RelationDefinitionSchema, buildobjs.RelationDefinition]
+    _SchemaType = schema.Schema
 
 else:
     _RdefType = TypeVar("rdef")
+    _SchemaType = TypeVar("schema")



More information about the cubicweb-devel mailing list