|
1 | 1 | import inspect |
| 2 | +import os |
2 | 3 | import sys |
| 4 | +import warnings |
3 | 5 | from collections import defaultdict, deque |
| 6 | +from pathlib import Path |
4 | 7 | from typing import Any, Callable, Dict, List, Optional, TypeVar, get_type_hints |
5 | 8 |
|
6 | 9 | from graphlib import TopologicalSorter |
@@ -171,19 +174,64 @@ def _build_graph(self) -> None: # noqa: C901 |
171 | 174 | if inspect.isclass(origin): |
172 | 175 | # If this is a class, we need to get signature of |
173 | 176 | # an __init__ method. |
174 | | - hints = get_type_hints(origin.__init__) |
| 177 | + try: |
| 178 | + hints = get_type_hints(origin.__init__) |
| 179 | + except NameError: |
| 180 | + _, src_lineno = inspect.getsourcelines(dep.dependency) |
| 181 | + src_file = Path(inspect.getfile(dep.dependency)).relative_to( |
| 182 | + Path.cwd(), |
| 183 | + ) |
| 184 | + warnings.warn( |
| 185 | + "Cannot resolve type hints for " |
| 186 | + f"a class {dep.dependency.__name__} defined " |
| 187 | + f"at {src_file}:{src_lineno}.", |
| 188 | + RuntimeWarning, |
| 189 | + stacklevel=2, |
| 190 | + ) |
| 191 | + continue |
175 | 192 | sign = inspect.signature( |
176 | 193 | origin.__init__, |
177 | 194 | **signature_kwargs, |
178 | 195 | ) |
179 | 196 | elif inspect.isfunction(dep.dependency): |
180 | 197 | # If this is function or an instance of a class, we get it's type hints. |
181 | | - hints = get_type_hints(dep.dependency) |
| 198 | + try: |
| 199 | + hints = get_type_hints(dep.dependency) |
| 200 | + except NameError: |
| 201 | + _, src_lineno = inspect.getsourcelines(dep.dependency) |
| 202 | + src_file = Path(inspect.getfile(dep.dependency)).relative_to( |
| 203 | + Path.cwd(), |
| 204 | + ) |
| 205 | + warnings.warn( |
| 206 | + "Cannot resolve type hints for " |
| 207 | + f"a function {dep.dependency.__name__} defined " |
| 208 | + f"at {src_file}:{src_lineno}.", |
| 209 | + RuntimeWarning, |
| 210 | + stacklevel=2, |
| 211 | + ) |
| 212 | + continue |
182 | 213 | sign = inspect.signature(origin, **signature_kwargs) # type: ignore |
183 | 214 | else: |
184 | | - hints = get_type_hints( |
185 | | - dep.dependency.__call__, # type: ignore |
186 | | - ) |
| 215 | + try: |
| 216 | + hints = get_type_hints( |
| 217 | + dep.dependency.__call__, # type: ignore |
| 218 | + ) |
| 219 | + except NameError: |
| 220 | + _, src_lineno = inspect.getsourcelines(dep.dependency.__class__) |
| 221 | + src_file = Path( |
| 222 | + inspect.getfile(dep.dependency.__class__), |
| 223 | + ).relative_to( |
| 224 | + Path.cwd(), |
| 225 | + ) |
| 226 | + cls_name = dep.dependency.__class__.__name__ |
| 227 | + warnings.warn( |
| 228 | + "Cannot resolve type hints for " |
| 229 | + f"an object of class {cls_name} defined " |
| 230 | + f"at {src_file}:{src_lineno}.", |
| 231 | + RuntimeWarning, |
| 232 | + stacklevel=2, |
| 233 | + ) |
| 234 | + continue |
187 | 235 | sign = inspect.signature(origin, **signature_kwargs) # type: ignore |
188 | 236 |
|
189 | 237 | # Now we need to iterate over parameters, to |
|
0 commit comments