diff --git a/frappe/core/doctype/doctype/doctype.py b/frappe/core/doctype/doctype/doctype.py index b35bdf27da..68061b93e6 100644 --- a/frappe/core/doctype/doctype/doctype.py +++ b/frappe/core/doctype/doctype/doctype.py @@ -701,12 +701,22 @@ class DocType(Document): file_content = code.replace(old, new) # replace str with full str (js controllers) elif fname.endswith(".py"): - file_content = code.replace( - frappe.scrub(old), frappe.scrub(new) - ) # replace str with _ (py imports) - file_content = file_content.replace( - old.replace(" ", ""), new.replace(" ", "") - ) # replace str (py controllers) + new_scrub = frappe.scrub(new) + new_no_space_no_hyphen = new.replace(" ", "").replace("-", "") + new_no_space = new.replace(" ", "") + old_scrub = frappe.scrub(old) + old_no_space_no_hyphen = old.replace(" ", "").replace("-", "") + old_no_space = old.replace(" ", "") + # replace in one go + file_content = re.sub( + rf"{old_scrub}|{old_no_space}|{old_no_space_no_hyphen}", + lambda x: new_scrub + if x.group() == old_scrub + else new_no_space_no_hyphen + if x.group() == old_no_space_no_hyphen + else new_no_space, + code, + ) f.write(file_content)