fix: stop __version__ drift (#85) and mypy [no-redef] in optional mcp import (#87)#100
Merged
Merged
Conversation
Two remaining infra fixes from the #85-#88 batch (#86/#88 already merged in d83609d). - #85: derive agent_kernel.__version__ from installed distribution metadata (importlib.metadata.version("weaver-kernel")) instead of a hardcoded literal that had drifted to "0.5.0" while the package shipped 0.7.0/0.8.0. Falls back to "0.0.0+local" without dist metadata. This matches RELEASE.md, which only ever documented bumping pyproject.toml. - #87: resolve the optional McpError import through a _load_mcp_error() helper (mirroring mcp_support.import_optional) so the _McpError global is declared exactly once, fixing the mypy [no-redef] that surfaced when the mcp extra was not installed. Adds regression tests (tests/test_version.py; two _load_mcp_error cases in tests/test_mcp_driver.py) and CHANGELOG entries. make ci passes (564 passed, 1 skipped; mypy clean with and without the mcp extra). https://claude.ai/code/session_0185WabrTuoL3jCNZiER7ES6
There was a problem hiding this comment.
Pull request overview
Fixes two infra/correctness issues: preventing agent_kernel.__version__ from drifting from the packaged distribution version, and removing a mypy [no-redef] error caused by an optional mcp import pattern.
Changes:
- Derive
agent_kernel.__version__fromimportlib.metadata.version("weaver-kernel")with a local fallback when dist metadata is unavailable. - Refactor optional
McpErrorloading into a_load_mcp_error()helper to avoid mypy redefinition errors. - Add regression tests and document both fixes in the changelog.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| tests/test_version.py | New tests pin __version__ to distribution metadata and ensure it remains exported. |
| tests/test_mcp_driver.py | Adds regression tests for _load_mcp_error() when mcp is present vs missing. |
| src/agent_kernel/drivers/mcp.py | Introduces _load_mcp_error() and resolves _McpError once to avoid mypy [no-redef]. |
| src/agent_kernel/init.py | Replaces hardcoded __version__ with importlib.metadata-derived version + fallback. |
| CHANGELOG.md | Adds Unreleased “Fixed” entries for issues #85 and #87. |
Address PR review feedback: _load_mcp_error accessed exceptions.McpError directly, so a module that imports but lacks/renames McpError would raise AttributeError at import time and break agent_kernel.drivers.mcp entirely. Use getattr(..., None) plus a type check to degrade to None, restoring the old ImportError -> None fallback semantics for the optional-dependency path. Add a regression test covering the module-present-but-attribute-missing case. https://claude.ai/code/session_01TnKC4bmhsCCJTju77jKwdJ
Audit follow-up (#85 AC1): the existing test only re-derived __version__ from the same importlib.metadata source it is defined from, so it could not catch a pyproject bump that the installed/editable metadata had not picked up. Add test_version_matches_pyproject_declaration, which parses [project].version directly (regex, no tomllib so it runs on 3.10) and skips gracefully when the source tree is absent. https://claude.ai/code/session_01TnKC4bmhsCCJTju77jKwdJ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #85 and #87 — the two remaining infra/correctness defects from the
#85–#88triage batch (the other two, #86 and #88, were already fixed ind83609d).What changed
#85 —
agent_kernel.__version__drifted frompyproject.tomlsrc/agent_kernel/__init__.py:__version__is now derived from installed distribution metadata viaimportlib.metadata.version("weaver-kernel")(single source of truth), with aPackageNotFoundError→"0.0.0+local"fallback for source trees without dist metadata. The hardcoded"0.5.0"literal is gone.tests/test_version.py(new): asserts__version__ == importlib.metadata.version("weaver-kernel")and that__version__stays in__all__.#87 — optional
McpErrorimport triggered mypy[no-redef]whenmcpwas absentsrc/agent_kernel/drivers/mcp.py: replaced the annotate-then-import ... as _McpErrorpattern with a_load_mcp_error()helper that imports throughimportlib.import_module(mirroring the existingmcp_support.import_optionalidiom)._McpErroris now declared exactly once, so the[no-redef]is gone.tests/test_mcp_driver.py: added two regression tests covering both branches of_load_mcp_error(mcp present → returnsMcpError; mcp absent → returnsNone).Docs
CHANGELOG.md: added### Fixedentries for agent_kernel.__version__ reports "0.5.0" but pyproject.toml declares 0.7.0 #85 and _McpError annotation+import pattern triggers mypy [no-redef] when mcp is unavailable #87 under[Unreleased], matching the existing issue-referenced style.Why
__version__reported0.5.0while the package shipped0.7.0/0.8.0.RELEASE.mdonly ever documented bumpingpyproject.toml, so the literal was a latent drift bug; deriving from metadata aligns the code with the documented release process and makes future drift impossible._McpErrortwice (annotation +import as), which mypy reads as a redefinition once themcpimport resolves toAnyunderignore_missing_imports(i.e. on a base install without the[mcp]extra).How verified
All commands run in a clean dev venv (
pip install -e ".[dev,mcp]"):metadata: 0.8.0 | module: 0.5.0mcpuninstalled):mcp.py:28: error: Name "_McpError" already defined on line 26 [no-redef]metadata: 0.8.0 | module: 0.8.0python -m mypy src/agent_kernel/drivers/mcp.py→Successboth with and without themcpextra installed.make ci→ exit 0:ruff format --checkclean,ruff checkAll checks passed!,mypySuccess: no issues found in 41 source files, 564 passed / 1 skipped, coverage 94%, examples ran.tests/test_version.py(2) +_load_mcp_errorcases (2) all pass; the mcp-absent branch is covered.Scope notes (Mode B)
Strictly limited to #85 and #87. Diffstat: 5 files, +62/−10 (4 tracked + 1 new test). No back-compat shims (per request — none needed; both are internal/diagnostic surfaces). Other open triage issues (#92–#96, #99, #94) are independent feature/docs work and intentionally not bundled here.
Risks / caveats
importlib.metadata.version()must use the distribution name (weaver-kernel), not the import name (agent_kernel) — handled and covered bytest_version.py. In an uninstalled raw source tree the value is0.0.0+local; in editable/CI installs it reflects the real dist version.https://claude.ai/code/session_0185WabrTuoL3jCNZiER7ES6
Generated by Claude Code