]> khamidou — GitHub mirrors - safetensors.git/commitdiff
Fix wrong signature of `safe_open.__init__` in stub file (#557)
authorSunghwanShim <128351792+SunghwanShim@users.noreply.github.com>
Wed, 8 Jan 2025 17:17:49 +0000 (02:17 +0900)
committerGitHub <noreply@github.com>
Wed, 8 Jan 2025 17:17:49 +0000 (18:17 +0100)
* fix: pyi binding bug

* Fixing the stubbing script (breaking change in PyO3).

---------

Co-authored-by: Nicolas Patry <patry.nicolas@protonmail.com>
bindings/python/py_src/safetensors/__init__.pyi
bindings/python/stub.py

index 81442bd3f65e68c43a0099c19d53654a108f0110..7781229fe91d0649996e257dccf9f6d0c38823cd 100644 (file)
@@ -69,7 +69,7 @@ class safe_open:
             The device on which you want the tensors.
     """
 
-    def __init__(filename, framework, device=...):
+    def __init__(self, filename, framework, device=...):
         pass
     def __enter__(self):
         """
index 78ddc47f1844dbacb56435548d50a65aa8a2455b..71fef2cc198e2322ba7deb9a7b5a1fd0f9283897 100644 (file)
@@ -42,10 +42,7 @@ def fn_predicate(obj):
         return (
             obj.__doc__
             and obj.__text_signature__
-            and (
-                not obj.__name__.startswith("_")
-                or obj.__name__ in {"__enter__", "__exit__"}
-            )
+            and (not obj.__name__.startswith("_") or obj.__name__ in {"__enter__", "__exit__"})
         )
     if inspect.isgetsetdescriptor(obj):
         return obj.__doc__ and not obj.__name__.startswith("_")
@@ -81,15 +78,14 @@ def pyi_file(obj, indent=""):
 
         body = ""
         if obj.__doc__:
-            body += (
-                f'{indent}"""\n{indent}{do_indent(obj.__doc__, indent)}\n{indent}"""\n'
-            )
+            body += f'{indent}"""\n{indent}{do_indent(obj.__doc__, indent)}\n{indent}"""\n'
 
         fns = inspect.getmembers(obj, fn_predicate)
 
         # Init
         if obj.__text_signature__:
-            body += f"{indent}def __init__{obj.__text_signature__}:\n"
+            signature = obj.__text_signature__.replace("(", "(self, ")
+            body += f"{indent}def __init__{signature}:\n"
             body += f"{indent+INDENT}pass\n"
             body += "\n"
 
@@ -146,11 +142,7 @@ def do_black(content, is_pyi):
 
 
 def write(module, directory, origin, check=False):
-    submodules = [
-        (name, member)
-        for name, member in inspect.getmembers(module)
-        if inspect.ismodule(member)
-    ]
+    submodules = [(name, member) for name, member in inspect.getmembers(module) if inspect.ismodule(member)]
 
     filename = os.path.join(directory, "__init__.pyi")
     pyi_content = pyi_file(module)
@@ -159,9 +151,7 @@ def write(module, directory, origin, check=False):
     if check:
         with open(filename, "r") as f:
             data = f.read()
-            assert (
-                data == pyi_content
-            ), f"The content of {filename} seems outdated, please run `python stub.py`"
+            assert data == pyi_content, f"The content of {filename} seems outdated, please run `python stub.py`"
     else:
         with open(filename, "w") as f:
             f.write(pyi_content)
@@ -184,9 +174,7 @@ def write(module, directory, origin, check=False):
         if check:
             with open(filename, "r") as f:
                 data = f.read()
-                assert (
-                    data == py_content
-                ), f"The content of {filename} seems outdated, please run `python stub.py`"
+                assert data == py_content, f"The content of {filename} seems outdated, please run `python stub.py`"
         else:
             with open(filename, "w") as f:
                 f.write(py_content)