Skip to content

Commit 66abda8

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 fa47cdb commit 66abda8

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

src/libtmux/options.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@
120120
int,
121121
list[t.Union[str, int]],
122122
dict[str, list[t.Union[str, int]]],
123-
"SparseArray[str]",
123+
SparseArray[t.Union[str, int]],
124124
]
125125
],
126126
]
@@ -381,7 +381,7 @@ def explode_arrays(
381381
"""
382382
options: dict[str, t.Any] = {}
383383
for key, val in _dict.items():
384-
Default: type[dict[t.Any, t.Any] | SparseArray[str]] = (
384+
Default: type[dict[t.Any, t.Any] | SparseArray[str | int | bool | None]] = (
385385
dict if isinstance(key, str) and key == "terminal-features" else SparseArray
386386
)
387387
if "[" not in key:
@@ -1084,13 +1084,20 @@ def _show_option(
10841084
),
10851085
)
10861086

1087-
if not isinstance(output_exploded, dict):
1088-
return output_exploded
1087+
if not isinstance(output_exploded, (dict, SparseArray)):
1088+
return t.cast("ConvertedValue", output_exploded)
10891089

1090-
if option not in output_exploded:
1090+
if isinstance(output_exploded, dict) and option not in output_exploded:
10911091
return None
10921092

1093-
return t.cast("t.Optional[ConvertedValue]", output_exploded[option])
1093+
if isinstance(output_exploded, SparseArray):
1094+
try:
1095+
index = int(option)
1096+
return t.cast("ConvertedValue", output_exploded[index])
1097+
except (ValueError, KeyError):
1098+
return None
1099+
1100+
return t.cast("ConvertedValue", output_exploded[option])
10941101

10951102
def show_option(
10961103
self,

0 commit comments

Comments
 (0)