[PATCH 4 of 5] [Fix] sorting on the right key

julien tayon julien.tayon at logilab.fr
Fri Jun 7 15:40:06 CEST 2019


# HG changeset patch
# User julien tayon <julien.tayon at logilab.fr>
# Date 1559131704 -7200
#      Wed May 29 14:08:24 2019 +0200
# Node ID 0d1516ed43f806125ef957d8a302b95702f3c051
# Parent  28e9e128daea260ca076951857bbd5108726a774
[Fix] sorting on the right key

If you applied previous patch, tried to click in the schema of an entity with
cubicweb in py3 on "vue en boite" you are probably experiencing an
infinite spinner (which implies cw > 3.26)

What happened ?

This "vue en boite used to work" at least since ...

hg  diff -r a8c1ea39040097a942e084eeaaaa1992ab622f27:a8c1ea39040097a942e084eeaaaa1992ab622f27^  cubicweb/schema.py


+    @deprecated('use .rdef(subjtype, objtype).role_cardinality(role)')
+    def cardinality(self, subjtype, objtype, target):
+        return self.rdef(subjtype, objtype).role_cardinality(target)
+

But, wait ...
If I open a shell on an instance of cw 3.24 something seems off

>>> list(schema['CWUniqueTogetherConstraint'].relation_definitions())[0][0].cardinality
# <bound method CubicWebRelationSchema.wrapped of <constraint_of [CWUniqueTogetherConstraint,CWEType]>>

We have been sorting on a method the whole time ? Is it possible what
were the effects ?

1) We cannot sort function can't we ?

>>> def adder(i): return lambda x: x+i
>>> sorted(map(adder,range(10)))
[<function __main__.<lambda>>,
 <function __main__.<lambda>>,
...

Yes we can.

2) what does it means.

>>> { adder(1) : 1 }
Out[19]: {<function __main__.<lambda>>: 1}

In fact the function object as a __hash__ method (which is practical for making
memoizers (cache)), and return truly random results (pseudo random).

My take on this patch is relations have NEVER been sorted by cardinality.
No one never ever noticed. Hence, I propose to not fix a bug that never was
reported.

diff --git a/cubicweb/web/schemaviewer.py b/cubicweb/web/schemaviewer.py
--- a/cubicweb/web/schemaviewer.py
+++ b/cubicweb/web/schemaviewer.py
@@ -145,10 +145,8 @@ class SchemaViewer(object):
         rels = []
         first = True
 
-        def get_type_card(el):
-            return (el[0].type, el[0].cardinality)
-
-        rel_defs = sorted(eschema.relation_definitions(), key=get_type_card)
+        rel_defs = sorted(eschema.relation_definitions(),
+                          key=lambda el: el[0].type)
 
         for rschema, targetschemas, role in rel_defs:
             if rschema.type in skiptypes:


More information about the cubicweb-devel mailing list