Skip to content

Commit fffcf74

Browse files
authored
Merge pull request #579 from adanaja/type-fix
Cleanup most of Optional, Dict, List,... type hints
2 parents 6daebc2 + 697557a commit fffcf74

Some content is hidden

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

47 files changed

+345
-393
lines changed

src/e3/anod/action/__init__.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from e3.anod.spec import Anod
77

88
if TYPE_CHECKING:
9-
from typing import Any, Final, Literal, Optional, Union
9+
from typing import Any, Final, Literal, Union
1010
from e3.anod.package import Source, SourceBuilder
1111
from e3.collection.dag import DAG
1212

@@ -418,7 +418,7 @@ class Decision(Action, metaclass=abc.ABCMeta):
418418
BOTH: Final = 2
419419

420420
def __init__(
421-
self, root: Action, left: Action, right: Action, choice: Optional[Choice] = None
421+
self, root: Action, left: Action, right: Action, choice: Choice | None = None
422422
):
423423
"""Initialize a Decision instance.
424424
@@ -430,11 +430,11 @@ def __init__(
430430
super().__init__(uid=root.uid + ".decision", data=None)
431431
self.initiator = root.uid
432432
self.choice = choice
433-
self.expected_choice: Optional[Choice] = None
433+
self.expected_choice: Choice | None = None
434434
self.left_action = left
435435
self.right_action = right
436436
self.triggers: list[tuple[str, Choice, str]] = []
437-
self.decision_maker: Optional[str] = None
437+
self.decision_maker: str | None = None
438438

439439
@property
440440
def left(self) -> str:
@@ -474,7 +474,7 @@ def apply_triggers(self, dag: DAG) -> None:
474474
elif self.expected_choice != decision:
475475
self.expected_choice = Decision.BOTH
476476

477-
def get_decision(self) -> Optional[str]:
477+
def get_decision(self) -> str | None:
478478
"""Return uid of the choice made by the plan, or None.
479479
480480
This function returns the choice made by the plan, if any.
@@ -501,7 +501,7 @@ def get_decision(self) -> Optional[str]:
501501
else:
502502
return self.right
503503

504-
def get_expected_decision(self) -> Optional[str]:
504+
def get_expected_decision(self) -> str | None:
505505
"""Get expected decision.
506506
507507
:return: uid of the expected action or None if no specific decision
@@ -514,7 +514,7 @@ def get_expected_decision(self) -> Optional[str]:
514514
else:
515515
return None
516516

517-
def set_decision(self, which: Choice, decision_maker: Optional[str]) -> None:
517+
def set_decision(self, which: Choice, decision_maker: str | None) -> None:
518518
"""Record a choice made by an action in a plan.
519519
520520
This method should be caused when an entry in our plan
@@ -544,7 +544,7 @@ def description(cls, decision: Choice) -> str:
544544
"""
545545
pass # all: no cover
546546

547-
def suggest_plan_fix(self, choice: Choice) -> Optional[str]:
547+
def suggest_plan_fix(self, choice: Choice) -> str | None:
548548
"""Suggest a plan line that would fix the conflict.
549549
550550
:param choice: Decision.LEFT or Decision.RIGHT

src/e3/anod/buildspace.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from __future__ import annotations
22

33
import os
4-
from typing import TYPE_CHECKING
54

65
import e3.error
76
import e3.log
@@ -12,10 +11,6 @@
1211
logger = e3.log.getLogger("buildspace")
1312

1413

15-
if TYPE_CHECKING:
16-
from typing import Optional
17-
18-
1914
class BuildSpace:
2015
"""Build space located inside a sandbox."""
2116

@@ -98,7 +93,7 @@ def create(self, quiet: bool = False) -> None:
9893

9994
self.initialized = True
10095

