Repository navigation
Python: fix kernel_function decorator mutating shared Annotated metadata dicts - #14442
Manohar Paturi (ManoharPaturi) wants to merge 1 commit into
Conversation
…ata dicts
_parse_parameter() called meta.pop("description") on dict metadata used
in typing.Annotated, mutating the user-provided dict. When the same
metadata dict is reused across multiple kernel functions (shared
parameter docs), every function after the first silently lost its
parameter description, and the dict itself was permanently modified.
Use meta.get() instead of meta.pop() and filter the description key out
when applying the remaining metadata keys, so semantics are unchanged
but the caller's dict is left untouched.
Eduard van Valkenburg (eavanvalkenburg)
left a comment
There was a problem hiding this comment.
The original mutation issue is fixed on main. The inline comment identifies an additional behavior change that is not described in this PR. Its old label/review/gate runs also need fresh validation if this is retained as a separate fix.
| if "description" not in ret and (description := meta.get("description", None)): | ||
| ret["description"] = description | ||
| ret.update(meta) | ||
| ret.update({key: value for key, value in meta.items() if key != "description"}) |
There was a problem hiding this comment.
The mutation bug is already fixed on main by #14496 using a local dict copy. This version also changes description precedence: Annotated[int, "earlier string", {"description": "later dictionary"}] currently produces "later dictionary", but this filtering produces "earlier string". I reproduced both results. Is that additional behavior change intentional? Please clarify the remaining bug and cover the precedence rule before rebasing; otherwise the original shared-metadata fix is already delivered.
Fixes #14441.
_parse_parameter()now reads the description withget()and copies only known keys into the parameter metadata instead ofpop()-ing from the Annotated dict, so decoration never mutates caller-owned dicts and shared metadata keeps working across functions.2 regression tests (shared-dict reuse keeps descriptions; source dict untouched) fail on main and pass here;
tests/unit/functionsgreen.