chore: share constants between backend and frontend via JSON - #935
chore: share constants between backend and frontend via JSON#935Bhavd33p wants to merge 2 commits into
Conversation
kale/rpc/errors.py's Code enum and labextension's RPC_CALL_STATUS enum independently hardcoded the same seven name-to-int mappings, with nothing to catch them drifting apart if one side changed without the other. Move the mapping into kale/rpc/error_codes.json and have both sides read their values from it: the backend at import time, the frontend via a relative import resolved at build time (webpack/tsc bundle the JSON's contents into the compiled output, so there's no runtime dependency on the Python package's file layout). Add a test asserting Code's values match the JSON, to catch anyone reverting to hardcoded values on the backend side. Signed-off-by: [Bhavdeep Singh] <bhavdeep3singh@gmail.com>
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
|
@Bhavd33p thanks for working on this, one thing that I want to ask, is that we do this fix in a proper way adding 'human' messages to folks understand in the UI whats is going on. (instead of machine errors) |
Extend the shared JSON from RPC error codes to every constant that was defined once in Python and once in TypeScript: the reserved cell names, the cell tag prefixes, the step name pattern, the cache/report tag values, the notebook metadata key and the default base image. Rename the file to kale/shared_constants.json accordingly, read it on the backend through kale/shared_constants.py and on the frontend through labextension/src/lib/sharedConstants.ts. The notebook processor now assembles its tag patterns from the shared prefixes instead of spelling them out; test_shared_constants.py pins the tags that must keep being accepted and rejected. The shared file also carries a plain-language explanation per error code, and the RPC dialog leads with it instead of with a numeric code, so users read what went wrong. The diagnostics stay, below the explanation. Signed-off-by: Bhavdeep Singh <bhavdeep3singh@gmail.com>
|
Read through the backend side of this. The JSON-as-single-source approach looks right to me, and the test asserting 1. The new comment on // `annotation:` and `label:` are deliberately absent - the backend only accepts
// those in the notebook-level `steps_defaults`, never on a cell.
That matters because
Both predate this PR, so not something you broke. But since this is the change that centralises the list, is it worth adding them to 2. The description has drifted from the diff. It describes adding 3. Coordination question, and I should declare an interest here — I'm the author of #909, which moves the They don't seem to be in tension conceptually — yours is "values the frontend must agree on," mine is "values |
Summary
kale/rpc/errors.py'sCodeenum andlabextension/src/lib/RPCUtils.tsx'sRPC_CALL_STATUSenum independently hardcoded the same seven name→int error-code mappings. Nothing would catch it if one side changed a value (or added/removed a code) without updating the other — exactly the kind of drift risk described in the issue.kale/rpc/error_codes.jsonas the single source of truth for these values.Code's member values are now read from the JSON at import time (kept as an explicitenum.Enumclass body, rather than building the enum dynamically from the dict, soCode.SERVICE_UNAVAILABLEetc. stay fully statically type-checkable).RPC_CALL_STATUS's member values are now read from the same JSON via a relative import (../../../kale/rpc/error_codes.json). This only needs the file to exist in the repo at build time — webpack/tsc bake the JSON's contents into the compiled output, so there's no runtime dependency between the built labextension and the Python package's file layout (verified:tsc --noEmit,tscwith emit, and the fulljlpm buildwebpack bundle all resolve it cleanly, and the compiled bundle contains the baked-in values).Code's values match the JSON, so a future hardcoded edit that skips the JSON gets caught.I picked this as the first (smallest, self-contained) case to tackle out of several duplicated-constant instances I found while looking into this — happy to follow up with others (e.g. the cell-tag vocabulary in
kale/processors/nbprocessor.pyvslabextension/src/widgets/cell-metadata/constants.ts) in separate PRs, since that one overlaps with the in-flight #909 (nbprocessor constants extraction) and deserves its own focused pass.Fixes #925
Test plan
pytest kale/tests/unit_tests— 269 passed (268 + new test).ruff check kale/ruff format --check kalepass.tsc --noEmit,eslint:check,prettier:checkpass.jestpasses.jlpm build(webpack) succeeds; confirmed the compiled bundle contains the JSON's values (grep SERVICE_UNAVAILABLEon the builtlib_index_js.*.js).