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
8 changes: 2 additions & 6 deletions infrahub_sdk/branch.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,7 @@ async def all(self) -> dict[str, BranchData]:
query = Query(name="GetAllBranch", query=QUERY_ALL_BRANCHES_DATA)
data = await self.client.execute_graphql(query=query.render(), tracker="query-branch-all")

branches = {branch["name"]: BranchData(**branch) for branch in data["Branch"]}

return branches
return {branch["name"]: BranchData(**branch) for branch in data["Branch"]}

async def get(self, branch_name: str) -> BranchData:
query = Query(name="GetBranch", query=QUERY_ONE_BRANCH_DATA, variables={"branch_name": str})
Expand Down Expand Up @@ -230,9 +228,7 @@ def all(self) -> dict[str, BranchData]:
query = Query(name="GetAllBranch", query=QUERY_ALL_BRANCHES_DATA)
data = self.client.execute_graphql(query=query.render(), tracker="query-branch-all")

branches = {branch["name"]: BranchData(**branch) for branch in data["Branch"]}

return branches
return {branch["name"]: BranchData(**branch) for branch in data["Branch"]}

def get(self, branch_name: str) -> BranchData:
query = Query(name="GetBranch", query=QUERY_ONE_BRANCH_DATA, variables={"branch_name": str})
Expand Down
6 changes: 2 additions & 4 deletions infrahub_sdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,7 @@ def _initialize(self) -> None:
async def get_version(self) -> str:
"""Return the Infrahub version."""
response = await self.execute_graphql(query="query { InfrahubInfo { version }}")
version = response.get("InfrahubInfo", {}).get("version", "")
return version
return response.get("InfrahubInfo", {}).get("version", "")

async def get_user(self) -> dict:
"""Return user information"""
Expand Down Expand Up @@ -1602,8 +1601,7 @@ def _initialize(self) -> None:
def get_version(self) -> str:
"""Return the Infrahub version."""
response = self.execute_graphql(query="query { InfrahubInfo { version }}")
version = response.get("InfrahubInfo", {}).get("version", "")
return version
return response.get("InfrahubInfo", {}).get("version", "")

def get_user(self) -> dict:
"""Return user information"""
Expand Down
3 changes: 1 addition & 2 deletions infrahub_sdk/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,11 @@ def diff_tree_node_to_node_diff(node_dict: dict[str, Any], branch_name: str) ->
)
relationship_diff["peers"] = peer_diffs
element_diffs.append(relationship_diff)
node_diff = NodeDiff(
return NodeDiff(
branch=branch_name,
kind=str(node_dict.get("kind")),
id=str(node_dict.get("uuid")),
action=str(node_dict.get("status")),
display_label=str(node_dict.get("label")),
elements=element_diffs,
)
return node_diff
6 changes: 2 additions & 4 deletions infrahub_sdk/node/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,8 +579,7 @@ async def artifact_fetch(self, name: str) -> str | dict[str, Any]:
self._validate_artifact_support(ARTIFACT_GENERATE_FEATURE_NOT_SUPPORTED_MESSAGE)

artifact = await self._client.get(kind="CoreArtifact", name__value=name, object__ids=[self.id])
content = await self._client.object_store.get(identifier=artifact._get_attribute(name="storage_id").value)
return content
return await self._client.object_store.get(identifier=artifact._get_attribute(name="storage_id").value)

async def delete(self, timeout: int | None = None, request_context: RequestContext | None = None) -> None:
input_data = {"data": {"id": self.id}}
Expand Down Expand Up @@ -1208,8 +1207,7 @@ def artifact_generate(self, name: str) -> None:
def artifact_fetch(self, name: str) -> str | dict[str, Any]:
self._validate_artifact_support(ARTIFACT_FETCH_FEATURE_NOT_SUPPORTED_MESSAGE)
artifact = self._client.get(kind="CoreArtifact", name__value=name, object__ids=[self.id])
content = self._client.object_store.get(identifier=artifact._get_attribute(name="storage_id").value)
return content
return self._client.object_store.get(identifier=artifact._get_attribute(name="storage_id").value)

def delete(self, timeout: int | None = None, request_context: RequestContext | None = None) -> None:
input_data = {"data": {"id": self.id}}
Expand Down
3 changes: 1 addition & 2 deletions infrahub_sdk/playback.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,4 @@ def _read_request(
with Path(f"{self.directory}/{filename}.json").open(encoding="utf-8") as fobj:
data = ujson.load(fobj)

response = httpx.Response(status_code=data["status_code"], content=data["response_content"], request=request)
return response
return httpx.Response(status_code=data["status_code"], content=data["response_content"], request=request)
3 changes: 1 addition & 2 deletions infrahub_sdk/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,4 @@ def initialize_repo(self) -> Repo:

@property
def active_branch(self) -> str | None:
active_branch = porcelain.active_branch(self.root_directory).decode("utf-8")
return active_branch
return porcelain.active_branch(self.root_directory).decode("utf-8")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Handle detached HEAD to avoid AttributeError.

porcelain.active_branch() can return None (e.g., detached HEAD). Decoding without a guard may crash.

Apply:

-        return porcelain.active_branch(self.root_directory).decode("utf-8")
+        branch = porcelain.active_branch(self.root_directory)
+        return branch.decode("utf-8") if branch else None
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
return porcelain.active_branch(self.root_directory).decode("utf-8")
branch = porcelain.active_branch(self.root_directory)
return branch.decode("utf-8") if branch else None
🤖 Prompt for AI Agents
In infrahub_sdk/repository.py around line 32,
porcelain.active_branch(self.root_directory) can return None (detached HEAD) so
calling .decode("utf-8") will raise AttributeError; update the code to first
assign the result to a variable, check if it's None, and if so return a sensible
fallback (e.g., None or the current commit id), otherwise decode and return the
branch name.

3 changes: 1 addition & 2 deletions infrahub_sdk/timestamp.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ def obj(self) -> ZonedDateTime:
@classmethod
def _parse_string(cls, value: str) -> ZonedDateTime:
try:
zoned_date = ZonedDateTime.parse_common_iso(value)
return zoned_date
return ZonedDateTime.parse_common_iso(value)
except ValueError:
pass

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ ignore = [
"PLW1641", # Object does not implement `__hash__` method
"PTH100", # `os.path.abspath()` should be replaced by `Path.resolve()`
"PTH109", # `os.getcwd()` should be replaced by `Path.cwd()`
"RET504", # Unnecessary assignment to `data` before `return` statement
"RUF005", # Consider `[*path, str(key)]` instead of concatenation
"RUF015", # Prefer `next(iter(input_data["variables"].keys()))` over single element slice
"RUF029", # Function is declared `async`, but doesn't `await` or use `async` features.
Expand Down Expand Up @@ -273,6 +272,7 @@ max-complexity = 17
"ANN201", # ANN201 Missing return type annotation for public function
"ANN202", # Missing return type annotation for private function
"ANN204", # Missing return type annotation for special method
"RET504", # Unnecessary assignment to `data` before `return` statement
]

"tests/unit/sdk/test_client.py" = [
Expand Down