-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathconf.py
320 lines (275 loc) · 10.7 KB
/
conf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
#
"""Sphinx configuration for MLOS documentation."""
# pylint: disable=invalid-name
# Configuration file for the Sphinx documentation builder.
#
# This file only contains a selection of the most common options. For a full
# list see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html
# -- Path setup --------------------------------------------------------------
# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#
import json
import os
import sys
from logging import warning
from docutils.nodes import Element
from intersphinx_registry import get_intersphinx_mapping
from sphinx.addnodes import pending_xref
from sphinx.application import Sphinx as SphinxApp
from sphinx.environment import BuildEnvironment
# Note: doc requirements aren't installed by default.
# To install them, run `pip install -r doc/requirements.txt`
sys.path.insert(0, os.path.abspath("../../mlos_core/mlos_core"))
sys.path.insert(1, os.path.abspath("../../mlos_bench/mlos_bench"))
sys.path.insert(1, os.path.abspath("../../mlos_viz/mlos_viz"))
# -- Project information -----------------------------------------------------
project = "MLOS"
copyright = "2024, Microsoft GSL" # pylint: disable=redefined-builtin
author = "Microsoft GSL"
# The full version, including alpha/beta/rc tags
try:
from version import VERSION
except ImportError:
VERSION = "0.0.1-dev"
warning(f"version.py not found, using dummy VERSION={VERSION}")
try:
from setuptools_scm import get_version
version = get_version(root="../..", relative_to=__file__, fallback_version=VERSION)
if version is not None:
VERSION = version
except ImportError:
warning("setuptools_scm not found, using VERSION {VERSION}")
except LookupError as e:
warning(f"setuptools_scm failed to find git version, using VERSION {VERSION}: {e}")
release = VERSION
# -- General configuration ---------------------------------------------------
# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
"sphinx.ext.autodoc",
"autoapi.extension",
"nbsphinx",
"sphinx.ext.intersphinx",
"sphinx.ext.linkcode",
"sphinx.ext.napoleon",
"matplotlib.sphinxext.plot_directive",
"myst_parser",
]
autodoc_typehints = "both" # signature and description
napoleon_numpy_docstring = True
napoleon_include_init_with_doc = False
napoleon_include_private_with_doc = False
napoleon_include_special_with_doc = True
napoleon_use_admonition_for_examples = False
napoleon_use_admonition_for_notes = False
napoleon_use_admonition_for_references = False
napoleon_use_ivar = False
napoleon_use_param = True
napoleon_use_rtype = True
napoleon_use_keyword = True
napoleon_custom_sections = None
_base_path = os.path.abspath(os.path.join(__file__, "../../.."))
_path_cache: dict[str, bool] = {}
def _check_path(path: str) -> bool:
"""Check if a path exists and cache the result."""
path = os.path.join(_base_path, path)
result = _path_cache.get(path)
if result is None:
result = os.path.exists(path)
_path_cache[path] = result
return result
def linkcode_resolve(domain: str, info: dict[str, str]):
"""Linkcode extension override to link to the source code on GitHub."""
if domain != "py":
return None
if not info["module"]:
return None
if not info["module"].startswith("mlos_"):
return None
package = info["module"].split(".")[0]
filename = info["module"].replace(".", "/")
path = f"{package}/{filename}.py"
if not _check_path(path):
path = f"{package}/{filename}/__init__.py"
if not _check_path(path):
warning(f"linkcode_resolve failed to find {path}")
warning(f"linkcode_resolve info: {json.dumps(info, indent=2)}")
return f"https://github.com/microsoft/MLOS/tree/main/{path}"
def is_on_github_actions():
"""Check if the documentation is being built on GitHub Actions."""
return os.environ.get("CI") and os.environ.get("GITHUB_RUN_ID")
# Add mappings to link to external documentation.
intersphinx_mapping = get_intersphinx_mapping(
packages={
"asyncssh",
# Azure SDKs removed their intersphinx publishing.
# https://github.com/Azure/azure-sdk-for-python/issues/39316
# "azure-core",
# "azure-identity",
"configspace",
"matplotlib",
"numpy",
"pandas",
"python",
"referencing",
"smac",
"typing_extensions",
}
)
intersphinx_mapping.update(
{
"alembic": ("https://alembic.sqlalchemy.org/en/latest/", None),
"dabl": ("https://dabl.github.io/stable/", None),
}
)
# Hack to resolve type aliases as attributes instead of classes.
# See Also: https://github.com/sphinx-doc/sphinx/issues/10785
# Type alias resolution map
# (original, refname) -> new
CUSTOM_REF_TYPE_MAP: dict[tuple[str, str], str] = {
# Internal typevars and aliases:
("BaseTypeVar", "class"): "data",
("ConcreteOptimizer", "class"): "data",
("ConcreteSpaceAdapter", "class"): "data",
("DistributionName", "class"): "data",
("mlos_bench.tunables.tunable_types.DistributionName", "class"): "data",
("FlamlDomain", "class"): "data",
("mlos_core.spaces.converters.flaml.FlamlDomain", "class"): "data",
("TunableValue", "class"): "data",
("mlos_bench.tunables.tunable_types.TunableValue", "class"): "data",
("TunableValueType", "class"): "data",
("mlos_bench.tunables.tunable_types.TunableValueType", "class"): "data",
("TunableValueTypeName", "class"): "data",
("mlos_bench.tunables.tunable_types.TunableValueTypeName", "class"): "data",
("T_co", "class"): "data",
("CoroReturnType", "class"): "data",
("FutureReturnType", "class"): "data",
("NullableT", "class"): "data",
}
def resolve_type_aliases(
app: SphinxApp,
env: BuildEnvironment,
node: pending_xref,
contnode: Element,
) -> Element | None:
"""Resolve :class: references to our type aliases as :attr: instead."""
if node["refdomain"] != "py":
return None
(orig_type, reftarget) = (node["reftype"], node["reftarget"])
new_type = CUSTOM_REF_TYPE_MAP.get((reftarget, orig_type))
if new_type:
# warning(f"Resolved {orig_type} {reftarget} to {new_type}")
return app.env.get_domain("py").resolve_xref(
env,
node["refdoc"],
app.builder,
new_type,
reftarget,
node,
contnode,
)
return None
def setup(app: SphinxApp) -> None:
"""Connect the missing-reference event to resolve type aliases."""
app.connect("missing-reference", resolve_type_aliases)
# Ignore some cross references to external things we can't intersphinx with.
# sphinx has a hard time finding typealiases and typevars instead of classes.
# See Also: https://github.com/sphinx-doc/sphinx/issues/10974
nitpick_ignore = [
("py:class", "Ellipsis"),
# Internal typevars and aliases:
("py:class", "EnvironType"),
# External typevars and aliases:
("py:class", "numpy.typing.NDArray"),
# External classes that refuse to resolve:
("py:class", "contextlib.nullcontext"),
("py:class", "sqlalchemy.engine.Connection"),
("py:class", "sqlalchemy.engine.Engine"),
("py:class", "sqlalchemy.schema.Table"),
("py:class", "sqlalchemy.MetaData"),
("py:exc", "jsonschema.exceptions.SchemaError"),
("py:exc", "jsonschema.exceptions.ValidationError"),
]
nitpick_ignore_regex = [
# Ignore some external references that don't use sphinx for their docs.
(r"py:.*", r"flaml\..*"),
# Azure SDKs removed their intersphinx publishing.
# https://github.com/Azure/azure-sdk-for-python/issues/39316
(r"py:.*", r"azure\..*"),
]
# Which documents to include in the build.
source_suffix = {
".rst": "restructuredtext",
# '.txt': 'markdown',
".md": "markdown",
}
# Add any paths that contain templates here, relative to this directory.
# templates_path = ["_templates"]
# Generate the plots for the gallery
# plot_gallery = True
# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns = ["_build", "_templates"]
autoapi_dirs = [
# Don't index setup.py or other utility scripts.
"../../mlos_core/mlos_core/",
"../../mlos_bench/mlos_bench/",
"../../mlos_viz/mlos_viz/",
]
autoapi_ignore = [
"*/tests/*",
# Don't document internal environment scripts that aren't part of a module.
"*/mlos_bench/config/environments/*/*.py",
"*/mlos_bench/config/services/*/*.py",
# Don't document schema evolution scripts.
"*/mlos_bench/storage/sql/alembic/*.py",
"*/mlos_bench/storage/sql/alembic/versions/*.py",
]
autoapi_options = [
"members",
# Can't document externally inherited members due to broken references.
# "inherited-members",
"undoc-members",
# Don't document private members.
# "private-members",
"show-inheritance",
# Causes issues when base class is a typing protocol.
# "show-inheritance-diagram",
"show-module-summary",
"special-members",
# Causes duplicate reference issues. For instance:
# - mlos_bench.environments.LocalEnv
# - mlos_bench.environments.local.LocalEnv
# - mlos_bench.environments.local.local_env.LocalEnv
# "imported-members",
]
autoapi_python_class_content = "both"
autoapi_member_order = "groupwise"
autoapi_add_toctree_entry = False # handled manually
autoapi_keep_files = not is_on_github_actions() # for local testing
# -- Options for HTML output -------------------------------------------------
# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
#
html_theme = "sphinx_rtd_theme"
html_theme_options = {
"canonical_url": "https://microsoft.github.io/MLOS/",
"navigation_depth": -1,
}
# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ["_static"]
# -- nbsphinx options for rendering notebooks -------------------------------
# nbsphinx_execute = 'never' # enable to stop nbsphinx from executing notebooks
nbsphinx_kernel_name = "python3"
# Exclude build directory and Jupyter backup files:
exclude_patterns = ["_build", "**.ipynb_checkpoints"]