Skip to content

Commit 87bcb59

Browse files
committed
refactor: Add mypy annotations (monkeytype + manual edits)
1 parent 6d951a8 commit 87bcb59

27 files changed

+417
-258
lines changed

conftest.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
@pytest.mark.skipif(USING_ZSH, reason="Using ZSH")
2727
@pytest.fixture(autouse=USING_ZSH, scope="session")
28-
def zshrc(user_path: pathlib.Path):
28+
def zshrc(user_path: pathlib.Path) -> pathlib.Path:
2929
"""This quiets ZSH default message.
3030
3131
Needs a startup file .zshenv, .zprofile, .zshrc, .zlogin.
@@ -41,7 +41,7 @@ def home_path_default(monkeypatch: pytest.MonkeyPatch, user_path: pathlib.Path)
4141

4242

4343
@pytest.fixture(scope="function")
44-
def monkeypatch_plugin_test_packages(monkeypatch):
44+
def monkeypatch_plugin_test_packages(monkeypatch: pytest.MonkeyPatch) -> None:
4545
paths = [
4646
"tests/fixtures/pluginsystem/plugins/tmuxp_test_plugin_bwb/",
4747
"tests/fixtures/pluginsystem/plugins/tmuxp_test_plugin_bs/",
@@ -55,7 +55,7 @@ def monkeypatch_plugin_test_packages(monkeypatch):
5555

5656

5757
@pytest.fixture(scope="function")
58-
def socket_name(request) -> str:
58+
def socket_name(request: pytest.FixtureRequest) -> str:
5959
return "tmuxp_test%s" % next(namer)
6060

6161

src/tmuxp/cli/__init__.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from .load import command_load
2727
from .shell import command_shell
2828
from .utils import tmuxp_echo
29+
from pathlib import PosixPath
2930

3031
logger = logging.getLogger(__name__)
3132

@@ -59,7 +60,7 @@ def cli(log_level):
5960
setup_logger(logger=logger, level=log_level.upper())
6061

6162

62-
def startup(config_dir):
63+
def startup(config_dir: PosixPath) -> None:
6364
"""
6465
Initialize CLI.
6566

src/tmuxp/cli/import_config.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
import pathlib
33
import sys
4+
from typing import Callable
45

56
import click
67

@@ -10,7 +11,7 @@
1011
from .utils import ConfigPath, _validate_choices, get_abs_path, tmuxp_echo
1112

1213

13-
def get_tmuxinator_dir():
14+
def get_tmuxinator_dir() -> str:
1415
"""
1516
Return tmuxinator configuration directory.
1617
@@ -31,7 +32,7 @@ def get_tmuxinator_dir():
3132
return os.path.expanduser("~/.tmuxinator/")
3233

3334

34-
def get_teamocil_dir():
35+
def get_teamocil_dir() -> str:
3536
"""
3637
Return teamocil configuration directory.
3738
@@ -47,7 +48,7 @@ def get_teamocil_dir():
4748
return os.path.expanduser("~/.teamocil/")
4849

4950

50-
def _resolve_path_no_overwrite(config):
51+
def _resolve_path_no_overwrite(config: str) -> str:
5152
path = get_abs_path(config)
5253
if os.path.exists(path):
5354
raise click.exceptions.UsageError("%s exists. Pick a new filename." % path)
@@ -59,7 +60,7 @@ def command_import():
5960
"""Import a teamocil/tmuxinator config."""
6061

6162

62-
def import_config(configfile, importfunc):
63+
def import_config(configfile: str, importfunc: Callable):
6364
existing_config = ConfigReader._from_file(pathlib.Path(configfile))
6465
new_config = ConfigReader(importfunc(existing_config))
6566

src/tmuxp/cli/load.py

+19-18
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,21 @@
1010
import pathlib
1111
import shutil
1212
import sys
13-
from typing import List
13+
from typing import Any, Dict, List, Optional, Union
1414

1515
import click
1616

1717
from libtmux.common import has_gte_version
1818
from libtmux.server import Server
19+
from libtmux.session import Session
1920
from tmuxp import config_reader
2021

2122
from .. import config, exc, log, util
2223
from ..workspacebuilder import WorkspaceBuilder
2324
from .utils import ConfigPath, _validate_choices, get_config_dir, tmuxp_echo
2425

2526

