Skip to content

Commit a63c87f

Browse files
authored
perf(types): avoid double iteration in transform_dict_value_to_str (#3988)
Combine the None-validation and str-conversion loops over dict.items() into a single pass, and rename the `dict` parameter to `d` to stop shadowing the builtin. Fixes #3987
1 parent 8c96d42 commit a63c87f

1 file changed

Lines changed: 5 additions & 3 deletions

File tree

pyiceberg/types.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,14 @@
7373
GEOGRAPHY_REGEX = re.compile(r"geography(?:\(\s*['\"]([^'\"]+)['\"](?:\s*,\s*['\"]([^'\"]+)['\"])?\s*\))?$")
7474

7575

76-
def transform_dict_value_to_str(dict: dict[str, Any]) -> dict[str, str]:
76+
def transform_dict_value_to_str(d: dict[str, Any]) -> dict[str, str]:
7777
"""Transform all values in the dictionary to string. Raise an error if any value is None."""
78-
for key, value in dict.items():
78+
result = {}
79+
for key, value in d.items():
7980
if value is None:
8081
raise ValueError(f"None type is not a supported value in properties: {key}")
81-
return {k: str(v).lower() if isinstance(v, bool) else str(v) for k, v in dict.items()}
82+
result[key] = str(value).lower() if isinstance(value, bool) else str(value)
83+
return result
8284

8385

8486
def _parse_decimal_type(decimal: Any) -> tuple[int, int]:

0 commit comments

Comments
 (0)