Coverage for src/renaissance/integrations/python/ast/util.py: 93%
14 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-10-15 10:34 +0000
« prev ^ index » next coverage.py v7.16.2, created at 2026-10-15 10:34 +0000
1# Copyright (C) 2024-2026 TNO
2#
3# Licensed under the terms specified in the LICENSE file in the root directory
4# of this repository, also available at:
5# https://github.com/TNO/Renaissance.Py/blob/main/LICENSE
7"""AI: Utility helpers for converting between source positions and node representations."""
10def convert(lines, line_nr, col):
11 """AI: Convert a (line, column) source position into an absolute character offset."""
12 if line_nr > len(lines):
13 return 0
14 return sum(len(lines[i]) + 1 for i in range(line_nr - 1)) + col
15 # add node to the node list for references
18def to_str(node) -> str:
19 """AI: Return the node's signature if it has one, otherwise its string representation."""
20 if hasattr(node, "signature"):
21 return node.signature
22 return str(node)
25def convert_function(fun):
26 """AI: Return the function's signature rewritten to take a leading 'self' parameter."""
27 signature: str = fun.signature + "\n\n\n"
28 if len(fun.node.args.args) == 0:
29 signature = signature.replace(f"{fun.name}()", f"{fun.name}(self)", 1)
30 else:
31 signature = signature.replace(f"{fun.name}(", f"{fun.name}(self,", 1)
32 return signature