Try to extract the actual location from a node, depending on available attributes: * "location": a pair (path, lineno) * "obj": a Python object that the node wraps. * "path": just a path :rtype: A tuple of (str|Path, int) with filename and 0-based line number.
(node: Node)
| 470 | |
| 471 | |
| 472 | def get_fslocation_from_item(node: Node) -> tuple[str | Path, int | None]: |
| 473 | """Try to extract the actual location from a node, depending on available attributes: |
| 474 | |
| 475 | * "location": a pair (path, lineno) |
| 476 | * "obj": a Python object that the node wraps. |
| 477 | * "path": just a path |
| 478 | |
| 479 | :rtype: A tuple of (str|Path, int) with filename and 0-based line number. |
| 480 | """ |
| 481 | # See Item.location. |
| 482 | location: tuple[str, int | None, str] | None = getattr(node, "location", None) |
| 483 | if location is not None: |
| 484 | return location[:2] |
| 485 | obj = getattr(node, "obj", None) |
| 486 | if obj is not None: |
| 487 | return getfslineno(obj) |
| 488 | return getattr(node, "path", "unknown location"), -1 |
| 489 | |
| 490 | |
| 491 | class Collector(Node, abc.ABC): |