Skip to content

Commit b5fff26

Browse files
committed
Increase type hint coverage.
1 parent a6f8691 commit b5fff26

File tree

17 files changed

+94
-75
lines changed

17 files changed

+94
-75
lines changed

repo_helper/cli/commands/add.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def requirement(requirement: str, file: Optional[str] = None) -> int:
127127

128128

129129
@add_command()
130-
def typed():
130+
def typed() -> None:
131131
"""
132132
Add a 'py.typed' file and the associated trove classifier.
133133
"""
@@ -189,7 +189,7 @@ def typed():
189189

190190
@click.argument("version", type=click.STRING, nargs=-1)
191191
@add_command()
192-
def version(version: str):
192+
def version(version: str) -> int:
193193
"""
194194
Add a new Python version to test on.
195195
"""
@@ -210,7 +210,7 @@ def version(version: str):
210210
if not isinstance(data, dict):
211211
return 1
212212

213-
def sort_key(value: str):
213+
def sort_key(value: str) -> str:
214214
if value.endswith("-dev"):
215215
return value[:-4]
216216
else:
@@ -225,3 +225,5 @@ def sort_key(value: str):
225225
rh.target_repo / "repo_helper.yml",
226226
mode='a',
227227
)
228+
229+
return 0

repo_helper/cli/commands/broomstick.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def detox(base_dir: pathlib.Path, quiet: bool = False) -> None:
109109
rmdir(base_dir / ".tox", quiet)
110110

111111

112-
def crack(base_dir: pathlib.Path, quiet: bool = False):
112+
def crack(base_dir: pathlib.Path, quiet: bool = False) -> None:
113113
"""
114114
Removes the ``*.egg-info`` directory.
115115

repo_helper/cli/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def commit_changed_files(
124124

125125

126126
def run_repo_helper(
127-
path,
127+
path: PathLike,
128128
force: bool,
129129
initialise: bool,
130130
commit: Optional[bool],

repo_helper/configupdater2.py

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
Sequence,
7676
Set,
7777
Tuple,
78+
Type,
7879
TypeVar,
7980
Union,
8081
cast
@@ -143,7 +144,7 @@ class Block(ABC):
143144
a reference to a container wherein the object resides.
144145
"""
145146

146-
def __init__(self, container: Container, **kwargs):
147+
def __init__(self, container: Union[Container, List["Block"], None], **kwargs):
147148
self._container = container
148149
self.lines: List[str] = []
149150
self._updated: bool = False
@@ -155,13 +156,13 @@ def __str__(self) -> str:
155156
def __len__(self) -> int:
156157
return len(self.lines)
157158

158-
def __eq__(self, other) -> bool:
159+
def __eq__(self, other) -> bool: # noqa: MAN001
159160
if isinstance(other, self.__class__):
160161
return self.lines == other.lines
161162
else:
162163
return False
163164

164-
def add_line(self, line: str):
165+
def add_line(self, line: str) -> "Block":
165166
"""
166167
Add a line to the current block.
167168
@@ -172,7 +173,7 @@ def add_line(self, line: str):
172173
return self
173174

174175
@property
175-
def container(self) -> Container:
176+
def container(self) -> Union[Container, List["Block"], None]:
176177
return self._container
177178

178179
# @property
@@ -199,7 +200,7 @@ class BlockBuilder:
199200
Builder that injects blocks at a given index position.
200201
"""
201202

202-
def __init__(self, container: Container, idx):
203+
def __init__(self, container: Container, idx: int):
203204
self._container: Container = container
204205
self._idx = idx
205206

@@ -292,7 +293,7 @@ class Comment(Block):
292293
Comment block.
293294
"""
294295

295-
def __init__(self, container=None):
296+
def __init__(self, container: Union[Container, List[Block], None] = None):
296297
super().__init__(container=container)
297298

298299
def __repr__(self) -> str:
@@ -307,7 +308,7 @@ class Space(Block):
307308
def __init__(self, container=None):
308309
super().__init__(container=container)
309310

310-
def __repr__(self):
311+
def __repr__(self) -> str:
311312
return "<Space>"
312313

313314

@@ -326,7 +327,7 @@ def __init__(self, name: str, container, **kwargs):
326327
self._updated = False
327328
super().__init__(container=container, **kwargs)
328329

329-
def add_option(self, entry: "Option"):
330+
def add_option(self, entry: "Option") -> "Section":
330331
"""
331332
Add an Option object to the section.
332333
@@ -338,7 +339,7 @@ def add_option(self, entry: "Option"):
338339
self._structure.append(entry)
339340
return self
340341

