Skip to content

Commit

Permalink
fix: Be able to clear the process_cache manually in Python 3.11
Browse files Browse the repository at this point in the history
Given code like the following

```
class Foo:
    @process_cached
    def bar(self):
        pass
```

In Python 3.8 referencing `bar` would not call its `__get__` method.

```
x = Foo().bar
```

However in Python 3.11, making the same call would call the `__get__`
method, permanently replacing the underlying `process_cached` object
with the partial function that references it.

This meant that code to clear the cache would work in Python 3.8 but
would break in 3.11

```
Foo().bar.cache.clear()  # Works in 3.8 but not in 3.11
```

In 3.11 this results in the following error:
```
E       AttributeError: 'functools.partial' object has no attribute 'cache'
```

To make this compatible in both version, we just add the cache as an
accessible attribute on the partial we generate for our wrapped
function.
  • Loading branch information
feanil committed Apr 3, 2024
1 parent df64688 commit f433751
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion openedx/core/lib/cache_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,10 @@ def __get__(self, obj, objtype):
"""
Support instance methods.
"""
return functools.partial(self.__call__, obj)
partial = functools.partial(self.__call__, obj)
# Make the cache accessible on the wrapped object so it can be cleared if needed.
partial.cache = self.cache
return partial


class CacheInvalidationManager:
Expand Down

0 comments on commit f433751

Please sign in to comment.