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. * "fspath": just a path :rtype: A tuple of (str|Path, int) with filename and line number.
(node: "Node")
| 496 | |
| 497 | |
| 498 | def get_fslocation_from_item(node: "Node") -> Tuple[Union[str, Path], Optional[int]]: |
| 499 | """Try to extract the actual location from a node, depending on available attributes: |
| 500 | |
| 501 | * "location": a pair (path, lineno) |
| 502 | * "obj": a Python object that the node wraps. |
| 503 | * "fspath": just a path |
| 504 | |
| 505 | :rtype: A tuple of (str|Path, int) with filename and line number. |
| 506 | """ |
| 507 | # See Item.location. |
| 508 | location: Optional[Tuple[str, Optional[int], str]] = getattr(node, "location", None) |
| 509 | if location is not None: |
| 510 | return location[:2] |
| 511 | obj = getattr(node, "obj", None) |
| 512 | if obj is not None: |
| 513 | return getfslineno(obj) |
| 514 | return getattr(node, "fspath", "unknown location"), -1 |
| 515 | |
| 516 | |
| 517 | class Collector(Node): |