Skip to content

Clear CodeQL quality findings and guard repeated imports - #313

Open
chris-colinsky wants to merge 1 commit into
mainfrom
chore/clear-codeql-findings
Open

chris-colinsky wants to merge 1 commit into
mainfrom
chore/clear-codeql-findings

Conversation

@chris-colinsky

Copy link
Copy Markdown
Member

Summary

Starts the 0.18.0 cycle by clearing the CodeQL code-quality findings that are real, and adding a check so the most common kind cannot come back.

Cleanup

  • Removes the 10 py/repeated-import findings: function-local imports that rebind a name the module already imports.
  • Removes 2 of the 3 py/unused-import findings by unquoting cast("X", v) to cast(X, v) where X is already imported at runtime. The codeql-config.yml comment now recommends the unquoted form.
  • Removes 59 further redundant function-local imports in tests that CodeQL's rule does not see (from X import Y repeats, and modules it cannot resolve).

Guard

  • New scripts/check_repeated_imports.py. It flags a function-local import whose bound name and target match a module-level runtime import. Imports under if TYPE_CHECKING: are ignored, since re-importing those inside a function is the normal pattern.
  • Runs as a pre-commit hook and as a step in both ci.yml and release.yml, next to the conformance manifest check.

The single commit carries both parts; its message describes only the guard.

Remaining findings

The 44 findings left are deliberate and will be dismissed in the Code Quality UI, with the reasons already recorded in codeql-config.yml: 41 ineffectual-statement (Protocol ... stubs that pyright strict requires, and one needed await), 1 unused-import used as a subscripted base class, 1 unreachable-statement inside pytest.raises, and 1 signature mismatch from a deliberate test double.

Testing

  • ruff, ruff format, pyright: clean
  • pytest: 2318 passed, 498 skipped
  • Guard checked against cases it must catch (plain, from, nested, try/except bindings) and must ignore (TYPE_CHECKING, aliased imports). Re-adding a repeated import to a real test file turns the full-tree run red.

Ruff has no rule for a function-local import that repeats a
module-level one, and CodeQL's only covers plain `import X` it can
resolve. The new script flags any function-local import whose bound
name and target match a module-level runtime import, ignoring
TYPE_CHECKING-only imports.

It runs as a pre-commit hook and in both ci.yml and release.yml
beside the conformance manifest check.
Copilot AI balanced review requested due to automatic review settings October 6, 2026 05:25

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot review overview

🟡 Changes recommended

The new guard can reject necessary imports and recommend deletions that change runtime behavior.

Review effort: Balanced
Findings: 4 Medium severity

Open (4)
What changed in this PR

Cleans up CodeQL quality findings and adds a repeated-import guard to development and release checks.

Changes:

  • Removes redundant function-local imports from tests.
  • Unquotes casts whose types are already imported at runtime.
  • Adds repeated-import checks to pre-commit, CI, and release workflows.
File Description
tests/​unit/​test_prompts.py Removes redundant prompt and datetime imports.
tests/​unit/​test_prompts_langfuse.py Removes a repeated manager import.
tests/​unit/​test_observability_otel.py Removes redundant observability test imports.
tests/​unit/​test_observability_metadata.py Removes repeated validation imports.
tests/​unit/​test_llm_provider.py Removes redundant provider test imports.
tests/​unit/​test_langfuse_sdk_internals.py Removes a repeated re import.
tests/​unit/​test_langfuse_provider_fake.py Removes repeated tracing imports.
tests/​conformance/​test_observability.py Removes redundant conformance imports.
tests/​conformance/​test_checkpoint.py Unquotes Checkpointer casts.
src/​openarmature/​llm/​providers/​openai.py Unquotes the FinishReason cast.
scripts/​check_repeated_imports.py Adds the repeated-import checker.
.pre-commit-config.yaml Registers the checker as a hook.
.github/​workflows/​release.yml Adds release-time import checking.
.github/​workflows/​ci.yml Adds CI import checking.
.github/​codeql/​codeql-config.yml Updates comments recommending unquoted runtime casts.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +60 to +61
elif isinstance(stmt, ast.If) and not _is_type_checking_guard(stmt):
found |= _module_bindings(stmt.body) | _module_bindings(stmt.orelse)
Comment on lines +60 to +66
elif isinstance(stmt, ast.If) and not _is_type_checking_guard(stmt):
found |= _module_bindings(stmt.body) | _module_bindings(stmt.orelse)
elif isinstance(stmt, ast.Try):
for block in (stmt.body, stmt.orelse, stmt.finalbody):
found |= _module_bindings(block)
for handler in stmt.handlers:
found |= _module_bindings(handler.body)
Comment on lines +78 to +87
# Keyed on the import node, so an import inside a nested function is
# reported once against its innermost function rather than once per
# enclosing one. ast.walk is breadth-first, so the innermost function is
# the last to claim each node.
owner: dict[ast.AST, str] = {}
for fn in ast.walk(tree):
if isinstance(fn, (ast.FunctionDef, ast.AsyncFunctionDef)):
for node in ast.walk(fn):
if isinstance(node, (ast.Import, ast.ImportFrom)):
owner[node] = fn.name
for node, fn_name in sorted(owner.items(), key=lambda item: item[0].lineno):
assert isinstance(node, (ast.Import, ast.ImportFrom))
for binding in _bindings(node):
if binding in module_level:

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants