refactor(git): use SDK protocol classes for node kinds in tasks
Note: The below text covers the reasoning around this, but a review of the actual code changes might be quicker :)
- Swap out text strings for kinds -> protocols
- Remove resolved typing violations (mypy & ty)
- Split apart remaining ty rules for infrahub.git so that it's easier to solve specific violations in the future
Why
Fetching a node with a kind string makes the SDK return a bare InfrahubNode, so every attribute access on it goes through InfrahubNode.__getattr__ and resolves to Attribute | RelationshipManager[InfrahubNode] | RelatedNode[InfrahubNode]. Nothing useful can be narrowed from that union, which is why backend/infrahub/git/ suppresses unresolved-attribute and invalid-assignment for the whole module. 57 of the module's 133 suppressed ty diagnostics come from that one union, 29 of them on .value.
The module already knows the answer: it imports infrahub_sdk.protocols in three files and 16 call sites already pass a protocol class. This PR finishes that migration for tasks.py.
Goal: convert the remaining kind-string node fetches in tasks.py, and restructure the module's ty override so progress is visible per file instead of hidden behind one module-wide glob.
Non-goals: integrator.py's 16 equivalent sites (same edit, separate PR), and the remaining union errors that come from InfrahubNode-annotated parameters rather than from the kind argument.
What changed
-
14 call sites in
tasks.pynow pass the generated protocol class instead ofInfrahubKind.*:CoreGenericRepository(5),CoreProposedChange(2),CoreReadOnlyRepository(2),CoreFileCheck(2),CoreRepositoryValidator,CoreUserValidator,CoreStandardCheck.tasks.pydrops from 73 to 48 suppressed ty diagnostics; the module from 133 to 108. -
The
backend/infrahub/git/**ty override is split per rule, per file, matching the convention already used forcore/**andgraphql/**. Onlyunused-type-ignore-commentstays module-wide, since that is a genuine mypy/ty clash rather than debt.rule files still listed invalid-argument-typebase, integrator, repository, tasks, utils invalid-assignmentintegrator, tasks, utils unresolved-attributeintegrator, tasks, utils no-matching-overloadbase, integrator, repository, tasks invalid-return-typebase invalid-awaitandpossibly-missing-attributeare deleted outright: neither reports anything in the module. The split immediately enforces all five rules forsync.py,models.py,directory.py,worktree.py,graphql_queries.py,constants.py,__init__.pyand theclosure_builder/andfingerprint/subpackages, so no new violation can slip into any of them. -
Three stale mypy
disable_error_codeentries removed:attr-definedandindexoninfrahub.git.base,return-valueoninfrahub.git.repository. These no longer match any reported error and were already stale before this change.infrahub.git_credential.askpasswas measured too and genuinely needs all three of its entries, so it is untouched.
What stayed the same: behaviour. get, filters and create all declare kind: str | type[SchemaType], and a class is resolved through _get_schema_name (python_sdk/infrahub_sdk/schema/__init__.py:263), which returns __name__. Every converted constant's value is exactly the class name that replaced it, e.g. InfrahubKind.FILECHECK == "CoreFileCheck", so the identical string reaches the schema lookup as before. The typing gain comes purely from overload selection: kind: type[SchemaType] returns SchemaType, and the overload used when raise_when_missing is omitted returns a non-Optional node, so no new narrowing is introduced.
How to review
pyproject.toml is the part worth reading carefully; the tasks.py diff is 14 one-token substitutions plus 5 added imports, and formatting collapsed a few calls that now fit on one line.
How to test
uv run ty check . # All checks passed!
uv run invoke backend.mypy # Success: no issues found in 1601 source files
uv run invoke backend.ruff
uv run pytest backend/tests/unit/git/ --no-cov -q # 178 passed
Integration coverage for these code paths lives in backend/tests/integration/git/ and needs the testcontainers stack, so CI is the gate there.
Impact & rollout
- Backward compatibility: none affected; the same kind string reaches the SDK.
- Performance: none.
- Config/env changes: none.
- Deployment notes: safe to deploy.
Follow-ups
integrator.py: the same conversion at 16 sites (CoreTransformJinja2,CoreGraphQLQuery,CoreArtifactDefinition,CoreGroup,CoreGeneratorDefinition,CoreCheckDefinition,CoreTransformPython).- After that, 28 of
tasks.py's remaining 48 diagnostics are still the same SDK union, but sourced from parameters annotatedInfrahubNodeand from.peertraversals. Those need the surrounding signatures tightened, which is judgement work rather than substitution. - The 10
client.schema.get(kind=...)sites can also take a protocol class, but there is no typing payoff since they return a schema rather than a node.
Checklist
- Tests added/updated
- Changelog entry added
- External docs updated (if user-facing or ops-facing change)
- Internal .md docs updated (internal knowledge and AI code tools knowledge)
- I have reviewed AI generated content