Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions infrahub_sdk/_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import importlib
import sys
from pathlib import Path
from typing import TYPE_CHECKING, Optional
from typing import TYPE_CHECKING

Check warning on line 6 in infrahub_sdk/_importer.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/_importer.py#L6

Added line #L6 was not covered by tests

from .exceptions import ModuleImportError

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


def import_module(
module_path: Path, import_root: Optional[str] = None, relative_path: Optional[str] = None
) -> ModuleType:
def import_module(module_path: Path, import_root: str | None = None, relative_path: str | None = None) -> ModuleType:

Check warning on line 16 in infrahub_sdk/_importer.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/_importer.py#L16

Added line #L16 was not covered by tests
"""Imports a python module.

Args:
Expand Down
16 changes: 9 additions & 7 deletions infrahub_sdk/analyzer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from typing import Any, Optional
from __future__ import annotations

Check warning on line 1 in infrahub_sdk/analyzer.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/analyzer.py#L1

Added line #L1 was not covered by tests

from typing import Any

Check warning on line 3 in infrahub_sdk/analyzer.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/analyzer.py#L3

Added line #L3 was not covered by tests

from graphql import (
DocumentNode,
Expand All @@ -19,23 +21,23 @@
name: str
type: str
required: bool = False
default_value: Optional[Any] = None
default_value: Any | None = None

Check warning on line 24 in infrahub_sdk/analyzer.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/analyzer.py#L24

Added line #L24 was not covered by tests


class GraphQLOperation(BaseModel):
name: Optional[str] = None
name: str | None = None

Check warning on line 28 in infrahub_sdk/analyzer.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/analyzer.py#L28

Added line #L28 was not covered by tests
operation_type: OperationType


class GraphQLQueryAnalyzer:
def __init__(self, query: str, schema: Optional[GraphQLSchema] = None):
def __init__(self, query: str, schema: GraphQLSchema | None = None):

Check warning on line 33 in infrahub_sdk/analyzer.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/analyzer.py#L33

Added line #L33 was not covered by tests
self.query: str = query
self.schema: Optional[GraphQLSchema] = schema
self.schema: GraphQLSchema | None = schema
self.document: DocumentNode = parse(self.query)
self._fields: Optional[dict] = None
self._fields: dict | None = None

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

Expand Down
25 changes: 14 additions & 11 deletions infrahub_sdk/batch.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
from __future__ import annotations

Check warning on line 1 in infrahub_sdk/batch.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/batch.py#L1

Added line #L1 was not covered by tests

import asyncio
from collections.abc import AsyncGenerator, Awaitable
from collections.abc import AsyncGenerator, Awaitable, Generator

Check warning on line 4 in infrahub_sdk/batch.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/batch.py#L4

Added line #L4 was not covered by tests
from concurrent.futures import ThreadPoolExecutor
from dataclasses import dataclass
from typing import Any, Callable, Generator, Optional
from typing import TYPE_CHECKING, Any, Callable

Check warning on line 7 in infrahub_sdk/batch.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/batch.py#L7

Added line #L7 was not covered by tests

from .node import InfrahubNode, InfrahubNodeSync
if TYPE_CHECKING:
from .node import InfrahubNode, InfrahubNodeSync


@dataclass
class BatchTask:
task: Callable[[Any], Awaitable[Any]]
args: tuple[Any, ...]
kwargs: dict[str, Any]
node: Optional[Any] = None
node: Any | None = None

Check warning on line 18 in infrahub_sdk/batch.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/batch.py#L18

Added line #L18 was not covered by tests


@dataclass
class BatchTaskSync:
task: Callable[..., Any]
args: tuple[Any, ...]
kwargs: dict[str, Any]
node: Optional[InfrahubNodeSync] = None
node: InfrahubNodeSync | None = None

Check warning on line 26 in infrahub_sdk/batch.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/batch.py#L26

Added line #L26 was not covered by tests

def execute(self, return_exceptions: bool = False) -> tuple[Optional[InfrahubNodeSync], Any]:
def execute(self, return_exceptions: bool = False) -> tuple[InfrahubNodeSync | None, Any]:

Check warning on line 28 in infrahub_sdk/batch.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/batch.py#L28

Added line #L28 was not covered by tests
"""Executes the stored task."""
result = None
try:
Expand All @@ -37,7 +40,7 @@

async def execute_batch_task_in_pool(
task: BatchTask, semaphore: asyncio.Semaphore, return_exceptions: bool = False
) -> tuple[Optional[InfrahubNode], Any]:
) -> tuple[InfrahubNode | None, Any]:
async with semaphore:
try:
result = await task.task(*task.args, **task.kwargs)
Expand All @@ -52,7 +55,7 @@
class InfrahubBatch:
def __init__(
self,
semaphore: Optional[asyncio.Semaphore] = None,
semaphore: asyncio.Semaphore | None = None,
max_concurrent_execution: int = 5,
return_exceptions: bool = False,
):
Expand All @@ -64,7 +67,7 @@
def num_tasks(self) -> int:
return len(self._tasks)

def add(self, *args: Any, task: Callable, node: Optional[Any] = None, **kwargs: Any) -> None:
def add(self, *args: Any, task: Callable, node: Any | None = None, **kwargs: Any) -> None:

Check warning on line 70 in infrahub_sdk/batch.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/batch.py#L70

Added line #L70 was not covered by tests
self._tasks.append(BatchTask(task=task, node=node, args=args, kwargs=kwargs))

async def execute(self) -> AsyncGenerator:
Expand Down Expand Up @@ -96,10 +99,10 @@
def num_tasks(self) -> int:
return len(self._tasks)

def add(self, *args: Any, task: Callable[..., Any], node: Optional[Any] = None, **kwargs: Any) -> None:
def add(self, *args: Any, task: Callable[..., Any], node: Any | None = None, **kwargs: Any) -> None:

Check warning on line 102 in infrahub_sdk/batch.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/batch.py#L102

Added line #L102 was not covered by tests
self._tasks.append(BatchTaskSync(task=task, node=node, args=args, kwargs=kwargs))

def execute(self) -> Generator[tuple[Optional[InfrahubNodeSync], Any], None, None]:
def execute(self) -> Generator[tuple[InfrahubNodeSync | None, Any], None, None]:

Check warning on line 105 in infrahub_sdk/batch.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/batch.py#L105

Added line #L105 was not covered by tests
with ThreadPoolExecutor(max_workers=self.max_concurrent_execution) as executor:
futures = [executor.submit(task.execute, return_exceptions=self.return_exceptions) for task in self._tasks]
for future in futures:
Expand Down
36 changes: 18 additions & 18 deletions infrahub_sdk/branch.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

import warnings
from typing import TYPE_CHECKING, Any, Literal, Optional, Union, overload
from typing import TYPE_CHECKING, Any, Literal, overload

Check warning on line 4 in infrahub_sdk/branch.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/branch.py#L4

Added line #L4 was not covered by tests
from urllib.parse import urlencode

from pydantic import BaseModel
Expand All @@ -17,11 +17,11 @@
class BranchData(BaseModel):
id: str
name: str
description: Optional[str] = None
description: str | None = None

Check warning on line 20 in infrahub_sdk/branch.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/branch.py#L20

Added line #L20 was not covered by tests
sync_with_git: bool
is_default: bool
has_schema_changes: bool
origin_branch: Optional[str] = None
origin_branch: str | None = None

Check warning on line 24 in infrahub_sdk/branch.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/branch.py#L24

Added line #L24 was not covered by tests
branched_from: str


Expand Down Expand Up @@ -50,11 +50,11 @@
@classmethod
def generate_diff_data_url(
cls,
client: Union[InfrahubClient, InfrahubClientSync],
client: InfrahubClient | InfrahubClientSync,
branch_name: str,
branch_only: bool = True,
time_from: Optional[str] = None,
time_to: Optional[str] = None,
time_from: str | None = None,
time_to: str | None = None,
) -> str:
"""Generate the URL for the diff_data function."""
url = f"{client.address}/api/diff/data"
Expand All @@ -80,7 +80,7 @@
sync_with_git: bool = True,
description: str = "",
wait_until_completion: Literal[True] = True,
background_execution: Optional[bool] = False,
background_execution: bool | None = False,
) -> BranchData: ...

@overload
Expand All @@ -90,7 +90,7 @@
sync_with_git: bool = True,
description: str = "",
wait_until_completion: Literal[False] = False,
background_execution: Optional[bool] = False,
background_execution: bool | None = False,
) -> str: ...

async def create(
Expand All @@ -99,8 +99,8 @@
sync_with_git: bool = True,
description: str = "",
wait_until_completion: bool = True,
background_execution: Optional[bool] = False,
) -> Union[BranchData, str]:
background_execution: bool | None = False,
) -> BranchData | str:
if background_execution is not None:
warnings.warn(
"`background_execution` is deprecated, please use `wait_until_completion` instead.",
Expand Down Expand Up @@ -206,8 +206,8 @@
self,
branch_name: str,
branch_only: bool = True,
time_from: Optional[str] = None,
time_to: Optional[str] = None,
time_from: str | None = None,
time_to: str | None = None,
) -> dict[Any, Any]:
url = self.generate_diff_data_url(
client=self.client,
Expand Down Expand Up @@ -251,7 +251,7 @@
sync_with_git: bool = True,
description: str = "",
wait_until_completion: Literal[True] = True,
background_execution: Optional[bool] = False,
background_execution: bool | None = False,
) -> BranchData: ...

@overload
Expand All @@ -261,7 +261,7 @@
sync_with_git: bool = True,
description: str = "",
wait_until_completion: Literal[False] = False,
background_execution: Optional[bool] = False,
background_execution: bool | None = False,
) -> str: ...

def create(
Expand All @@ -270,8 +270,8 @@
sync_with_git: bool = True,
description: str = "",
wait_until_completion: bool = True,
background_execution: Optional[bool] = False,
) -> Union[BranchData, str]:
background_execution: bool | None = False,
) -> BranchData | str:
if background_execution is not None:
warnings.warn(
"`background_execution` is deprecated, please use `wait_until_completion` instead.",
Expand Down Expand Up @@ -313,8 +313,8 @@
self,
branch_name: str,
branch_only: bool = True,
time_from: Optional[str] = None,
time_to: Optional[str] = None,
time_from: str | None = None,
time_to: str | None = None,
) -> dict[Any, Any]:
url = self.generate_diff_data_url(
client=self.client,
Expand Down
28 changes: 14 additions & 14 deletions infrahub_sdk/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import os
import warnings
from abc import abstractmethod
from typing import TYPE_CHECKING, Any, Optional
from typing import TYPE_CHECKING, Any

Check warning on line 8 in infrahub_sdk/checks.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/checks.py#L8

Added line #L8 was not covered by tests

import ujson
from git.repo import Repo
Expand Down Expand Up @@ -33,20 +33,20 @@


class InfrahubCheck:
name: Optional[str] = None
name: str | None = None

Check warning on line 36 in infrahub_sdk/checks.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/checks.py#L36

Added line #L36 was not covered by tests
query: str = ""
timeout: int = 10

def __init__(
self,
branch: Optional[str] = None,
branch: str | None = None,
root_directory: str = "",
output: Optional[str] = None,
initializer: Optional[InfrahubCheckInitializer] = None,
params: Optional[dict] = None,
client: Optional[InfrahubClient] = None,
output: str | None = None,
initializer: InfrahubCheckInitializer | None = None,
params: dict | None = None,
client: InfrahubClient | None = None,
):
self.git: Optional[Repo] = None
self.git: Repo | None = None
self.initializer = initializer or InfrahubCheckInitializer()

self.logs: list[dict[str, Any]] = []
Expand Down Expand Up @@ -82,7 +82,7 @@
self._client = value

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

def _write_log_entry(
self, message: str, level: str, object_id: Optional[str] = None, object_type: Optional[str] = None
self, message: str, level: str, object_id: str | None = None, object_type: str | None = None
) -> None:
log_message = {"level": level, "message": message, "branch": self.branch_name}
if object_id:
Expand All @@ -113,10 +113,10 @@
if self.output == "stdout":
print(ujson.dumps(log_message))

def log_error(self, message: str, object_id: Optional[str] = None, object_type: Optional[str] = None) -> None:
def log_error(self, message: str, object_id: str | None = None, object_type: str | None = None) -> None:

Check warning on line 116 in infrahub_sdk/checks.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/checks.py#L116

Added line #L116 was not covered by tests
self._write_log_entry(message=message, level="ERROR", object_id=object_id, object_type=object_type)

def log_info(self, message: str, object_id: Optional[str] = None, object_type: Optional[str] = None) -> None:
def log_info(self, message: str, object_id: str | None = None, object_type: str | None = None) -> None:

Check warning on line 119 in infrahub_sdk/checks.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/checks.py#L119

Added line #L119 was not covered by tests
self._write_log_entry(message=message, level="INFO", object_id=object_id, object_type=object_type)

@property
Expand Down Expand Up @@ -155,7 +155,7 @@

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

async def run(self, data: Optional[dict] = None) -> bool:
async def run(self, data: dict | None = None) -> bool:

Check warning on line 158 in infrahub_sdk/checks.py

View check run for this annotation

Codecov / codecov/patch

infrahub_sdk/checks.py#L158

Added line #L158 was not covered by tests
"""Execute the check after collecting the data from the GraphQL query.
The result of the check is determined based on the presence or not of ERROR log messages."""

Expand All @@ -179,7 +179,7 @@


def get_check_class_instance(
check_config: InfrahubCheckDefinitionConfig, search_path: Optional[Path] = None
check_config: InfrahubCheckDefinitionConfig, search_path: Path | None = None
) -> InfrahubCheck:
if check_config.file_path.is_absolute() or search_path is None:
search_location = check_config.file_path
Expand Down
Loading
Loading