Skip to content

Commit 7356afb

Browse files
committed
fix(options): improve type safety for SparseArray handling
- Update SparseArray type parameter to include all value types - Add proper type checking for both dict and SparseArray cases - Fix return type casting in _show_option method - Resolve mypy errors related to string indexing of SparseArray
1 parent 8572639 commit 7356afb

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

src/libtmux/options.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,11 @@
108108
]
109109
ExplodedComplexUntypedOptionsDict: TypeAlias = dict[
110110
str,
111-
t.Union[str, int, list[str | int], dict[str, list[str | int]], "SparseArray[str]"]
111+
str
112+
| int
113+
| list[str | int]
114+
| dict[str, list[str | int]]
115+
| SparseArray[str | int]
112116
| None,
113117
]
114118

@@ -368,7 +372,7 @@ def explode_arrays(
368372
"""
369373
options: dict[str, t.Any] = {}
370374
for key, val in _dict.items():
371-
Default: type[dict[t.Any, t.Any] | SparseArray[str]] = (
375+
Default: type[dict[t.Any, t.Any] | SparseArray[str | int | bool | None]] = (
372376
dict if isinstance(key, str) and key == "terminal-features" else SparseArray
373377
)
374378
if "[" not in key:
@@ -1071,12 +1075,19 @@ def _show_option(
10711075
),
10721076
)
10731077

1074-
if not isinstance(output_exploded, dict):
1075-
return output_exploded
1078+
if not isinstance(output_exploded, (dict, SparseArray)):
1079+
return t.cast("ConvertedValue", output_exploded)
10761080

1077-
if option not in output_exploded:
1081+
if isinstance(output_exploded, dict) and option not in output_exploded:
10781082
return None
10791083

1084+
if isinstance(output_exploded, SparseArray):
1085+
try:
1086+
index = int(option)
1087+
return output_exploded[index]
1088+
except (ValueError, KeyError):
1089+
return None
1090+
10801091
return t.cast("ConvertedValue | None", output_exploded[option])
10811092

10821093
def show_option(

0 commit comments

Comments
 (0)