Skip to content

Commit 42ee01c

Browse files
committed
add feature flag for default key behavior
1 parent 5727ab4 commit 42ee01c

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

src/idom/config.py

+26
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,29 @@
5252
Absolute URL
5353
``ABSOLUTE_PATH/my-module.js``
5454
"""
55+
56+
IDOM_FEATURE_INDEX_AS_DEFAULT_KEY = _option.Option(
57+
"IDOM_FEATURE_INDEX_AS_DEFAULT_KEY",
58+
default=False,
59+
mutable=False,
60+
validator=lambda x: bool(int(x)),
61+
)
62+
"""A feature flag for using the index of a sibling element as its default key
63+
64+
In a future release this flag's default value will be set to true, and after that, this
65+
flag willbe removed entirely and the indices will always be the default key.
66+
67+
For more information on changes to this feature flag see: https://github.com/idom-team/idom/issues/351
68+
"""
69+
70+
if not IDOM_FEATURE_INDEX_AS_DEFAULT_KEY.get(): # pragma: no cover
71+
from warnings import warn
72+
73+
warn(
74+
(
75+
"In a future release 'IDOM_FEATURE_INDEX_AS_DEFAULT_KEY' will be turned on "
76+
"by default. For more information on changes to this feature flag, see: "
77+
"https://github.com/idom-team/idom/issues/351"
78+
),
79+
UserWarning,
80+
)

src/idom/core/layout.py

+15-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from jsonpatch import apply_patch, make_patch
2222
from typing_extensions import TypedDict
2323

24-
from idom.config import IDOM_DEBUG_MODE
24+
from idom.config import IDOM_DEBUG_MODE, IDOM_FEATURE_INDEX_AS_DEFAULT_KEY
2525

2626
from .component import AbstractComponent
2727
from .events import EventHandler
@@ -453,7 +453,7 @@ async def get(self) -> AbstractComponent:
453453
def _process_child_type_and_key(
454454
children: List[Any],
455455
) -> Iterator[Tuple[Any, int, Any]]:
456-
for child in children:
456+
for index, child in enumerate(children):
457457
if isinstance(child, dict):
458458
child_type = _DICT_TYPE
459459
key = child.get("key")
@@ -466,11 +466,23 @@ def _process_child_type_and_key(
466466
key = None
467467

468468
if key is None:
469-
key = object()
469+
key = _default_key(index)
470470

471471
yield (child, child_type, key)
472472

473473

474+
if IDOM_FEATURE_INDEX_AS_DEFAULT_KEY.get():
475+
476+
def _default_key(index: int) -> Any: # pragma: no cover
477+
return index
478+
479+
480+
else:
481+
482+
def _default_key(index: int) -> Any:
483+
return object()
484+
485+
474486
# used in _process_child_type_and_key
475487
_DICT_TYPE = 1
476488
_COMPONENT_TYPE = 2

0 commit comments

Comments
 (0)