Best-effort source location for a node. Preference order: - CustomNode forward function (common for user code) - node.forward callable (if any) - node class definition
(node)
| 114 | return value |
| 115 | |
| 116 | def _source_info(node) -> tuple[str | None, int | None]: |
| 117 | """ |
| 118 | Best-effort source location for a node. |
| 119 | |
| 120 | Preference order: |
| 121 | - CustomNode forward function (common for user code) |
| 122 | - node.forward callable (if any) |
| 123 | - node class definition |
| 124 | """ |
| 125 | target: object = node.__class__ |
| 126 | try: |
| 127 | forward = getattr(node, "_forward_function", None) |
| 128 | if callable(forward): |
| 129 | target = _unwrap_callable(forward) |
| 130 | else: |
| 131 | maybe_forward = getattr(node, "forward", None) |
| 132 | if callable(maybe_forward): |
| 133 | target = _unwrap_callable(maybe_forward) |
| 134 | except Exception: |
| 135 | target = node.__class__ |
| 136 | |
| 137 | file_path: str | None |
| 138 | try: |
| 139 | file_path = inspect.getsourcefile(target) or inspect.getfile(target) |
| 140 | except Exception: |
| 141 | file_path = None |
| 142 | if file_path: |
| 143 | try: |
| 144 | file_path = os.path.abspath(file_path) |
| 145 | except Exception: |
| 146 | pass |
| 147 | |
| 148 | line: int | None |
| 149 | try: |
| 150 | _lines, line = inspect.getsourcelines(target) |
| 151 | if not isinstance(line, int) or line <= 0: |
| 152 | line = None |
| 153 | except Exception: |
| 154 | line = None |
| 155 | |
| 156 | return file_path, line |
| 157 | |
| 158 | def ensure_node(node, *, parent: str | None = None) -> str: |
| 159 | nid = node_id(node) |
no test coverage detected