26-
def set_layout_hook(session, hook_name):
27+
def set_layout_hook(session: Session, hook_name: str) -> None:
2728
"""Set layout hooks to normalize layout.
2829
2930
References:
@@ -84,7 +85,7 @@ def set_layout_hook(session, hook_name):
8485
session.cmd(*cmd)
8586

8687

87-
def load_plugins(sconf):
88+
def load_plugins(sconf: Dict[str, Any]) -> List[Any]:
8889
"""
8990
Load and return plugins in config
9091
"""
@@ -119,7 +120,7 @@ def load_plugins(sconf):
119120
return plugins
120121

121122

122-
def _reattach(builder):
123+
def _reattach(builder: WorkspaceBuilder):
123124
"""
124125
Reattach session (depending on env being inside tmux already or not)
125126
@@ -148,7 +149,7 @@ def _reattach(builder):
148149
builder.session.attach_session()
149150

150151

151-
def _load_attached(builder, detached):
152+
def _load_attached(builder: WorkspaceBuilder, detached: bool) -> None:
152153
"""
153154
Load config in new session
154155
@@ -181,7 +182,7 @@ def _load_attached(builder, detached):
181182
builder.session.attach_session()
182183

183184

184-
def _load_detached(builder):
185+
def _load_detached(builder: WorkspaceBuilder) -> None:
185186
"""
186187
Load config in new session but don't attach
187188
@@ -198,7 +199,7 @@ def _load_detached(builder):
198199
print("Session created in detached state.")
199200

200201

201-
def _load_append_windows_to_current_session(builder):
202+
def _load_append_windows_to_current_session(builder: WorkspaceBuilder) -> None:
202203
"""
203204
Load config as new windows in current session
204205
@@ -213,7 +214,7 @@ def _load_append_windows_to_current_session(builder):
213214
set_layout_hook(builder.session, "client-session-changed")
214215

215216

216-
def _setup_plugins(builder):
217+
def _setup_plugins(builder: WorkspaceBuilder) -> Session:
217218
"""
218219
Runs after before_script
219220
@@ -228,16 +229,16 @@ def _setup_plugins(builder):
228229

229230

230231
def load_workspace(
231-
config_file,
232-
socket_name=None,
233-
socket_path=None,
234-
tmux_config_file=None,
235-
new_session_name=None,
236-
colors=None,
237-
detached=False,
238-
answer_yes=False,
239-
append=False,
240-
):
232+
config_file: Union[str, pathlib.PosixPath],
233+
socket_name: Optional[str] = None,
234+
socket_path: None = None,
235+
tmux_config_file: None = None,
236+
new_session_name: Optional[str] = None,
237+
colors: Optional[int] = None,
238+
detached: bool = False,
239+
answer_yes: bool = False,
240+
append: bool = False,
241+
) -> Session:
241242
"""
242243
Load a tmux "workspace" session via tmuxp file.
243244

src/tmuxp/cli/utils.py

+17-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import logging
22
import os
3+
import typing as t
34

45
import click
6+
from click.core import Argument, Context
57
from click.exceptions import FileError
68

79
from .. import config, log
@@ -10,7 +12,12 @@
1012
logger = logging.getLogger(__name__)
1113

1214

13-
def tmuxp_echo(message=None, log_level="INFO", style_log=False, **click_kwargs):
15+
def tmuxp_echo(
16+
message: t.Optional[str] = None,
17+
log_level: str = "INFO",
18+
style_log: bool = False,
19+
**click_kwargs,
20+
) -> None:
1421
"""
1522
Combines logging.log and click.echo
1623
"""
@@ -22,7 +29,7 @@ def tmuxp_echo(message=None, log_level="INFO", style_log=False, **click_kwargs):
2229
click.echo(message, **click_kwargs)
2330

2431

25-
def get_config_dir():
32+
def get_config_dir() -> str:
2633
"""
2734
Return tmuxp configuration directory.
2835
@@ -54,7 +61,7 @@ def get_config_dir():
5461
return path
5562

5663

57-
def _validate_choices(options):
64+
def _validate_choices(options: t.List[str]) -> t.Callable:
5865
"""
5966
Callback wrapper for validating click.prompt input.
6067
@@ -84,11 +91,13 @@ def func(value):
8491

8592

8693
class ConfigPath(click.Path):
87-
def __init__(self, config_dir=None, *args, **kwargs):
94+
def __init__(
95+
self, config_dir: t.Optional[t.Union[t.Callable, str]] = None, *args, **kwargs
96+
) -> None:
8897
super().__init__(*args, **kwargs)
8998
self.config_dir = config_dir
9099

91-
def convert(self, value, param, ctx):
100+
def convert(self, value: str, param: Argument, ctx: Context) -> str:
92101
config_dir = self.config_dir
93102
if callable(config_dir):
94103
config_dir = config_dir()
@@ -118,7 +127,7 @@ def scan_config_argument(ctx, param, value, config_dir=None):
118127
return value
119128

120129

121-
def get_abs_path(config):
130+
def get_abs_path(config: str) -> str:
122131
path = os.path
123132
join, isabs = path.join, path.isabs
124133
dirname, normpath = path.dirname, path.normpath
@@ -131,7 +140,7 @@ def get_abs_path(config):
131140
return config
132141

133142

134-
def scan_config(config, config_dir=None):
143+
def scan_config(config: str, config_dir: t.Optional[str] = None) -> str:
135144
"""
136145
Return the real config path or raise an exception.
137146
@@ -236,7 +245,7 @@ def scan_config(config, config_dir=None):
236245
return config
237246

