Skip to content

Commit

Permalink
fix!: Overriden doctypes must inherit same base class
Browse files Browse the repository at this point in the history
There is almost never a real need to completely override a class. After
this change we'll only allow extending and not overriding completely.
  • Loading branch information
ankush committed Apr 24, 2024
1 parent 07f7287 commit 46b54b7
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions frappe/model/base_document.py
Expand Up @@ -87,16 +87,22 @@ def import_controller(doctype):

module_path = None
class_overrides = frappe.get_hooks("override_doctype_class")

module = load_doctype_module(doctype, module_name)
classname = doctype.replace(" ", "").replace("-", "")
class_ = getattr(module, classname, None)

if class_overrides and class_overrides.get(doctype):
import_path = class_overrides[doctype][-1]
module_path, classname = import_path.rsplit(".", 1)
module = frappe.get_module(module_path)

else:
module = load_doctype_module(doctype, module_name)
classname = doctype.replace(" ", "").replace("-", "")
module_path, custom_classname = import_path.rsplit(".", 1)
custom_module = frappe.get_module(module_path)
custom_class_ = getattr(custom_module, custom_classname, None)
if not issubclass(custom_class_, class_):
raise ImportError(
f"{doctype}: Invalid override, {classname} is not a subclass of {class_.__name__}"
)
class_ = custom_class_

class_ = getattr(module, classname, None)
if class_ is None:
raise ImportError(
doctype
Expand Down

0 comments on commit 46b54b7

Please sign in to comment.