Skip to content

Commit 35ef1c6

Browse files
committed
Added the ability to set EntityDictionary's factory to None to explicitely disable the auto-construction of missing instances.
Changed EntityDictionary.__missing__ so that it only raise a KeyError when constructing a missing key failed.
1 parent a156f96 commit 35ef1c6

File tree

1 file changed

+12
-1
lines changed
  • addons/source-python/packages/source-python/entities

1 file changed

+12
-1
lines changed

addons/source-python/packages/source-python/entities/dictionary.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,18 @@ def __init__(self, factory=Entity, *args, **kwargs):
5050

5151
def __missing__(self, index):
5252
"""Add and return the entity instance for the given index."""
53-
instance = self._factory(index, *self._args, **self._kwargs)
53+
# Get the factory
54+
factory = self._factory
55+
56+
# Let's mimic dict's behaviour if the factory is set to None
57+
if factory is None:
58+
raise KeyError(index)
59+
60+
# For uniformity reasons, ensure we only raise a KeyError
61+
try:
62+
instance = factory(index, *self._args, **self._kwargs)
63+
except Exception as e:
64+
raise KeyError(str(e))
5465

5566
# Only cache entities that are not marked for deletion.
5667
# This is required, because if someone request an entity instance

0 commit comments

Comments
 (0)