[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>
changeset4ffc56103a11
branchdefault
phasedraft
hiddenyes
parent revision#762472143df0 [schema] cleanup default values handling in entity schema (closes #109207)
child revision<not specified>
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 4ffc56103a117fee36b7b15c28598297c235e104
# Parent 762472143df0eff098c48b6fa24acb0edf9fd709
[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
@@ -296,13 +296,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"""