(filename: str, cmd: code_writer_cmd)
| 29 | |
| 30 | |
| 31 | def process_functions(filename: str, cmd: code_writer_cmd) -> str: |
| 32 | with ( |
| 33 | NamedTemporaryFile( |
| 34 | mode="w", |
| 35 | delete=False, |
| 36 | suffix=".py", |
| 37 | ) as buf, |
| 38 | open(filename) as orig_py, |
| 39 | ): |
| 40 | indent = "" |
| 41 | in_block = False |
| 42 | alias_mapping: dict[str, str] = {} |
| 43 | |
| 44 | for line in orig_py: |
| 45 | m = re.match( |
| 46 | r"^( *)# START GENERATED FUNCTION ACCESSORS", |
| 47 | line, |
| 48 | ) |
| 49 | if m: |
| 50 | in_block = True |
| 51 | buf.write(line) |
| 52 | indent = m.group(1) |
| 53 | buf.write( |
| 54 | textwrap.indent( |
| 55 | """ |
| 56 | # code within this block is **programmatically, |
| 57 | # statically generated** by tools/generate_sql_functions.py |
| 58 | """, |
| 59 | indent, |
| 60 | ) |
| 61 | ) |
| 62 | |
| 63 | builtins = set(dir(__builtins__)) |
| 64 | for key, fn_class in _fns_in_deterministic_order(): |
| 65 | is_reserved_word = key in builtins |
| 66 | |
| 67 | class_name = f"_{fn_class.__name__}_func" |
| 68 | if issubclass(fn_class, ReturnTypeFromArgs): |
| 69 | guess_its_generic = True |
| 70 | if issubclass(fn_class, ReturnTypeFromOptionalArgs): |
| 71 | _TEE = "Optional[_T]" |
| 72 | else: |
| 73 | _TEE = "_T" |
| 74 | |
| 75 | buf.write( |
| 76 | textwrap.indent( |
| 77 | f""" |
| 78 | |
| 79 | # set ColumnElement[_T] as a separate overload, to appease |
| 80 | # mypy which seems to not want to accept _T from |
| 81 | # _ColumnExpressionArgument. Seems somewhat related to the covariant |
| 82 | # _HasClauseElement as of mypy 1.15 |
| 83 | |
| 84 | @overload |
| 85 | def {key}( {' # noqa: A001' if is_reserved_word else ''} |
| 86 | self, |
| 87 | col: ColumnElement[_T], |
| 88 | *args: _ColumnExpressionOrLiteralArgument[Any], |
no test coverage detected