Skip to content

Commit 096a4ba

Browse files
committed
[ADD] util.direct_inherit_parents
Well named function to get only the direct inherted parents. Needed for usages in functions that call themself recursively. Mostly useful for inheritS. Rewrite `util.inherit_parents` to use this function. Part-of: #22
1 parent fffc361 commit 096a4ba

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

src/base/tests/test_util.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,15 @@ def test_inherit_parents(self, model, expected):
513513
result = sorted(util.inherit_parents(cr, model))
514514
self.assertEqual(result, sorted(expected))
515515

516+
def test_direct_inherit_parents(self):
517+
cr = self.env.cr
518+
result = sorted(util.direct_inherit_parents(cr, "product.product"))
519+
self.assertEqual(len(result), 3)
520+
parents, inhs = zip(*result)
521+
self.assertEqual(parents, ("mail.activity.mixin", "mail.thread", "product.template"))
522+
self.assertTrue(all(inh.model == "product.product" for inh in inhs))
523+
self.assertEqual([inh.via for inh in inhs], [None, None, "product_tmpl_id"])
524+
516525

517526
class TestNamedCursors(UnitTestCase):
518527
@staticmethod

src/util/inherit.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,10 @@ def for_each_inherit(cr, model, skip=(), interval="[)"):
9393
yield inh
9494

9595

96-
def inherit_parents(cr, model, skip=(), interval="[)"):
96+
def direct_inherit_parents(cr, model, skip=(), interval="[)"):
97+
"""
98+
Yield the *direct* inherits parents.
99+
"""
97100
if skip == "*":
98101
return
99102
skip = set(skip)
@@ -103,7 +106,21 @@ def inherit_parents(cr, model, skip=(), interval="[)"):
103106
continue
104107
for inh in inhs:
105108
if inh.model == model and cmp_(inh):
106-
yield parent
109+
yield parent, inh
107110
skip.add(parent)
108-
for grand_parent in inherit_parents(cr, parent, skip=skip, interval=interval):
109-
yield grand_parent
111+
112+
113+
def inherit_parents(cr, model, skip=(), interval="[)"):
114+
"""
115+
Recursively yield all inherit parents model names
116+
"""
117+
if skip == "*":
118+
return
119+
skip = set(skip)
120+
for parent, _inh in direct_inherit_parents(cr, model, skip=skip, interval=interval):
121+
if parent in skip:
122+
continue
123+
yield parent
124+
skip.add(parent)
125+
for grand_parent in inherit_parents(cr, parent, skip=skip, interval=interval):
126+
yield grand_parent

0 commit comments

Comments
 (0)