9191 frozen_default = False ,
9292 field_specifiers = DATACLASS_FIELD_SPECIFIERS ,
9393)
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"
9696
9797
9898class DataclassAttribute :
@@ -422,7 +422,7 @@ def _add_internal_replace_method(self, attributes: list[DataclassAttribute]) ->
422422 Stashes the signature of 'dataclasses.replace(...)' for this specific dataclass
423423 to be used later whenever 'dataclasses.replace' is called for this dataclass.
424424 """
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 )
426426 add_method_to_class (
427427 self ._api ,
428428 self ._cls ,
@@ -433,7 +433,7 @@ def _add_internal_replace_method(self, attributes: list[DataclassAttribute]) ->
433433 )
434434
435435 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 )
437437 add_method_to_class (
438438 self ._api ,
439439 self ._cls ,
@@ -971,6 +971,15 @@ def dataclass_class_maker_callback(ctx: ClassDefContext) -> bool:
971971 return transformer .transform ()
972972
973973
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+
974983def _get_transform_spec (reason : Expression ) -> DataclassTransformSpec :
975984 """Find the relevant transform parameters from the decorator/parent class/metaclass that
976985 triggered the dataclasses plugin.
@@ -1029,7 +1038,7 @@ def _get_expanded_dataclasses_fields(
10291038 ctx , get_proper_type (typ .upper_bound ), display_typ , parent_typ
10301039 )
10311040 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 )
10331042 replace_sym = typ .type .get_method (mangled_name )
10341043 if replace_sym is None :
10351044 return None
@@ -1114,7 +1123,7 @@ def check_post_init(api: TypeChecker, defn: FuncItem, info: TypeInfo) -> None:
11141123 return
11151124 assert isinstance (defn .type , FunctionLike )
11161125
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 )
11181127 ideal_sig_method = info .get_method (mangled_name )
11191128 assert ideal_sig_method is not None and ideal_sig_method .type is not None
11201129 ideal_sig = ideal_sig_method .type
0 commit comments