From edaa5421c5f6576f13a428101c4bd5d7ebf1bb21 Mon Sep 17 00:00:00 2001 From: Diederik van der Boor Date: Tue, 30 Jul 2024 13:27:26 +0200 Subject: [PATCH] Fix clearing cached properties when 'auth' or 'filterAuth' are updated 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. --- src/schematools/types.py | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/schematools/types.py b/src/schematools/types.py index cdf9f9b6..67928d94 100644 --- a/src/schematools/types.py +++ b/src/schematools/types.py @@ -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: