[PATCH 18 of 24 yams V2] [mod] force string convertion to please mypy

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


# HG changeset patch
# User Laurent Peuch <cortex at worlddomination.be>
# Date 1581689236 -3600
#      Fri Feb 14 15:07:16 2020 +0100
# Node ID 0d0a7dd7272ed0043b00c8c353779ddbcbf1bff2
# Parent  499c2e99508417aaf627758c77a1fb445c452f50
# Available At https://hg.logilab.org/users/lpeuch/yams
#              hg pull https://hg.logilab.org/users/lpeuch/yams -r 0d0a7dd7272e
# EXP-Topic type_annotations
[mod] force string convertion to please mypy

Right now it's working because we are doing magic in __eq__ to behave like
string so this modification doesn't change the code behavior but that makes it
more explicit and avoid doing dirty things with typing.

diff --git a/yams/__init__.py b/yams/__init__.py
--- a/yams/__init__.py
+++ b/yams/__init__.py
@@ -103,9 +103,9 @@ def convert_default_value(rdef: _RdefRde
     if isinstance(default, str) and rdef.object != 'String':
         # real Strings can be anything,
         # including things that look like keywords for other base types
-        if rdef.object in KEYWORD_MAP:
+        if str(rdef.object) in KEYWORD_MAP:
             try:
-                return KEYWORD_MAP[rdef.object][default.upper()]()
+                return KEYWORD_MAP[str(rdef.object)][default.upper()]()
             except KeyError:
                 # the default was likely not a special constant
                 # like TODAY but some literal
@@ -122,11 +122,11 @@ def convert_default_value(rdef: _RdefRde
 
             try:
                 if rdef.object == "Time":
-                    cast(Callable[[str], float], DATE_FACTORY_MAP[rdef.object])
-                    return DATE_FACTORY_MAP[rdef.object](default)
+                    cast(Callable[[str], float], DATE_FACTORY_MAP[str(rdef.object)])
+                    return DATE_FACTORY_MAP[str(rdef.object)](default)
                 else:
-                    cast(Callable[[str], datetime], DATE_FACTORY_MAP[rdef.object])
-                    return DATE_FACTORY_MAP[rdef.object](default)
+                    cast(Callable[[str], datetime], DATE_FACTORY_MAP[str(rdef.object)])
+                    return DATE_FACTORY_MAP[str(rdef.object)](default)
 
             except ValueError as verr:
                 raise ValueError('creating a default value for '
diff --git a/yams/schema2dot.py b/yams/schema2dot.py
--- a/yams/schema2dot.py
+++ b/yams/schema2dot.py
@@ -79,10 +79,12 @@ class SchemaDotPropsHandler(object):
                       'color': 'black', 'style': 'filled'}
             rdef = rschema.rdef(subjnode, objnode)
 
-            if rdef.composite == 'subject':  # type: ignore # magic!
+            # mypy: "RelationDefinitionSchema" has no attribute "composite"
+            # this is a dynamically setted attribue using self.__dict__.update(some_dict)
+            if rdef.composite == 'subject':  # type: ignore
                 kwargs['arrowhead'] = 'none'
                 kwargs['arrowtail'] = 'diamond'
-            elif rdef.composite == 'object':  # type: ignore # magic!
+            elif rdef.composite == 'object':  # type: ignore
                 kwargs['arrowhead'] = 'diamond'
                 kwargs['arrowtail'] = 'none'
             else:
@@ -90,11 +92,14 @@ class SchemaDotPropsHandler(object):
                 kwargs['arrowtail'] = 'none'
 
             # UML like cardinalities notation, omitting 1..1
-            if rdef.cardinality[1] != '1':  # type: ignore # magic!
-                kwargs['taillabel'] = CARD_MAP[rdef.cardinality[1]]  # type: ignore # magic!
 
-            if rdef.cardinality[0] != '1':  # type: ignore # magic!
-                kwargs['headlabel'] = CARD_MAP[rdef.cardinality[0]]  # type: ignore # magic!
+            # mypy: "RelationDefinitionSchema" has no attribute "cardinality"
+            # this is a dynamically setted attribue using self.__dict__.update(some_dict)
+            if rdef.cardinality[1] != '1':  # type: ignore
+                kwargs['taillabel'] = CARD_MAP[rdef.cardinality[1]]  # type: ignore
+
+            if rdef.cardinality[0] != '1':  # type: ignore
+                kwargs['headlabel'] = CARD_MAP[rdef.cardinality[0]]  # type: ignore
 
             kwargs['color'] = self.nextcolor()
 
@@ -207,11 +212,17 @@ class OneHopESchemaVisitor(SchemaVisitor
         # Inheritance relations.
         if eschema.specializes():
             nodes.add((eschema.specializes().type, eschema.specializes()))
+            # mypy: Argument 1 to "add" of "set" has incompatible type "Tuple[str, Any, None]";
+            # mypy: expected "Tuple[str, str, RelationSchema]"
+            # situation too complex for mypy to handle
             edges.add((eschema.type, eschema.specializes().type, None))  # type: ignore
 
         if eschema.specialized_by():
             for pschema in eschema.specialized_by():
                 nodes.add((pschema.type, pschema))
+                # mypy: Argument 1 to "add" of "set" has incompatible type "Tuple[str, str, None]";
+                # mypy: expected "Tuple[str, str, RelationSchema]"
+                # situation too complex for mypy to handle
                 edges.add((pschema.type, eschema.type, None))  # type: ignore
 
         self._nodes = nodes



More information about the cubicweb-devel mailing list