MCPcopy Index your code
hub / github.com/python/mypy / replace_function_sig_callback

Function replace_function_sig_callback

mypy/plugins/dataclasses.py:1074–1103  ·  view source on GitHub ↗

Returns a signature for the 'dataclasses.replace' function that's dependent on the type of the first positional argument.

(ctx: FunctionSigContext)

Source from the content-addressed store, hash-verified

1072
1073
1074def 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
1106def is_processed_dataclass(info: TypeInfo) -> bool:

Callers

nothing calls this directly

Calls 8

get_proper_typeFunction · 0.90
format_type_bareFunction · 0.90
lenFunction · 0.85
_meet_replace_sigsFunction · 0.85
failMethod · 0.45
get_expression_typeMethod · 0.45
copy_modifiedMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…