Skip to content

Commit fbde890

Browse files
pylint-backport-bot[bot]JulfriedPierre-Sassoulas
authored
Fix Mermaid printer rendering double underscores as bold formatting (#10403) (#10410)
(cherry picked from commit 25a0f9e) Co-authored-by: Julian Grimm <[email protected]> Co-authored-by: Pierre Sassoulas <[email protected]>
1 parent 51234e5 commit fbde890

File tree

5 files changed

+40
-22
lines changed

5 files changed

+40
-22
lines changed

doc/whatsnew/fragments/10402.bugfix

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix double underscores erroneously rendering as bold in pyreverse's Mermaid output.
2+
3+
Closes #10402

pylint/pyreverse/mermaidjs_printer.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ def _open_graph(self) -> None:
3232
self.emit("classDiagram")
3333
self._inc_indent()
3434

35+
def _escape_mermaid_text(self, text: str) -> str:
36+
"""Escape characters that conflict with Markdown formatting."""
37+
text = text.replace("__", r"\_\_") # Double underscore → escaped
38+
return text
39+
3540
def emit_node(
3641
self,
3742
name: str,
@@ -48,14 +53,24 @@ def emit_node(
4853
nodetype = self.NODES[type_]
4954
body = []
5055
if properties.attrs:
51-
body.extend(properties.attrs)
56+
# Escape attribute names to prevent Markdown formatting issues
57+
escaped_attrs = [
58+
self._escape_mermaid_text(attr) for attr in properties.attrs
59+
]
60+
body.extend(escaped_attrs)
5261
if properties.methods:
5362
for func in properties.methods:
5463
args = self._get_method_arguments(func)
55-
line = f"{func.name}({', '.join(args)})"
64+
# Escape method name and arguments
65+
escaped_method_name = self._escape_mermaid_text(func.name)
66+
escaped_args = [self._escape_mermaid_text(arg) for arg in args]
67+
line = f"{escaped_method_name}({', '.join(escaped_args)})"
5668
line += "*" if func.is_abstract() else ""
5769
if func.returns:
58-
line += f" {get_annotation_label(func.returns)}"
70+
# Escape return type annotation
71+
return_type = get_annotation_label(func.returns)
72+
escaped_return_type = self._escape_mermaid_text(return_type)
73+
line += f" {escaped_return_type}"
5974
body.append(line)
6075
name = name.split(".")[-1]
6176
self.emit(f"{nodetype} {name} {{")
@@ -77,7 +92,7 @@ def emit_edge(
7792
to_node = to_node.split(".")[-1]
7893
edge = f"{from_node} {self.ARROWS[type_]} {to_node}"
7994
if label:
80-
edge += f" : {label}"
95+
edge += f" : {self._escape_mermaid_text(label)}"
8196
self.emit(edge)
8297

8398
def _close_graph(self) -> None:
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
classDiagram
22
class P {
33
name : str
4-
__init__(name: str)
4+
\_\_init\_\_(name: str)
55
}
66
class PrivateAttr {
7-
__x
8-
__init__()
7+
\_\_x
8+
\_\_init\_\_()
99
}
1010
class ProtectedAttr {
1111
_x
12-
__init__()
12+
\_\_init\_\_()
1313
}
1414
class PublicAttr {
1515
x
16-
__init__()
16+
\_\_init\_\_()
1717
}
1818
class SpecialAttr {
19-
__x__
20-
__init__()
19+
\_\_x\_\_
20+
\_\_init\_\_()
2121
}
22-
P --* PrivateAttr : __x
22+
P --* PrivateAttr : \_\_x
2323
P --* ProtectedAttr : _x
2424
P --* PublicAttr : x
25-
P --* SpecialAttr : __x__
25+
P --* SpecialAttr : \_\_x\_\_
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
classDiagram
22
class P {
33
name : str
4-
__init__(name: str)
4+
\_\_init\_\_(name: str)
55
}
66
class PrivateAttr {
7-
__init__()
7+
\_\_init\_\_()
88
}
99
class ProtectedAttr {
10-
__init__()
10+
\_\_init\_\_()
1111
}
1212
class PublicAttr {
1313
x
14-
__init__()
14+
\_\_init\_\_()
1515
}
1616
class SpecialAttr {
17-
__x__
18-
__init__()
17+
\_\_x\_\_
18+
\_\_init\_\_()
1919
}
2020
P --* PublicAttr : x
21-
P --* SpecialAttr : __x__
21+
P --* SpecialAttr : \_\_x\_\_

tests/pyreverse/functional/class_diagrams/aggregation_filtering/special.mmd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ classDiagram
33
name : str
44
}
55
class PrivateAttr {
6-
__x
6+
\_\_x
77
}
88
class ProtectedAttr {
99
_x
@@ -13,6 +13,6 @@ classDiagram
1313
}
1414
class SpecialAttr {
1515
}
16-
P --* PrivateAttr : __x
16+
P --* PrivateAttr : \_\_x
1717
P --* ProtectedAttr : _x
1818
P --* PublicAttr : x

0 commit comments

Comments
 (0)