91
91
frozen_default = False ,
92
92
field_specifiers = DATACLASS_FIELD_SPECIFIERS ,
93
93
)
94
- _INTERNAL_REPLACE_SYM_NAME : Final = "__mypy -replace"
95
- _INTERNAL_POST_INIT_SYM_NAME : Final = "__mypy -post_init"
94
+ _INTERNAL_REPLACE_SYM_NAME : Final = "mypy -replace"
95
+ _INTERNAL_POST_INIT_SYM_NAME : Final = "mypy -post_init"
96
96
97
97
98
98
class DataclassAttribute :
@@ -422,7 +422,7 @@ def _add_internal_replace_method(self, attributes: list[DataclassAttribute]) ->
422
422
Stashes the signature of 'dataclasses.replace(...)' for this specific dataclass
423
423
to be used later whenever 'dataclasses.replace' is called for this dataclass.
424
424
"""
425
- mangled_name = f"_ { self ._cls .name . lstrip ( '_' ) } { _INTERNAL_REPLACE_SYM_NAME } "
425
+ mangled_name = _mangle_internal_sym_name ( self ._cls .fullname , _INTERNAL_REPLACE_SYM_NAME )
426
426
add_method_to_class (
427
427
self ._api ,
428
428
self ._cls ,
@@ -433,7 +433,7 @@ def _add_internal_replace_method(self, attributes: list[DataclassAttribute]) ->
433
433
)
434
434
435
435
def _add_internal_post_init_method (self , attributes : list [DataclassAttribute ]) -> None :
436
- mangled_name = f"_ { self ._cls .name . lstrip ( '_' ) } { _INTERNAL_POST_INIT_SYM_NAME } "
436
+ mangled_name = _mangle_internal_sym_name ( self ._cls .fullname , _INTERNAL_POST_INIT_SYM_NAME )
437
437
add_method_to_class (
438
438
self ._api ,
439
439
self ._cls ,
@@ -971,6 +971,15 @@ def dataclass_class_maker_callback(ctx: ClassDefContext) -> bool:
971
971
return transformer .transform ()
972
972
973
973
974
+ def _mangle_internal_sym_name (type_fullname : str , member_name : str ) -> str :
975
+ """Create an internal symbol name with the class name mangled in as usual, but that also
976
+ contains the class module path to avoid false positives when subclassing a dataclass with
977
+ same name."""
978
+ module_name , type_name = type_fullname .rsplit ("." )
979
+ module_name = module_name .replace ("." , "-" )
980
+ type_name = type_name .lstrip ('_' )
981
+ return f"_{ type_name } __{ module_name } __{ member_name } "
982
+
974
983
def _get_transform_spec (reason : Expression ) -> DataclassTransformSpec :
975
984
"""Find the relevant transform parameters from the decorator/parent class/metaclass that
976
985
triggered the dataclasses plugin.
@@ -1029,7 +1038,7 @@ def _get_expanded_dataclasses_fields(
1029
1038
ctx , get_proper_type (typ .upper_bound ), display_typ , parent_typ
1030
1039
)
1031
1040
elif isinstance (typ , Instance ):
1032
- mangled_name = f"_ { typ .type .name . lstrip ( '_' ) } { _INTERNAL_REPLACE_SYM_NAME } "
1041
+ mangled_name = _mangle_internal_sym_name ( typ .type .fullname , _INTERNAL_REPLACE_SYM_NAME )
1033
1042
replace_sym = typ .type .get_method (mangled_name )
1034
1043
if replace_sym is None :
1035
1044
return None
@@ -1114,7 +1123,7 @@ def check_post_init(api: TypeChecker, defn: FuncItem, info: TypeInfo) -> None:
1114
1123
return
1115
1124
assert isinstance (defn .type , FunctionLike )
1116
1125
1117
- mangled_name = f"_ { info .name . lstrip ( '_' ) } { _INTERNAL_POST_INIT_SYM_NAME } "
1126
+ mangled_name = _mangle_internal_sym_name ( info .fullname , _INTERNAL_POST_INIT_SYM_NAME )
1118
1127
ideal_sig_method = info .get_method (mangled_name )
1119
1128
assert ideal_sig_method is not None and ideal_sig_method .type is not None
1120
1129
ideal_sig = ideal_sig_method .type
0 commit comments