Sanitize function names by replacing invalid characters with valid ones. This is needed because function names with special characters like angle brackets are not valid Python syntax.
(name: str)
| 101 | |
| 102 | |
| 103 | def _sanitize_function_name(name: str) -> str: |
| 104 | """ |
| 105 | Sanitize function names by replacing invalid characters with valid ones. |
| 106 | This is needed because function names with special characters like angle brackets |
| 107 | are not valid Python syntax. |
| 108 | """ |
| 109 | # 1) Replace every non‐identifier character with underscore |
| 110 | sanitized = re.sub(r"[^0-9A-Za-z_]", "_", name) |
| 111 | # 2) Prevent leading digit |
| 112 | if re.match(r"^\d", sanitized): |
| 113 | sanitized = "_" + sanitized |
| 114 | # 3) Avoid plain keywords |
| 115 | if keyword.iskeyword(sanitized): |
| 116 | sanitized = "_" + sanitized |
| 117 | return sanitized |
| 118 | |
| 119 | |
| 120 | def compile_function( |
no test coverage detected