74
74
frozen_default = False ,
75
75
field_specifiers = ("dataclasses.Field" , "dataclasses.field" ),
76
76
)
77
- _INTERNAL_REPLACE_METHOD = "__mypy_replace"
77
+ _INTERNAL_REPLACE_SYM_NAME = "__mypy_replace"
78
78
79
79
80
80
class DataclassAttribute :
@@ -337,6 +337,10 @@ def transform(self) -> bool:
337
337
return True
338
338
339
339
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
+ """
340
344
arg_types : list [Type ] = [Instance (self ._cls .info , [])]
341
345
arg_kinds = [ARG_POS ]
342
346
arg_names : list [str | None ] = [None ]
@@ -357,7 +361,7 @@ def _add_internal_replace_method(self, attributes: list[DataclassAttribute]) ->
357
361
name = f"replace of { self ._cls .info .name } " ,
358
362
)
359
363
360
- self ._cls .info .names [_INTERNAL_REPLACE_METHOD ] = SymbolTableNode (
364
+ self ._cls .info .names [_INTERNAL_REPLACE_SYM_NAME ] = SymbolTableNode (
361
365
kind = MDEF , node = FuncDef (typ = signature ), plugin_generated = True
362
366
)
363
367
@@ -802,8 +806,8 @@ def _is_dataclasses_decorator(node: Node) -> bool:
802
806
803
807
def replace_function_sig_callback (ctx : FunctionSigContext ) -> CallableType :
804
808
"""
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.
807
811
"""
808
812
if len (ctx .args ) != 2 :
809
813
# 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:
826
830
if not isinstance (obj_type , Instance ):
827
831
return ctx .default_signature
828
832
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 :
831
835
inst_type_str = format_type_bare (obj_type )
832
836
ctx .api .fail (
833
837
f'Argument 1 to "replace" has incompatible type "{ inst_type_str } "; expected a dataclass' ,
834
838
ctx .context ,
835
839
)
836
840
return ctx .default_signature
837
841
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