[schema] add a warning to avoid a potential silent bug (follows #109207)

In some cases there are several target entities for one given rtype.

If no target etype is given, only the first one is used to compute the returned relation definition.

It may be a misuse by the user.

authorAurelien Campeas <aurelien.campeas@logilab.fr>
changeset99d6f01f853d
branchdefault
phasepublic
hiddenno
parent revision#e38902402af5 [xy] fail assertion only if existing prefix is being changed (closes #139999)
child revision#7cf7c70a3bab [schema] extend .rdef(...) with a parameter to silence the ambiguity warning, #45f486c54ad5 Remove dead and deprecated code (closes #149361), #de517b46639d Remove dead and deprecated code (closes #149361), #74807a09aacc [schema building] Fix key in context.defined for RelationDefinition, #b5d7178f8cb4 "add" permission for attributes (to distinguish from "update")
files modified by this revision
schema.py
# HG changeset patch
# User Aurelien Campeas <aurelien.campeas@logilab.fr>
# Date 1367589747 -7200
# Fri May 03 16:02:27 2013 +0200
# Node ID 99d6f01f853db229223d8df2a95ba17cf9201343
# Parent e38902402af59539fc64407186cc2bfcfaae6315
[schema] add a warning to avoid a potential silent bug (follows #109207)

In some cases there are several target entities for one given rtype.

If no target etype is given, only the first one is used to compute the
returned relation definition.

It may be a misuse by the user.

diff --git a/schema.py b/schema.py
@@ -297,13 +297,19 @@
1          randomly.
2          """
3          rschema = self.schema.rschema(rtype)
4          if targettype is None:
5              if role == 'subject':
6 -                targettype = rschema.objects(self)[0]
7 +                types = rschema.objects(self)
8              else:
9 -                targettype = rschema.subjects(self)[0]
10 +                types = rschema.subjects(self)
11 +            if len(types) != 1:
12 +                warnings.warn('[yams 0.38] no targettype specified and there are several '
13 +                              'relation definitions for rtype %s: %s. Yet you get the first '
14 +                              'rdef.' % (rtype, [eschema.type for eschema in types]),
15 +                              Warning, stacklevel=2)
16 +            targettype = types[0]
17          return rschema.role_rdef(self, targettype, role)
18 
19      @cached
20      def ordered_relations(self):
21          """return subject relations in an ordered way"""