Skip to content

fix(decorators): avoid mutating original function metadata on functools.wraps - #729

Open
Aryan9891 wants to merge 1 commit into
google:masterfrom
Aryan9891:fix/copy-metadata-on-parse-fns-728
Open

Aryan9891 wants to merge 1 commit into
google:masterfrom
Aryan9891:fix/copy-metadata-on-parse-fns-728

Conversation

@Aryan9891

Copy link
Copy Markdown

Problem

As reported in #728, decorating a wrapper created with \ unctools.wraps\ using \SetParseFn\ or \SetParseFns\ unexpectedly mutates how the original wrapped function parses its arguments. Because \ unctools.wraps\ copies the function's attribute dictionary, the underlying Fire metadata dictionary is shared, and updating it in place affects both wrapper and wrapped functions.

Solution

  • In _SetMetadata, create a shallow copy of the metadata dictionary before assigning the new attribute, ensuring the target function receives its own distinct metadata map.
  • In \SetParseFn\ and \SetParseFns, create a fresh \parse_fns\ mapping with a copied
    amed\ dictionary so updates to named parsers do not mutate shared dictionaries in place.
  • Add regression unit tests in \ ire/decorators_test.py\ verifying that wrapping a function decorated with \SetParseFns\ / \SetParseFn\ preserves independent parser behavior on both the original and wrapped functions.

Fixes #728.

…ls.wraps

When decorators like SetParseFn and SetParseFns are applied to functions
wrapped with functools.wraps, copying existing parse_fns and metadata
dictionaries prevents mutating the underlying function's metadata in place.

Fixes google#728.

Signed-off-by: Aryan Sachin <aryansachinixd@gmail.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant