Repository navigation
Pass the return annotation as otypes to numpy.vectorize in the loop strategy - #160
Conversation
…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 Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
hmgaudecker
left a comment
There was a problem hiding this comment.
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.
-
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
withnumpy.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. -
src/ttsim/tt/vectorization.py:66: anything numpy cannot parse as a dtype now
raises a bare numpyTypeErrorat vectorization time. Checked with numpy 2.4.2:otypes=[...]result "float",floatfloat64, [1908., 1906.8]"int",intint64 "bool",boolbool "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",strsilently produces a <U18string columnFor 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 returnsfloat,intorbool(AST count
overgettsim/srcon main: 543float, 105bool, 65intforpolicy_function;
no other annotation). Butvectorize_functiontakes any callable, and the error does
not name the function. Either keep the simplification and convert the failure into a
TTSIMErrorthat names the function and its annotation, or restrictotypesto 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
-> intfunction that returns a float is now truncated silently. Before, the
column took the dtype of the first row. Withotypes=[int],1906.8becomes1906
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
floatcase (tests/tt/test_vectorization.py:936).
Suggest parametrizing overintandbool, and adding one test that pins the
behaviour chosen in item 2 for an unreadable annotation. - The test function
capped_shareis module-level between two tests; fine, but a
one-line docstring on why the cap is anintliteral would keep someone from
"fixing" it to1908.0and making the test vacuous. CHANGES.mdentry 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>
|
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 |
Under the
"loop"vectorization strategy,numpy.vectorizeinferred the output dtype from the first row. A-> floatfunction returning anintin 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.otypestonumpy.vectorize.@policy_functionwithvectorization_strategy"loop"or"vectorize"requires anint,floatorboolreturn annotation; anything else raisesPolicyFunctionDefinitionErrorat decoration. This guarantees a validotypes.vectorization_strategy="not_required"; they are never vectorized.test_opaque_return_demands_opt_outnow declares its tuple-returning functionnot_required, as a scalar one is rejected at decoration.🤖 Generated with Claude Code