Skip to content

refactor: type the generated inventory as a group mapping - #70

Merged
cigamit merged 1 commit into
ctrliq:mainfrom
blaipr:refactor/type-the-generated-inventory
Sep 13, 2026
Merged

cigamit merged 1 commit into
ctrliq:mainfrom
blaipr:refactor/type-the-generated-inventory

Conversation

@blaipr

@blaipr blaipr commented Sep 13, 2026

Copy link
Copy Markdown
Contributor

generate_inventory built one dict holding two unrelated shapes: _meta, whose value is host variables, and one entry per group name, whose value is hosts, children and vars. Indexing that dict means the checker sees the union of both, so inv_list[group]['hosts'].append(...) reads as .append on a dict and ty reported three unresolved attributes for lines that are correct at runtime.

The two shapes are now two variables:

class InventoryGroup(TypedDict):
    hosts: list[str]
    children: list[str]
    vars: dict[str, str]

groups: dict[str, InventoryGroup] = {}
hostvars: dict[str, dict[str, str | int]] = {}

They are joined into the Ansible dynamic inventory format on the way out, _meta first and the groups in the order they were met, which is the order the single dict produced before.

Verification

No behaviour change, checked rather than reasoned about. json_inventory and ini_inventory return byte-identical output at 0, 1, 2, 9, 10, 11, 100 and 1000 hosts, compared against main for each size. Those sizes exercise the empty case, each group modulus, the boundary either side of ten, and the by-10s, by-100s and by-1000s rollups that build children.

ty goes from 35 diagnostics to 32, which is these three and nothing else. ruff format --check, ruff check and the unit suite (366 passing) are unchanged.

Why this one and not the rest

Of the remaining type-checker findings this is the only one that is a single decision about a single structure. The others each need their own judgment, mostly about where ty cannot see through self.__class__ in pages/page.py or a widened dict in api/client.py. None of them is a runtime bug, checked individually, and none is in this pull request.

generate_inventory built one dict holding two unrelated shapes: `_meta`, whose
value is host variables, and a group name per entry, whose value is hosts,
children and vars. Indexing it meant the checker saw the union of both, so
`inv_list[group]['hosts'].append(...)` read as `.append` on a dict and reported
three unresolved attributes.

The two shapes are now two variables. `groups` is a `dict[str, InventoryGroup]`,
a TypedDict naming the three keys a group actually has, and `hostvars` is its
own mapping. They are joined into the Ansible dynamic inventory format on the
way out, `_meta` first and the groups in the order they were met, which is the
order the single dict produced before.

No behaviour change. `json_inventory` and `ini_inventory` return byte-identical
output at 0, 1, 2, 9, 10, 11, 100 and 1000 hosts, the sizes that exercise the
empty case, each group modulus and the by-10s, by-100s and by-1000s rollups.

ty goes from 35 diagnostics to 32.
@ciq-it-service-account

Copy link
Copy Markdown

Snyk checks have passed. No issues have been found so far.

Status Scan Engine Critical High Medium Low Total (0)
Code Security 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

@cigamit cigamit self-assigned this Sep 13, 2026
@cigamit cigamit added the enhancement New feature or request label Sep 13, 2026
@cigamit
cigamit merged commit 60bf7b9 into ctrliq:main Sep 13, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Development

Successfully merging this pull request may close these issues.

3 participants