The functools documentation states the following when working with lru_cache-decorated methods:
The above example assumes that the [attribute] never changes. If the relevant instance attributes are mutable, the cached_property approach can’t be made to work because it cannot detect changes to the attributes.
To make the lru_cache approach work when the [attribute] is mutable, the class needs to define the __eq__() and __hash__() methods so that the cache can detect relevant attribute updates: [...]
I therefore recommend to implement the _S3ConfigurationMap.__hash__() and _S3ConfigurationMap.__eq__() magic methods, while considering the instance attributes attributes, resources and general_options.
Example
I have an active AWS session with a temporary token. The following snippet works:
After some time, the old token expires, so a new one is acquired and registered within
s3path
.However, the previous command now fails:
After closer inspection I found out that the resource accessed by
S3Path
objects is the same before and after refreshing the session:I believe this is related to the usage of the
lru_cache
decorator forget_configuration()
inside the_S3ConfigurationMap
class.Workaround
At present I can work around this issue by manually invalidating the cache after calling
register_configuration_parameter()
:Suggested fix
The
functools
documentation states the following when working withlru_cache
-decorated methods:I therefore recommend to implement the
_S3ConfigurationMap.__hash__()
and_S3ConfigurationMap.__eq__()
magic methods, while considering the instance attributesattributes
,resources
andgeneral_options
.If desired, I can submit a pull request as well.