Skip to content

Commit f849a98

Browse files
Cleanup for PR: docs, version bump, and pre-compile regex
1 parent bdfa2fc commit f849a98

File tree

4 files changed

+70
-33
lines changed

4 files changed

+70
-33
lines changed

docs/config.md

Lines changed: 64 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,77 @@ that the handler name should be `python_xref` instead of `python`. Because
33
this handler extends the standard [mkdocstrings-python][] handler, the same options are
44
available.
55

6-
Additional options are added by this extension. Currently, there are two:
6+
Additional options are added by this extension. Currently, there are three:
77

8-
* **relative_crossrefs** - if set to true enables use of relative path syntax in
8+
* **relative_crossrefs**: `bool` - if set to true enables use of relative path syntax in
99
cross-references.
1010

11-
* **check_crossrefs** - enables early checking of all cross-references. Note that
11+
* **check_crossrefs**: `bool` - enables early checking of all cross-references. Note that
1212
this option only takes affect if **relative_crossrefs** is also true. This option is
1313
true by default, so this option is used to disable checking. Checking can
1414
also be disabled on a per-case basis by prefixing the reference with '?', e.g.
1515
`[something][?dontcheckme]`.
1616

17-
!!! Example "mkdocs.yml plugins specification using this handler"
18-
19-
```yaml
20-
plugins:
21-
- search
22-
- mkdocstrings:
23-
default_handler: python_xref
24-
handlers:
25-
python_xref:
26-
import:
27-
- https://docs.python.org/3/objects.inv
28-
options:
29-
docstring_style: google
30-
docstring_options:
31-
ignore_init_summary: yes
32-
merge_init_into_class: yes
33-
relative_crossrefs: yes
34-
check_crossrefs: no
35-
separate_signature: yes
36-
show_source: no
37-
show_root_full_path: no
38-
```
17+
* **check_crossrefs_exclude**: `list[str]` - exclude cross-references matching any of these
18+
regex patterns from crossref checking. This option can be used disabling checking on
19+
libraries which are very expensive to import without having to disable checking for all
20+
cross-references.
21+
22+
!!! Example "mkdocs.yml plugins specifications using this handler"
23+
24+
=== "Always check"
25+
26+
!!! warning
27+
28+
Crossrefs to libraries which are expensive to import (e.g., machine learning
29+
frameworks) can cause very slow build times when checked!
30+
31+
```yaml
32+
plugins:
33+
- mkdocstrings:
34+
default_handler: python_xref
35+
handlers:
36+
python_xref:
37+
import:
38+
- https://docs.python.org/3/objects.inv
39+
- https://pytorch.org/docs/stable/objects.inv
40+
options:
41+
relative_crossrefs: yes
42+
```
43+
44+
=== "Check all but listed exclusions"
45+
46+
```yaml
47+
plugins:
48+
- mkdocstrings:
49+
default_handler: python_xref
50+
handlers:
51+
python_xref:
52+
import:
53+
- https://docs.python.org/3/objects.inv
54+
- https://pytorch.org/docs/stable/objects.inv
55+
options:
56+
relative_crossrefs: yes
57+
check_crossrefs_exclude:
58+
- "^torch\\.(.*)"
59+
```
60+
61+
=== "Never check"
62+
63+
```yaml
64+
plugins:
65+
- mkdocstrings:
66+
default_handler: python_xref
67+
handlers:
68+
python_xref:
69+
import:
70+
- https://docs.python.org/3/objects.inv
71+
- https://pytorch.org/docs/stable/objects.inv
72+
options:
73+
relative_crossrefs: yes
74+
check_crossrefs: no
75+
```
76+
77+
3978

4079
[mkdocstrings-python]: https://mkdocstrings.github.io/python/

docs/index.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ but has not yet been accepted.
105105

106106
If `relative_crossrefs` and `check_crossrefs` are both enabled (the latter is true by default),
107107
then all cross-reference expressions will be checked to ensure that they exist and failures
108-
will be reported with the source location. Otherwise, missing cross-references will be reported
108+
will be reported with the source location (with the exception of any crossrefs which match a pattern in `check_crossrefs_exclude`). Otherwise, missing cross-references will be reported
109109
by mkdocstrings without the source location, in which case it is often difficult to locate the source
110110
of the error. Note that the errors generated by this feature are in addition to the errors
111111
from mkdocstrings.
@@ -121,5 +121,3 @@ This function returns a [Path][?pathlib.] instance.
121121
[mkdocstrings]: https://mkdocstrings.github.io/
122122
[mkdocstrings_python]: https://mkdocstrings.github.io/python/
123123
[relative-crossref-issue]: https://github.com/mkdocstrings/python/issues/27
124-
125-
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.16.2
1+
1.16.3

src/mkdocstrings_handlers/python_xref/handler.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
@dataclass(**_dataclass_options)
4646
class PythonRelXRefOptions(PythonOptions):
4747
check_crossrefs: bool = True
48-
check_crossrefs_exclude: list[str] = field(default_factory=list)
48+
check_crossrefs_exclude: list[str | re.Pattern] = field(default_factory=list)
4949

5050
class PythonRelXRefHandler(PythonHandler):
5151
"""Extended version of mkdocstrings Python handler
@@ -66,8 +66,8 @@ def __init__(self, config: PythonConfig, base_dir: Path, **kwargs: Any) -> None:
6666
**kwargs: Arguments passed to the parent constructor.
6767
"""
6868
self.check_crossrefs = config.options.pop('check_crossrefs', None)
69-
self.check_crossrefs_exclude = config.options.pop(
70-
'check_crossrefs_exclude', [])
69+
exclude = config.options.pop('check_crossrefs_exclude', [])
70+
self.check_crossrefs_exclude = [re.compile(p) for p in exclude]
7171
super().__init__(config, base_dir, **kwargs)
7272

7373
def get_options(self, local_options: Mapping[str, Any]) -> PythonRelXRefOptions:
@@ -105,7 +105,7 @@ def get_templates_dir(self, handler: Optional[str] = None) -> Path:
105105
handler = 'python'
106106
return super().get_templates_dir(handler)
107107

108-
def _check_ref(self, ref : str, exclude: list[str] = []) -> bool:
108+
def _check_ref(self, ref : str, exclude: list[str | re.Pattern] = []) -> bool:
109109
"""Check for existence of reference"""
110110
for ex in exclude:
111111
if re.match(ex, ref):

0 commit comments

Comments
 (0)