From 6cad3c95dd85a3d6594e4accf2c6368d2af21cee Mon Sep 17 00:00:00 2001 From: Saugat Pachhai Date: Tue, 15 Dec 2020 19:06:23 +0545 Subject: [PATCH] Remove key/value magic strings (#5106) --- dvc/parsing/__init__.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/dvc/parsing/__init__.py b/dvc/parsing/__init__.py index cbc913b59..90839c9e9 100644 --- a/dvc/parsing/__init__.py +++ b/dvc/parsing/__init__.py @@ -1,7 +1,7 @@ import logging from collections.abc import Mapping, Sequence from copy import deepcopy -from typing import TYPE_CHECKING, Any, Dict, Union +from typing import TYPE_CHECKING, Any, Dict, NamedTuple, Union from funcy import cached_property, collecting, first, isa, join, reraise @@ -295,6 +295,11 @@ class EntryDefinition: ) +class IterationPair(NamedTuple): + key: str = "key" + value: str = "item" + + class ForeachDefinition: def __init__( self, @@ -313,6 +318,7 @@ class ForeachDefinition: self.foreach_data = definition[FOREACH_KWD] self._do_definition = definition[DO_KWD] + self.pair = IterationPair() self.where = where @cached_property @@ -363,11 +369,10 @@ class ForeachDefinition: self.name, ) - @staticmethod - def _inserted_keys(iterable): - keys = ["item"] + def _inserted_keys(self, iterable): + keys = [self.pair.value] if isinstance(iterable, Mapping): - keys.append("key") + keys.append(self.pair.key) return keys @cached_property @@ -410,9 +415,10 @@ class ForeachDefinition: # not the normalized ones to figure out whether to make item/key # available inserted = self._inserted_keys(self.resolved_iterable) - temp_dict = {"item": value} - if "key" in inserted: - temp_dict["key"] = key + temp_dict = {self.pair.value: value} + key_str = self.pair.key + if key_str in inserted: + temp_dict[key_str] = key with self.context.set_temporarily(temp_dict): # optimization: item and key can be removed on __exit__() as they