Skip to content

Commit 6f8e87d

Browse files
committed
Make sure Python code in documentation is formatted with Python, fixes #165
1 parent ce3b396 commit 6f8e87d

File tree

2 files changed

+38
-36
lines changed

2 files changed

+38
-36
lines changed

crates/zuban_python/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,7 @@ impl<'project> Document<'project> {
427427
docs
428428
} else {
429429
let mut out = String::default();
430+
out += "```python\n";
430431
if !declaration_kinds.is_empty() {
431432
out.push('(');
432433
out += &declaration_kinds.join(", ");
@@ -456,6 +457,7 @@ impl<'project> Document<'project> {
456457
}
457458
}
458459
out += &types.join(" | ");
460+
out += "\n```";
459461
if !results.is_empty() {
460462
out += "\n---\n";
461463
out += &docs;

crates/zuban_python/tests/mypylike/tests/documentation.test

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ x
2020
1
2121

2222
[out]
23-
__main__.py:8:documentation -> "(param) x: A | B\n---\na doc\n\nb doc"
24-
__main__.py:10:documentation -> "(param) a: A\n---\na doc"
23+
__main__.py:8:documentation -> "```python\n(param) x: A | B\n```\n---\na doc\n\nb doc"
24+
__main__.py:10:documentation -> "```python\n(param) a: A\n```\n---\na doc"
2525
__main__.py:12:documentation -> "a doc"
26-
__main__.py:15:documentation -> "(variable) x: int"
27-
__main__.py:17:documentation -> "(variable) x: int"
26+
__main__.py:15:documentation -> "```python\n(variable) x: int\n```"
27+
__main__.py:17:documentation -> "```python\n(variable) x: int\n```"
2828
__main__.py:19:documentation -> No docs found
2929

3030
[case docs_class]
@@ -45,10 +45,10 @@ WithGeneric
4545
WithoutGeneric
4646

4747
[out]
48-
__main__.py:2:documentation -> "(class) WithGeneric\n---\nwith docs"
49-
__main__.py:7:documentation -> "(class) WithoutGeneric\n---\nwithout docs"
50-
__main__.py:13:documentation -> "(class) WithGeneric\n---\nwith docs"
51-
__main__.py:15:documentation -> "(class) WithoutGeneric\n---\nwithout docs"
48+
__main__.py:2:documentation -> "```python\n(class) WithGeneric\n```\n---\nwith docs"
49+
__main__.py:7:documentation -> "```python\n(class) WithoutGeneric\n```\n---\nwithout docs"
50+
__main__.py:13:documentation -> "```python\n(class) WithGeneric\n```\n---\nwith docs"
51+
__main__.py:15:documentation -> "```python\n(class) WithoutGeneric\n```\n---\nwithout docs"
5252

5353
[case docs_alias]
5454

@@ -64,10 +64,10 @@ OtherAlias = str
6464
def bar() -> OtherAlias: ...
6565

6666
[out]
67-
__main__.py:3:documentation -> "(type) Alias = int"
68-
__main__.py:5:documentation -> "(type) Alias = int"
69-
__main__.py:8:documentation -> "(variable) OtherAlias: type[str]"
70-
__main__.py:11:documentation -> "(variable) OtherAlias: type[str]"
67+
__main__.py:3:documentation -> "```python\n(type) Alias = int"
68+
__main__.py:5:documentation -> "```python\n(type) Alias = int"
69+
__main__.py:8:documentation -> "```python\n(variable) OtherAlias: type[str]"
70+
__main__.py:11:documentation -> "```python\n(variable) OtherAlias: type[str]"
7171

7272
[case docs_function_mypy_compatible]
7373
# flags: --mypy-compatible
@@ -86,10 +86,10 @@ typed
8686
untyped
8787

8888
[out]
89-
__main__.py:3:documentation -> "(function) def [T] typed(x: list[T]) -> T\n---\ntyped doc"
90-
__main__.py:7:documentation -> "(function) def untyped(x: Any) -> Any\n---\nuntyped doc"
91-
__main__.py:12:documentation -> "(function) def [T] typed(x: list[T]) -> T\n---\ntyped doc"
92-
__main__.py:14:documentation -> "(function) def untyped(x: Any) -> Any\n---\nuntyped doc"
89+
__main__.py:3:documentation -> "```python\n(function) def [T] typed(x: list[T]) -> T\n```\n---\ntyped doc"
90+
__main__.py:7:documentation -> "```python\n(function) def untyped(x: Any) -> Any\n```\n---\nuntyped doc"
91+
__main__.py:12:documentation -> "```python\n(function) def [T] typed(x: list[T]) -> T\n```\n---\ntyped doc"
92+
__main__.py:14:documentation -> "```python\n(function) def untyped(x: Any) -> Any\n```\n---\nuntyped doc"
9393

9494
[case docs_function_no_mypy_compatible]
9595
# flags: --no-mypy-compatible
@@ -108,10 +108,10 @@ typed
108108
untyped
109109

110110
[out]
111-
__main__.py:3:documentation -> "(function) def [T] typed(x: list[T]) -> T\n---\ntyped doc"
112-
__main__.py:7:documentation -> "(function) def [T1: Any] untyped(x: T1) -> T1\n---\nuntyped doc"
113-
__main__.py:12:documentation -> "(function) def [T] typed(x: list[T]) -> T\n---\ntyped doc"
114-
__main__.py:14:documentation -> "(function) def [T1: Any] untyped(x: T1) -> T1\n---\nuntyped doc"
111+
__main__.py:3:documentation -> "```python\n(function) def [T] typed(x: list[T]) -> T\n```\n---\ntyped doc"
112+
__main__.py:7:documentation -> "```python\n(function) def [T1: Any] untyped(x: T1) -> T1\n```\n---\nuntyped doc"
113+
__main__.py:12:documentation -> "```python\n(function) def [T] typed(x: list[T]) -> T\n```\n---\ntyped doc"
114+
__main__.py:14:documentation -> "```python\n(function) def [T1: Any] untyped(x: T1) -> T1\n```\n---\nuntyped doc"
115115

116116
[case docs_property]
117117
class X:
@@ -136,12 +136,12 @@ X.prop
136136
X().prop
137137

138138
[out]
139-
__main__.py:3:documentation -> "(function) def [T] func(self, x: list[T]) -> T\n---\nfunc doc"
140-
__main__.py:9:documentation -> "(property) def prop(self) -> int\n---\nproperty doc"
141-
__main__.py:14:documentation -> "(function) def [T] func(self, x: list[T]) -> T\n---\nfunc doc"
142-
__main__.py:16:documentation -> "(function) def [T] func(x: list[T]) -> T\n---\nfunc doc"
143-
__main__.py:18:documentation -> "(function) prop: property"
144-
__main__.py:20:documentation -> "(property) prop: int"
139+
__main__.py:3:documentation -> "```python\n(function) def [T] func(self, x: list[T]) -> T\n```\n---\nfunc doc"
140+
__main__.py:9:documentation -> "```python\n(property) def prop(self) -> int\n```\n---\nproperty doc"
141+
__main__.py:14:documentation -> "```python\n(function) def [T] func(self, x: list[T]) -> T\n```\n---\nfunc doc"
142+
__main__.py:16:documentation -> "```python\n(function) def [T] func(x: list[T]) -> T\n```\n---\nfunc doc"
143+
__main__.py:18:documentation -> "```python\n(function) prop: property\n```"
144+
__main__.py:20:documentation -> "```python\n(property) prop: int\n```"
145145

146146
[case docs_classmethod_staticmethod]
147147
class X:
@@ -167,12 +167,12 @@ X.staticm
167167
X().staticm
168168

169169
[out]
170-
__main__.py:4:documentation -> "(function) def classm(cls, cls) -> int\n---\nclassm doc"
171-
__main__.py:10:documentation -> "(function) def staticm(x: str) -> int\n---\nstaticm doc"
172-
__main__.py:15:documentation -> "(function) def classm(cls) -> int\n---\nclassm doc"
173-
__main__.py:17:documentation -> "(function) def classm(cls) -> int\n---\nclassm doc"
174-
__main__.py:19:documentation -> "(function) def staticm(x: str) -> int\n---\nstaticm doc"
175-
__main__.py:21:documentation -> "(function) def staticm(x: str) -> int\n---\nstaticm doc"
170+
__main__.py:4:documentation -> "```python\n(function) def classm(cls, cls) -> int\n```\n---\nclassm doc"
171+
__main__.py:10:documentation -> "```python\n(function) def staticm(x: str) -> int\n```\n---\nstaticm doc"
172+
__main__.py:15:documentation -> "```python\n(function) def classm(cls) -> int\n```\n---\nclassm doc"
173+
__main__.py:17:documentation -> "```python\n(function) def classm(cls) -> int\n```\n---\nclassm doc"
174+
__main__.py:19:documentation -> "```python\n(function) def staticm(x: str) -> int\n```\n---\nstaticm doc"
175+
__main__.py:21:documentation -> "```python\n(function) def staticm(x: str) -> int\n```\n---\nstaticm doc"
176176

177177
[case docs_goto_on_operator]
178178
class X:
@@ -192,9 +192,9 @@ def foo(a: X, b: X):
192192
1 | 1
193193

194194
[out]
195-
__main__.py:11:documentation -> "(function) def __eq__(other: object) -> bool\n---\nHello world"
196-
__main__.py:13:documentation -> "(function) def __and__(other: X) -> X"
197-
__main__.py:15:documentation -> "(function) def __or__(int, /) -> int"
195+
__main__.py:11:documentation -> "```python\n(function) def __eq__(other: object) -> bool\n```\n---\nHello world"
196+
__main__.py:13:documentation -> "```python\n(function) def __and__(other: X) -> X\n```"
197+
__main__.py:15:documentation -> "```python\n(function) def __or__(int, /) -> int\n```"
198198

199199
[case docs_on_overload]
200200
# From GH #162
@@ -211,4 +211,4 @@ def f(x: str | int) -> str | int: ...
211211
f(1)
212212

213213
[out]
214-
__main__.py:12:documentation -> "(function) Overload(def f(x: int) -> int, def f(x: str) -> str)"
214+
__main__.py:12:documentation -> "```python\n(function) Overload(def f(x: int) -> int, def f(x: str) -> str)\n```"

0 commit comments

Comments
 (0)