Skip to content

Commit bdfa2fc

Browse files
Change check_crossrefs to be stored as a class attribute instead of via global_config, and add a blacklist option
1 parent 1bf9d1d commit bdfa2fc

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

src/mkdocstrings_handlers/python_xref/handler.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717

1818
from __future__ import annotations
1919

20+
import re
2021
import sys
21-
from dataclasses import dataclass, fields
22+
from dataclasses import dataclass, field, fields
23+
from functools import partial
2224
from pathlib import Path
2325
from typing import Any, ClassVar, Mapping, MutableMapping, Optional
2426
from warnings import warn
@@ -43,6 +45,7 @@
4345
@dataclass(**_dataclass_options)
4446
class PythonRelXRefOptions(PythonOptions):
4547
check_crossrefs: bool = True
48+
check_crossrefs_exclude: list[str] = field(default_factory=list)
4649

4750
class PythonRelXRefHandler(PythonHandler):
4851
"""Extended version of mkdocstrings Python handler
@@ -62,26 +65,30 @@ def __init__(self, config: PythonConfig, base_dir: Path, **kwargs: Any) -> None:
6265
base_dir: The base directory of the project.
6366
**kwargs: Arguments passed to the parent constructor.
6467
"""
65-
check_crossrefs = config.options.pop('check_crossrefs', None) # Remove
68+
self.check_crossrefs = config.options.pop('check_crossrefs', None)
69+
self.check_crossrefs_exclude = config.options.pop(
70+
'check_crossrefs_exclude', [])
6671
super().__init__(config, base_dir, **kwargs)
67-
if check_crossrefs is not None:
68-
self.global_options["check_crossrefs"] = check_crossrefs
6972

7073
def get_options(self, local_options: Mapping[str, Any]) -> PythonRelXRefOptions:
7174
local_options = dict(local_options)
72-
check_crossrefs = local_options.pop('check_crossrefs', None)
75+
check_crossrefs = local_options.pop(
76+
'check_crossrefs', self.check_crossrefs)
77+
check_crossrefs_exclude = local_options.pop(
78+
'check_crossrefs_exclude', self.check_crossrefs_exclude)
7379
_opts = super().get_options(local_options)
7480
opts = PythonRelXRefOptions(
81+
check_crossrefs=check_crossrefs,
82+
check_crossrefs_exclude=check_crossrefs_exclude,
7583
**{field.name: getattr(_opts, field.name) for field in fields(_opts)}
7684
)
77-
if check_crossrefs is not None:
78-
opts.check_crossrefs = bool(check_crossrefs)
7985
return opts
8086

8187
def render(self, data: CollectorItem, options: PythonOptions) -> str:
8288
if options.relative_crossrefs:
83-
if isinstance(options, PythonRelXRefOptions):
84-
checkref = self._check_ref if options.check_crossrefs else None
89+
if isinstance(options, PythonRelXRefOptions) and options.check_crossrefs:
90+
checkref = partial(
91+
self._check_ref, exclude=options.check_crossrefs_exclude)
8592
else:
8693
checkref = None
8794
substitute_relative_crossrefs(data, checkref=checkref)
@@ -98,8 +105,11 @@ def get_templates_dir(self, handler: Optional[str] = None) -> Path:
98105
handler = 'python'
99106
return super().get_templates_dir(handler)
100107

101-
def _check_ref(self, ref:str) -> bool:
108+
def _check_ref(self, ref : str, exclude: list[str] = []) -> bool:
102109
"""Check for existence of reference"""
110+
for ex in exclude:
111+
if re.match(ex, ref):
112+
return True
103113
try:
104114
self.collect(ref, PythonOptions())
105115
return True

0 commit comments

Comments
 (0)