refactor(git): enforce invalid-argument-type for the integrator
Why
backend/infrahub/git/integrator.py was listed under four ty overrides. This clears one of them outright: invalid-argument-type is now enforced for the file and its entry is gone from pyproject.toml. That is the first file in git/ to shed a rule since the module-wide override was split per file in #10125, and integrator.py is the largest file in the module at 91 KB.
Goal: take a rule from "suppressed for this file" to "enforced for this file", so no new violation of it can land here again.
Non-goals: the other three rules integrator.py is still listed under (unresolved-attribute 9, no-matching-overload 9, invalid-assignment 7). Those need the surrounding function signatures tightened, which is judgement work, not substitution.
What changed
Nine invalid-argument-type diagnostics, four mechanical causes plus one behaviour fix.
Mechanical (no runtime effect):
source=self.idat four sites passed aUUIDwhere the SDK declaresstr | None.generate_payload_createdoesitem_metadata["source"] = str(source)internally, so wrapping it produces the identical string. Two other sites in the same file already did this; now all six agree.load_class(import_root=...)received aPathwherestr | Noneis declared. The SDK doesPath(import_root, relative_path, module_path.name), which accepts either, sostr()is a no-op.- Two artifact early-returns fed
CoreArtifact.storage_id.value(aStringOptional) intoArtifactGenerateResult.storage_id: str. The invariant is real, the storage id is written in the samesave()as the checksum, but it is not visible at the point of use. The returns now guard on both fields, so the impossible state falls through and regenerates rather than reporting a result with no storage id.
Behaviour change, one place: import_schema_files detected unloadable schema files, logged them, and then left them in the list. The failure was therefore produced accidentally by Pydantic choking on None further down, so a user with an empty schema file was told:
Schema not valid, found '1' error(s) in foo.yml : Input should be a valid dictionary or instance of InfrahubSchemaWrite
rather than the reason YamlFile.load_content() had already recorded. Now:
Unable to load 1 schema file(s): foo.yml (Empty YAML/JSON file)
Every invalid file is still logged individually first, exactly as before, and all of them are named in the error instead of whichever one Pydantic happened to reach first. Payloads are collected into a narrowed list because re-reading .content in the later loops would lose the non-None narrowing.
Also included: the eight remaining kind-string node fetches in the file now pass the generated SDK protocol classes, finishing what #10125 started for tasks.py. That required correcting two return annotations from InfrahubNode to the precise protocol type, since the generated protocols are not InfrahubNode subclasses (CoreArtifactDefinition.__mro__ is CoreArtifactDefinition -> CoreTaskTarget -> CoreNode -> CoreNodeBase -> object). Both callers discard the return value, so there is no cascade. Only two of the module's diagnostics now come from the SDK __getattr__ union, down from 57 before this work began.
integrator.py 37 -> 27 suppressed ty diagnostics, the module 108 -> 98.
How to review
Start with the one-line pyproject.toml diff, that is the point of the PR. Then the import_schema_files restructure, which is the only behaviour change. The rest is str() wrappers and one-token kind substitutions.
Known inconsistency worth a decision: six functions now read schema.get(kind=InfrahubKind.X) a few lines above sdk.create(kind=CoreX). I stopped at the node calls deliberately, because schema.get returns a schema rather than a node so there is no typing gain, but the two spellings sitting three lines apart do look odd. schema.get accepts type[SchemaType | SchemaTypeSync] | str, so converting them is safe if you would rather have the consistency. Say the word and I will.
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
The ratchet is armed, not just asserted. With a deliberate violation injected into integrator.py, ty check . now fails on it, where before the override would have swallowed it:
backend/infrahub/git/integrator.py:835:34: error[invalid-argument-type] Argument to bound method
`InfrahubSchemaBase.validate` is incorrect: Expected `dict[str, Any]`, found `None`
Tests
The behaviour change is covered by backend/tests/unit/git/test_integrator.py: 6 unit tests, 0.09s, no database and no containers. find_unloadable_schema_files and format_unloadable_schema_files are extracted as pure functions over list[SchemaFile], which is directly constructible, so no fixtures or patching are needed. The cases include a document containing only {}, which loads to an empty dict rather than None, so the loader's valid flag rather than the content is the signal. The tests were mutation-checked: dropping that condition makes them fail.
The storage_id guard is deliberately not tested. It only changes behaviour when the checksum matches while storage_id is empty, and in that state the previous code did not work either, it raised ValidationError: storage_id from Pydantic because the field is declared str. The guard replaces that crash with a re-upload that repairs the record, so it cannot regress that state. The state should also be unreachable, since checksum and storage_id are only ever written together in the same save().
Where this logic belongs is a separate question from whether it is tested. infrahubctl schema load performs the identical flow using SDK helpers that Infrahub cannot call because they are coupled to a rich Console and typer.Exit, which is why this hand-rolled copy diverged and lost the invalid-file handling in the first place. Tracked in opsmill/infrahub-sdk-python#1225, which proposes a library-level load_schema_files(). The two functions extracted here are shaped to be replaced by it.
Impact & rollout
- Backward compatibility: one user-visible error message improves; no API or schema change.
- Performance: none.
- Config/env changes: none.
- Deployment notes: safe to deploy.
Follow-ups
- opsmill/infrahub-sdk-python#1225, after which this file loses ~20 lines and gains real test coverage.
- Deduplicating
generate_artifactand the near-identical method below it, roughly 35 lines each, which would collapse the two storage-id guards into one. - The remaining three rules for this file, plus
repository.py, whereassignmentandcall-overloadare one mypy error each and would remove two more override entries.
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