Skip to content

Commit 9d1f14e

Browse files
committed
refactor(mypy) Adopt Python 3.10 typing idioms
- replace remaining Optional/Union patterns with PEP 604 unions aligned to the new baseline - guard typing_extensions imports under TYPE_CHECKING while dropping obsolete version branches - add explicit strict=False to zip-based format parsing to satisfy Ruff B905 uv run ruff check . --fix --show-fixes uv run ruff format . uv run mypy uv run py.test
1 parent 46c5c4c commit 9d1f14e

File tree

9 files changed

+28
-42
lines changed

9 files changed

+28
-42
lines changed

src/libtmux/_internal/query_list.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ def __eq__(
492492
return False
493493

494494
if len(self) == len(data):
495-
for a, b in zip(self, data):
495+
for a, b in zip(self, data, strict=False):
496496
if isinstance(a, Mapping):
497497
a_keys = a.keys()
498498
if a.keys == b.keys():

src/libtmux/_vendor/version.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,13 @@
2121

2222
__all__ = ["VERSION_PATTERN", "InvalidVersion", "Version", "parse"]
2323

24-
InfiniteTypes = t.Union[InfinityType, NegativeInfinityType]
25-
PrePostDevType = t.Union[InfiniteTypes, tuple[str, int]]
26-
SubLocalType = t.Union[InfiniteTypes, int, str]
27-
LocalType = t.Union[
28-
NegativeInfinityType,
29-
tuple[
30-
SubLocalType
31-
| tuple[SubLocalType, str]
32-
| tuple[NegativeInfinityType, SubLocalType],
33-
...,
34-
],
35-
]
24+
InfiniteTypes = InfinityType | NegativeInfinityType
25+
PrePostDevType = InfiniteTypes | tuple[str, int]
26+
SubLocalType = InfiniteTypes | int | str
27+
LocalTuple = (
28+
SubLocalType | tuple[SubLocalType, str] | tuple[NegativeInfinityType, SubLocalType]
29+
)
30+
LocalType = NegativeInfinityType | tuple[LocalTuple, ...]
3631
CmpKey = tuple[
3732
int,
3833
tuple[int, ...],

src/libtmux/neo.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
if t.TYPE_CHECKING:
1515
ListCmd = t.Literal["list-sessions", "list-windows", "list-panes"]
16-
ListExtraArgs = t.Optional[Iterable[str]]
16+
ListExtraArgs = Iterable[str] | None
1717

1818
from libtmux.server import Server
1919

@@ -174,7 +174,7 @@ def _refresh(
174174
obj_key: str,
175175
obj_id: str,
176176
list_cmd: ListCmd = "list-panes",
177-
list_extra_args: ListExtraArgs | None = None,
177+
list_extra_args: ListExtraArgs = None,
178178
) -> None:
179179
assert isinstance(obj_id, str)
180180
obj = fetch_obj(
@@ -193,7 +193,7 @@ def _refresh(
193193
def fetch_objs(
194194
server: Server,
195195
list_cmd: ListCmd,
196-
list_extra_args: ListExtraArgs | None = None,
196+
list_extra_args: ListExtraArgs = None,
197197
) -> OutputsRaw:
198198
"""Fetch a listing of raw data from a tmux command."""
199199
formats = list(Obj.__dataclass_fields__.keys())
@@ -224,7 +224,7 @@ def fetch_objs(
224224
obj_output = proc.stdout
225225

226226
obj_formatters = [
227-
dict(zip(formats, formatter.split(FORMAT_SEPARATOR)))
227+
dict(zip(formats, formatter.split(FORMAT_SEPARATOR), strict=False))
228228
for formatter in obj_output
229229
]
230230

@@ -237,7 +237,7 @@ def fetch_obj(
237237
obj_key: str,
238238
obj_id: str,
239239
list_cmd: ListCmd = "list-panes",
240-
list_extra_args: ListExtraArgs | None = None,
240+
list_extra_args: ListExtraArgs = None,
241241
) -> OutputRaw:
242242
"""Fetch raw data from tmux command."""
243243
obj_formatters_filtered = fetch_objs(

src/libtmux/pane.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,9 @@ def split(
704704

705705
pane_output = pane_cmd.stdout[0]
706706

707-
pane_formatters = dict(zip(["pane_id"], pane_output.split(FORMAT_SEPARATOR)))
707+
pane_formatters = dict(
708+
zip(["pane_id"], pane_output.split(FORMAT_SEPARATOR), strict=False),
709+
)
708710

709711
return self.from_pane_id(server=self.server, pane_id=pane_formatters["pane_id"])
710712

src/libtmux/server.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,12 @@
3333
)
3434

3535
if t.TYPE_CHECKING:
36-
import sys
3736
import types
37+
from typing import TypeAlias
3838

39-
from libtmux._internal.types import StrPath
40-
41-
if sys.version_info >= (3, 10):
42-
from typing import Self, TypeAlias
43-
else:
44-
from typing import TypeAlias
39+
from typing_extensions import Self
4540

46-
from typing_extensions import Self
41+
from libtmux._internal.types import StrPath
4742

4843
DashLiteral: TypeAlias = t.Literal["-"]
4944

@@ -582,7 +577,11 @@ def new_session(
582577
os.environ["TMUX"] = env
583578

584579
session_formatters = dict(
585-
zip(["session_id"], session_stdout.split(formats.FORMAT_SEPARATOR)),
580+
zip(
581+
["session_id"],
582+
session_stdout.split(formats.FORMAT_SEPARATOR),
583+
strict=False,
584+
),
586585
)
587586

588587
return Session.from_session_id(

src/libtmux/session.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,7 @@ def new_window(
731731
window_output = cmd.stdout[0]
732732

733733
window_formatters = dict(
734-
zip(["window_id"], window_output.split(FORMAT_SEPARATOR)),
734+
zip(["window_id"], window_output.split(FORMAT_SEPARATOR), strict=False),
735735
)
736736

737737
return Window.from_window_id(

tests/legacy_api/test_version.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,8 @@
1111
from libtmux._compat import LooseVersion
1212

1313
if t.TYPE_CHECKING:
14-
import sys
1514
from collections.abc import Callable
16-
17-
if sys.version_info >= (3, 10):
18-
from typing import TypeAlias
19-
else:
20-
from typing import TypeAlias
15+
from typing import TypeAlias
2116

2217
try:
2318
from _pytest.raises import RaisesExc

tests/test_dataclasses.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from libtmux.server import Server
2323

2424
ListCmd = t.Literal["list-sessions", "list-windows", "list-panes"]
25-
ListExtraArgs = t.Optional[tuple[str]]
25+
ListExtraArgs = tuple[str] | None
2626

2727

2828
OutputRaw = dict[str, t.Any]

tests/test_version.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,8 @@
1111
from libtmux._compat import LooseVersion
1212

1313
if t.TYPE_CHECKING:
14-
import sys
1514
from collections.abc import Callable
16-
17-
if sys.version_info >= (3, 10):
18-
from typing import TypeAlias
19-
else:
20-
from typing import TypeAlias
15+
from typing import TypeAlias
2116

2217
try:
2318
from _pytest.raises import RaisesExc

0 commit comments

Comments
 (0)