Returns a signature for the 'dataclasses.replace' function that's dependent on the type of the first positional argument.
(ctx: FunctionSigContext)
| 1072 | |
| 1073 | |
| 1074 | def replace_function_sig_callback(ctx: FunctionSigContext) -> CallableType: |
| 1075 | """ |
| 1076 | Returns a signature for the 'dataclasses.replace' function that's dependent on the type |
| 1077 | of the first positional argument. |
| 1078 | """ |
| 1079 | if len(ctx.args) != 2: |
| 1080 | # Ideally the name and context should be callee's, but we don't have it in FunctionSigContext. |
| 1081 | ctx.api.fail(f'"{ctx.default_signature.name}" has unexpected type annotation', ctx.context) |
| 1082 | return ctx.default_signature |
| 1083 | |
| 1084 | if len(ctx.args[0]) != 1: |
| 1085 | return ctx.default_signature # leave it to the type checker to complain |
| 1086 | |
| 1087 | obj_arg = ctx.args[0][0] |
| 1088 | obj_type = get_proper_type(ctx.api.get_expression_type(obj_arg)) |
| 1089 | inst_type_str = format_type_bare(obj_type, ctx.api.options) |
| 1090 | |
| 1091 | replace_sigs = _get_expanded_dataclasses_fields(ctx, obj_type, obj_type, obj_type) |
| 1092 | if replace_sigs is None: |
| 1093 | return ctx.default_signature |
| 1094 | replace_sig = _meet_replace_sigs(replace_sigs) |
| 1095 | |
| 1096 | return replace_sig.copy_modified( |
| 1097 | arg_names=[None, *replace_sig.arg_names], |
| 1098 | arg_kinds=[ARG_POS, *replace_sig.arg_kinds], |
| 1099 | arg_types=[obj_type, *replace_sig.arg_types], |
| 1100 | ret_type=obj_type, |
| 1101 | fallback=ctx.default_signature.fallback, |
| 1102 | name=f"{ctx.default_signature.name} of {inst_type_str}", |
| 1103 | ) |
| 1104 | |
| 1105 | |
| 1106 | def is_processed_dataclass(info: TypeInfo) -> bool: |
nothing calls this directly
no test coverage detected
searching dependent graphs…