Parser decorators on functools.wraps wrappers mutate the original function #728
Description
Decorating a functools.wraps wrapper with SetParseFns also changes how the original function parses arguments. wraps copies the wrapped function's attribute dictionary, so the nested Fire metadata dictionaries are shared. SetParseFns updates those dictionaries in place.
Reproduced using complete current master 716bbc23d7eca949fdb682172283c8d18f742cb6 on Linux/Python 3.14:
import functools
import fire
from fire import decorators
@decorators.SetParseFns(value=int)
def original(value):
return type(value).__name__
@decorators.SetParseFns(value=str)
@functools.wraps(original)
def wrapped(value):
return original(value)
print(fire.Fire(original, command=['01'])) # 'str', expected 'int'
print(fire.Fire(wrapped, command=['01'])) # 'str', as configuredThe wrapper's parser should not silently replace the original function's parser. This is observable through fire.Fire, without mocking argument parsing or copying implementation fragments.
I intend to investigate a focused copy-on-write fix for the metadata owned by the parser decorators, with regression tests covering named, positional and default parsers. This does not propose changing Fire's wrapper-signature handling. I searched open and closed issues/PRs for metadata, wrappers, decorator copying and SetParseFns and found no exact covering change. No repair PR is open yet; full-suite qualification is still pending.
Activity