Skip to content

Commit

Permalink
If a decorator is added to a function / method, then we could get err…
Browse files Browse the repository at this point in the history
…ors like:

```
ValueError: fn() requires a code object with 0 free vars, not 1
```

This fixes this problem.

PiperOrigin-RevId: 718943581
  • Loading branch information
The etils Authors committed Jan 26, 2025
1 parent 4d3843d commit 32ed127
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
2 changes: 1 addition & 1 deletion etils/ecolab/inplace_reload.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def _update_function(self, old: types.FunctionType, new: types.FunctionType):
]:
try:
setattr(old, name, getattr(new, name))
except (AttributeError, TypeError):
except (AttributeError, TypeError, ValueError):
pass

def _update_property(self, old: property, new: property):
Expand Down
32 changes: 30 additions & 2 deletions etils/ecolab/inplace_reload_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

"""Tests."""

from __future__ import annotations

import dataclasses
Expand Down Expand Up @@ -227,6 +225,36 @@ def fn(self):
assert a.fn() == 2


def test_reload_decorated_method(reloader: _Reloader): # pylint: disable=redefined-outer-name
old_module = reloader.reimport_module(
'test_module',
"""
class A:
def fn(self, value):
return value
""",
)

a = old_module.A()
assert a.fn('value') == 'value'

reloader.reimport_module(
'test_module',
"""
def decorator(fn):
def wrapper(self, value, *args, **kwargs):
return fn(self, 'decorated-' + value, *args, **kwargs)
return wrapper
class A:
@decorator
def fn(self, value):
return value
""",
)
assert a.fn('value') == 'decorated-value'


def test_reload_inter_module(reloader: _Reloader): # pylint: disable=redefined-outer-name
old_mod_a = reloader.reimport_module(
'test_module_a',
Expand Down

0 comments on commit 32ed127

Please sign in to comment.