17
17
18
18
from __future__ import annotations
19
19
20
+ import re
20
21
import sys
21
- from dataclasses import dataclass , fields
22
+ from dataclasses import dataclass , field , fields
23
+ from functools import partial
22
24
from pathlib import Path
23
25
from typing import Any , ClassVar , Mapping , MutableMapping , Optional
24
26
from warnings import warn
43
45
@dataclass (** _dataclass_options )
44
46
class PythonRelXRefOptions (PythonOptions ):
45
47
check_crossrefs : bool = True
48
+ check_crossrefs_exclude : list [str ] = field (default_factory = list )
46
49
47
50
class PythonRelXRefHandler (PythonHandler ):
48
51
"""Extended version of mkdocstrings Python handler
@@ -62,26 +65,30 @@ def __init__(self, config: PythonConfig, base_dir: Path, **kwargs: Any) -> None:
62
65
base_dir: The base directory of the project.
63
66
**kwargs: Arguments passed to the parent constructor.
64
67
"""
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' , [])
66
71
super ().__init__ (config , base_dir , ** kwargs )
67
- if check_crossrefs is not None :
68
- self .global_options ["check_crossrefs" ] = check_crossrefs
69
72
70
73
def get_options (self , local_options : Mapping [str , Any ]) -> PythonRelXRefOptions :
71
74
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 )
73
79
_opts = super ().get_options (local_options )
74
80
opts = PythonRelXRefOptions (
81
+ check_crossrefs = check_crossrefs ,
82
+ check_crossrefs_exclude = check_crossrefs_exclude ,
75
83
** {field .name : getattr (_opts , field .name ) for field in fields (_opts )}
76
84
)
77
- if check_crossrefs is not None :
78
- opts .check_crossrefs = bool (check_crossrefs )
79
85
return opts
80
86
81
87
def render (self , data : CollectorItem , options : PythonOptions ) -> str :
82
88
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 )
85
92
else :
86
93
checkref = None
87
94
substitute_relative_crossrefs (data , checkref = checkref )
@@ -98,8 +105,11 @@ def get_templates_dir(self, handler: Optional[str] = None) -> Path:
98
105
handler = 'python'
99
106
return super ().get_templates_dir (handler )
100
107
101
- def _check_ref (self , ref : str ) -> bool :
108
+ def _check_ref (self , ref : str , exclude : list [ str ] = [] ) -> bool :
102
109
"""Check for existence of reference"""
110
+ for ex in exclude :
111
+ if re .match (ex , ref ):
112
+ return True
103
113
try :
104
114
self .collect (ref , PythonOptions ())
105
115
return True
0 commit comments