Skip to content

Commit 4df57ba

Browse files
authored
de-glob mypy whitelist and improve type annotations (#139)
1 parent cab674a commit 4df57ba

22 files changed

+112
-66
lines changed

cleo/_utils.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import math
22

33
from html.parser import HTMLParser
4+
from typing import Any
45
from typing import List
56

67
from pylev import levenshtein
@@ -34,7 +35,7 @@ def _strip(value) -> str:
3435
return s.get_data()
3536

3637

37-
def strip_tags(value):
38+
def strip_tags(value: Any) -> str:
3839
value = str(value)
3940
while "<" in value and ">" in value:
4041
new_value = _strip(value)
@@ -46,7 +47,7 @@ def strip_tags(value):
4647
return value
4748

4849

49-
def find_similar_names(name, names): # type: (str, List[str]) -> List[str]
50+
def find_similar_names(name: str, names: List[str]) -> List[str]:
5051
"""
5152
Finds names similar to a given command name.
5253
"""

cleo/application.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,21 +60,21 @@ class Application:
6060
def __init__(self, name: str = "console", version: str = "") -> None:
6161
self._name = name
6262
self._version = version
63-
self._display_name = None
63+
self._display_name: Optional[str] = None
6464
self._terminal = Terminal()
6565
self._default_command = "list"
6666
self._single_command = False
6767
self._commands: Dict[str, Command] = {}
6868
self._running_command = None
6969
self._want_helps = False
70-
self._definition = None
70+
self._definition: Optional[Definition] = None
7171
self._catch_exceptions = True
7272
self._auto_exit = True
7373
self._initialized = False
74-
self._ui = None
74+
self._ui: Optional[UI] = None
7575

7676
# TODO: signals support
77-
self._event_dispatcher = None
77+
self._event_dispatcher: Optional[EventDispatcher] = None
7878

7979
self._command_loader: Optional[CommandLoader] = None
8080

@@ -185,7 +185,7 @@ def add(self, command: Command) -> Optional[Command]:
185185
if not command.enabled:
186186
command.set_application()
187187

188-
return
188+
return None
189189

190190
if not command.name:
191191
raise LogicException(

cleo/color.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,12 @@ def _degrade_hex_color_to_ansi(self, r: int, g: int, b: int) -> int:
141141
return (round(b / 255) << 2) | (round(g / 255) << 1) | round(r / 255)
142142

143143
def _get_saturation(self, r: int, g: int, b: int) -> int:
144-
r = r / 255
145-
g = g / 255
146-
b = b / 255
147-
v = max(r, g, b)
144+
r_float = r / 255
145+
g_float = g / 255
146+
b_float = b / 255
147+
v = max(r_float, g_float, b_float)
148148

149-
diff = v - min(r, g, b)
149+
diff = v - min(r_float, g_float, b_float)
150150
if diff == 0:
151151
return 0
152152

cleo/commands/base_command.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import inspect
22

33
from typing import TYPE_CHECKING
4+
from typing import Dict
5+
from typing import List
46
from typing import Optional
57

68
from cleo.exceptions import CleoException
@@ -14,7 +16,7 @@
1416

1517
class BaseCommand:
1618

17-
name = None
19+
name: Optional[str] = None
1820

1921
description = ""
2022

@@ -23,14 +25,14 @@ class BaseCommand:
2325
enabled = True
2426
hidden = False
2527

26-
usages = []
28+
usages: List[str] = []
2729

2830
def __init__(self) -> None:
2931
self._definition = Definition()
3032
self._full_definition = None
3133
self._application = None
3234
self._ignore_validation_errors = False
33-
self._synopsis = {}
35+
self._synopsis: Dict[str, str] = {}
3436

3537
self.configure()
3638

cleo/commands/command.py

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from cleo.io.null_io import NullIO
1414
from cleo.io.outputs.output import Verbosity
1515
from cleo.parser import Parser
16+
from cleo.ui.table_separator import TableSeparator
1617

1718
from .base_command import BaseCommand
1819

@@ -34,7 +35,7 @@ class Command(BaseCommand):
3435

3536
commands = []
3637

37-
def __init__(self):
38+
def __init__(self) -> None:
3839
self._io: Optional[IO] = None
3940
super().__init__()
4041

@@ -222,23 +223,23 @@ def table(self, header=None, rows=None, style=None):
222223

223224
return table
224225

225-
def table_separator(self):
226+
def table_separator(self) -> "TableSeparator":
226227
"""
227228
Return a TableSeparator instance.
228229
"""
229230
from cleo.ui.table_separator import TableSeparator
230231

231232
return TableSeparator()
232233

233-
def render_table(self, headers, rows, style=None):
234+
def render_table(self, headers, rows, style=None) -> None:
234235
"""
235236
Format input to textual table.
236237
"""
237238
table = self.table(headers, rows, style)
238239

239240
table.render()
240241

241-
def write(self, text, style=None):
242+
def write(self, text: str, style: Optional[str] = None) -> None:
242243
"""
243244
Writes a string without a new line.
244245
Useful if you want to use overwrite().
@@ -255,7 +256,7 @@ def line(
255256
text: str,
256257
style: Optional[str] = None,
257258
verbosity: Verbosity = Verbosity.NORMAL,
258-
):
259+
) -> None:
259260
"""
260261
Write a string as information output.
261262
"""
@@ -271,7 +272,7 @@ def line_error(
271272
text: str,
272273
style: Optional[str] = None,
273274
verbosity: Verbosity = Verbosity.NORMAL,
274-
):
275+
) -> None:
275276
"""
276277
Write a string as information output to stderr.
277278
"""
@@ -282,7 +283,7 @@ def line_error(
282283

283284
self._io.write_error_line(styled, verbosity)
284285

285-
def info(self, text):
286+
def info(self, text: str) -> None:
286287
"""
287288
Write a string as information output.
288289
@@ -291,7 +292,7 @@ def info(self, text):
291292
"""
292293
self.line(text, "info")
293294

294-
def comment(self, text):
295+
def comment(self, text: str) -> None:
295296
"""
296297
Write a string as comment output.
297298
@@ -300,7 +301,7 @@ def comment(self, text):
300301
"""
301302
self.line(text, "comment")
302303

303-
def question(self, text):
304+
def question(self, text: str) -> None:
304305
"""
305306
Write a string as question output.
306307
@@ -330,15 +331,28 @@ def progress_indicator(
330331

331332
return ProgressIndicator(self.io, fmt, interval, values)
332333

333-
def spin(self, start_message, end_message, fmt=None, interval=100, values=None):
334+
def spin(
335+
self,
336+
start_message: str,
337+
end_message: str,
338+
fmt: Optional[str] = None,
339+
interval=100,
340+
values: Optional[List[str]] = None,
341+
):
334342
"""
335343
Automatically spin a progress indicator.
336344
"""
337345
spinner = ProgressIndicator(self.io, fmt, interval, values)
338346

339347
return spinner.auto(start_message, end_message)
340348

341-
def add_style(self, name, fg=None, bg=None, options=None):
349+
def add_style(
350+
self,
351+
name: str,
352+
fg: Optional[str] = None,
353+
bg: Optional[str] = None,
354+
options: Optional[List[str]] = None,
355+
) -> None:
342356
"""
343357
Adds a new style
344358
"""
@@ -359,7 +373,7 @@ def add_style(self, name, fg=None, bg=None, options=None):
359373
self._io.output.formatter.add_style(style)
360374
self._io.error_output.formatter.add_style(style)
361375

362-
def overwrite(self, text):
376+
def overwrite(self, text: str) -> None:
363377
"""
364378
Overwrites the current line.
365379

cleo/commands/completions_command.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import re
66
import subprocess
77

8+
from typing import Optional
9+
810
from .. import helpers
911
from .command import Command
1012
from .completions.templates import TEMPLATES
@@ -381,7 +383,7 @@ def render_fish(self):
381383

382384
return output
383385

384-
def get_shell_type(self):
386+
def get_shell_type(self) -> str:
385387
shell = os.getenv("SHELL")
386388
if not shell:
387389
raise RuntimeError(
@@ -391,18 +393,18 @@ def get_shell_type(self):
391393

392394
return os.path.basename(shell)
393395

394-
def _generate_function_name(self, script_name, script_path):
396+
def _generate_function_name(self, script_name: str, script_path: str) -> str:
395397
return "_{}_{}_complete".format(
396398
self._sanitize_for_function_name(script_name),
397399
hashlib.md5(script_path.encode()).hexdigest()[0:16],
398400
)
399401

400-
def _sanitize_for_function_name(self, name):
402+
def _sanitize_for_function_name(self, name: str) -> str:
401403
name = name.replace("-", "_")
402404

403405
return re.sub("[^A-Za-z0-9_]+", "", name)
404406

405-
def _zsh_describe(self, value, description=None):
407+
def _zsh_describe(self, value: str, description: Optional[str] = None) -> str:
406408
value = '"' + value.replace(":", "\\:")
407409
if description:
408410
description = re.sub(

cleo/descriptors/application_description.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def command(self, name: str) -> Command:
4242

4343
return self._commands.get(name, self._aliases.get(name))
4444

45-
def _inspect_application(self):
45+
def _inspect_application(self) -> None:
4646
namespace = None
4747
if self._namespace:
4848
namespace = self._application.find_namespace(self._namespace)

cleo/descriptors/text_descriptor.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from typing import Any
55
from typing import List
6+
from typing import Sequence
67
from typing import Union
78

89
from cleo.application import Application
@@ -263,7 +264,7 @@ def _calculate_total_width_for_options(self, options: List[Option]) -> int:
263264

264265
return total_width
265266

266-
def _get_column_width(self, commands: List[Union[Command, str]]) -> int:
267+
def _get_column_width(self, commands: Sequence[Union[Command, str]]) -> int:
267268
widths = []
268269

269270
for command in commands:

cleo/events/console_error_event.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def __init__(self, command: Optional["Command"], io: IO, error: Exception) -> No
2020
super().__init__(command, io)
2121

2222
self._error = error
23-
self._exit_code = None
23+
self._exit_code: Optional[int] = None
2424

2525
@property
2626
def error(self) -> Exception:

cleo/formatters/formatter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ class Formatter:
1414

1515
TAG_REGEX = re.compile(r"(?ix)<(([a-z](?:[^<>]*)) | /([a-z](?:[^<>]*))?)>")
1616

17-
_inline_styles_cache = {}
17+
_inline_styles_cache: Dict[str, Style] = {}
1818

1919
def __init__(
2020
self, decorated: bool = False, styles: Optional[Dict[str, Style]] = None
2121
) -> None:
2222
self._decorated = decorated
23-
self._styles = {}
23+
self._styles: Dict[str, Style] = {}
2424

2525
self.set_style("error", Style("red", options=["bold"]))
2626
self.set_style("info", Style("blue"))

0 commit comments

Comments
 (0)