101-
def reset(self, keep: Optional[list[str]] = None) -> None:
96+
def reset(self, keep: list[str] | None = None) -> None:
10297
"""Reset build space.
10398
10499
The function delete the complete buildspace. The only elements that

src/e3/anod/checkout.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from e3.vcs.svn import SVNError, SVNRepository
1717

1818
if TYPE_CHECKING:
19-
from typing import Literal, Optional
19+
from typing import Literal
2020
from collections.abc import Callable
2121
from e3.mypy import assert_never
2222

@@ -59,7 +59,7 @@ def update(
5959
self,
6060
vcs: Literal["git"] | Literal["svn"] | Literal["external"],
6161
url: str,
62-
revision: Optional[str] = None,
62+
revision: str | None = None,
6363
) -> ReturnValue:
6464
"""Update content of the working directory.
6565
@@ -77,9 +77,7 @@ def update(
7777
if os.path.isfile(self.changelog_file):
7878
rm(self.changelog_file)
7979

80-
update: Callable[
81-
[str, Optional[str]], tuple[ReturnValue, Optional[str], Optional[str]]
82-
]
80+
update: Callable[[str, str | None], tuple[ReturnValue, str | None, str | None]]
8381
if vcs == "git":
8482
update = self.update_git
8583
elif vcs == "svn":
@@ -105,7 +103,7 @@ def update(
105103
return result
106104

107105
def update_external(
108-
self, url: str, revision: Optional[str]
106+
self, url: str, revision: str | None
109107
) -> tuple[ReturnValue, str, str]:
110108
"""Update working dir using a local directory.
111109
@@ -205,8 +203,8 @@ def git_remote_name(url: str) -> str:
205203
return hashlib.sha256(url.encode("utf-8")).hexdigest()
206204

207205
def update_git(
208-
self, url: str, revision: Optional[str]
209-
) -> tuple[ReturnValue, Optional[str], Optional[str]]:
206+
self, url: str, revision: str | None
207+
) -> tuple[ReturnValue, str | None, str | None]:
210208
"""Update working dir using a Git repository.
211209
212210
:param url: git repository url
@@ -297,8 +295,8 @@ def update_git(
297295
return result, old_commit, new_commit
298296

299297
def update_svn(
300-
self, url: str, revision: Optional[str]
301-
) -> tuple[ReturnValue, Optional[str], Optional[str]]:
298+
self, url: str, revision: str | None
299+
) -> tuple[ReturnValue, str | None, str | None]:
302300
"""Update working dir using a SVN repository.
303301
304302
:param url: git repository url

src/e3/anod/context.py

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,7 @@
3232
from e3.error import E3Error
3333

3434
if TYPE_CHECKING:
35-
from typing import (
36-
cast,
37-
NoReturn,
38-
Optional,
39-
Tuple,
40-
)
35+
from typing import cast, NoReturn, Optional, Tuple
4136
from collections.abc import Callable
4237
from e3.anod.action import Action
4338
from e3.anod.package import SourceBuilder
@@ -64,9 +59,9 @@ class SchedulingError(E3Error):
6459
def __init__(
6560
self,
6661
message: str | list[str],
67-
origin: Optional[str] = None,
68-
uid: Optional[VertexID] = None,
69-
initiators: Optional[list[VertexID]] = None,
62+
origin: str | None = None,
63+
uid: VertexID | None = None,
64+
initiators: list[VertexID] | None = None,
7065
):
7166
"""Scheduling error initialization.
7267
@@ -104,7 +99,7 @@ class AnodContext:
10499
def __init__(
105100
self,
106101
spec_repository: AnodSpecRepository,
107-
default_env: Optional[BaseEnv] = None,
102+
default_env: BaseEnv | None = None,
108103
reject_duplicates: bool = False,
109104
):
110105
"""Initialize a new context.
@@ -134,11 +129,11 @@ def __init__(
134129
def load(
135130
self,
136131
name: str,
137-
env: Optional[BaseEnv],
138-
qualifier: Optional[str],
132+
env: BaseEnv | None,
133+
qualifier: str | None,
139134
kind: PRIMITIVE,
140-
sandbox: Optional[SandBox] = None,
141-
source_name: Optional[str] = None,
135+
sandbox: SandBox | None = None,
136+
source_name: str | None = None,
142137
) -> Anod:
143138
"""Load a spec instance.
144139
@@ -264,8 +259,8 @@ def link_to_plan(self, vertex_id: str, plan_line: str, plan_args: dict) -> None:
264259
self.tree.add_tag(vertex_id, {"plan_line": plan_line, "plan_args": plan_args})
265260

266261
def add_plan_action(
267-
self, plan_action_env: PlanActionEnv, sandbox: Optional[SandBox] = None
268-
) -> Optional[Action]:
262+
self, plan_action_env: PlanActionEnv, sandbox: SandBox | None = None
263+
) -> Action | None:
269264
"""Add an Anod action to the context.
270265
271266
:param plan_action_env: the PlanActionEnv object as returned by PlanContext
@@ -307,12 +302,12 @@ def add_anod_action(
307302
name: str,
308303
env: BaseEnv,
309304
primitive: PRIMITIVE,
310-
qualifier: Optional[str] = None,
311-
source_packages: Optional[list[str]] = None,
305+
qualifier: str | None = None,
306+
source_packages: list[str] | None = None,
312307
upload: bool = True,
313-
plan_line: Optional[str] = None,
314-
plan_args: Optional[dict] = None,
315-
sandbox: Optional[SandBox] = None,
308+
plan_line: str | None = None,
309+
plan_args: dict | None = None,
310+
sandbox: SandBox | None = None,
316311
) -> Action:
317312
"""Add an Anod action to the context (internal function).
318313
@@ -392,13 +387,13 @@ def add_spec(
392387
name: str,
393388
env: BaseEnv,
394389
primitive: PRIMITIVE,
395-
qualifier: Optional[str] = None,
396-
source_packages: Optional[list[str]] = None,
390+
qualifier: str | None = None,
391+
source_packages: list[str] | None = None,
397392
expand_build: bool = True,
398-
source_name: Optional[str] = None,
399-
plan_line: Optional[str] = None,
400-
plan_args: Optional[dict] = None,
401-
sandbox: Optional[SandBox] = None,
393+
source_name: str | None = None,
394+
plan_line: str | None = None,
395+
plan_args: dict | None = None,
396+
sandbox: SandBox | None = None,
402397
upload: bool = False,
403398
force_download: bool = False,
404399
) -> Build | CreateSources | CreateSource | Install | Test:
@@ -424,7 +419,7 @@ def add_spec(
424419
:param force_download: if True force a download
425420
"""
426421

427-
def add_action(data: Action, connect_with: Optional[Action] = None) -> None:
422+
def add_action(data: Action, connect_with: Action | None = None) -> None:
428423
self.add(data)
429424
if connect_with is not None:
430425
self.connect(connect_with, data)

src/e3/anod/deps.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from e3.env import BaseEnv
88

99
if TYPE_CHECKING:
10-
from typing import Any, Hashable, Literal, Optional
10+
from typing import Any, Hashable, Literal
1111
from e3.anod.spec import Anod, DEPENDENCY_PRIMITIVE
1212
from e3.mypy import assert_never
1313

@@ -36,12 +36,12 @@ class Dependency:
3636
def __init__(
3737
self,
3838
name: str,
39-
product_version: Optional[str] = None,
40-
host: Optional[str] = None,
41-
target: Optional[str] = None,
42-
build: Optional[str] = None,
43-
qualifier: Optional[str] = None,
44-
local_name: Optional[str] = None,
39+
product_version: str | None = None,
40+
host: str | None = None,
41+
target: str | None = None,
42+
build: str | None = None,
43+
qualifier: str | None = None,
44+
local_name: str | None = None,
4545
require: Literal["build_tree"]
4646
| Literal["installation"]
4747
| Literal["download"]

src/e3/anod/error.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import e3.error
66

77
if TYPE_CHECKING:
8-
from typing import Optional
98
from e3.os.process import Run
109

1110

@@ -33,8 +32,8 @@ class ShellError(AnodError):
3332
def __init__(
3433
self,
3534
message: str,
36-
origin: Optional[str] = None,
37-
process: Optional["Run"] = None,
35+
origin: str | None = None,
36+
process: "Run" | None = None,
3837
):
3938
super().__init__(message, origin)
4039
self.process = process

src/e3/anod/helper.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
from e3.os.fs import unixpath
1414

1515
if TYPE_CHECKING:
16-
from typing import Optional
1716
from e3.os.process import Run
1817

1918
log = e3.log.getLogger("anod.helpers")
@@ -25,9 +24,9 @@ class Make:
2524
def __init__(
2625
self,
2726
anod_instance: Anod,
28-
makefile: Optional[str] = None,
29-
exec_dir: Optional[str] = None,
30-
jobs: Optional[int] = None,
27+
makefile: str | None = None,
28+
exec_dir: str | None = None,
29+
jobs: int | None = None,
3130
make_exe: str = "make",
3231
):
3332
"""Initialize a Make object.
@@ -49,7 +48,7 @@ def __init__(
4948
if jobs is None:
5049
self.jobs = anod_instance.jobs
5150
self.var_list = {} # type: dict[str, str]
52-
self.default_target = None # type: Optional[str]
51+
self.default_target = None # type: str | None
5352
self.make_exe = make_exe
5453

5554
def set_var(self, name: str, value: str | list[str]) -> None:
@@ -76,10 +75,10 @@ def set_default_target(self, target: str) -> None:
7675

7776
def __call__(
7877
self,
79-
target: Optional[str] = None,
80-
jobs: Optional[int] = None,
81-
exec_dir: Optional[str] = None,
82-
timeout: Optional[float] = None,
78+
target: str | None = None,
79+
jobs: int | None = None,
80+
exec_dir: str | None = None,
81+
timeout: float | None = None,
8382
) -> Run:
8483
"""Call a make target.
8584
@@ -95,10 +94,10 @@ def __call__(
9594

9695
def cmdline(
9796
self,
98-
target: Optional[str | list[str]] = None,
99-
jobs: Optional[int] = None,
100-
exec_dir: Optional[str] = None,
101-
timeout: Optional[float] = None,
97+
target: str | list[str] | None = None,
98+
jobs: int | None = None,
99+
exec_dir: str | None = None,
100+
timeout: float | None = None,
102101
) -> dict[str, list[str] | dict]:
103102
"""Return the make command line.
104103
@@ -147,8 +146,8 @@ class Configure:
147146
def __init__(
148147
self,
149148
anod_instance: Anod,
150-
src_dir: Optional[str] = None,
151-
exec_dir: Optional[str] = None,
149+
src_dir: str | None = None,
150+
exec_dir: str | None = None,
152151
auto_target: bool = True,
153152
):
154153
"""Initialize a Configure object.

0 commit comments

Comments
 (0)