Skip to content

Commit 2c0b0b4

Browse files
branchvabn
authored andcommitted
refactor: rewrite type annotations to use pep 585
1 parent f819735 commit 2c0b0b4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+290
-426
lines changed

cleo/_utils.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
from html.parser import HTMLParser
66
from typing import Any
7-
from typing import List
87

98
from pylev import levenshtein
109

@@ -49,7 +48,7 @@ def strip_tags(value: Any) -> str:
4948
return value
5049

5150

52-
def find_similar_names(name: str, names: List[str]) -> List[str]:
51+
def find_similar_names(name: str, names: list[str]) -> list[str]:
5352
"""
5453
Finds names similar to a given command name.
5554
"""

cleo/application.py

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66

77
from contextlib import suppress
88
from typing import TYPE_CHECKING
9-
from typing import Dict
10-
from typing import List
11-
from typing import Optional
129
from typing import cast
1310

1411
from .commands.command import Command
@@ -62,27 +59,25 @@ class Application:
6259
def __init__(self, name: str = "console", version: str = "") -> None:
6360
self._name = name
6461
self._version = version
65-
self._display_name: Optional[str] = None
62+
self._display_name: str | None = None
6663
self._terminal = Terminal()
6764
self._default_command = "list"
6865
self._single_command = False
69-
self._commands: Dict[str, Command] = {}
66+
self._commands: dict[str, Command] = {}
7067
self._running_command = None
7168
self._want_helps = False
72-
self._definition: Optional[Definition] = None
69+
self._definition: Definition | None = None
7370
self._catch_exceptions = True
7471
self._auto_exit = True
7572
self._initialized = False
76-
self._ui: Optional[UI] = None
73+
self._ui: UI | None = None
7774

7875
# TODO: signals support
79-
self._event_dispatcher: Optional[EventDispatcher] = None
76+
self._event_dispatcher: EventDispatcher | None = None
8077

81-
self._command_loader: Optional[CommandLoader] = None
78+
self._command_loader: CommandLoader | None = None
8279

83-
self._solution_provider_repository: Optional[
84-
"SolutionProviderRepository"
85-
] = None
80+
self._solution_provider_repository: SolutionProviderRepository | None = None
8681

8782
@property
8883
def name(self) -> str:
@@ -123,7 +118,7 @@ def definition(self) -> Definition:
123118
return self._definition
124119

125120
@property
126-
def default_commands(self) -> List[Command]:
121+
def default_commands(self) -> list[Command]:
127122
return [HelpCommand(), ListCommand(), CompletionsCommand()]
128123

129124
@property
@@ -138,7 +133,7 @@ def ui(self) -> UI:
138133
return self._ui
139134

140135
@property
141-
def event_dispatcher(self) -> Optional[EventDispatcher]:
136+
def event_dispatcher(self) -> EventDispatcher | None:
142137
return self._event_dispatcher
143138

144139
def set_event_dispatcher(self, event_dispatcher: EventDispatcher) -> None:
@@ -175,11 +170,11 @@ def is_single_command(self) -> bool:
175170
return self._single_command
176171

177172
def set_solution_provider_repository(
178-
self, solution_provider_repository: "SolutionProviderRepository"
173+
self, solution_provider_repository: SolutionProviderRepository
179174
) -> None:
180175
self._solution_provider_repository = solution_provider_repository
181176

182-
def add(self, command: Command) -> Optional[Command]:
177+
def add(self, command: Command) -> Command | None:
183178
self._init()
184179

185180
command.set_application(self)
@@ -238,7 +233,7 @@ def has(self, name: str) -> bool:
238233
self._command_loader.get(name)
239234
)
240235

241-
def get_namespaces(self) -> List[str]:
236+
def get_namespaces(self) -> list[str]:
242237
namespaces = []
243238
seen = set()
244239

@@ -287,7 +282,7 @@ def find(self, name: str) -> Command:
287282

288283
raise CommandNotFoundException(name, all_commands)
289284

290-
def all(self, namespace: Optional[str] = None) -> Dict[str, Command]:
285+
def all(self, namespace: str | None = None) -> dict[str, Command]:
291286
self._init()
292287

293288
if namespace is None:
@@ -320,9 +315,9 @@ def all(self, namespace: Optional[str] = None) -> Dict[str, Command]:
320315

