Co-authored-by: ptosco <paolo.tosco@novartis.com>
This commit is contained in:
Paolo Tosco
2025-10-02 05:58:04 +02:00
committed by greg landrum
parent 1f9d85e035
commit ebccc05bc9
3 changed files with 97 additions and 1 deletions

View File

@@ -1,5 +1,6 @@
import sys
import os
import glob
import builtins
import importlib
import re
@@ -14,6 +15,8 @@ logger = logging.getLogger(__name__)
WORKER_SCRIPT = "worker.py"
PATCH_DIR = "patch"
PATCH_EXT = ".diff"
IMPORT_MODULES = re.compile(r"^\s*import\s+(.*)$")
FROM_IMPORT_MODULES = re.compile(r"^\s*from\s+(\S+)\s+import\s+(.*)$")
PYI_TO_PY = re.compile(r"\.pyi$")
@@ -67,6 +70,26 @@ def find_rdkit_include_path():
rdkit_include_path = None
return rdkit_include_path
def apply_patch(tempdir, patch_file):
"""Apply a patch file to the corresponding stub file."""
cmd = ["git", "--git-dir=", "apply", patch_file]
proc = subprocess.run(cmd, cwd=tempdir, capture_output=True)
if proc.returncode:
msg = proc.stderr.decode("utf-8") or "(no error message)"
cmd_as_str = " ".join(cmd)
logger.critical(f"Failed to apply patch {patch_file};\n\"{cmd_as_str}\" failed with:\n{msg}")
def patch_stubs(tempdir, src_entry):
"""Patch the stubs in src_entry if a diff fail is available."""
patch_dir = os.path.join(os.path.dirname(__file__), PATCH_DIR)
if not os.path.isdir(patch_dir):
return
for pyi in glob.glob(os.path.join(src_entry, "**/*.pyi")):
pyi_rel = os.path.relpath(pyi, tempdir)
patch_file = os.path.join(patch_dir, pyi_rel + PATCH_EXT)
if os.path.exists(patch_file):
apply_patch(tempdir, patch_file)
def copy_stubs(src_entry, outer_dirs):
"""Copy src_entry to each directory in outer_dirs.
If src_entry is a directory it will be recursively copied.
@@ -183,6 +206,7 @@ def generate_stubs_internal(modules, outer_dirs, args):
if concat_out and args.verbose:
logger.warning(concat_out)
if os.path.isdir(src_dir):
patch_stubs(tempdir, src_dir)
for f in os.listdir(src_dir):
src_entry = os.path.join(src_dir, f)
if os.path.exists(src_entry):

View File

@@ -0,0 +1,70 @@
--- a/rdkit/Chem/inchi.pyi 2025-09-29 14:01:41.137282007 +0200
+++ b/rdkit/Chem/inchi.pyi 2025-09-29 14:26:05.297333355 +0200
@@ -1,16 +1,19 @@
from __future__ import annotations
import logging as logging
+from rdkit.Chem import rdchem
+import rdkit.Chem.rdchem
from rdkit.Chem import rdinchi
-import rdkit.RDLogger
from rdkit import RDLogger
+import rdkit.RDLogger
+import typing as typing
__all__: list = ['MolToInchiAndAuxInfo', 'MolToInchi', 'MolBlockToInchiAndAuxInfo', 'MolBlockToInchi', 'MolFromInchi', 'InchiReadWriteError', 'InchiToInchiKey', 'MolToInchiKey', 'GetInchiVersion', 'INCHI_AVAILABLE']
class InchiReadWriteError(Exception):
pass
-def InchiToInchiKey(inchi):
+def InchiToInchiKey(inchi: str) -> typing.Optional[str]:
"""
Return the InChI key for the given InChI string. Return None on error
"""
-def MolBlockToInchi(molblock, options = '', logLevel = None, treatWarningAsError = False):
+def MolBlockToInchi(molblock: str, options: str = '', logLevel: typing.Optional[int] = None, treatWarningAsError: bool = False) -> str:
"""
Returns the standard InChI string for a mol block
@@ -26,7 +29,7 @@
the standard InChI string returned by InChI API for the input molecule
"""
-def MolBlockToInchiAndAuxInfo(molblock, options = '', logLevel = None, treatWarningAsError = False):
+def MolBlockToInchiAndAuxInfo(molblock: str, options: str = '', logLevel: typing.Optional[int] = None, treatWarningAsError: bool = False) -> typing.Tuple[str, str]:
"""
Returns the standard InChI string and InChI auxInfo for a mol block
@@ -43,7 +46,7 @@
InChI API, in that order, for the input molecule
"""
-def MolFromInchi(inchi, sanitize = True, removeHs = True, logLevel = None, treatWarningAsError = False):
+def MolFromInchi(inchi: str, sanitize: bool = True, removeHs: bool = True, logLevel: typing.Optional[int] = None, treatWarningAsError: bool = False) -> typing.Optional[rdkit.Chem.rdchem.Mol]:
"""
Construct a molecule from a InChI string
@@ -62,7 +65,7 @@
a rdkit.Chem.rdchem.Mol instance
"""
-def MolToInchi(mol, options = '', logLevel = None, treatWarningAsError = False):
+def MolToInchi(mol: rdkit.Chem.rdchem.Mol, options: str = '', logLevel: typing.Optional[int] = None, treatWarningAsError: bool = False) -> str:
"""
Returns the standard InChI string for a molecule
@@ -78,7 +81,7 @@
the standard InChI string returned by InChI API for the input molecule
"""
-def MolToInchiAndAuxInfo(mol, options = '', logLevel = None, treatWarningAsError = False):
+def MolToInchiAndAuxInfo(mol: rdkit.Chem.rdchem.Mol, options: str = '', logLevel: typing.Optional[int] = None, treatWarningAsError: bool = False) -> typing.Tuple[str, str]:
"""
Returns the standard InChI string and InChI auxInfo for a molecule
@@ -95,7 +98,7 @@
InChI API, in that order, for the input molecule
"""
-def MolToInchiKey(mol, options = ''):
+def MolToInchiKey(mol: rdkit.Chem.rdchem.Mol, options: str = '') -> typing.Optional[str]:
"""
Returns the standard InChI key for a molecule

View File

@@ -74,4 +74,6 @@ if __name__ == "__main__":
print(str(e))
finally:
sys.argv = stored_argv
gen_rdkit_stubs.copy_stubs(os.path.join(args.tempdir, *args.module_name.split(".")), args.outer_dirs)
src_path = os.path.join(args.tempdir, *args.module_name.split("."))
gen_rdkit_stubs.patch_stubs(args.tempdir, src_path)
gen_rdkit_stubs.copy_stubs(src_path, args.outer_dirs)