chore(DX): add type hints to Document, BaseDocument and get_doc (#21060)

* chore: add type hints to Document, BaseDocument and get_doc

* refactor: better type hints

get_doc has multiple ways to use it, added all known ways

---------

Co-authored-by: Raphael Krupinski <10319569-mattesilver@users.noreply.gitlab.com>
Co-authored-by: Ankush Menat <ankush@frappe.io>
This commit is contained in:
Raphael Krupinski 2023-05-29 07:31:51 +02:00 committed by GitHub
parent 38c1207abe
commit b66d8e8a40
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -17,7 +17,7 @@ import json
import os
import re
import warnings
from typing import TYPE_CHECKING, Any, Callable, Literal, Optional, overload
from typing import TYPE_CHECKING, Any, Callable, Literal, Optional, TypeAlias, overload
import click
from werkzeug.local import Local, release_local
@ -1142,7 +1142,42 @@ def get_cached_value(
return values
def get_doc(*args, **kwargs) -> "Document":
_SingleDocument: TypeAlias = "Document"
_NewDocument: TypeAlias = "Document"
@overload
def get_doc(document: "Document", /) -> "Document":
pass
@overload
def get_doc(doctype: str, /) -> _SingleDocument:
"""Retrieve Single DocType from DB, doctype must be positional argument."""
pass
@overload
def get_doc(doctype: str, name: str, /, for_update: bool | None = None) -> "Document":
"""Retrieve DocType from DB, doctype and name must be positional argument."""
pass
@overload
def get_doc(**kwargs: dict) -> "_NewDocument":
"""Initialize document from kwargs.
Not recommended. Use `frappe.new_doc` instead."""
pass
@overload
def get_doc(documentdict: dict) -> "_NewDocument":
"""Create document from dict.
Not recommended. Use `frappe.new_doc` instead."""
pass
def get_doc(*args, **kwargs):
"""Return a `frappe.model.document.Document` object of the given type and name.
:param arg1: DocType name as string **or** document JSON.