321316
def run(
322317
self,
323-
input: Optional[Input] = None,
324-
output: Optional[Output] = None,
325-
error_output: Optional[Output] = None,
318+
input: Input | None = None,
319+
output: Output | None = None,
320+
error_output: Output | None = None,
326321
) -> int:
327322
try:
328323
io = self.create_io(input, output, error_output)
@@ -470,9 +465,9 @@ def _run_command(self, command: Command, io: IO) -> int:
470465

471466
def create_io(
472467
self,
473-
input: Optional[Input] = None,
474-
output: Optional[Output] = None,
475-
error_output: Optional[Output] = None,
468+
input: Input | None = None,
469+
output: Output | None = None,
470+
error_output: Output | None = None,
476471
) -> IO:
477472
if input is None:
478473
input = ArgvInput()
@@ -599,7 +594,7 @@ def _get_command_name(self, io: IO) -> str:
599594

600595
return io.input.first_argument
601596

602-
def extract_namespace(self, name: str, limit: Optional[int] = None) -> str:
597+
def extract_namespace(self, name: str, limit: int | None = None) -> str:
603598
parts = name.split(" ")[:-1]
604599

605600
if limit is not None:
@@ -612,7 +607,7 @@ def _get_default_ui(self) -> UI:
612607

613608
return UI([ProgressBar()])
614609

615-
def _extract_all_namespaces(self, name: str) -> List[str]:
610+
def _extract_all_namespaces(self, name: str) -> list[str]:
616611
parts = name.split(" ")[:-1]
617612
namespaces = []
618613

cleo/color.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22

33
import os
44

5-
from typing import List
6-
from typing import Optional
7-
85
from .exceptions import ValueException
96

107

@@ -44,7 +41,7 @@ def __init__(
4441
self,
4542
foreground: str = "",
4643
background: str = "",
47-
options: Optional[List[str]] = None,
44+
options: list[str] | None = None,
4845
) -> None:
4946
self._foreground = self._parse_color(foreground, False)
5047
self._background = self._parse_color(background, True)

cleo/commands/base_command.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
import inspect
44

55
from typing import TYPE_CHECKING
6-
from typing import Dict
7-
from typing import List
8-
from typing import Optional
96

107
from cleo.exceptions import CleoException
118
from cleo.io.inputs.definition import Definition
@@ -18,7 +15,7 @@
1815

1916
class BaseCommand:
2017

21-
name: Optional[str] = None
18+
name: str | None = None
2219

2320
description = ""
2421

@@ -27,14 +24,14 @@ class BaseCommand:
2724
enabled = True
2825
hidden = False
2926

30-
usages: List[str] = []
27+
usages: list[str] = []
3128

3229
def __init__(self) -> None:
3330
self._definition = Definition()
3431
self._full_definition = None
3532
self._application = None
3633
self._ignore_validation_errors = False
37-
self._synopsis: Dict[str, str] = {}
34+
self._synopsis: dict[str, str] = {}
3835

3936
self.configure()
4037

@@ -43,7 +40,7 @@ def __init__(self) -> None:
4340
self.usages[i] = f"{self.name} {usage}"
4441

4542
@property
46-
def application(self) -> "Application":
43+
def application(self) -> Application:
4744
return self._application
4845

4946
@property
@@ -77,7 +74,7 @@ def processed_help(self) -> str:
7774
def ignore_validation_errors(self) -> None:
7875
self._ignore_validation_errors = True
7976

80-
def set_application(self, application: Optional["Application"] = None) -> None:
77+
def set_application(self, application: Application | None = None) -> None:
8178
self._application = application
8279

8380
self._full_definition = None

cleo/commands/command.py

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55

66
from typing import TYPE_CHECKING
77
from typing import Any
8-
from typing import List
9-
from typing import Optional
10-
from typing import Union
118

129
from cleo.formatters.style import Style
1310
from cleo.io.inputs.string_input import StringInput
@@ -38,7 +35,7 @@ class Command(BaseCommand):
3835
commands = []
3936

4037
def __init__(self) -> None:
41-
self._io: Optional[IO] = None
38+
self._io: IO | None = None
4239
super().__init__()
4340

