Coverage for Users/vladimirpavlov/PycharmProjects/parameterizable/src/mixinforge/dict_sorter.py: 100%

1"""Utility for sorting dictionaries by keys.

2

3 This module provides a simple helper function for creating sorted dictionaries,

4 which is useful for ensuring consistent ordering in serialization and display.

5"""

6 from typing import Any

7

8

9 def sort_dict_by_keys(d: dict[str, Any]) -> dict[str, Any]:

10 """Return a new dictionary with keys sorted alphabetically.

11

12 Args:

13 d: The input mapping to sort by keys.

14

15 Returns:

16 A new dictionary containing the same key-value pairs as

17 d, but with keys in ascending alphabetical order.

18

19 Raises:

20 TypeError: If d is not a dictionary.

21 """

22 if not isinstance(d, dict):

23 raise TypeError(

24 f"d must be a dictionary, got {type(d).__name__} instead"

25 )

26 return {k: d[k] for k in sorted(d.keys())}