Skip to content

[bug] Make CallableModel validators cheap when rejecting a value - #294

Merged
ptomecek merged 1 commit into
mainfrom
feature/cheap-callable-reject
Nov 11, 2026
Merged

ptomecek merged 1 commit into
mainfrom
feature/cheap-callable-reject

Conversation

@ptomecek

@ptomecek ptomecek commented Nov 10, 2026 •

Copy link
Copy Markdown
Collaborator

Description

_validate_callable_model_generic_type is a wrap validator, so pydantic runs it on every rejected branch of a union like CallableModelGenericType[Ctx, R] | R. When a plain R is passed, the old rejection message formatted the whole value (f"{m} ...") just to build an error that pydantic throws away before trying the next branch. For values that are expensive to repr this dominated construction — ~36 ms of a ~37 ms build in one case.

The message now carries no value, and I applied the same fix to the other coercion validators that formatted the whole value in their reject path.

Performance-sensitive locations (formatted the whole input value on a rejected branch):

  • callable.py — _validate_callable_model_generic_type
  • enums.py — Enum.validate
  • exttypes/arrow.py — ArrowSchema._validate, PyArrowDatatype._validate
  • exttypes/exprtk.py — ExprTkExpression._validate
  • exttypes/polars.py — _PolarsExprPydanticAnnotation._validate
  • exttypes/pydantic_numpy/ndarray.py — NDArray._validate
  • exttypes/pyobjectpath.py — PyObjectPath._validate

pydantic already appends input_value=/input_type= to the error (rendered lazily, so the discarded branch pays nothing), so the messages also stop restating the type. That drops a redundant got {type(...)} from a few validators that weren't a perf problem but duplicated pydantic: exttypes/narwhals.py, exttypes/arrow.py (Table), and publishers/file.py.

The failing value isn't lost: on a genuine failure pydantic's input_value=/input_type= still show it.

Measurement

Union field given an R whose repr renders a 3,000×72 frame: ~13 ms → ~3 µs per construction.

Type of Change

  • Bug fix
  • New feature
  • Documentation update
  • Refactor / code cleanup
  • CI / build configuration
  • Other (describe below)

Checklist

  • Linting passes (make lint)
  • Tests pass (make test)
  • New tests added for new functionality
  • Documentation updated (if applicable)
  • Changelog / version bump (if applicable)

@codecov

codecov Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.85714% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.89%. Comparing base (ad028bb) to head (f325e77).

Files with missing lines Patch % Lines
ccflow/exttypes/arrow.py 66.66% 1 Missing ⚠️
ccflow/exttypes/narwhals.py 75.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #294      +/-   ##
==========================================
+ Coverage   93.88%   93.89%   +0.01%     
==========================================
  Files         203      203              
  Lines       22706    22719      +13     
  Branches     1491     1491              
==========================================
+ Hits        21318    21333      +15     
+ Misses       1100     1099       -1     
+ Partials      288      287       -1     

☔ View full report in Codecov by Harness.
📢 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.

@github-actions

github-actions Bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Test Results

    1 files  ±0      1 suites  ±0   2m 12s ⏱️ +19s
1 582 tests +1  1 577 ✅ +1  5 💤 ±0  0 ❌ ±0 
1 659 runs  +1  1 654 ✅ +1  5 💤 ±0  0 ❌ ±0 

Results for commit f325e77. ± Comparison against base commit ad028bb.

♻️ This comment has been updated with latest results.

@ptomecek
ptomecek force-pushed the feature/cheap-callable-reject branch 2 times, most recently from c2756d2 to 5b2cd62 Compare November 11, 2026 00:22
@ptomecek ptomecek changed the title Make CallableModel validators cheap when rejecting a value [bug] Make CallableModel validators cheap when rejecting a value Nov 11, 2026
@ptomecek
ptomecek force-pushed the feature/cheap-callable-reject branch 5 times, most recently from 5a8193e to 1a1581c Compare November 11, 2026 01:02
CallableModelGeneric._validate_callable_model_generic_type is a wrap
validator, so pydantic runs it on every rejected branch of a union like
`CallableModelGenericType[Ctx, R] | R`. The old rejection message
formatted the whole input value (`f"{m} ..."`) just to build an error
that pydantic throws away before trying the next branch. For values
expensive to repr this dominated construction -- ~36 ms of a ~37 ms
build in one case.

Drop the value from the message, and do the same for the other coercion
validators that formatted the whole value in their reject path (enums,
arrow, exprtk, polars, numpy ndarray, pyobjectpath). pydantic already
appends input_value=/input_type= to the error (lazily, so the discarded
branch pays nothing), so the messages also stop restating the type --
including the redundant `got {type(...)}` in the narwhals, arrow-table,
and file-publisher validators. Exception type stays ValueError so union
fallback keeps working.

Signed-off-by: Pascal Tomecek <40371786+ptomecek@users.noreply.github.com>
@ptomecek
ptomecek force-pushed the feature/cheap-callable-reject branch from 1a1581c to f325e77 Compare November 11, 2026 14:23
@ptomecek
ptomecek merged commit 05ce245 into main Nov 11, 2026
19 of 20 checks passed
@ptomecek
ptomecek deleted the feature/cheap-callable-reject branch November 11, 2026 15:32
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