@@ -59,7 +59,15 @@ def visit_ClassDef(self, node: ast.ClassDef) -> None:
59
59
60
60
61
61
def is_subclass (class_obj : type , classes_ : list [type ]) -> bool :
62
- """Check if the given class object is a subclass of any class in list classes."""
62
+ """Check if the given class object is a subclass of any class in list classes.
63
+
64
+ Args:
65
+ class_obj: The class to check.
66
+ classes_: A list of classes to check against.
67
+
68
+ Returns:
69
+ True if `class_obj` is a subclass of any class in `classes_`, False otherwise.
70
+ """
63
71
return any (
64
72
issubclass (class_obj , kls )
65
73
for kls in classes_
@@ -68,7 +76,15 @@ def is_subclass(class_obj: type, classes_: list[type]) -> bool:
68
76
69
77
70
78
def find_subclasses_in_module (module : ModuleType , classes_ : list [type ]) -> list [str ]:
71
- """Find all classes in the module that inherit from one of the classes."""
79
+ """Find all classes in the module that inherit from one of the classes.
80
+
81
+ Args:
82
+ module: The module to inspect.
83
+ classes_: A list of classes to check against.
84
+
85
+ Returns:
86
+ A list of class names that are subclasses of any class in `classes_`.
87
+ """
72
88
subclasses = []
73
89
# Iterate over all attributes of the module that are classes
74
90
for _name , obj in inspect .getmembers (module , inspect .isclass ):
@@ -91,7 +107,15 @@ def identify_all_imports_in_file(
91
107
* ,
92
108
from_package : Optional [str ] = None ,
93
109
) -> list [tuple [str , str ]]:
94
- """Let's also identify all the imports in the given file."""
110
+ """Identify all the imports in the given file.
111
+
112
+ Args:
113
+ file: The file to analyze.
114
+ from_package: If provided, only return imports from this package.
115
+
116
+ Returns:
117
+ A list of tuples `(module, name)` representing the imports found in the file.
118
+ """
95
119
code = Path (file ).read_text (encoding = "utf-8" )
96
120
return find_imports_from_package (code , from_package = from_package )
97
121
@@ -106,6 +130,9 @@ def identify_pkg_source(pkg_root: str) -> pathlib.Path:
106
130
Returns:
107
131
Returns the path to the source code for the package.
108
132
133
+ Raises:
134
+ ValueError: If there is not exactly one directory starting with `'langchain_'`
135
+ in the package root.
109
136
"""
110
137
dirs = [d for d in Path (pkg_root ).iterdir () if d .is_dir ()]
111
138
matching_dirs = [d for d in dirs if d .name .startswith ("langchain_" )]
@@ -116,7 +143,15 @@ def identify_pkg_source(pkg_root: str) -> pathlib.Path:
116
143
117
144
118
145
def list_classes_by_package (pkg_root : str ) -> list [tuple [str , str ]]:
119
- """List all classes in a package."""
146
+ """List all classes in a package.
147
+
148
+ Args:
149
+ pkg_root: the root of the package.
150
+
151
+ Returns:
152
+ A list of tuples `(module, class_name)` representing all classes found in the
153
+ package, excluding test files.
154
+ """
120
155
module_classes = []
121
156
pkg_source = identify_pkg_source (pkg_root )
122
157
files = list (pkg_source .rglob ("*.py" ))
@@ -130,7 +165,15 @@ def list_classes_by_package(pkg_root: str) -> list[tuple[str, str]]:
130
165
131
166
132
167
def list_init_imports_by_package (pkg_root : str ) -> list [tuple [str , str ]]:
133
- """List all the things that are being imported in a package by module."""
168
+ """List all the things that are being imported in a package by module.
169
+
170
+ Args:
171
+ pkg_root: the root of the package.
172
+
173
+ Returns:
174
+ A list of tuples `(module, name)` representing the imports found in
175
+ `__init__.py` files.
176
+ """
134
177
imports = []
135
178
pkg_source = identify_pkg_source (pkg_root )
136
179
# Scan all the files in the package
@@ -150,7 +193,15 @@ def find_imports_from_package(
150
193
* ,
151
194
from_package : Optional [str ] = None ,
152
195
) -> list [tuple [str , str ]]:
153
- """Find imports in code."""
196
+ """Find imports in code.
197
+
198
+ Args:
199
+ code: The code to analyze.
200
+ from_package: If provided, only return imports from this package.
201
+
202
+ Returns:
203
+ A list of tuples `(module, name)` representing the imports found.
204
+ """
154
205
# Parse the code into an AST
155
206
tree = ast .parse (code )
156
207
# Create an instance of the visitor
0 commit comments