238247

239-
def is_pure_name(path):
248+
def is_pure_name(path: str) -> bool:
240249
"""
241250
Return True if path is a name and not a file path.
242251

src/tmuxp/config.py

+20-12
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@
66
"""
77
import logging
88
import os
9-
from typing import Dict
9+
from pathlib import PosixPath
10+
from typing import Any, Dict, List, Optional, Union
1011

1112
from . import exc
1213

1314
logger = logging.getLogger(__name__)
1415

1516

16-
def validate_schema(session_config):
17+
def validate_schema(session_config: Any) -> bool:
1718
"""
1819
Return True if config schema is correct.
1920
@@ -45,7 +46,9 @@ def validate_schema(session_config):
4546
return True
4647

4748

48-
def is_config_file(filename, extensions=[".yml", ".yaml", ".json"]):
49+
def is_config_file(
50+
filename: str, extensions: List[str] = [".yml", ".yaml", ".json"]
51+
) -> bool:
4952
"""
5053
Return True if file has a valid config file type.
5154
@@ -65,8 +68,9 @@ def is_config_file(filename, extensions=[".yml", ".yaml", ".json"]):
6568

6669

6770
def in_dir(
68-
config_dir=os.path.expanduser("~/.tmuxp"), extensions=[".yml", ".yaml", ".json"]
69-
):
71+
config_dir: PosixPath = os.path.expanduser("~/.tmuxp"),
72+
extensions: List[str] = [".yml", ".yaml", ".json"],
73+
) -> List[str]:
7074
"""
7175
Return a list of configs in ``config_dir``.
7276
@@ -90,7 +94,7 @@ def in_dir(
9094
return configs
9195

9296

93-
def in_cwd():
97+
def in_cwd() -> List[str]:
9498
"""
9599
Return list of configs in current working directory.
96100
@@ -115,7 +119,7 @@ def in_cwd():
115119
return configs
116120

117121

118-
def expandshell(_path):
122+
def expandshell(_path: str) -> str:
119123
"""
120124
Return expanded path based on user's ``$HOME`` and ``env``.
121125
@@ -134,7 +138,7 @@ def expandshell(_path):
134138
return os.path.expandvars(os.path.expanduser(_path))
135139

136140

137-
def inline(session_config):
141+
def inline(session_config: Dict[str, Any]) -> Any:
138142
"""
139143
Return config in inline form, opposite of :meth:`config.expand`.
140144
@@ -210,7 +214,11 @@ def expand_cmd(p: Dict) -> Dict:
210214
return p
211215

212216

213-
def expand(session_config, cwd=None, parent=None):
217+
def expand(
218+
session_config: Any,
219+
cwd: Optional[Union[str, PosixPath]] = None,
220+
parent: Optional[Any] = None,
221+
) -> Dict[str, Any]:
214222
"""Return config with shorthand and inline properties expanded.
215223
216224
This is necessary to keep the code in the :class:`WorkspaceBuilder` clean
@@ -330,7 +338,7 @@ def expand(session_config, cwd=None, parent=None):
330338
return session_config
331339

332340

333-
def trickle(session_config):
341+
def trickle(session_config: Dict[str, Any]) -> Dict[str, Any]:
334342
"""Return a dict with "trickled down" / inherited config values.
335343
336344
This will only work if config has been expanded to full form with
@@ -414,7 +422,7 @@ def trickle(session_config):
414422
return session_config
415423

416424

417-
def import_tmuxinator(session_config):
425+
def import_tmuxinator(session_config: Dict[str, Any]) -> Dict[str, Any]:
418426
"""Return tmuxp config from a `tmuxinator`_ yaml config.
419427
420428
.. _tmuxinator: https://github.com/aziz/tmuxinator
@@ -508,7 +516,7 @@ def import_tmuxinator(session_config):
508516
return tmuxp_config
509517

510518

511-
def import_teamocil(session_config):
519+
def import_teamocil(session_config: Dict[str, Any]) -> Dict[str, Any]:
512520
"""Return tmuxp config from a `teamocil`_ yaml config.
513521
514522
.. _teamocil: https://github.com/remiprev/teamocil

0 commit comments

Comments
 (0)