6
6
from typing_extensions import Final
7
7
8
8
from mypy import errorcodes , message_registry
9
- from mypy .expandtype import expand_type
9
+ from mypy .expandtype import expand_type , expand_type_by_instance
10
10
from mypy .messages import format_type_bare
11
11
from mypy .nodes import (
12
12
ARG_NAMED ,
@@ -350,12 +350,15 @@ def _add_internal_replace_method(self, attributes: list[DataclassAttribute]) ->
350
350
Stashes the signature of 'dataclasses.replace(...)' for this specific dataclass
351
351
to be used later whenever 'dataclasses.replace' is called for this dataclass.
352
352
"""
353
- arg_types : list [Type ] = [Instance (self ._cls .info , [])]
354
- arg_kinds = [ARG_POS ]
355
- arg_names : list [str | None ] = [None ]
353
+ arg_types : list [Type ] = []
354
+ arg_kinds = []
355
+ arg_names : list [str | None ] = []
356
+
357
+ info = self ._cls .info
356
358
for attr in attributes :
357
- assert attr .type is not None
358
- arg_types .append (attr .type )
359
+ attr_type = attr .expand_type (info )
360
+ assert attr_type is not None
361
+ arg_types .append (attr_type )
359
362
arg_kinds .append (
360
363
ARG_NAMED if attr .is_init_var and not attr .has_default else ARG_NAMED_OPT
361
364
)
@@ -365,7 +368,7 @@ def _add_internal_replace_method(self, attributes: list[DataclassAttribute]) ->
365
368
arg_types = arg_types ,
366
369
arg_kinds = arg_kinds ,
367
370
arg_names = arg_names ,
368
- ret_type = Instance ( self . _cls . info , [] ),
371
+ ret_type = NoneType ( ),
369
372
fallback = self ._api .named_type ("builtins.function" ),
370
373
name = f"replace of { self ._cls .info .name } " ,
371
374
)
@@ -883,4 +886,12 @@ def replace_function_sig_callback(ctx: FunctionSigContext) -> CallableType:
883
886
884
887
signature = get_proper_type (replace_func .type )
885
888
assert isinstance (signature , CallableType )
889
+ signature = expand_type_by_instance (signature , obj_type )
890
+ # re-add the instance type
891
+ signature = signature .copy_modified (
892
+ arg_types = [obj_type , * signature .arg_types ],
893
+ arg_kinds = [ARG_POS , * signature .arg_kinds ],
894
+ arg_names = [None , * signature .arg_names ],
895
+ ret_type = obj_type ,
896
+ )
886
897
return signature
0 commit comments