From 2c92043998019ac2b1c5dfa9d5548e822bf9efc5 Mon Sep 17 00:00:00 2001 From: Corentin Flr <10946971+cogk@users.noreply.github.com> Date: Mon, 31 Jul 2023 18:10:53 +0200 Subject: [PATCH] fix(doctype): Allow cached_property decorator in controllers (#21881) --- frappe/core/doctype/doctype/doctype.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/frappe/core/doctype/doctype/doctype.py b/frappe/core/doctype/doctype/doctype.py index 65f0b826b95..f7e6f285278 100644 --- a/frappe/core/doctype/doctype/doctype.py +++ b/frappe/core/doctype/doctype/doctype.py @@ -240,9 +240,7 @@ def validate_field_name_conflicts(self): controller = Document available_objects = {x for x in dir(controller) if isinstance(x, str)} - property_set = { - x for x in available_objects if isinstance(getattr(controller, x, None), property) - } + property_set = {x for x in available_objects if is_a_property(getattr(controller, x, None))} method_set = { x for x in available_objects if x not in property_set and callable(getattr(controller, x, None)) } @@ -1795,13 +1793,18 @@ def make_module_and_roles(doc, perm_fieldname="permissions"): raise +def is_a_property(x) -> bool: + """Get properties (@property, @cached_property) in a controller class""" + from functools import cached_property + + return isinstance(x, (property, cached_property)) + + def check_fieldname_conflicts(docfield): """Checks if fieldname conflicts with methods or properties""" doc = frappe.get_doc({"doctype": docfield.dt}) available_objects = [x for x in dir(doc) if isinstance(x, str)] - property_list = [ - x for x in available_objects if isinstance(getattr(type(doc), x, None), property) - ] + property_list = [x for x in available_objects if is_a_property(getattr(type(doc), x, None))] method_list = [ x for x in available_objects if x not in property_list and callable(getattr(doc, x)) ]