Workaround for pyright bug on ccflow.BaseModel subclasses#222
Merged
Conversation
…defaults v0.8.4 introduced a runtime-conditional metaclass on ccflow.BaseModel (`metaclass=_BASE_MODEL_METACLASS`) so that the SerializeAsAny annotation rewriting could be bypassed on pydantic >= 2.13, which now supports runtime polymorphic serialization natively. Pyright cannot statically resolve a metaclass whose value is selected at runtime, and falls back to a treatment that disables pydantic's __init__ synthesis. As a result, every ccflow.BaseModel subclass has its field defaults treated as required, e.g. `ccflow.FlowOptions()` is reported as missing arguments for log_level, verbose, validate_result, volatile, cacheable, and evaluator. Restore the static `metaclass=_SerializeAsAnyMeta` declaration and instead gate the annotation-rewriting body of `_SerializeAsAnyMeta.__new__` on `_USE_RUNTIME_POLYMORPHIC_SERIALIZATION`. Runtime behavior is preserved on both pydantic < 2.13 and pydantic >= 2.13, while pyright once again recognizes the metaclass as a pydantic ModelMetaclass and synthesizes __init__ with field defaults. Signed-off-by: Pascal Tomecek <pascal.tomecek@cubistsystematic.com>
Contributor
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #222 +/- ##
=======================================
Coverage 94.02% 94.02%
=======================================
Files 144 144
Lines 11726 11726
Branches 648 649 +1
=======================================
Hits 11025 11025
+ Misses 571 570 -1
- Partials 130 131 +1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
timkpaine
approved these changes
Jun 3, 2026
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.
A user reported that on v0.8.4, running pyright over
fails with:
even though all those fields have defaults. v0.8.3 was clean.
The cause is the conditional metaclass introduced in #215 for pydantic 2.13 compat:
Pyright can't resolve a metaclass picked at runtime, so it stops synthesizing the pydantic
__init__and every field with a default ends up looking required. This affects everyccflow.BaseModelsubclass, not justFlowOptions.Fix: keep the metaclass statically declared as
_SerializeAsAnyMetaand move the_USE_RUNTIME_POLYMORPHIC_SERIALIZATIONgate one level inward, into the body of_SerializeAsAnyMeta.__new__. On pydantic ≥ 2.13 the metaclass is now a thin pass-through overModelMetaclass; on older pydantic the annotation rewrite still runs as before.Verified:
python -m pyright repro.py→ 0 errorspytest ccflow/tests/test_base_serialize.py ccflow/tests/test_base_cloudpickle.py→ all pass