File tree Expand file tree Collapse file tree 2 files changed +9
-7
lines changed Expand file tree Collapse file tree 2 files changed +9
-7
lines changed Original file line number Diff line number Diff line change @@ -121,18 +121,20 @@ def reference_ancestor(
121
121
"""
122
122
Returns a reference to a python type in a package which is an ancestor to the current package,
123
123
and adds the required import that is aliased (if possible) to avoid name conflicts.
124
+
125
+ Adds trailing __ to avoid name mangling (python.org/dev/peps/pep-0008/#id34).
124
126
"""
125
127
distance_up = len (current_package ) - len (py_package )
126
128
if py_package :
127
129
string_import = py_package [- 1 ]
128
- # Add trailing __ to avoid name mangling (python.org/dev/peps/pep-0008/#id34)
129
130
string_alias = f"_{ '_' * distance_up } { string_import } __"
130
131
string_from = f"..{ '.' * distance_up } "
131
132
imports .add (f"from { string_from } import { string_import } as { string_alias } " )
132
133
return f"{ string_alias } .{ py_type } "
133
134
else :
134
- imports .add (f"from .{ '.' * distance_up } import { py_type } " )
135
- return py_type
135
+ string_alias = f"{ '_' * distance_up } { py_type } __"
136
+ imports .add (f"from .{ '.' * distance_up } import { py_type } as { string_alias } " )
137
+ return string_alias
136
138
137
139
138
140
def reference_cousin (
Original file line number Diff line number Diff line change @@ -214,8 +214,8 @@ def test_reference_root_package_from_child():
214
214
package = "package.child" , imports = imports , source_type = "Message"
215
215
)
216
216
217
- assert imports == {"from ... import Message" }
218
- assert name == "Message "
217
+ assert imports == {"from ... import Message as __Message__ " }
218
+ assert name == "__Message__ "
219
219
220
220
221
221
def test_reference_root_package_from_deeply_nested_child ():
@@ -224,8 +224,8 @@ def test_reference_root_package_from_deeply_nested_child():
224
224
package = "package.deeply.nested.child" , imports = imports , source_type = "Message"
225
225
)
226
226
227
- assert imports == {"from ..... import Message" }
228
- assert name == "Message "
227
+ assert imports == {"from ..... import Message as ____Message__ " }
228
+ assert name == "____Message__ "
229
229
230
230
231
231
def test_reference_unrelated_package ():
You can’t perform that action at this time.
0 commit comments