Skip to content
Open
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: 3 additions & 3 deletions src/flext_cli/_typings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

from flext_core import t
from jinja2.sandbox import SandboxedEnvironment
from tomlkit.container import Container
from tomlkit.items import AoT, Array, Item, Table
from tomlkit.container import Container, OutOfOrderTableProxy
from tomlkit.items import AoT, Array, InlineTable, Item, Table
from tomlkit.toml_document import TOMLDocument
from typing_extensions import TypeForm

Expand Down Expand Up @@ -44,7 +44,7 @@ class FlextCliTypesBase:
type TomlArray = Array
type TomlAoT = AoT
type TomlContainer = Container
type TomlParent = TOMLDocument | Table
type TomlParent = TOMLDocument | Table | InlineTable | OutOfOrderTableProxy
type TomlValue = TOMLDocument | Table | Item | Array | AoT | Container
type RuntimeAnnotation = TypeForm
type SignalHandler = int | Callable[[int, FrameType | None], None] | None
Expand Down
10 changes: 8 additions & 2 deletions src/flext_cli/_typings/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from flext_core import p, t
from ruamel.yaml.comments import CommentedMap, CommentedSeq
from ruamel.yaml.tokens import CommentToken
from tomlkit.container import Container
from tomlkit.container import Container, OutOfOrderTableProxy
from tomlkit.items import AoT, Array, Item, Table
from tomlkit.toml_document import TOMLDocument

