4
4
import re
5
5
import sys
6
6
import textwrap
7
- from types import ModuleType , FunctionType
8
7
from typing import TYPE_CHECKING
9
8
from pathlib import Path , PurePosixPath
10
9
from importlib import import_module
11
- from dataclasses import field , dataclass
12
10
13
11
import pytest
14
12
from sphinx .config import Config
15
- from legacy_api_wrap import legacy_api
16
13
17
- import scanpydoc
18
- from scanpydoc import rtd_github_links
19
14
from scanpydoc .rtd_github_links import (
15
+ _testdata ,
20
16
github_url ,
21
17
_infer_vars ,
22
18
_get_linenos ,
19
+ _module_path ,
23
20
_get_obj_module ,
24
21
)
25
22
from scanpydoc .rtd_github_links ._linkcode import CInfo , PyInfo , linkcode_resolve
26
23
27
24
28
25
if TYPE_CHECKING :
26
+ from types import ModuleType
29
27
from typing import Literal
30
- from collections .abc import Callable , Generator
28
+ from collections .abc import Callable
31
29
32
30
from sphinx .application import Sphinx
33
31
from _pytest .monkeypatch import MonkeyPatch
@@ -123,7 +121,7 @@ def test_app(monkeypatch: MonkeyPatch, make_app_setup: Callable[..., Sphinx]) ->
123
121
),
124
122
)
125
123
assert app .config ["linkcode_resolve" ] is linkcode_resolve
126
- assert filters == dict (github_url = rtd_github_links . github_url )
124
+ assert filters == dict (github_url = github_url )
127
125
128
126
129
127
@pytest .mark .parametrize (
@@ -150,55 +148,11 @@ def test_as_function(
150
148
assert github_url (f"scanpydoc.{ module } .{ name } " ) == f"{ prefix } /{ obj_path } #L{ s } -L{ e } "
151
149
152
150
153
- class _TestMod :
154
- modname = "testing.scanpydoc"
155
-
156
- @dataclass
157
- class TestDataCls :
158
- test_attr : dict [str , str ] = field (default_factory = dict )
159
-
160
- class TestCls :
161
- test_anno : int
162
-
163
- def test_func (self ) -> None : # pragma: no cover
164
- pass
165
-
166
- test_func_wrap : Callable [[], None ] # is set below
167
-
168
-
169
- @pytest .fixture ()
170
- def test_mod () -> Generator [ModuleType , None , None ]:
171
- mod = sys .modules [_TestMod .modname ] = ModuleType (_TestMod .modname )
172
- mod .__file__ = str (
173
- Path (scanpydoc .__file__ ).parent .parent
174
- / Path (* _TestMod .modname .split ("." ))
175
- / "__init__.py"
176
- )
177
- for name , obj in vars (_TestMod ).items ():
178
- if not isinstance (obj , (type , FunctionType )):
179
- continue
180
- # pretend things are in the same module
181
- if "test_rtd_github_links" in obj .__module__ :
182
- obj .__module__ = _TestMod .modname
183
- setattr (mod , name , obj )
184
-
185
- mod .test_func_wrap = legacy_api ()(mod .test_func ) # type: ignore[attr-defined]
186
-
187
- try :
188
- yield mod
189
- finally :
190
- sys .modules .pop (_TestMod .modname , None )
191
-
192
-
193
- def test_get_github_url_only_annotation (
194
- prefix : PurePosixPath ,
195
- test_mod : ModuleType , # noqa: ARG001
196
- ) -> None :
151
+ def test_get_github_url_only_annotation (prefix : PurePosixPath ) -> None :
197
152
"""Doesn’t really work but shouldn’t crash either."""
198
- url = github_url (f"{ _TestMod .modname } .TestCls.test_anno" )
199
- assert url == str (
200
- prefix .parent / Path (* _TestMod .modname .split ("." )) / "__init__.py"
201
- )
153
+ url = github_url (f"{ _testdata .__name__ } .TestCls.test_anno" )
154
+ path = prefix .parent / Path (* _testdata .__name__ .split ("." ))
155
+ assert url == str (path .with_suffix (".py" ))
202
156
203
157
204
158
def test_get_github_url_error () -> None :
@@ -209,50 +163,56 @@ def test_get_github_url_error() -> None:
209
163
210
164
211
165
@pytest .mark .parametrize (
212
- ("obj_path" , "get_obj " , "get_mod " ),
166
+ ("obj_path" , "obj " , "mod" , "path_expected " ),
213
167
[
214
168
pytest .param (
215
- "scanpydoc.indent" ,
216
- lambda _ : textwrap .indent ,
217
- lambda _ : textwrap ,
218
- id = "reexport" ,
169
+ "scanpydoc.indent" , textwrap .indent , textwrap , "textwrap.py" , id = "reexport"
219
170
),
220
171
pytest .param (
221
- "testing.scanpydoc.test_func" ,
222
- lambda m : m .test_func ,
223
- lambda m : m ,
172
+ "scanpydoc.rtd_github_links._testdata.test_func" ,
173
+ _testdata .test_func ,
174
+ _testdata ,
175
+ "scanpydoc/rtd_github_links/_testdata.py" ,
224
176
id = "func" ,
225
177
),
226
178
pytest .param (
227
- "testing.scanpydoc.test_func_wrap" ,
228
- lambda m : m .test_func_wrap ,
229
- lambda m : m ,
179
+ "scanpydoc.rtd_github_links._testdata.test_func_wrap" ,
180
+ _testdata .test_func_wrap ,
181
+ _testdata ,
182
+ "scanpydoc/rtd_github_links/_testdata.py" ,
230
183
id = "wrapper" ,
231
184
),
232
- pytest .param ("testing.scanpydoc" , lambda m : m , lambda m : m , id = "mod" ),
233
185
pytest .param (
234
- "testing.scanpydoc.TestDataCls.test_attr" ,
235
- lambda m : m .TestDataCls .__dataclass_fields__ ["test_attr" ],
236
- lambda m : m ,
186
+ "scanpydoc.rtd_github_links._testdata" ,
187
+ _testdata ,
188
+ _testdata ,
189
+ "scanpydoc/rtd_github_links/_testdata.py" ,
190
+ id = "mod" ,
191
+ ),
192
+ pytest .param (
193
+ "scanpydoc.rtd_github_links._testdata.TestDataCls.test_attr" ,
194
+ _testdata .TestDataCls .__dataclass_fields__ ["test_attr" ],
195
+ _testdata ,
196
+ "scanpydoc/rtd_github_links/_testdata.py" ,
237
197
id = "dataclass_field" ,
238
198
),
239
199
pytest .param (
240
- "testing.scanpydoc.TestCls.test_anno" ,
241
- lambda _ : None ,
242
- lambda m : m ,
200
+ "scanpydoc.rtd_github_links._testdata.TestCls.test_anno" ,
201
+ None ,
202
+ _testdata ,
203
+ "scanpydoc/rtd_github_links/_testdata.py" ,
243
204
id = "anno" ,
244
205
),
245
206
],
246
207
)
247
- def test_get_obj_module (
248
- test_mod : ModuleType ,
249
- obj_path : str ,
250
- get_obj : Callable [[ModuleType ], object ],
251
- get_mod : Callable [[ModuleType ], ModuleType ],
208
+ def test_get_obj_module_path (
209
+ obj_path : str , obj : object , mod : ModuleType , path_expected : PurePosixPath
252
210
) -> None :
253
211
obj_rcv , mod_rcv = _get_obj_module (obj_path )
254
- assert obj_rcv is get_obj (test_mod )
255
- assert mod_rcv is get_mod (test_mod )
212
+ assert obj_rcv is obj
213
+ assert mod_rcv is mod
214
+ path = _module_path (obj_rcv , mod_rcv )
215
+ assert path == PurePosixPath (path_expected )
256
216
257
217
258
218
def test_linkdoc (prefix : PurePosixPath ) -> None :
0 commit comments