341-
def add_comment(self, line: str):
342+
def add_comment(self, line: str) -> "Section":
342343
"""
343344
Add a Comment object to the section.
344345
@@ -353,7 +354,7 @@ def add_comment(self, line: str):
353354
self.last_item.add_line(line)
354355
return self
355356

356-
def add_space(self, line: str):
357+
def add_space(self, line: str) -> "Section":
357358
"""
358359
Add a Space object to the section.
359360
@@ -368,7 +369,7 @@ def add_space(self, line: str):
368369
self.last_item.add_line(line)
369370
return self
370371

371-
def _get_option_idx(self, key):
372+
def _get_option_idx(self, key: str) -> int:
372373
idx = [i for i, entry in enumerate(self._structure) if isinstance(entry, Option) and entry.key == key]
373374
if idx:
374375
return idx[0]
@@ -391,12 +392,12 @@ def __str__(self) -> str:
391392
def __repr__(self) -> str:
392393
return f"<Section: {self.name}>"
393394

394-
def __getitem__(self, key) -> "Option":
395+
def __getitem__(self, key: str): # noqa: MAN002
395396
if key not in self.options():
396397
raise KeyError(key)
397398
return self._structure[self._get_option_idx(key=key)]
398399

399-
def __setitem__(self, key, value):
400+
def __setitem__(self, key: str, value: Any) -> None:
400401
str_value = convert_to_string(value, key)
401402

402403
if key in self:
@@ -407,26 +408,26 @@ def __setitem__(self, key, value):
407408
option.value = str_value
408409
self._structure.append(option)
409410

410-
def __delitem__(self, key):
411+
def __delitem__(self, key: str) -> None:
411412
if key not in self.options():
412413
raise KeyError(key)
413414
idx = self._get_option_idx(key=key)
414415
del self._structure[idx]
415416

416-
def __contains__(self, key):
417+
def __contains__(self, key) -> bool: # noqa: MAN001
417418
return key in self.options()
418419

419420
def __len__(self) -> int:
420421
return len(self._structure)
421422

422-
def __iter__(self):
423+
def __iter__(self) -> Iterator[Block]:
423424
"""
424425
Return all entries, not just options.
425426
"""
426427

427428
return self._structure.__iter__()
428429

429-
def __eq__(self, other) -> bool:
430+
def __eq__(self, other) -> bool: # noqa: MAN001
430431
if isinstance(other, self.__class__):
431432
return self.name == other.name and self._structure == other._structure
432433
else:
@@ -471,7 +472,7 @@ def name(self) -> str:
471472
return self._name
472473

473474
@name.setter
474-
def name(self, value):
475+
def name(self, value: str) -> None:
475476
self._name = str(value)
476477
self._updated = True
477478