4441
@property
@@ -94,7 +91,7 @@ def handle(self) -> int:
9491
"""
9592
raise NotImplementedError()
9693

97-
def call(self, name: str, args: Optional[str] = None) -> int:
94+
def call(self, name: str, args: str | None = None) -> int:
9895
"""
9996
Call another command.
10097
"""
@@ -106,7 +103,7 @@ def call(self, name: str, args: Optional[str] = None) -> int:
106103

107104
return self.application._run_command(command, self._io.with_input(input))
108105

109-
def call_silent(self, name: str, args: Optional[str] = None) -> int:
106+
def call_silent(self, name: str, args: str | None = None) -> int:
110107
"""
111108
Call another command silently.
112109
"""
@@ -144,9 +141,7 @@ def confirm(
144141

145142
return question.ask(self._io)
146143

147-
def ask(
148-
self, question: Union[str, "Question"], default: Optional[Any] = None
149-
) -> Any:
144+
def ask(self, question: str | Question, default: Any | None = None) -> Any:
150145
"""
151146
Prompt the user for input.
152147
"""
@@ -157,9 +152,7 @@ def ask(
157152

158153
return question.ask(self._io)
159154

160-
def secret(
161-
self, question: Union[str, "Question"], default: Optional[Any] = None
162-
) -> Any:
155+
def secret(self, question: str | Question, default: Any | None = None) -> Any:
163156
"""
164157
Prompt the user for input but hide the answer from the console.
165158
"""
@@ -175,9 +168,9 @@ def secret(
175168
def choice(
176169
self,
177170
question: str,
178-
choices: List[str],
179-
default: Optional[Any] = None,
180-
attempts: Optional[int] = None,
171+
choices: list[str],
172+
default: Any | None = None,
173+
attempts: int | None = None,
181174
multiple: bool = False,
182175
) -> Any:
183176
"""
@@ -225,7 +218,7 @@ def table(self, header=None, rows=None, style=None):
225218

226219
return table
227220

228-
def table_separator(self) -> "TableSeparator":
221+
def table_separator(self) -> TableSeparator:
229222
"""
230223
Return a TableSeparator instance.
231224
"""
@@ -241,7 +234,7 @@ def render_table(self, headers, rows, style=None) -> None:
241234

242235
table.render()
243236

244-
def write(self, text: str, style: Optional[str] = None) -> None:
237+
def write(self, text: str, style: str | None = None) -> None:
245238
"""
246239
Writes a string without a new line.
247240
Useful if you want to use overwrite().
@@ -256,7 +249,7 @@ def write(self, text: str, style: Optional[str] = None) -> None:
256249
def line(
257250
self,
258251
text: str,
259-
style: Optional[str] = None,
252+
style: str | None = None,
260253
verbosity: Verbosity = Verbosity.NORMAL,
261254
) -> None:
262255
"""
@@ -272,7 +265,7 @@ def line(
272265
def line_error(
273266
self,
274267
text: str,
275-
style: Optional[str] = None,
268+
style: str | None = None,
276269
verbosity: Verbosity = Verbosity.NORMAL,
277270
) -> None:
278271
"""
@@ -312,7 +305,7 @@ def question(self, text: str) -> None:
312305
"""
313306
self.line(text, "question")
314307

315-
def progress_bar(self, max: int = 0) -> "ProgressBar":
308+
def progress_bar(self, max: int = 0) -> ProgressBar:
316309
"""
317310
Creates a new progress bar
318311
"""
@@ -322,10 +315,10 @@ def progress_bar(self, max: int = 0) -> "ProgressBar":
322315

323316
def progress_indicator(
324317
self,
325-
fmt: Optional[str] = None,
318+
fmt: str | None = None,
326319
interval: int = 100,
327-
values: Optional[List[str]] = None,
328-
) -> "ProgressIndicator":
320+
values: list[str] | None = None,
321+
) -> ProgressIndicator:
329322
"""
330323
Creates a new progress indicator.
331324
"""
@@ -337,9 +330,9 @@ def spin(
337330
self,
338331
start_message: str,
339332
end_message: str,
340-
fmt: Optional[str] = None,
333+
fmt: str | None = None,
341334
interval=100,
342-
values: Optional[List[str]] = None,
335+
values: list[str] | None = None,
343336
):
344337
"""
345338
Automatically spin a progress indicator.
@@ -351,9 +344,9 @@ def spin(
351344
def add_style(
352345
self,
353346
name: str,
354-
fg: Optional[str] = None,
355-
bg: Optional[str] = None,
356-
options: Optional[List[str]] = None,
347+
fg: str | None = None,
348+
bg: str | None = None,
349+
options: list[str] | None = None,
357350
) -> None:
358351
"""
359352
Adds a new style

0 commit comments

Comments
 (0)