Expand Down Expand Up @@ -98,7 +98,12 @@ class FlextCliTypesDomain:
]
type ServiceMessageFormatter = Callable[[str, ResultValue], str]
type TomlMappingSource = (
t.JsonPayload | t.JsonMapping | Mapping[str, t.Scalar] | Item | TOMLDocument
t.JsonPayload
| t.JsonMapping
| Mapping[str, t.Scalar]
| Item
| TOMLDocument
| OutOfOrderTableProxy
)
type TomlUnwrappedSource = t.JsonPayload | t.JsonMapping | Item
type TomlStringListSource = (
Expand All @@ -111,6 +116,7 @@ class FlextCliTypesDomain:
| Array
| AoT
| Container
| OutOfOrderTableProxy
| t.JsonMapping
| t.JsonPayload
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import tomlkit
from flext_core import r, u
from tomlkit.container import OutOfOrderTableProxy
from tomlkit.items import AoT, Array, Item, Table
from tomlkit.toml_document import TOMLDocument

Expand Down Expand Up @@ -52,7 +53,9 @@ def toml_unwrap_item(

"""
return FlextCliUtilitiesConversion.JSON_VALUE_ADAPTER.validate_python(
value.unwrap() if isinstance(value, TOMLDocument | Item) else value,
value.unwrap()
if isinstance(value, TOMLDocument | Item | OutOfOrderTableProxy)
else value,
strict=True,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def toml_table_child(container: TOMLDocument | Table, key: str) -> Table | None:
return value if isinstance(value, Table) else None

@staticmethod
def toml_item_child(container: TOMLDocument | Table, key: str) -> Item | None:
def toml_item_child(container: t.Cli.TomlParent, key: str) -> Item | None:
"""Return a raw TOML item from a container.

Returns:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def toml_ensure_tool_table(doc: TOMLDocument) -> Table:
return FlextCliUtilitiesTomlPart02.toml_ensure_table(doc, "tool")

@staticmethod
def toml_value(container: TOMLDocument | Table, key: str) -> t.JsonValue | None:
def toml_value(container: t.Cli.TomlParent, key: str) -> t.JsonValue | None:
"""Return a normalized TOML value from a container.

Returns:
Expand Down
135 changes: 126 additions & 9 deletions src/flext_cli/_utilities/_toml/flextcliutilitiestoml_part_04.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,147 @@

from __future__ import annotations

from collections.abc import Sequence
from typing import TYPE_CHECKING
from collections.abc import Mapping, Sequence
from itertools import starmap

from flext_core import u
from tomlkit.items import Array
from tomlkit.container import OutOfOrderTableProxy

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2: Custom agent: Flag AI Slop and Fabricated Changes

This import relies on TOMLKit’s undocumented OutOfOrderTableProxy implementation type, contradicting the PR’s claim that it uses no private TOMLKit access. Resolve out-of-order views through a supported public API or revise that claim.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At src/flext_cli/_utilities/_toml/flextcliutilitiestoml_part_04.py, line 12:

<comment>This import relies on TOMLKit’s undocumented `OutOfOrderTableProxy` implementation type, contradicting the PR’s claim that it uses no private TOMLKit access. Resolve out-of-order views through a supported public API or revise that claim.</comment>

<file context>
@@ -6,30 +6,140 @@
 
 from flext_core import u
-from tomlkit.items import Array
+from tomlkit.container import OutOfOrderTableProxy
+from tomlkit.items import Array, InlineTable, Table
+from tomlkit.toml_document import TOMLDocument
</file context>

from tomlkit.items import Array, InlineTable, Table
from tomlkit.toml_document import TOMLDocument

from flext_cli import t
from flext_cli._utilities import FlextCliUtilitiesConversion
from flext_cli._utilities._toml import (
FlextCliUtilitiesTomlPart01,
FlextCliUtilitiesTomlPart02,
FlextCliUtilitiesTomlPart03,
)

if TYPE_CHECKING:
from tomlkit.items import Table
from tomlkit.toml_document import TOMLDocument


class FlextCliUtilitiesTomlPart04:
"""Implementation part for FlextCliUtilitiesTomlPart04."""

@staticmethod
def toml_sync_document_values(
document: TOMLDocument | Table | InlineTable,
payload: t.JsonMapping,
*,
prune: bool = False,
) -> None:
"""Synchronize native TOML values while preserving unchanged source items.

Validate the complete JSON payload and TOML conversion before mutation.
Absent keys remain untouched; prune=True removes them recursively. The
root must be a stable document or table. Fragmented child views are read
again from that root before every mutation; a proxy is not a valid root.
Native conversion and validation errors propagate unchanged.
"""
validated = FlextCliUtilitiesConversion.JSON_MAPPING_ADAPTER.validate_python(
payload,
strict=True,
)
FlextCliUtilitiesTomlPart01.toml_document_from_mapping(validated)
current = FlextCliUtilitiesConversion.JSON_MAPPING_ADAPTER.validate_python(
document.unwrap(),
strict=True,
)
FlextCliUtilitiesTomlPart04._sync_document_values(
document, (), validated, current, prune=prune
)

@staticmethod
def _toml_parent_at(
document: TOMLDocument | Table | InlineTable,
path: Sequence[str],
) -> t.Cli.TomlParent:
"""Resolve a fresh native view from the stable document root.

Returns:
The original table or its current out-of-order view.

Raises:
TypeError: If a path component no longer identifies a native table.

"""
parent: t.Cli.TomlParent = document
for key in path:
child: object = parent[key]
if not isinstance(child, Table | InlineTable | OutOfOrderTableProxy):
msg = f"TOML sync path is not a table: {key}"
raise TypeError(msg)
parent = child
return parent

@staticmethod
def _sync_document_values(

Check failure on line 81 in src/flext_cli/_utilities/_toml/flextcliutilitiestoml_part_04.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this function to reduce its Cognitive Complexity from 16 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=flext-sh_flext-cli&issues=AaJL7FKjNXN7swAzm3vi&open=AaJL7FKjNXN7swAzm3vi&pullRequest=515
document: TOMLDocument | Table | InlineTable,
path: tuple[str, ...],
payload: t.JsonMapping,
current: t.JsonMapping,
*,
prune: bool,
) -> None:
"""Apply validated values without retaining invalidated native views."""
if prune:
for key in current:
if key not in payload:
del FlextCliUtilitiesTomlPart04._toml_parent_at(document, path)[

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1: This prune operation relies on TOMLKit's known-unfixed #732 dotted-key deletion behavior, so prune=True can fail the new reproducer or damage sibling keys. Validate an admissible TOMLKit release or add a safe workaround before enabling this path.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At src/flext_cli/_utilities/_toml/flextcliutilitiestoml_part_04.py, line 88:

<comment>This prune operation relies on TOMLKit's known-unfixed #732 dotted-key deletion behavior, so `prune=True` can fail the new reproducer or damage sibling keys. Validate an admissible TOMLKit release or add a safe workaround before enabling this path.</comment>

<file context>
@@ -6,30 +6,140 @@
+        if prune:
+            for key in current:
+                if key not in payload:
+                    del FlextCliUtilitiesTomlPart04._toml_parent_at(document, path)[
+                        key
+                    ]
</file context>

key
]
for key, value in payload.items():
current_value = current.get(key)
if isinstance(value, Mapping) and isinstance(current_value, Mapping):
FlextCliUtilitiesTomlPart04._sync_document_values(
document, (*path, key), value, current_value, prune=prune
)
elif not FlextCliUtilitiesTomlPart04._toml_values_equal(
current_value, value
):
parent = FlextCliUtilitiesTomlPart04._toml_parent_at(document, path)
child: object = parent.get(key)
if (
isinstance(child, Array)
and isinstance(value, list)
and all(isinstance(item, str) for item in child)
and all(isinstance(item, str) for item in value)
):
FlextCliUtilitiesTomlPart04.toml_sync_string_list(
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
parent,
key,
tuple(item for item in value if isinstance(item, str)),
)
else:
parent[key] = value

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1: This assignment still writes through an OutOfOrderTableProxy, whose locked TOMLKit 0.15.2 has the #677 sibling-loss defect. Block this path until CI uses an admissible TOMLKit version or the proxy is safely consolidated before mutation.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At src/flext_cli/_utilities/_toml/flextcliutilitiestoml_part_04.py, line 114:

<comment>This assignment still writes through an `OutOfOrderTableProxy`, whose locked TOMLKit 0.15.2 has the #677 sibling-loss defect. Block this path until CI uses an admissible TOMLKit version or the proxy is safely consolidated before mutation.</comment>

<file context>
@@ -6,30 +6,140 @@
+                        tuple(item for item in value if isinstance(item, str)),
+                    )
+                else:
+                    parent[key] = value
+
+    @staticmethod
</file context>


@staticmethod
def _toml_values_equal(current: t.JsonValue, expected: t.JsonValue) -> bool:
"""Compare TOML scalar kinds through nested array and table values.

Returns:
Whether the values and every nested native kind are identical.

"""
if not u.same_type(current, expected):
return False
if isinstance(current, dict) and isinstance(expected, dict):
return current.keys() == expected.keys() and all(
FlextCliUtilitiesTomlPart04._toml_values_equal(
current[key], expected[key]
)
for key in current
)
if isinstance(current, list) and isinstance(expected, list):
return len(current) == len(expected) and all(
starmap(
FlextCliUtilitiesTomlPart04._toml_values_equal,
zip(current, expected, strict=True),
)
)
return current == expected

@staticmethod
def toml_sync_value(
container: TOMLDocument | Table,
container: t.Cli.TomlParent,
key: str,
expected: t.JsonValue,
) -> bool:
Expand All @@ -47,7 +164,7 @@

@staticmethod
def toml_sync_string_list(
container: TOMLDocument | Table,
container: t.Cli.TomlParent,
key: str,
expected: Sequence[str],
*,
Expand Down
Loading
Loading