Restore NullContext as a distinct class with virtual subclass semantics#218
Merged
Conversation
Contributor
Previously `NullContext` was a direct alias of `ContextBase`. This preserved substitutability (any context could be passed where a `NullContext` was expected) but degraded introspection: `.context_type.__name__` reported "ContextBase" with the abstract base's docstring, which was confusing for users of models that declare `context: NullContext`. Reintroduce `NullContext` as a distinct subclass of `ContextBase` whose metaclass lies in `__instancecheck__` / `__subclasscheck__` so that any `ContextBase` instance/subclass virtually satisfies `NullContext`. The lie is gated to `NullContext` itself, so user-defined subclasses follow normal class-hierarchy rules. Pydantic's v2 instance fast-path in `model_validate` respects the lie, so original context instances flow through unchanged. Net effect: zero behavior change at runtime; introspection now reports `NullContext` with its own docstring. Signed-off-by: Pascal Tomecek <pascal.tomecek@cubistsystematic.com>
03fc8ab to
3a93c18
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #218 +/- ##
=======================================
Coverage 95.37% 95.37%
=======================================
Files 142 142
Lines 11428 11437 +9
Branches 623 625 +2
=======================================
+ Hits 10899 10908 +9
Misses 399 399
Partials 130 130 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
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.
Problem
NullContextis currently aliased toContextBase(commit fc64400, PR #166). This preserved substitutability (any context can be passed whereNullContextis expected) but degraded introspection: for a model declaringcontext: NullContext,.context_type.__name__reports"ContextBase"with the abstract base's docstring, which is confusing for users.Approach
Reintroduce
NullContextas a distinct subclass ofContextBasewith a metaclass that lies in__instancecheck__/__subclasscheck__: anyContextBaseinstance or subclass virtually satisfiesNullContext. The lie is gated toNullContextitself, so user-defined subclasses ofNullContextfollow normal class-hierarchy rules.Pydantic v2's instance fast-path in
model_validaterespects the lie, so original context instances flow through unchanged — same as the alias today.Behavior
m.context_type.__name__ContextBaseNullContext✅ContextBase'sNullContext's ✅isinstance(date_ctx, NullContext)m(date_ctx)onNullContextmodelMyNull(NullContext)subclass behaves as plain subclasstype_discriminatorContextBaseNullContextTests
All 841 tests pass on the local suite. No existing tests required modification.
Cache invalidation note
tokenize(NullContext())differs fromtokenize(ContextBase())(the discriminator value changes). Any persisted cache keyed under the old alias semantics will miss once on first re-evaluation. Not a correctness issue, just a one-time invalidation worth a release note.