Skip to content

Commit

Permalink
chore(docref): fix circular imports (frappe#28282)
Browse files Browse the repository at this point in the history
  • Loading branch information
blaggacao authored Oct 24, 2024
1 parent 268c33d commit d5fd8d7
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 27 deletions.
2 changes: 1 addition & 1 deletion frappe/database/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
from functools import cached_property, wraps

import frappe
from frappe.model.document import DocRef
from frappe.query_builder.builder import MariaDB, Postgres
from frappe.query_builder.functions import Function
from frappe.types import DocRef

Query = str | MariaDB | Postgres
QueryValues = tuple | list | dict | None
Expand Down
2 changes: 0 additions & 2 deletions frappe/model/base_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,8 +368,6 @@ def _get_table_fields(self):
def get_valid_dict(
self, sanitize=True, convert_dates_to_str=False, ignore_nulls=False, ignore_virtual=False
) -> _dict:
from frappe.model.document import DocRef

d = _dict()
field_values = self.__dict__

Expand Down
23 changes: 1 addition & 22 deletions frappe/model/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from frappe.model.naming import set_new_name, validate_name
from frappe.model.utils import is_virtual_doctype, simple_singledispatch
from frappe.model.workflow import set_workflow_state_on_action, validate_workflow
from frappe.types import DF
from frappe.types import DF, DocRef
from frappe.utils import compare, cstr, date_diff, file_lock, flt, now
from frappe.utils.data import get_absolute_url, get_datetime, get_timedelta, getdate
from frappe.utils.global_search import update_global_search
Expand All @@ -38,27 +38,6 @@
DOCUMENT_LOCK_SOFT_EXPIRY = 60 * 60 # Let users force-unlock after 60 minutes


class DocRef:
"""A lightweight reference to a document, containing just the doctype and name."""

def __init__(self, doctype: str, name: str):
self.doctype = doctype
self.name = name

def __value__(self):
# Used when requiring its value representation for db interactions, serializations, etc
return self.name

def __hash__(self):
return hash(self.doctype + self.name or "")

def __str__(self):
return f"{self.doctype} ({self.name or 'n/a'})"

def __repr__(self):
return f"<{self.__class__.__name__}: doctype={self.doctype} name={self.name or 'n/a'}>"


@simple_singledispatch
def get_doc(*args, **kwargs) -> "Document":
"""Return a `frappe.model.Document` object.
Expand Down
3 changes: 2 additions & 1 deletion frappe/model/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@
TABLE_DOCTYPES_FOR_DOCTYPE,
BaseDocument,
)
from frappe.model.document import DocRef, Document
from frappe.model.document import Document
from frappe.model.workflow import get_workflow_name
from frappe.modules import load_doctype_module
from frappe.types import DocRef
from frappe.utils import cast, cint, cstr

DEFAULT_FIELD_LABELS = {
Expand Down
3 changes: 2 additions & 1 deletion frappe/tests/test_doc_ref.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import frappe
from frappe.model.document import DocRef, Document, get_doc
from frappe.model.document import Document, get_doc
from frappe.tests import IntegrationTestCase
from frappe.types import DocRef

EXTRA_TEST_RECORD_DEPENDENCIES = ["User"]

Expand Down
2 changes: 2 additions & 0 deletions frappe/types/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .docref import DocRef
from .frappedict import _dict
25 changes: 25 additions & 0 deletions frappe/types/docref.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from typing_extensions import override


class DocRef:
"""A lightweight reference to a document, containing just the doctype and name."""

def __init__(self, doctype: str, name: str):
self.doctype = doctype
self.name = name

def __value__(self) -> str:
# Used when requiring its value representation for db interactions, serializations, etc
return self.name

@override
def __hash__(self) -> int:
return hash(self.doctype + self.name or "")

@override
def __str__(self) -> str:
return f"{self.doctype} ({self.name or 'n/a'})"

@override
def __repr__(self) -> str:
return f"<{self.__class__.__name__}: doctype={self.doctype} name={self.name or 'n/a'}>"

0 comments on commit d5fd8d7

Please sign in to comment.