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 29, 2024
1 parent 7308754 commit a8ce3c3
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions frappe/model/base_document.py
Expand Up @@ -87,16 +87,24 @@ 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_):
original_class_path = frappe.bold(f"{class_.__module__}.{class_.__name__}")
frappe.throw(
f"{doctype}: {frappe.bold(import_path)} must be a subclass of {original_class_path}",
title=_("Invalid Override"),
)
class_ = custom_class_

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

0 comments on commit a8ce3c3

Please sign in to comment.