|
| 1 | +# coding=utf-8 |
| 2 | +# Copyright 2022 The Fiddle-Config Authors. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +"""Adds type signatures to modules. |
| 17 | +
|
| 18 | +For now, we only populate return types. |
| 19 | +""" |
| 20 | + |
| 21 | +import inspect |
| 22 | + |
| 23 | +from fiddle._src import config as config_lib |
| 24 | +from fiddle._src import signatures |
| 25 | +from fiddle._src.codegen import import_manager as import_manager_lib |
| 26 | +from fiddle._src.codegen.auto_config import code_ir |
| 27 | +from fiddle._src.codegen.auto_config import import_manager_wrapper |
| 28 | + |
| 29 | + |
| 30 | +_BUILTIN_TYPE_MAP = { |
| 31 | + type(None): "None", |
| 32 | + str: "str", |
| 33 | + int: "int", |
| 34 | + float: "float", |
| 35 | + bool: "bool", |
| 36 | +} |
| 37 | + |
| 38 | + |
| 39 | +def _get_annotation_from_type(typ) -> code_ir.CodegenNode: |
| 40 | + if typ in _BUILTIN_TYPE_MAP: |
| 41 | + return code_ir.BuiltinReference(code_ir.Name(_BUILTIN_TYPE_MAP[typ])) |
| 42 | + else: |
| 43 | + # TODO(b/293352960): import typing.Any correctly. |
| 44 | + # TODO(b/293509806): Handle more types, especially from function return |
| 45 | + # signatures. |
| 46 | + return code_ir.BuiltinReference(code_ir.Name("Any")) |
| 47 | + |
| 48 | + |
| 49 | +def get_type_annotation( |
| 50 | + value, import_manager: import_manager_lib.ImportManager |
| 51 | +) -> code_ir.CodegenNode: |
| 52 | + """Gets the type annotation for a given value.""" |
| 53 | + if isinstance(value, config_lib.Buildable): |
| 54 | + buildable_type = import_manager_wrapper.add(type(value), import_manager) |
| 55 | + fn_or_cls = config_lib.get_callable(value) |
| 56 | + if isinstance(fn_or_cls, type): |
| 57 | + sub_type = import_manager_wrapper.add(fn_or_cls, import_manager) |
| 58 | + else: |
| 59 | + signature = signatures.get_signature(fn_or_cls) |
| 60 | + if isinstance(signature.return_annotation, type) and ( |
| 61 | + signature.return_annotation is not inspect.Signature.empty |
| 62 | + ): |
| 63 | + sub_type = _get_annotation_from_type(signature.return_annotation) |
| 64 | + else: |
| 65 | + return buildable_type |
| 66 | + return code_ir.ParameterizedTypeExpression(buildable_type, [sub_type]) |
| 67 | + elif isinstance(value, (list, tuple)): |
| 68 | + base_expression = code_ir.BuiltinReference( |
| 69 | + code_ir.Name("list" if isinstance(value, list) else "tuple") |
| 70 | + ) |
| 71 | + sub_value_annotations = [ |
| 72 | + get_type_annotation(item, import_manager) for item in value |
| 73 | + ] |
| 74 | + if sub_value_annotations and all( |
| 75 | + annotation == sub_value_annotations[0] |
| 76 | + for annotation in sub_value_annotations |
| 77 | + ): |
| 78 | + return code_ir.ParameterizedTypeExpression( |
| 79 | + base_expression, [sub_value_annotations[0]] |
| 80 | + ) |
| 81 | + else: |
| 82 | + return base_expression |
| 83 | + elif isinstance(value, dict): |
| 84 | + base_expression = code_ir.BuiltinReference(code_ir.Name("dict")) |
| 85 | + key_annotations = [ |
| 86 | + get_type_annotation(item, import_manager) for item in value.keys() |
| 87 | + ] |
| 88 | + value_annotations = [ |
| 89 | + get_type_annotation(item, import_manager) for item in value.values() |
| 90 | + ] |
| 91 | + if key_annotations and all( |
| 92 | + annotation == key_annotations[0] for annotation in key_annotations |
| 93 | + ): |
| 94 | + key_annotation = key_annotations[0] |
| 95 | + else: |
| 96 | + # TODO(b/293352960): import typing.Any correctly. |
| 97 | + key_annotation = code_ir.BuiltinReference(code_ir.Name("Any")) |
| 98 | + if value_annotations and all( |
| 99 | + annotation == value_annotations[0] for annotation in value_annotations |
| 100 | + ): |
| 101 | + value_annotation = value_annotations[0] |
| 102 | + else: |
| 103 | + value_annotation = code_ir.BuiltinReference(code_ir.Name("Any")) |
| 104 | + return code_ir.ParameterizedTypeExpression( |
| 105 | + base_expression, [key_annotation, value_annotation] |
| 106 | + ) |
| 107 | + else: |
| 108 | + return _get_annotation_from_type(type(value)) |
| 109 | + |
| 110 | + |
| 111 | +def add_return_types(task: code_ir.CodegenTask) -> None: |
| 112 | + """Adds return type signatures. |
| 113 | +
|
| 114 | + This is normally based on config types, so for `auto_config`, it would reflect |
| 115 | + the as_buildable() path. Hence, we don't add it by default yet. |
| 116 | +
|
| 117 | + Args: |
| 118 | + task: Codegen task. |
| 119 | + """ |
| 120 | + for fn in task.top_level_call.all_fixture_functions(): |
| 121 | + fn.return_type_annotation = get_type_annotation( |
| 122 | + fn.output_value, task.import_manager |
| 123 | + ) |
0 commit comments