Skip to content

Commit e231c93

Browse files
committed
Change ctl check to not use InfrahubCheck.init()
1 parent efdcfb7 commit e231c93

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

changelog/+89d1d0b7.deprecated.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Marked InfrahubCheck.init() as deprecated and scheduled to be removed in Infrahub SDK 2.0.0

infrahub_sdk/checks.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,26 @@
33
import asyncio
44
import importlib
55
import os
6+
import warnings
67
from abc import abstractmethod
78
from typing import TYPE_CHECKING, Any, Optional
89

910
import ujson
1011
from git.repo import Repo
1112
from pydantic import BaseModel, Field
1213

13-
from . import InfrahubClient
1414
from .exceptions import InfrahubCheckNotFoundError, UninitializedError
1515

1616
if TYPE_CHECKING:
1717
from pathlib import Path
1818

19+
from . import InfrahubClient
1920
from .schema import InfrahubCheckDefinitionConfig
2021

2122
INFRAHUB_CHECK_VARIABLE_TO_IMPORT = "INFRAHUB_CHECKS"
2223

24+
_client_class = "InfrahubClient"
25+
2326

2427
class InfrahubCheckInitializer(BaseModel):
2528
"""Information about the originator of the check."""
@@ -81,11 +84,17 @@ def client(self, value: InfrahubClient) -> None:
8184
@classmethod
8285
async def init(cls, client: Optional[InfrahubClient] = None, *args: Any, **kwargs: Any) -> InfrahubCheck:
8386
"""Async init method, If an existing InfrahubClient client hasn't been provided, one will be created automatically."""
84-
85-
instance = cls(*args, **kwargs)
86-
instance.client = client or InfrahubClient()
87-
88-
return instance
87+
warnings.warn(
88+
"InfrahubCheck.init has been deprecated and will be removed in the version in Infrahub SDK 2.0.0",
89+
DeprecationWarning,
90+
stacklevel=1,
91+
)
92+
if not client:
93+
client_module = importlib.import_module("infrahub_sdk.client")
94+
client_class = getattr(client_module, _client_class)
95+
client = client_class()
96+
kwargs["client"] = client
97+
return cls(*args, **kwargs)
8998

9099
@property
91100
def errors(self) -> list[dict[str, Any]]:

infrahub_sdk/ctl/check.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from dataclasses import dataclass
66
from pathlib import Path
77
from types import ModuleType
8-
from typing import Optional
8+
from typing import Optional, Type
99

1010
import typer
1111
from rich.console import Console
@@ -30,7 +30,7 @@ class CheckModule:
3030
module: ModuleType
3131
definition: InfrahubCheckDefinitionConfig
3232

33-
def get_check(self) -> InfrahubCheck:
33+
def get_check(self) -> Type[InfrahubCheck]:
3434
return getattr(self.module, self.definition.class_name)
3535

3636

@@ -100,7 +100,7 @@ async def run_check(
100100
log = logging.getLogger("infrahub")
101101
passed = True
102102
check_class = check_module.get_check()
103-
check = await check_class.init(client=client, params=params, output=output, root_directory=path, branch=branch)
103+
check = check_class(client=client, params=params, output=output, root_directory=path, branch=branch)
104104
param_log = f" - {params}" if params else ""
105105
try:
106106
data = execute_graphql_query(

0 commit comments

Comments
 (0)