Ubospica commented on code in PR #608:
URL: https://github.com/apache/tvm-ffi/pull/608#discussion_r3397946533


##########
python/tvm_ffi/stub/python_generator/utils.py:
##########
@@ -0,0 +1,303 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""Python generator helpers for ``tvm-ffi-stubgen``.
+
+This module groups two Python-specific concerns:
+
+- import modelling (:class:`ImportItem`, :class:`PythonImports`)
+- stub rendering helpers for function/object signatures
+"""
+
+from __future__ import annotations
+
+import dataclasses
+from io import StringIO
+from typing import Callable
+
+from ..utils import FuncInfo, ObjectInfo
+from . import consts as C
+
+
[email protected](frozen=True, eq=True)
+class ImportItem:
+    """An import statement item."""
+
+    mod: str
+    name: str
+    type_checking_only: bool = False
+    alias: str | None = None
+
+    def __init__(
+        self,
+        full_name: str,
+        type_checking_only: bool = False,
+        alias: str | None = None,
+    ) -> None:
+        """Initialize an `ImportItem` from a dotted ``module.symbol`` name and 
optional alias."""
+        if "." in full_name:
+            mod, name = full_name.rsplit(".", 1)
+            for mod_prefix, mod_replacement in C.MOD_MAP.items():
+                if mod == mod_prefix or mod.startswith(mod_prefix + "."):
+                    mod = mod.replace(mod_prefix, mod_replacement, 1)
+                    break
+        else:
+            mod, name = "", full_name
+        object.__setattr__(self, "mod", mod)
+        object.__setattr__(self, "name", name)
+        object.__setattr__(self, "type_checking_only", type_checking_only)
+        object.__setattr__(self, "alias", alias)
+
+    @property
+    def name_with_alias(self) -> str:
+        """Generate a string of the form `name as alias` if an alias is set, 
otherwise just `name`."""
+        return f"{self.name} as {self.alias}" if self.alias else self.name
+
+    @property
+    def full_name(self) -> str:
+        """Generate a string of the form `mod.name` or `name` if no module is 
set."""
+        return f"{self.mod}.{self.name}" if self.mod else self.name
+
+    def __repr__(self) -> str:
+        """Generate an import statement string for this item."""
+        return str(self)
+
+    def __str__(self) -> str:
+        """Generate an import statement string for this item."""
+        if self.mod:
+            ret = f"from {self.mod} import {self.name_with_alias}"
+        else:
+            ret = f"import {self.name_with_alias}"
+        return ret
+
+
[email protected]
+class PythonImports:
+    """Opaque import collector threaded through the Python generation pipeline.
+
+    The language-agnostic ``cli`` treats this as an opaque token: it asks the
+    generator to create one, seed it from ``import-object`` directives, and 
later
+    render it. Only the Python generator reaches inside.
+    """
+
+    items: list[ImportItem] = dataclasses.field(default_factory=list)
+    has_lib_load: bool = False
+    """Whether an FFI library-loading import was seen (adds ``LIB`` to 
``__all__``)."""
+
+
+#: Renders a :class:`~tvm_ffi.core.TypeSchema` into a Python type expression.
+#: The second argument is the per-block leaf-name mapper (records imports as a
+#: side effect). ``None`` means "use the built-in Python rendering"
+#: (:meth:`TypeSchema.repr`).
+RenderType = Callable[..., str]
+
+
+def _bind_render(

Review Comment:
   If this is a python specific logic, likely we don't need to write this 
wrapper?



##########
python/tvm_ffi/stub/file_utils.py:
##########
@@ -60,19 +65,21 @@ def indent(self) -> int:
         return len(first_line) - len(first_line.lstrip(" "))
 
     @staticmethod
-    def from_begin_line(lineo: int, line: str) -> CodeBlock:
+    def from_begin_line(
+        lineo: int, line: str, syntax: C.MarkerSyntax = C.PYTHON_SYNTAX

Review Comment:
   We can limit the default value (python) to one (or a few) place and avoid 
providing default value elsewhere



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to