details:   https://code.tryton.org/tryton/commit/738200b67741
branch:    default
user:      Cédric Krier <[email protected]>
date:      Fri Jan 02 12:32:43 2026 +0100
description:
        Use defaultdict with float as default value when calculating product 
quantities
diffstat:

 modules/stock/move.py      |  8 +++-----
 modules/stock_lot/stock.py |  2 +-
 2 files changed, 4 insertions(+), 6 deletions(-)

diffs (44 lines):

diff -r d929198e2b79 -r 738200b67741 modules/stock/move.py
--- a/modules/stock/move.py     Fri Jan 02 13:11:49 2026 +0100
+++ b/modules/stock/move.py     Fri Jan 02 12:32:43 2026 +0100
@@ -81,8 +81,7 @@
         pool = Pool()
         Product = pool.get('product.product')
 
-        record_ids = [r.id for r in records]
-        quantities = dict.fromkeys(record_ids, 0.0)
+        quantities = defaultdict(float)
         if not location_ids:
             return quantities
 
@@ -98,8 +97,7 @@
 
         for key, quantity in pbl.items():
             # pbl could return None in some keys
-            if (key[position] is not None
-                    and key[position] in quantities):
+            if key[position] is not None:
                 quantities[key[position]] += quantity
         return quantities
 
@@ -1739,7 +1737,7 @@
             Model = Template
         id_getter = operator.itemgetter(grouping.index(id_name) + 1)
         ids = set()
-        quantities = defaultdict(int)
+        quantities = defaultdict(float)
         keys = set()
         # We can do a quick loop without propagation if the request is for a
         # single location because all the locations are children and we can sum
diff -r d929198e2b79 -r 738200b67741 modules/stock_lot/stock.py
--- a/modules/stock_lot/stock.py        Fri Jan 02 13:11:49 2026 +0100
+++ b/modules/stock_lot/stock.py        Fri Jan 02 12:32:43 2026 +0100
@@ -81,7 +81,7 @@
     def get_quantity(cls, lots, name):
         location_ids = Transaction().context.get('locations')
         product_ids = list(set(l.product.id for l in lots))
-        quantities = {}
+        quantities = defaultdict(float)
         for product_ids in grouped_slice(product_ids):
             quantities.update(cls._get_quantity(lots, name, location_ids,
                     grouping=('product', 'lot',),

Reply via email to