test: real protocol assertions and an exercised enable_runtime_checks - #209
Merged
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #209 +/- ##
=======================================
- Coverage 94.1% 93.3% -0.8%
=======================================
Files 41 41
Lines 2479 2503 +24
Branches 290 298 +8
=======================================
+ Hits 2333 2336 +3
- Misses 98 119 +21
Partials 48 48 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
thomaspinder
enabled auto-merge (squash)
July 29, 2026 07:17
Closes #174: `assert hasattr(P, "__protocol_attrs__") or runtime_checkable` is always true (`runtime_checkable` is a truthy function object). Each of the five instances now pins the protocol's declared member set via `typing_extensions.get_protocol_members` — version-stable, since stdlib `__protocol_attrs__` only exists on 3.12+ — plus an isinstance pair over a conforming and a non-conforming stub. Those isinstance calls are what pin runtime-checkability: isinstance against a non-`@runtime_checkable` Protocol raises TypeError. Closes #181: adds a subprocess test that enables runtime checks in a fresh interpreter (the beartype wrapping mutates classes in place, so it must not leak into the rest of the suite) and drives a synthetic no-MCMC pipeline: VARData.from_df -> VAR -> FittedVAR.sigma/forecast -> set_identification_strategy -> impulse_response, then asserts a BeartypeCallHintViolation fires for `forecast(steps="two")`. That test surfaced two real defects in `enable_runtime_checks`: * Every wrapped method annotated with a TYPE_CHECKING-only name (e.g. `FittedVAR.forecast -> "ForecastResult"`) raised BeartypeCallHintForwardRefException on call — beartype resolves stringified annotations against the defining module at call time. `_bind_deferred_imports` now materialises each wrapped module's `if TYPE_CHECKING:` imports first. * The wrap loop wrapped every class in `dir(mod)`, including imported ones — `typing.Protocol`, `typing.Any`, `impulso.volatility.Constant`, the result types — mutating classes the module does not own. It now wraps only classes defined in the module. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Egjd7ToFeb9TQqFnfRQZxV
thomaspinder
force-pushed
the
test/174-181-runtime-checks
branch
from
July 29, 2026 07:21
3f91446 to
4175dc5
Compare
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.
Summary
Two test-infrastructure issues, one PR — and the new test immediately earned its keep by surfacing two real defects.
tests/test_protocols.py(assert hasattr(P, "__protocol_attrs__") or runtime_checkable— the second operand is a truthy function object) replaced with real ones:typing_extensions.is_protocol+ exactget_protocol_memberssets per protocol (py3.11-verified — stdlib__protocol_attrs__is 3.12+), plus conforming/non-conforming stubisinstancechecks that genuinely pin runtime-checkability.enable_runtime_checks()was only ever asserted callable. A new subprocess test enables it in a fresh interpreter, drives a full synthetic pipeline (VARData → VAR → FittedVAR → sigma/forecast → IdentifiedVAR → IRF), and asserts aBeartypeCallHintViolationactually fires for a mistyped call. It failed immediately, exposing two pre-existing defects that made the feature unusable (exactly as the feat(diagnostics): VAR-aware convergence and stability report #176 review predicted):-> "ForecastResult","xr.Dataset") against the defining module at call time, and every such name wasTYPE_CHECKING-only →BeartypeCallHintForwardRefExceptionon first use. Fixed with_bind_deferred_imports()— AST-parses each wrapped module'sif TYPE_CHECKING:block and binds the imports for real (noexec), including dotted aliases.dir(mod)— includingtyping.Protocoland classes owned by other modules (Constantreached throughspec), mutating shared state. Fixed with a__module__ == mod.__name__ownership guard.Test teeth verified: with the src fix stashed, the subprocess test fails with the original forward-ref error.
Closes #174
Closes #181
Gates
ruff/ty/ fast suite green (532 passed, 29 deselected); the rewritten protocol file passes standalone under Python 3.11.🤖 Generated with Claude Code
https://claude.ai/code/session_01Egjd7ToFeb9TQqFnfRQZxV