Skip to content

Commit

Permalink
Fix clearing cached properties when 'auth' or 'filterAuth' are updated
Browse files Browse the repository at this point in the history
This makes sure the init function isn't slower, and checking for changes
is more robust this way. Also support camelCase schema attributes.
Finally, improved the logging message too.
  • Loading branch information
vdboor committed Jul 30, 2024
1 parent 956c7d5 commit 94b4b33
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions src/schematools/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,18 +210,24 @@ def __deepcopy__(self, memo):
def __repr__(self) -> str:
return f"{self.__class__.__name__}({self.data!r})"

def __init__(self, *args, **kwargs):
self.__initialized = False
super().__init__(*args, **kwargs)
self.__initialized = True

def __setitem__(self, key, value):
"""Check for changes to the dictionary data. Note this is also called on __init__()."""
if key == "auth" and key in self.data:
logger.warning("auth field is patched by tests")
if self.__initialized:
logger.info("patching '%s' on %r id %d", key, self, id(self))
property_name = to_snake_case(key) # filterAuth / filter_auth
if (
property_name in self.__dict__
and (prop := getattr(self.__class__, property_name, None)) is not None
and isinstance(prop, cached_property)
):
# Clear the @cached_property cache value
del self.__dict__[property_name]

if (
key in self.__dict__
and (prop := getattr(self.__class__, key, None)) is not None
and isinstance(prop, cached_property)
):
# Clear the @cached_property cache value
del self.__dict__[key]
super().__setitem__(key, value)

if IS_DEBUGGER:
Expand Down

0 comments on commit 94b4b33

Please sign in to comment.