Skip to content

Commit

Permalink
add feature flag for default key behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
rmorshea committed Apr 17, 2021
1 parent 5727ab4 commit 42ee01c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
26 changes: 26 additions & 0 deletions src/idom/config.py
Expand Up @@ -52,3 +52,29 @@
Absolute URL
``ABSOLUTE_PATH/my-module.js``
"""

IDOM_FEATURE_INDEX_AS_DEFAULT_KEY = _option.Option(
"IDOM_FEATURE_INDEX_AS_DEFAULT_KEY",
default=False,
mutable=False,
validator=lambda x: bool(int(x)),
)
"""A feature flag for using the index of a sibling element as its default key
In a future release this flag's default value will be set to true, and after that, this
flag willbe removed entirely and the indices will always be the default key.
For more information on changes to this feature flag see: https://github.com/idom-team/idom/issues/351
"""

if not IDOM_FEATURE_INDEX_AS_DEFAULT_KEY.get(): # pragma: no cover
from warnings import warn

warn(
(
"In a future release 'IDOM_FEATURE_INDEX_AS_DEFAULT_KEY' will be turned on "
"by default. For more information on changes to this feature flag, see: "
"https://github.com/idom-team/idom/issues/351"
),
UserWarning,
)
18 changes: 15 additions & 3 deletions src/idom/core/layout.py
Expand Up @@ -21,7 +21,7 @@
from jsonpatch import apply_patch, make_patch
from typing_extensions import TypedDict

from idom.config import IDOM_DEBUG_MODE
from idom.config import IDOM_DEBUG_MODE, IDOM_FEATURE_INDEX_AS_DEFAULT_KEY

from .component import AbstractComponent
from .events import EventHandler
Expand Down Expand Up @@ -453,7 +453,7 @@ async def get(self) -> AbstractComponent:
def _process_child_type_and_key(
children: List[Any],
) -> Iterator[Tuple[Any, int, Any]]:
for child in children:
for index, child in enumerate(children):
if isinstance(child, dict):
child_type = _DICT_TYPE
key = child.get("key")
Expand All @@ -466,11 +466,23 @@ def _process_child_type_and_key(
key = None

if key is None:
key = object()
key = _default_key(index)

yield (child, child_type, key)


if IDOM_FEATURE_INDEX_AS_DEFAULT_KEY.get():

def _default_key(index: int) -> Any: # pragma: no cover
return index


else:

def _default_key(index: int) -> Any:
return object()


# used in _process_child_type_and_key
_DICT_TYPE = 1
_COMPONENT_TYPE = 2
Expand Down

0 comments on commit 42ee01c

Please sign in to comment.