Skip to content

Commit 7a93c9a

Browse files
authored
Docstring updates, bolster linting (#514)
- Extracted from #513 # Changes ## docstring fixes Server, Session, Window, Pane, and common docstring fixes ## ruff linting ### flake8-commas (COM) - https://docs.astral.sh/ruff/rules/#flake8-commas-com - https://pypi.org/project/flake8-commas/ ### flake8-builtins (A) - https://docs.astral.sh/ruff/rules/#flake8-builtins-a - https://pypi.org/project/flake8-builtins/ ### flake8-errmsg (EM) - https://docs.astral.sh/ruff/rules/#flake8-errmsg-em - https://pypi.org/project/flake8-errmsg/
2 parents dbdc181 + 0c7db83 commit 7a93c9a

25 files changed

+247
-192
lines changed

CHANGES

+23
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,29 @@ $ pip install --user --upgrade --pre libtmux
1414

1515
<!-- Maintainers and contributors: Insert change notes for the next release above -->
1616

17+
### Documentation
18+
19+
- Various docstring fixes and tweaks (#514)
20+
21+
### Development
22+
23+
- Strengthen linting (#514)
24+
25+
- Add flake8-commas (COM)
26+
27+
- https://docs.astral.sh/ruff/rules/#flake8-commas-com
28+
- https://pypi.org/project/flake8-commas/
29+
30+
- Add flake8-builtins (A)
31+
32+
- https://docs.astral.sh/ruff/rules/#flake8-builtins-a
33+
- https://pypi.org/project/flake8-builtins/
34+
35+
- Add flake8-errmsg (EM)
36+
37+
- https://docs.astral.sh/ruff/rules/#flake8-errmsg-em
38+
- https://pypi.org/project/flake8-errmsg/
39+
1740
### CI
1841

1942
- Move CodeQL from advanced configuration file to GitHub's default

docs/conf.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
master_doc = "index"
5555

5656
project = about["__title__"]
57-
copyright = about["__copyright__"]
57+
project_copyright = about["__copyright__"]
5858

5959
version = "%s" % (".".join(about["__version__"].split("."))[:2])
6060
release = "%s" % (about["__version__"])
@@ -98,7 +98,7 @@
9898
"sidebar/navigation.html",
9999
"sidebar/projects.html",
100100
"sidebar/scroll-end.html",
101-
]
101+
],
102102
}
103103

104104
# linkify_issues

pyproject.toml