@@ -513,10 +514,10 @@ def __init__(
513514
self,
514515
key: str,
515516
value: Optional[str],
516-
container,
517+
container: Container,
517518
delimiter: str = '=',
518519
space_around_delimiters: bool = True,
519-
line=None,
520+
line: Optional[str] = None,
520521
):
521522
super().__init__(container=container)
522523
self._key: str = key
@@ -530,9 +531,10 @@ def __init__(
530531
if line:
531532
self.lines.append(line)
532533

533-
def add_line(self, line) -> None:
534+
def add_line(self, line: str) -> "Option":
534535
super().add_line(line)
535536
self._values.append(line.strip())
537+
return self
536538

537539
def _join_multiline_value(self) -> None:
538540
if not self._multiline_value_joined and not self._value_is_none:
@@ -570,7 +572,7 @@ def key(self) -> str:
570572
return self._key
571573

572574
@key.setter
573-
def key(self, value: str):
575+
def key(self, value: str) -> None:
574576
self._join_multiline_value()
575577
self._key = value
576578
self._updated = True
@@ -581,7 +583,7 @@ def value(self) -> str:
581583
return cast(str, self._value)
582584

583585
@value.setter
584-
def value(self, value: str):
586+
def value(self, value: str) -> None:
585587
self._updated = True
586588
self._multiline_value_joined = True
587589
self._value = value
@@ -695,14 +697,14 @@ def __init__(
695697
self._empty_lines_in_values = False
696698
super().__init__()
697699

698-
def _get_section_idx(self, name):
700+
def _get_section_idx(self, name: str) -> int:
699701
idx = [i for i, entry in enumerate(self._structure) if isinstance(entry, Section) and entry.name == name]
700702
if idx:
701703
return idx[0]
702704
else:
703705
raise ValueError
704706

705-
def read(self, filename: PathLike, encoding: Optional[str] = "UTF-8"):
707+
def read(self, filename: PathLike, encoding: Optional[str] = "UTF-8") -> None:
706708
"""
707709
Read and parse a filename.
708710
@@ -711,11 +713,11 @@ def read(self, filename: PathLike, encoding: Optional[str] = "UTF-8"):
711713
"""
712714

713715
with open(filename, encoding=encoding) as fp:
714-
self._read(fp, filename)
716+
self._read(fp, os.fspath(filename))
715717

716718
self._filename = os.path.abspath(filename)
717719

718-
def read_file(self, f: IO, source: Optional[str] = None):
720+
def read_file(self, f: IO, source: Optional[str] = None) -> None:
719721
"""
720722
Like read() but the argument must be a file-like object.
721723
@@ -736,7 +738,7 @@ def read_file(self, f: IO, source: Optional[str] = None):
736738
source = "<???>"
737739
self._read(f, source)
738740

739-
def read_string(self, string: str, source: str = "<string>"):
741+
def read_string(self, string: str, source: str = "<string>") -> None:
740742
"""
741743
Read configuration from a given string.
742744
@@ -756,12 +758,12 @@ def optionxform(self, optionstr: str) -> str:
756758
"""
757759
return optionstr.lower()
758760

759-
def _update_curr_block(self, block_type):
761+
def _update_curr_block(self, block_type: Type[Block]) -> None:
760762
if not isinstance(self.last_item, block_type):
761763
new_block = block_type(container=self)
762764
self._structure.append(new_block)
763765

764-
def _add_comment(self, line):
766+
def _add_comment(self, line: str) -> None:
765767
if isinstance(self.last_item, Section):
766768
self.last_item.add_comment(line)
767769
elif self.last_item is not None:
@@ -770,12 +772,12 @@ def _add_comment(self, line):
770772
# else:
771773
# raise ValueError("Cannot add a comment without somewhere to add it to.")
772774

773-
def _add_section(self, sectname, line):
775+
def _add_section(self, sectname: str, line: str) -> None:
774776
new_section = Section(sectname, container=self)
775777
new_section.add_line(line)
776778
self._structure.append(new_section)
777779

778-
def _add_option(self, key, vi, value, line):
780+
def _add_option(self, key: str, vi: str, value: Optional[str], line: str) -> None:
779781
entry = Option(
780782
key,
781783
value,
@@ -786,14 +788,14 @@ def _add_option(self, key, vi, value, line):
786788
)
787789
self.last_item.add_option(entry)
788790

789-
def _add_space(self, line):
791+
def _add_space(self, line: str) -> None:
790792
if isinstance(self.last_item, Section):
791793
self.last_item.add_space(line)
792794
else:
793795
self._update_curr_block(Space)
794796
self.last_item.add_line(line)
795797

796-
def _read(self, fp, fpname):
798+
def _read(self, fp: IO, fpname: str) -> None:
797799
"""
798800
Parse a sectioned configuration file.
799801
@@ -928,7 +930,7 @@ def _read(self, fp, fpname):
928930
if e:
929931
raise e
930932

931-
def _handle_error(self, exc, fpname, lineno, line):
933+
def _handle_error(self, exc: Optional[ParsingError], fpname: str, lineno: int, line: str) -> ParsingError:
932934
if not exc:
933935
exc = ParsingError(fpname)
934936
exc.append(lineno, repr(line))
@@ -1013,7 +1015,7 @@ def __delitem__(self, section: str) -> None:
10131015
raise KeyError(section)
10141016
self.remove_section(section)
10151017

1016-
def __contains__(self, section) -> bool:
1018+
def __contains__(self, section) -> bool: # noqa: MAN001
10171019
"""
10181020
Returns whether the given section exists.
10191021
"""
@@ -1034,13 +1036,13 @@ def __iter__(self) -> Iterator[Block]:
10341036

10351037
return self._structure.__iter__()
10361038

1037-
def __eq__(self, other) -> bool:
1039+
def __eq__(self, other) -> bool: # noqa: MAN001
10381040
if isinstance(other, self.__class__):
10391041
return self._structure == other._structure
10401042
else:
10411043
return False
10421044

1043-
def add_section(self, section: Union[str, Section]):
1045+
def add_section(self, section: Union[str, Section]) -> None:
10441046
"""Create a new section in the configuration.
10451047
10461048
:raises: :exc:`~.DuplicateSectionError` if a section by the specified name already exists.
@@ -1070,7 +1072,7 @@ def options(self, section: str) -> List[str]:
10701072
raise NoSectionError(section) from None
10711073
return self[section].options()
10721074

1073-
def get(self, section: str, option: str): # type: ignore[override]
1075+
def get(self, section: str, option: str) -> "Option": # type: ignore[override]
10741076
"""Gets an option value for a given section.
10751077
10761078
:param section: section name
@@ -1198,7 +1200,7 @@ def to_dict(self) -> Dict:
11981200
return {sect: self[sect].to_dict() for sect in self.sections()}
11991201

12001202

1201-
def convert_to_string(value, key):
1203+
def convert_to_string(value: Any, key: str) -> str:
12021204
if isinstance(value, str):
12031205

12041206
split_lines = value.split('\n')

0 commit comments

Comments
 (0)