chore(backend): move nbprocessor constants into a separate module - #909
chore(backend): move nbprocessor constants into a separate module#909harshhh817 wants to merge 2 commits into
Conversation
|
[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 |
5a788b4 to
50a7658
Compare
ada333
left a comment
There was a problem hiding this comment.
hey @harshhh817 looking mostly good, I have few comments
|
|
||
|
|
||
| # Separator between a tag's parts, e.g. `limit:nvidia.com/gpu:2`. | ||
| TAG_SEPARATOR = ":" |
| if detected and ( | ||
| ("tags" not in c.metadata or len(c.metadata["tags"]) == 0) | ||
| or all(not any(re.match(tag, t) for t in c.metadata["tags"]) for tag in language) | ||
| ("tags" not in c.metadata or len(c.metadata[CELL_METADATA_TAGS]) == 0) |
| # -------------------------------------------------------------------------- | ||
|
|
||
| FNS_FREE_VARIABLES = "fns_free_variables" | ||
| PARAMETERS = "parameters" |
|
Thanks @ada333 — all three addressed in ddb8fdb.
Suite still passes (268) and ruff is clean. |
Per ada333's review: - Derive SECRET_TAG in nbprocessor.py from the same validator regexes applied to StepConfig.secrets (K8sSecretNameValidator/K8sSecretKeyValidator/ EnvVarNameValidator), instead of duplicating the patterns inline. Also extract the 'secret_name'/'secret_key' dict keys into shared constants in validators.py, reused by both call sites in nbprocessor.py. - SecretsSection.tsx: a row is now only written to the notebook's tags once every field (secret name, secret key, env var name) is present and matches its expected format, instead of just checking envName. The same check now also disables "Add Secret" until the current row is complete, so users can't pile up empty or malformed rows. Per harshhh817's review: - K8sSecretNameValidator now validates Secret names as DNS subdomains (dot-separated labels), matching real Kubernetes Secret name rules, instead of inheriting K8sNameValidator's stricter single-label rule. Deriving SECRET_TAG from the validator means the tag regex picks up this fix for free. - Added a template comment noting that secret_name/secret_key/env_name land inside string literals in generated code, so the validators are what keep quote characters out of them. The pipeline-level steps_defaults path bypassing field validators (harshhh817's point 2) and the kale/processors/constants.py move in kubeflow#909 (heads-up, not a review point) are pre-existing/out-of-scope for this PR. Signed-off-by: Pedro Sbaraini Cordeiro <pedro.sbarainicordeiro@gmail.com>
|
@ada333 gentle nudge on this one whenever you have a moment — the three points from your review are addressed in ddb8fdb (the missed No rush — mainly flagging it because the follow-up extractions from #879 build on this one, so it's the piece gating the rest of that series. |
nbprocessor.py mixed module-level constants with a large number of
inline magic strings ("enable_caching", "generate_html_report",
"step_names", "tags", ...). A wrong dict key reads as "not set" and a
wrong getattr name returns None, so a typo fails silently.
Move the existing constants and the repeated string literals into
kale/processors/constants.py, grouped by what each string is: notebook
JSON keys, the cell tag language, the parsed-tag dictionary keys shared
between parse_cell_metadata and parse_notebook, dynamically accessed
Step attributes, and templates/type maps.
Pure move: every constant keeps its value. Part of kubeflow#879.
Signed-off-by: Harsh Gupta <harshgupta93198@gmail.com>
…e tags constant - use CELL_METADATA_TAGS for the remaining 'tags' literal - apply TAG_SEPARATOR at the four tag split/join sites - apply PARAMETERS at its getattr/hasattr sites Signed-off-by: Harsh Gupta <harshgupta93198@gmail.com>
ddb8fdb to
dec563b
Compare
|
Rebased onto current main to clear the conflict with #867 (multi-notebook composition). Verification: all 25 constants compare byte-identical to upstream/main (checked programmatically, including the Two judgement calls I'd rather flag than assume:
|
What this does
Moves the constants out of
nbprocessor.pyinto a newkale/processors/constants.py, and replaces the inline magic strings with those constants.Part of #879 — this is the "start small" step @ada333 asked for.
Why
nbprocessor.pyhad module-level constants at the top and a lot of repeated string literals inline:"step_names"(10x),"tags"(11x),"limits"(5x),"prev_steps"(4x),"labels"(4x),"enable_caching"(3x),"base_image"(3x),"fns_free_variables"(3x),"code"(3x), and others. A typo in any of them fails silently — a wrong dict key reads as "not set", and a wronggetattrname returnsNone— rather than raising.How it's grouped
Rather than one flat list, the new module groups by what each string actually is, since they aren't interchangeable:
.ipynbstructure (KALE_NB_METADATA_KEY,CELL_METADATA_TAGS,CELL_TYPE_CODE).STEP_NAMES,PREV_STEPS,LIMITS,LABELS,ANNOTATIONS,BASE_IMAGE,ENABLE_CACHING. These are the contract betweenparse_cell_metadata(which produces the dict) andparse_notebook(which consumes it), so both sides now reference the same names.Stepattributes —FNS_FREE_VARIABLES,PARAMETERS, looked up viagetattr/hasattr.METRICS_TEMPLATE,KFP_ARTIFACT_TYPE_MAP, plusDEFAULT_VARIABLE_TYPE/DEFAULT_ARTIFACT_TYPEfor the"str"/"Artifact"fallbacks.Testing
pytest kale/tests/unit_tests→ 244 passedruff check kaleandruff format --check kale→ cleanmainagainst the new module programmatically — all 21 are identical in value. No behaviour change.Two judgment calls, happy to change either
_TAGS_LANGUAGE→TAGS_LANGUAGE(and_STEPS_DEFAULTS_LANGUAGE→STEPS_DEFAULTS_LANGUAGE). The leading underscore marked them module-private; now that they're imported across modules that's no longer accurate. Say the word if you'd rather keep the underscore..format()calls became f-strings. Not gratuitous: with the quoted dict keys replaced by constants, ruff'sUP032became applicable andruff checkfails without it. They're the only non-mechanical lines in the diff.Follow-ups from #879 (extracting the tag parsing, dependency analysis and metrics into their own modules) are deliberately left out of this PR.