+6
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,11 @@ select = [
122122
"F", # pyflakes
123123
"I", # isort
124124
"UP", # pyupgrade
125+
"A", # flake8-builtins
125126
"B", # flake8-bugbear
126127
"C4", # flake8-comprehensions
128+
"COM", # flake8-commas
129+
"EM", # flake8-errmsg
127130
"Q", # flake8-quotes
128131
"PTH", # flake8-use-pathlib
129132
"SIM", # flake8-simplify
@@ -132,6 +135,9 @@ select = [
132135
"RUF", # Ruff-specific rules
133136
"D", # pydocstyle
134137
]
138+
ignore = [
139+
"COM812", # missing trailing comma, ruff format conflict
140+
]
135141

136142
[tool.ruff.lint.isort]
137143
known-first-party = [

src/libtmux/_internal/query_list.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ def keygetter(
6666

6767

6868
def parse_lookup(
69-
obj: "Mapping[str, t.Any]", path: str, lookup: str
69+
obj: "Mapping[str, t.Any]",
70+
path: str,
71+
lookup: str,
7072
) -> t.Optional[t.Any]:
7173
"""Check if field lookup key, e.g. "my__path__contains" has comparator, return val.
7274

src/libtmux/_vendor/version.py

+13-5
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,18 @@
3333
],
3434
]
3535
CmpKey = Tuple[
36-
int, Tuple[int, ...], PrePostDevType, PrePostDevType, PrePostDevType, LocalType
36+
int,
37+
Tuple[int, ...],
38+
PrePostDevType,
39+
PrePostDevType,
40+
PrePostDevType,
41+
LocalType,
3742
]
3843
VersionComparisonMethod = Callable[[CmpKey, CmpKey], bool]
3944

4045
_Version = collections.namedtuple(
41-
"_Version", ["epoch", "release", "dev", "pre", "post", "local"]
46+
"_Version",
47+
["epoch", "release", "dev", "pre", "post", "local"],
4248
)
4349

4450

@@ -220,7 +226,8 @@ def __init__(self, version: str) -> None:
220226
release=tuple(int(i) for i in match.group("release").split(".")),
221227
pre=_parse_letter_version(match.group("pre_l"), match.group("pre_n")),
222228
post=_parse_letter_version(
223-
match.group("post_l"), match.group("post_n1") or match.group("post_n2")
229+
match.group("post_l"),
230+
match.group("post_n1") or match.group("post_n2"),
224231
),
225232
dev=_parse_letter_version(match.group("dev_l"), match.group("dev_n")),
226233
local=_parse_local_version(match.group("local")),
@@ -468,7 +475,8 @@ def micro(self) -> int:
468475

469476

470477
def _parse_letter_version(
471-
letter: str, number: Union[str, bytes, SupportsInt]
478+
letter: str,
479+
number: Union[str, bytes, SupportsInt],
472480
) -> Optional[Tuple[str, int]]:
473481
if letter:
474482
# We consider there to be an implicit 0 in a pre-release if there is
@@ -529,7 +537,7 @@ def _cmpkey(
529537
# re-reverse it back into the correct order and make it a tuple and use
530538
# that for our sorting key.
531539
_release = tuple(
532-
reversed(list(itertools.dropwhile(lambda x: x == 0, reversed(release))))
540+
reversed(list(itertools.dropwhile(lambda x: x == 0, reversed(release)))),
533541
)
534542

535543
# We need to "trick" the sorting algorithm to put 1.0.dev0 before 1.0a0.

src/libtmux/common.py

+33-38
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ def __init__(self, add_option: Optional[str] = None) -> None:
4646
self._add_option = add_option
4747

4848
def set_environment(self, name: str, value: str) -> None:
49-
"""
50-
Set environment ``$ tmux set-environment <name> <value>``.
49+
"""Set environment ``$ tmux set-environment <name> <value>``.
5150
5251
Parameters
5352
----------
@@ -73,8 +72,7 @@ def set_environment(self, name: str, value: str) -> None:
7372
raise ValueError("tmux set-environment stderr: %s" % cmd.stderr)
7473

7574
def unset_environment(self, name: str) -> None:
76-
"""
77-
Unset environment variable ``$ tmux set-environment -u <name>``.
75+
"""Unset environment variable ``$ tmux set-environment -u <name>``.
7876
7977
Parameters
8078
----------
@@ -139,17 +137,17 @@ def show_environment(self) -> Dict[str, Union[bool, str]]:
139137
tmux_args += [self._add_option]
140138
cmd = self.cmd(*tmux_args)
141139
output = cmd.stdout
142-
vars = [tuple(item.split("=", 1)) for item in output]
143-
vars_dict: t.Dict[str, t.Union[str, bool]] = {}
144-
for _t in vars:
140+
opts = [tuple(item.split("=", 1)) for item in output]
141+
opts_dict: t.Dict[str, t.Union[str, bool]] = {}
142+
for _t in opts:
145143
if len(_t) == 2:
146-
vars_dict[_t[0]] = _t[1]
144+
opts_dict[_t[0]] = _t[1]
147145
elif len(_t) == 1:
148-
vars_dict[_t[0]] = True
146+
opts_dict[_t[0]] = True
149147
else:
150148
raise exc.VariableUnpackingError(variable=_t)
151149

152-
return vars_dict
150+
return opts_dict
153151

154152
def getenv(self, name: str) -> Optional[t.Union[str, bool]]:
155153
"""Show environment variable ``$ tmux show-environment -t [session] <name>``.
@@ -176,22 +174,21 @@ def getenv(self, name: str) -> Optional[t.Union[str, bool]]:
176174
tmux_args += (name,)
177175
cmd = self.cmd(*tmux_args)
178176
output = cmd.stdout
179-
vars = [tuple(item.split("=", 1)) for item in output]
180-
vars_dict: t.Dict[str, t.Union[str, bool]] = {}
181-
for _t in vars:
177+
opts = [tuple(item.split("=", 1)) for item in output]
178+
opts_dict: t.Dict[str, t.Union[str, bool]] = {}
179+
for _t in opts:
182180
if len(_t) == 2:
183-
vars_dict[_t[0]] = _t[1]
181+
opts_dict[_t[0]] = _t[1]
184182
elif len(_t) == 1:
185-
vars_dict[_t[0]] = True
183+
opts_dict[_t[0]] = True
186184
else:
187185
raise exc.VariableUnpackingError(variable=_t)
188186

189-
return vars_dict.get(name)
187+
return opts_dict.get(name)
190188

191189

192190
class tmux_cmd:
193-
"""
194-
:term:`tmux(1)` command via :py:mod:`subprocess`.
191+
""":term:`tmux(1)` command via :py:mod:`subprocess`.
195192
196193
Examples
197194
--------
@@ -231,7 +228,9 @@ def __init__(self, *args: t.Any, **kwargs: t.Any) -> None:
231228

232229
try:
233230
self.process = subprocess.Popen(
234-
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE
231+
cmd,
232+
stdout=subprocess.PIPE,
233+
stderr=subprocess.PIPE,
235234
)
236235
stdout, stderr = self.process.communicate()
237236
returncode = self.process.returncode
@@ -258,14 +257,14 @@ def __init__(self, *args: t.Any, **kwargs: t.Any) -> None:
258257

259258
logger.debug(
260259
"self.stdout for {cmd}: {stdout}".format(
261-
cmd=" ".join(cmd), stdout=self.stdout
262-
)
260+
cmd=" ".join(cmd),
261+
stdout=self.stdout,
262+
),
263263
)
264264

265265

266266
def get_version() -> LooseVersion:
267-
"""
268-
Return tmux version.
267+
"""Return tmux version.
269268
270269
If tmux is built from git master, the version returned will be the latest
271270
version appended with -master, e.g. ``2.4-master``.
@@ -285,7 +284,7 @@ def get_version() -> LooseVersion:
285284
return LooseVersion("%s-openbsd" % TMUX_MAX_VERSION)
286285
raise exc.LibTmuxException(
287286
"libtmux supports tmux %s and greater. This system"
288-
" is running tmux 1.3 or earlier." % TMUX_MIN_VERSION
287+
" is running tmux 1.3 or earlier." % TMUX_MIN_VERSION,
289288
)
290289
raise exc.VersionTooLow(proc.stderr)
291290

@@ -301,8 +300,7 @@ def get_version() -> LooseVersion:
301300

302301

303302
def has_version(version: str) -> bool:
304-
"""
305-
Return affirmative if tmux version installed.
303+
"""Return True if tmux version installed.
306304
307305
Parameters
308306
----------
@@ -318,8 +316,7 @@ def has_version(version: str) -> bool:
318316

319317

320318
def has_gt_version(min_version: str) -> bool:
321-
"""
322-
Return affirmative if tmux version greater than minimum.
319+
"""Return True if tmux version greater than minimum.
323320
324321
Parameters
325322
----------
@@ -335,8 +332,7 @@ def has_gt_version(min_version: str) -> bool:
335332

336333

337334
def has_gte_version(min_version: str) -> bool:
338-
"""
339-
Return True if tmux version greater or equal to minimum.
335+
"""Return True if tmux version greater or equal to minimum.
340336
341337
Parameters
342338
----------
@@ -352,8 +348,7 @@ def has_gte_version(min_version: str) -> bool:
352348

353349

354350
def has_lte_version(max_version: str) -> bool:
355-
"""
356-
Return True if tmux version less or equal to minimum.
351+
"""Return True if tmux version less or equal to minimum.
357352
358353
Parameters
359354
----------
@@ -369,8 +364,7 @@ def has_lte_version(max_version: str) -> bool:
369364

370365

371366
def has_lt_version(max_version: str) -> bool:
372-
"""
373-
Return True if tmux version less than minimum.
367+
"""Return True if tmux version less than minimum.
374368
375369
Parameters
376370
----------
@@ -386,8 +380,7 @@ def has_lt_version(max_version: str) -> bool:
386380

387381

388382
def has_minimum_version(raises: bool = True) -> bool:
389-
"""
390-
Return if tmux meets version requirement. Version >1.8 or above.
383+
"""Return True if tmux meets version requirement. Version >1.8 or above.
391384
392385
Parameters
393386
----------
@@ -416,12 +409,14 @@ def has_minimum_version(raises: bool = True) -> bool:
416409
"""
417410
if get_version() < LooseVersion(TMUX_MIN_VERSION):
418411
if raises:
419-
raise exc.VersionTooLow(
412+
msg = (
420413
"libtmux only supports tmux {} and greater. This system"
421414
" has {} installed. Upgrade your tmux to use libtmux.".format(
422-
TMUX_MIN_VERSION, get_version()
415+
TMUX_MIN_VERSION,
416+
get_version(),
423417
)
424418
)
419+
raise exc.VersionTooLow(msg)
425420
else:
426421
return False
427422
return True

src/libtmux/exc.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def __init__(
3838
if all(arg is not None for arg in [obj_key, obj_id, list_cmd, list_extra_args]):
3939
return super().__init__(
4040
f"Could not find {obj_key}={obj_id} for {list_cmd} "
41-
f'{list_extra_args if list_extra_args is not None else ""}'
41+
f'{list_extra_args if list_extra_args is not None else ""}',
4242
)
4343
return super().__init__("Could not find object")
4444

@@ -51,7 +51,10 @@ class BadSessionName(LibTmuxException):
5151
"""Disallowed session name for tmux (empty, contains periods or colons)."""
5252

5353
def __init__(
54-
self, reason: str, session_name: t.Optional[str] = None, *args: object
54+
self,
55+
reason: str,
56+
session_name: t.Optional[str] = None,
57+
*args: object,
5558
):
5659
msg = f"Bad session name: {reason}"
5760
if session_name is not None:

src/libtmux/neo.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,10 @@ def _refresh(
170170
) -> None:
171171
assert isinstance(obj_id, str)
172172
obj = fetch_obj(
173-
obj_key=obj_key, obj_id=obj_id, list_cmd=list_cmd, server=self.server
173+
obj_key=obj_key,
174+
obj_id=obj_id,
175+
list_cmd=list_cmd,
176+
server=self.server,
174177
)
175178
assert obj is not None
176179
if obj is not None:
@@ -233,7 +236,9 @@ def fetch_obj(
233236
) -> OutputRaw:
234237
"""Fetch raw data from tmux command."""
235238
obj_formatters_filtered = fetch_objs(
236-
server=server, list_cmd=list_cmd, list_extra_args=list_extra_args
239+
server=server,
240+
list_cmd=list_cmd,
241+
list_extra_args=list_extra_args,
237242
)
238243

239244
obj = None

0 commit comments

Comments
 (0)