Merge .pyi type stubs inline (#563)
* Merged types into .py code. Signed-off-by: dblock <[email protected]> * Fix: nox -rs generate. Signed-off-by: dblock <[email protected]> * Updated CHANGELOG. Signed-off-by: dblock <[email protected]> * Use lowest common python version for lint. Signed-off-by: dblock <[email protected]> * Fix: don't typeshed. Signed-off-by: dblock <[email protected]> * Removed unneeded comment. Signed-off-by: dblock <[email protected]> * Simplify OPENSEARCH_URL. Signed-off-by: dblock <[email protected]> * Fix: positional ignore_status used as chunk_size. Signed-off-by: dblock <[email protected]> * Fix: parse version string. Signed-off-by: dblock <[email protected]> * Remove future annotations for Python 3.6. Signed-off-by: dblock <[email protected]> * Fix: types in documentation. Signed-off-by: dblock <[email protected]> * Improve CHANGELOG text. Signed-off-by: dblock <[email protected]> * Re-added missing separator. Signed-off-by: dblock <[email protected]> * Remove duplicate licenses. Signed-off-by: dblock <[email protected]> * Get rid of Optional[Any]. Signed-off-by: dblock <[email protected]> * Fix docs with AsyncOpenSearch. Signed-off-by: dblock <[email protected]> * Fix: undo comment. Signed-off-by: dblock <[email protected]> --------- Signed-off-by: dblock <[email protected]>
This commit is contained in:
+19
-42
@@ -78,7 +78,7 @@ jinja_env = Environment(
|
||||
)
|
||||
|
||||
|
||||
def blacken(filename):
|
||||
def blacken(filename) -> None:
|
||||
runner = CliRunner()
|
||||
result = runner.invoke(black.main, [str(filename)])
|
||||
assert result.exit_code == 0, result.output
|
||||
@@ -90,29 +90,20 @@ def is_valid_url(url):
|
||||
|
||||
|
||||
class Module:
|
||||
def __init__(self, namespace, is_pyi=False):
|
||||
def __init__(self, namespace) -> None:
|
||||
self.namespace = namespace
|
||||
self.is_pyi = is_pyi
|
||||
self._apis = []
|
||||
self.parse_orig()
|
||||
|
||||
if not is_pyi:
|
||||
self.pyi = Module(namespace, is_pyi=True)
|
||||
self.pyi.orders = self.orders[:]
|
||||
|
||||
def add(self, api):
|
||||
def add(self, api) -> None:
|
||||
self._apis.append(api)
|
||||
|
||||
def parse_orig(self):
|
||||
self.orders = []
|
||||
self.header = ""
|
||||
if self.is_pyi is True:
|
||||
self.header = "from typing import Any, Collection, MutableMapping, Optional, Tuple, Union\n\n"
|
||||
self.header = "from typing import Any, Collection, Optional, Tuple, Union\n\n"
|
||||
|
||||
namespace_new = "".join(word.capitalize() for word in self.namespace.split("_"))
|
||||
self.header = (
|
||||
self.header + "class " + namespace_new + "Client(NamespacedClient):"
|
||||
)
|
||||
self.header += "class " + namespace_new + "Client(NamespacedClient):"
|
||||
if os.path.exists(self.filepath):
|
||||
with open(self.filepath) as f:
|
||||
content = f.read()
|
||||
@@ -127,12 +118,10 @@ class Module:
|
||||
for line in content.split("\n"):
|
||||
header_lines.append(line)
|
||||
if line.startswith("class"):
|
||||
if (
|
||||
"security.py" in str(self.filepath)
|
||||
and not self.filepath.suffix == ".pyi"
|
||||
):
|
||||
if "security.py" in str(self.filepath):
|
||||
# TODO: FIXME, import code
|
||||
header_lines.append(
|
||||
" from ._patch import health_check, update_audit_config"
|
||||
" from ._patch import health_check, update_audit_config # type: ignore"
|
||||
)
|
||||
break
|
||||
self.header = "\n".join(header_lines)
|
||||
@@ -146,10 +135,10 @@ class Module:
|
||||
except ValueError:
|
||||
return len(self.orders)
|
||||
|
||||
def sort(self):
|
||||
def sort(self) -> None:
|
||||
self._apis.sort(key=self._position)
|
||||
|
||||
def dump(self):
|
||||
def dump(self) -> None:
|
||||
self.sort()
|
||||
|
||||
# This code snippet adds headers to each generated module indicating that the code is generated.
|
||||
@@ -244,22 +233,15 @@ class Module:
|
||||
with open(self.filepath, "w") as f:
|
||||
f.write(file_content)
|
||||
|
||||
if not self.is_pyi:
|
||||
self.pyi.dump()
|
||||
|
||||
@property
|
||||
def filepath(self):
|
||||
return (
|
||||
CODE_ROOT
|
||||
/ f"opensearchpy/_async/client/{self.namespace}.py{'i' if self.is_pyi else ''}"
|
||||
)
|
||||
return CODE_ROOT / f"opensearchpy/_async/client/{self.namespace}.py"
|
||||
|
||||
|
||||
class API:
|
||||
def __init__(self, namespace, name, definition, is_pyi=False):
|
||||
def __init__(self, namespace, name, definition) -> None:
|
||||
self.namespace = namespace
|
||||
self.name = name
|
||||
self.is_pyi = is_pyi
|
||||
|
||||
# overwrite the dict to maintain key order
|
||||
definition["params"] = {
|
||||
@@ -429,13 +411,10 @@ class API:
|
||||
return required
|
||||
|
||||
def to_python(self):
|
||||
if self.is_pyi:
|
||||
t = jinja_env.get_template("base_pyi")
|
||||
else:
|
||||
try:
|
||||
t = jinja_env.get_template(f"overrides/{self.namespace}/{self.name}")
|
||||
except TemplateNotFound:
|
||||
t = jinja_env.get_template("base")
|
||||
try:
|
||||
t = jinja_env.get_template(f"overrides/{self.namespace}/{self.name}")
|
||||
except TemplateNotFound:
|
||||
t = jinja_env.get_template("base")
|
||||
|
||||
return t.render(
|
||||
api=self,
|
||||
@@ -658,7 +637,6 @@ def read_modules():
|
||||
modules[namespace] = Module(namespace)
|
||||
|
||||
modules[namespace].add(API(namespace, name, api))
|
||||
modules[namespace].pyi.add(API(namespace, name, api, is_pyi=True))
|
||||
|
||||
return modules
|
||||
|
||||
@@ -697,10 +675,9 @@ def dump_modules(modules):
|
||||
filepaths = []
|
||||
for root, _, filenames in os.walk(CODE_ROOT / "opensearchpy/_async"):
|
||||
for filename in filenames:
|
||||
if filename.rpartition(".")[-1] in (
|
||||
"py",
|
||||
"pyi",
|
||||
) and not filename.startswith("utils.py"):
|
||||
if filename.rpartition(".")[-1] in ("py",) and not filename.startswith(
|
||||
"utils.py"
|
||||
):
|
||||
filepaths.append(os.path.join(root, filename))
|
||||
|
||||
unasync.unasync_files(filepaths, rules)
|
||||
|
||||
Reference in New Issue
Block a user