Skip to content

Pass the return annotation as otypes to numpy.vectorize in the loop strategy - #160

Merged
MImmesberger merged 3 commits into
mainfrom
loop-vectorization-otypes
Nov 16, 2026
Merged

MImmesberger merged 3 commits into
mainfrom
loop-vectorization-otypes

Conversation

@MImmesberger

@MImmesberger MImmesberger commented Oct 11, 2026 •

Copy link
Copy Markdown
Member

Under the "loop" vectorization strategy, numpy.vectorize inferred the output dtype from the first row. A -> float function returning an int in row 0 (e.g. min(0.4 * x, 1908)) produced an int column and truncated later rows (1906.8 → 1906). Surfaced in ttsim-dev/gettsim#1238.

  • Pass the return annotation as otypes to numpy.vectorize.
  • @policy_function with vectorization_strategy "loop" or "vectorize" requires an int, float or bool return annotation; anything else raises PolicyFunctionDefinitionError at decoration. This guarantees a valid otypes.
  • The placeholder policy functions built for DAG plotting and the completeness check are declared vectorization_strategy="not_required"; they are never vectorized.
  • test_opaque_return_demands_opt_out now declares its tuple-returning function not_required, as a scalar one is rejected at decoration.

🤖 Generated with Claude Code

…trategy

Without otypes, numpy.vectorize infers the output dtype from the first
row's return value, so a -> float function returning an int for row 0
truncated every later row to an integer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Oct 11, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.29630% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
tests/tt/test_ttsim_objects.py 85.71% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@hmgaudecker hmgaudecker left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice, thanks a lot! Approved already subject to a few changes.

Verdict

Approve after two small changes: bring the PR description in line with the code, and
decide what should happen for a return annotation numpy cannot read. The fix itself is
right and the test reproduces the bug.

Blocking findings

None that affect gettsim today. The two items below are what I would ask for before
merging.

  1. The PR description describes the first commit, not the merged state.
    The description says the fix "resolves the scalar return annotation via
    resolve_kind_of_column_function" and that "unannotated or unrecognized returns keep
    the previous behaviour". The second commit ("Simplify.", f254432c) replaced that
    with numpy.vectorize(func, otypes=[get_annotations(func)["return"]])
    (src/ttsim/tt/vectorization.py:66). With that line, an unrecognized annotation no
    longer keeps the previous behaviour; it raises (next item). Please update the
    description, since it becomes the squash-commit message.

  2. src/ttsim/tt/vectorization.py:66: anything numpy cannot parse as a dtype now
    raises a bare numpy TypeError at vectorization time.
    Checked with numpy 2.4.2:

    otypes=[...] result
    "float", float float64, [1908., 1906.8]
    "int", int int64
    "bool", bool bool
    "no_annotation_found" (dags' default for a missing annotation) TypeError: data type 'no_annotation_found' not understood
    "FloatColumn", "tuple[float, float]", "float | int", "datetime.date" TypeError: data type '...' not understood
    "str", str silently produces a <U18 string column

    For decorated policy functions this is unreachable in practice: the decorators reject
    a missing return annotation (tests/tt/test_ttsim_objects.py:342), and every
    vectorize-strategy function in gettsim returns float, int or bool (AST count
    over gettsim/src on main: 543 float, 105 bool, 65 int for policy_function;
    no other annotation). But vectorize_function takes any callable, and the error does
    not name the function. Either keep the simplification and convert the failure into a
    TTSIMError that names the function and its annotation, or restrict otypes to the
    three scalar kinds as the first commit did. I have no strong preference; the first is
    less code.

Non-blocking findings and questions

  • A -> int function that returns a float is now truncated silently. Before, the
    column took the dtype of the first row. With otypes=[int], 1906.8 becomes 1906
    with no error. The annotation is the contract, so this is defensible, but it turns a
    visible mismatch into a silent one. Is there a check downstream that would still catch
    a float-valued function annotated -> int?
  • Test coverage is only the float case (tests/tt/test_vectorization.py:936).
    Suggest parametrizing over int and bool, and adding one test that pins the
    behaviour chosen in item 2 for an unreadable annotation.
  • The test function capped_share is module-level between two tests; fine, but a
    one-line docstring on why the cap is an int literal would keep someone from
    "fixing" it to 1908.0 and making the test vacuous.
  • CHANGES.md entry is present and accurate.

`@policy_function` with `vectorization_strategy` "loop" or "vectorize" now
rejects return annotations other than int, float or bool, so the `otypes`
passed to `numpy.vectorize` is always a valid dtype. Plotting placeholders
are declared `not_required`. Test `otypes` for int and bool returns.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@MImmesberger

MImmesberger commented Nov 16, 2026 •

Copy link
Copy Markdown
Member Author

RE the second blocking finding: I think the vectorization step is the wrong place to fix this. I added an additional policy function definition error that enforces the correct type hints. This way the type hints you listed in the table above never reach the vectorization step

@MImmesberger
MImmesberger merged commit 456a0f6 into main Nov 16, 2026
22 of 23 checks passed
@MImmesberger
MImmesberger deleted the loop-vectorization-otypes branch November 16, 2026 17:54
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