Skip to content

Commit 8aaec9a

Browse files
committed
Implement PEP 604 with future-annotations
1 parent 5011dbf commit 8aaec9a

Some content is hidden

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

61 files changed

+1209
-1125
lines changed

infrahub_sdk/_importer.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import importlib
44
import sys
55
from pathlib import Path
6-
from typing import TYPE_CHECKING, Optional
6+
from typing import TYPE_CHECKING
77

88
from .exceptions import ModuleImportError
99

@@ -13,9 +13,7 @@
1313
module_mtime_cache: dict[str, float] = {}
1414

1515

16-
def import_module(
17-
module_path: Path, import_root: Optional[str] = None, relative_path: Optional[str] = None
18-
) -> ModuleType:
16+
def import_module(module_path: Path, import_root: str | None = None, relative_path: str | None = None) -> ModuleType:
1917
"""Imports a python module.
2018
2119
Args:

infrahub_sdk/analyzer.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
from typing import Any, Optional
1+
from __future__ import annotations
2+
3+
from typing import Any
24

35
from graphql import (
46
DocumentNode,
@@ -19,23 +21,23 @@ class GraphQLQueryVariable(BaseModel):
1921
name: str
2022
type: str
2123
required: bool = False
22-
default_value: Optional[Any] = None
24+
default_value: Any | None = None
2325

2426

2527
class GraphQLOperation(BaseModel):
26-
name: Optional[str] = None
28+
name: str | None = None
2729
operation_type: OperationType
2830

2931

3032
class GraphQLQueryAnalyzer:
31-
def __init__(self, query: str, schema: Optional[GraphQLSchema] = None):
33+
def __init__(self, query: str, schema: GraphQLSchema | None = None):
3234
self.query: str = query
33-
self.schema: Optional[GraphQLSchema] = schema
35+
self.schema: GraphQLSchema | None = schema
3436
self.document: DocumentNode = parse(self.query)
35-
self._fields: Optional[dict] = None
37+
self._fields: dict | None = None
3638

3739
@property
38-
def is_valid(self) -> tuple[bool, Optional[list[GraphQLError]]]:
40+
def is_valid(self) -> tuple[bool, list[GraphQLError] | None]:
3941
if self.schema is None:
4042
return False, [GraphQLError("Schema is not provided")]
4143

infrahub_sdk/batch.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
1+
from __future__ import annotations
2+
13
import asyncio
2-
from collections.abc import AsyncGenerator, Awaitable
4+
from collections.abc import AsyncGenerator, Awaitable, Generator
35
from concurrent.futures import ThreadPoolExecutor
46
from dataclasses import dataclass
5-
from typing import Any, Callable, Generator, Optional
7+
from typing import TYPE_CHECKING, Any, Callable
68

7-
from .node import InfrahubNode, InfrahubNodeSync
9+
if TYPE_CHECKING:
10+
from .node import InfrahubNode, InfrahubNodeSync
811

912

1013
@dataclass
1114
class BatchTask:
1215
task: Callable[[Any], Awaitable[Any]]
1316
args: tuple[Any, ...]
1417
kwargs: dict[str, Any]
15-
node: Optional[Any] = None
18+
node: Any | None = None
1619

1720

1821
@dataclass
1922
class BatchTaskSync:
2023
task: Callable[..., Any]
2124
args: tuple[Any, ...]
2225
kwargs: dict[str, Any]
23-
node: Optional[InfrahubNodeSync] = None
26+
node: InfrahubNodeSync | None = None
2427

25-
def execute(self, return_exceptions: bool = False) -> tuple[Optional[InfrahubNodeSync], Any]:
28+
def execute(self, return_exceptions: bool = False) -> tuple[InfrahubNodeSync | None, Any]:
2629
"""Executes the stored task."""
2730
result = None
2831
try:
@@ -37,7 +40,7 @@ def execute(self, return_exceptions: bool = False) -> tuple[Optional[InfrahubNod
3740

3841
async def execute_batch_task_in_pool(
3942
task: BatchTask, semaphore: asyncio.Semaphore, return_exceptions: bool = False
40-
) -> tuple[Optional[InfrahubNode], Any]:
43+
) -> tuple[InfrahubNode | None, Any]:
4144
async with semaphore:
4245
try:
4346
result = await task.task(*task.args, **task.kwargs)
@@ -52,7 +55,7 @@ async def execute_batch_task_in_pool(
5255
class InfrahubBatch:
5356
def __init__(
5457
self,
55-
semaphore: Optional[asyncio.Semaphore] = None,
58+
semaphore: asyncio.Semaphore | None = None,
5659
max_concurrent_execution: int = 5,
5760
return_exceptions: bool = False,
5861
):
@@ -64,7 +67,7 @@ def __init__(
6467
def num_tasks(self) -> int:
6568
return len(self._tasks)
6669

67-
def add(self, *args: Any, task: Callable, node: Optional[Any] = None, **kwargs: Any) -> None:
70+
def add(self, *args: Any, task: Callable, node: Any | None = None, **kwargs: Any) -> None:
6871
self._tasks.append(BatchTask(task=task, node=node, args=args, kwargs=kwargs))
6972

7073
async def execute(self) -> AsyncGenerator:
@@ -96,10 +99,10 @@ def __init__(self, max_concurrent_execution: int = 5, return_exceptions: bool =
9699
def num_tasks(self) -> int:
97100
return len(self._tasks)
98101

99-
def add(self, *args: Any, task: Callable[..., Any], node: Optional[Any] = None, **kwargs: Any) -> None:
102+
def add(self, *args: Any, task: Callable[..., Any], node: Any | None = None, **kwargs: Any) -> None:
100103
self._tasks.append(BatchTaskSync(task=task, node=node, args=args, kwargs=kwargs))
101104

102-
def execute(self) -> Generator[tuple[Optional[InfrahubNodeSync], Any], None, None]:
105+
def execute(self) -> Generator[tuple[InfrahubNodeSync | None, Any], None, None]:
103106
with ThreadPoolExecutor(max_workers=self.max_concurrent_execution) as executor:
104107
futures = [executor.submit(task.execute, return_exceptions=self.return_exceptions) for task in self._tasks]
105108
for future in futures:

infrahub_sdk/branch.py

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

33
import warnings
4-
from typing import TYPE_CHECKING, Any, Literal, Optional, Union, overload
4+
from typing import TYPE_CHECKING, Any, Literal, overload
55
from urllib.parse import urlencode
66

77
from pydantic import BaseModel
@@ -17,11 +17,11 @@
1717
class BranchData(BaseModel):
1818
id: str
1919
name: str
20-
description: Optional[str] = None
20+
description: str | None = None
2121
sync_with_git: bool
2222
is_default: bool
2323
has_schema_changes: bool
24-
origin_branch: Optional[str] = None
24+
origin_branch: str | None = None
2525
branched_from: str
2626

2727

@@ -50,11 +50,11 @@ class InfraHubBranchManagerBase:
5050
@classmethod
5151
def generate_diff_data_url(
5252
cls,
53-
client: Union[InfrahubClient, InfrahubClientSync],
53+
client: InfrahubClient | InfrahubClientSync,
5454
branch_name: str,
5555
branch_only: bool = True,
56-
time_from: Optional[str] = None,
57-
time_to: Optional[str] = None,
56+
time_from: str | None = None,
57+
time_to: str | None = None,
5858
) -> str:
5959
"""Generate the URL for the diff_data function."""
6060
url = f"{client.address}/api/diff/data"
@@ -80,7 +80,7 @@ async def create(
8080
sync_with_git: bool = True,
8181
description: str = "",
8282
wait_until_completion: Literal[True] = True,
83-
background_execution: Optional[bool] = False,
83+
background_execution: bool | None = False,
8484
) -> BranchData: ...
8585

8686
@overload
@@ -90,7 +90,7 @@ async def create(
9090
sync_with_git: bool = True,
9191
description: str = "",
9292
wait_until_completion: Literal[False] = False,
93-
background_execution: Optional[bool] = False,
93+
background_execution: bool | None = False,
9494
) -> str: ...
9595

9696
async def create(
@@ -99,8 +99,8 @@ async def create(
9999
sync_with_git: bool = True,
100100
description: str = "",
101101
wait_until_completion: bool = True,
102-
background_execution: Optional[bool] = False,
103-
) -> Union[BranchData, str]:
102+
background_execution: bool | None = False,
103+
) -> BranchData | str:
104104
if background_execution is not None:
105105
warnings.warn(
106106
"`background_execution` is deprecated, please use `wait_until_completion` instead.",
@@ -206,8 +206,8 @@ async def diff_data(
206206
self,
207207
branch_name: str,
208208
branch_only: bool = True,
209-
time_from: Optional[str] = None,
210-
time_to: Optional[str] = None,
209+
time_from: str | None = None,
210+
time_to: str | None = None,
211211
) -> dict[Any, Any]:
212212
url = self.generate_diff_data_url(
213213
client=self.client,
@@ -251,7 +251,7 @@ def create(
251251
sync_with_git: bool = True,
252252
description: str = "",
253253
wait_until_completion: Literal[True] = True,
254-
background_execution: Optional[bool] = False,
254+
background_execution: bool | None = False,
255255
) -> BranchData: ...
256256

257257
@overload
@@ -261,7 +261,7 @@ def create(
261261
sync_with_git: bool = True,
262262
description: str = "",
263263
wait_until_completion: Literal[False] = False,
264-
background_execution: Optional[bool] = False,
264+
background_execution: bool | None = False,
265265
) -> str: ...
266266

267267
def create(
@@ -270,8 +270,8 @@ def create(
270270
sync_with_git: bool = True,
271271
description: str = "",
272272
wait_until_completion: bool = True,
273-
background_execution: Optional[bool] = False,
274-
) -> Union[BranchData, str]:
273+
background_execution: bool | None = False,
274+
) -> BranchData | str:
275275
if background_execution is not None:
276276
warnings.warn(
277277
"`background_execution` is deprecated, please use `wait_until_completion` instead.",
@@ -313,8 +313,8 @@ def diff_data(
313313
self,
314314
branch_name: str,
315315
branch_only: bool = True,
316-
time_from: Optional[str] = None,
317-
time_to: Optional[str] = None,
316+
time_from: str | None = None,
317+
time_to: str | None = None,
318318
) -> dict[Any, Any]:
319319
url = self.generate_diff_data_url(
320320
client=self.client,

infrahub_sdk/checks.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import os
66
import warnings
77
from abc import abstractmethod
8-
from typing import TYPE_CHECKING, Any, Optional
8+
from typing import TYPE_CHECKING, Any
99

1010
import ujson
1111
from git.repo import Repo
@@ -33,20 +33,20 @@ class InfrahubCheckInitializer(BaseModel):
3333

3434

3535
class InfrahubCheck:
36-
name: Optional[str] = None
36+
name: str | None = None
3737
query: str = ""
3838
timeout: int = 10
3939

4040
def __init__(
4141
self,
42-
branch: Optional[str] = None,
42+
branch: str | None = None,
4343
root_directory: str = "",
44-
output: Optional[str] = None,
45-
initializer: Optional[InfrahubCheckInitializer] = None,
46-
params: Optional[dict] = None,
47-
client: Optional[InfrahubClient] = None,
44+
output: str | None = None,
45+
initializer: InfrahubCheckInitializer | None = None,
46+
params: dict | None = None,
47+
client: InfrahubClient | None = None,
4848
):
49-
self.git: Optional[Repo] = None
49+
self.git: Repo | None = None
5050
self.initializer = initializer or InfrahubCheckInitializer()
5151

5252
self.logs: list[dict[str, Any]] = []
@@ -82,7 +82,7 @@ def client(self, value: InfrahubClient) -> None:
8282
self._client = value
8383

8484
@classmethod
85-
async def init(cls, client: Optional[InfrahubClient] = None, *args: Any, **kwargs: Any) -> InfrahubCheck:
85+
async def init(cls, client: InfrahubClient | None = None, *args: Any, **kwargs: Any) -> InfrahubCheck:
8686
"""Async init method, If an existing InfrahubClient client hasn't been provided, one will be created automatically."""
8787
warnings.warn(
8888
"InfrahubCheck.init has been deprecated and will be removed in the version in Infrahub SDK 2.0.0",
@@ -101,7 +101,7 @@ def errors(self) -> list[dict[str, Any]]:
101101
return [log for log in self.logs if log["level"] == "ERROR"]
102102

103103
def _write_log_entry(
104-
self, message: str, level: str, object_id: Optional[str] = None, object_type: Optional[str] = None
104+
self, message: str, level: str, object_id: str | None = None, object_type: str | None = None
105105
) -> None:
106106
log_message = {"level": level, "message": message, "branch": self.branch_name}
107107
if object_id:
@@ -113,10 +113,10 @@ def _write_log_entry(
113113
if self.output == "stdout":
114114
print(ujson.dumps(log_message))
115115

116-
def log_error(self, message: str, object_id: Optional[str] = None, object_type: Optional[str] = None) -> None:
116+
def log_error(self, message: str, object_id: str | None = None, object_type: str | None = None) -> None:
117117
self._write_log_entry(message=message, level="ERROR", object_id=object_id, object_type=object_type)
118118

119-
def log_info(self, message: str, object_id: Optional[str] = None, object_type: Optional[str] = None) -> None:
119+
def log_info(self, message: str, object_id: str | None = None, object_type: str | None = None) -> None:
120120
self._write_log_entry(message=message, level="INFO", object_id=object_id, object_type=object_type)
121121

122122
@property
@@ -155,7 +155,7 @@ async def collect_data(self) -> dict:
155155

156156
return await self.client.query_gql_query(name=self.query, branch_name=self.branch_name, variables=self.params)
157157

158-
async def run(self, data: Optional[dict] = None) -> bool:
158+
async def run(self, data: dict | None = None) -> bool:
159159
"""Execute the check after collecting the data from the GraphQL query.
160160
The result of the check is determined based on the presence or not of ERROR log messages."""
161161

@@ -179,7 +179,7 @@ async def run(self, data: Optional[dict] = None) -> bool:
179179

180180

181181
def get_check_class_instance(
182-
check_config: InfrahubCheckDefinitionConfig, search_path: Optional[Path] = None
182+
check_config: InfrahubCheckDefinitionConfig, search_path: Path | None = None
183183
) -> InfrahubCheck:
184184
if check_config.file_path.is_absolute() or search_path is None:
185185
search_location = check_config.file_path

0 commit comments

Comments
 (0)