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