7474 frozen_default = False ,
7575 field_specifiers = ("dataclasses.Field" , "dataclasses.field" ),
7676)
77- _INTERNAL_REPLACE_METHOD = "__mypy_replace"
77+ _INTERNAL_REPLACE_SYM_NAME = "__mypy_replace"
7878
7979
8080class DataclassAttribute :
@@ -337,6 +337,10 @@ def transform(self) -> bool:
337337 return True
338338
339339 def _add_internal_replace_method (self , attributes : list [DataclassAttribute ]) -> None :
340+ """
341+ Stashes a function with the signature of 'dataclasses.replace' for this specific dataclass,
342+ to be used later if someone calls 'dataclasses.replace' on this dataclass.
343+ """
340344 arg_types : list [Type ] = [Instance (self ._cls .info , [])]
341345 arg_kinds = [ARG_POS ]
342346 arg_names : list [str | None ] = [None ]
@@ -357,7 +361,7 @@ def _add_internal_replace_method(self, attributes: list[DataclassAttribute]) ->
357361 name = f"replace of { self ._cls .info .name } " ,
358362 )
359363
360- self ._cls .info .names [_INTERNAL_REPLACE_METHOD ] = SymbolTableNode (
364+ self ._cls .info .names [_INTERNAL_REPLACE_SYM_NAME ] = SymbolTableNode (
361365 kind = MDEF , node = FuncDef (typ = signature ), plugin_generated = True
362366 )
363367
@@ -802,8 +806,8 @@ def _is_dataclasses_decorator(node: Node) -> bool:
802806
803807def replace_function_sig_callback (ctx : FunctionSigContext ) -> CallableType :
804808 """
805- Generates a signature for the 'dataclasses.replace' function that's specific to the call site
806- and dependent on the type of the first argument.
809+ Returns a signature for the 'dataclasses.replace' function that's dependent on the type
810+ of the first positional argument.
807811 """
808812 if len (ctx .args ) != 2 :
809813 # Ideally the name and context should be callee's, but we don't have it in FunctionSigContext.
@@ -826,15 +830,15 @@ def replace_function_sig_callback(ctx: FunctionSigContext) -> CallableType:
826830 if not isinstance (obj_type , Instance ):
827831 return ctx .default_signature
828832
829- repl = obj_type .type .get_method (_INTERNAL_REPLACE_METHOD )
830- if repl is None :
833+ obj_replace = obj_type .type .get_method (_INTERNAL_REPLACE_SYM_NAME )
834+ if obj_replace is None :
831835 inst_type_str = format_type_bare (obj_type )
832836 ctx .api .fail (
833837 f'Argument 1 to "replace" has incompatible type "{ inst_type_str } "; expected a dataclass' ,
834838 ctx .context ,
835839 )
836840 return ctx .default_signature
837841
838- repl_type = get_proper_type (repl .type )
839- assert isinstance (repl_type , CallableType )
840- return repl_type
842+ obj_replace_sig = get_proper_type (obj_replace .type )
843+ assert isinstance (obj_replace_sig , CallableType )
844+ return obj_replace_sig
0 commit comments