Skip to content

Workaround for pyright bug on ccflow.BaseModel subclasses#222

Merged
timkpaine merged 1 commit into
mainfrom
pit/fix_pyright
Jun 3, 2026
Merged

Workaround for pyright bug on ccflow.BaseModel subclasses#222
timkpaine merged 1 commit into
mainfrom
pit/fix_pyright

Conversation

@ptomecek
Copy link
Copy Markdown
Collaborator

@ptomecek ptomecek commented Jun 3, 2026

A user reported that on v0.8.4, running pyright over

import ccflow
ccflow.FlowOptions()

fails with:

error: Arguments missing for parameters "log_level", "verbose", "validate_result", "volatile", "cacheable", "evaluator" (reportCallIssue)

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:

_BASE_MODEL_METACLASS = ModelMetaclass if _USE_RUNTIME_POLYMORPHIC_SERIALIZATION else _SerializeAsAnyMeta

class BaseModel(PydanticBaseModel, _RegistryMixin, metaclass=_BASE_MODEL_METACLASS):
    ...

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 every ccflow.BaseModel subclass, not just FlowOptions.

Fix: keep the metaclass statically declared as _SerializeAsAnyMeta and move the _USE_RUNTIME_POLYMORPHIC_SERIALIZATION gate one level inward, into the body of _SerializeAsAnyMeta.__new__. On pydantic ≥ 2.13 the metaclass is now a thin pass-through over ModelMetaclass; on older pydantic the annotation rewrite still runs as before.

Verified:

  • python -m pyright repro.py → 0 errors
  • pytest ccflow/tests/test_base_serialize.py ccflow/tests/test_base_cloudpickle.py → all pass

…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>
@ptomecek ptomecek marked this pull request as ready for review June 3, 2026 12:15
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jun 3, 2026

Test Results

  1 files  ±0    1 suites  ±0   2m 31s ⏱️ -3s
846 tests ±0  844 ✅ ±0  2 💤 ±0  0 ❌ ±0 
852 runs  ±0  850 ✅ ±0  2 💤 ±0  0 ❌ ±0 

Results for commit 7e1fad2. ± Comparison against base commit 3370bc1.

♻️ This comment has been updated with latest results.

@codecov
Copy link
Copy Markdown

codecov Bot commented Jun 3, 2026

Codecov Report

❌ Patch coverage is 9.09091% with 10 lines in your changes missing coverage. Please review.
✅ Project coverage is 94.02%. Comparing base (3370bc1) to head (7e1fad2).

Files with missing lines Patch % Lines
ccflow/base.py 9.09% 9 Missing and 1 partial ⚠️
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@timkpaine timkpaine changed the title Fix pyright regression on ccflow.BaseModel subclasses Workaround for pyright bug on ccflow.BaseModel subclasses Jun 3, 2026
@timkpaine timkpaine merged commit c9b3b2b into main Jun 3, 2026
19 of 20 checks passed
@timkpaine timkpaine deleted the pit/fix_pyright branch June 3, 2026 12:46
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