hykilpikonna / hyfetch

🏳️‍🌈 🏳️‍⚧️ Neofetch with LGBTQ+ pride flags!
MIT License
1.15k stars 90 forks source link

Fix ColorAlignment.custom_colors.keys() / slots type mismatch #303

Open luna-1024 opened 1 month ago

luna-1024 commented 1 month ago

This is something I am running into while working on an bigger PR (and need to resolve it) and I think it would be good to separately get a quick fix in.

In the ColorAlignment dataclass, the type annotation of custom_color keys (int) does not match the type of the of slots (set of str (which are numeric characters)), which is used to construct the ColorAlignment instance. (The value of that field ends up not the correct type).

Additionally, since this value is saved in the hyfetch.json, serialization for json only supports strings as keys means that the keys go into the json as strs. And it's involved to avoid saving the keys as str. There are outstanding user configs already with serialized string keys. De-serializing those configs also result in str keys in the custom_color field.

I think the resolution is one of the following options:

  1. Change the type annotation to custom_colors: dict[str, int] = ()
  2. Fix the construction of slots to be a set[int], and one of: a. Enhance the deserialization to generally recognise int str keys. b. Add a fixup step to ColorAlignment.from_dict

I chose solution 2b here as it is simple and it respects the apparent authorial intent that the custom_colors should be an dict[int, int], which in general makes sense to me. This solution would not in any way preclude smoothly moving to str keys in the future if more slots support is desired with for example letter based slots ${cA} ${cB}.