Skip to content

Commit 8f7f59e

Browse files
Yoshanuikabundicholdgraf
authored andcommitted
Update myst_nb/core/config.py to new warnings system
1 parent 9678a84 commit 8f7f59e

2 files changed

Lines changed: 29 additions & 17 deletions

File tree

myst_nb/core/config.py

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
"""Configuration for myst-nb."""
2+
from __future__ import annotations
3+
24
import dataclasses as dc
35
from enum import Enum
4-
from typing import Any, Callable, Dict, Iterable, Optional, Sequence, Tuple
6+
from typing import Any, Callable, Iterable, Sequence
57

68
from myst_parser.config.dc_validators import (
79
ValidatorType,
@@ -14,12 +16,14 @@
1416
)
1517
from typing_extensions import Literal
1618

19+
from myst_nb.warnings_ import MystNBWarnings
20+
1721

18-
def custom_formats_converter(value: dict) -> Dict[str, Tuple[str, dict, bool]]:
22+
def custom_formats_converter(value: dict) -> dict[str, tuple[str, dict, bool]]:
1923
"""Convert the custom format dict."""
2024
if not isinstance(value, dict):
2125
raise TypeError(f"`nb_custom_formats` must be a dict: {value}")
22-
output: Dict[str, Tuple[str, dict, bool]] = {}
26+
output: dict[str, tuple[str, dict, bool]] = {}
2327
for suffix, reader in value.items():
2428
if not isinstance(suffix, str):
2529
raise TypeError(f"`nb_custom_formats` keys must be a string: {suffix}")
@@ -54,7 +58,7 @@ def custom_formats_converter(value: dict) -> Dict[str, Tuple[str, dict, bool]]:
5458
return output
5559

5660

57-
def ipywidgets_js_factory() -> Dict[str, Dict[str, str]]:
61+
def ipywidgets_js_factory() -> dict[str, dict[str, str]]:
5862
"""Create a default ipywidgets js dict."""
5963
# see: https://ipywidgets.readthedocs.io/en/7.6.5/embedding.html
6064
return {
@@ -126,7 +130,7 @@ def __post_init__(self):
126130

127131
# file read options
128132

129-
custom_formats: Dict[str, Tuple[str, dict, bool]] = dc.field(
133+
custom_formats: dict[str, tuple[str, dict, bool]] = dc.field(
130134
default_factory=dict,
131135
metadata={
132136
"help": "Custom formats for reading notebook; suffix -> reader",
@@ -178,7 +182,7 @@ def __post_init__(self):
178182

179183
# notebook execution options
180184

181-
kernel_rgx_aliases: Dict[str, str] = dc.field(
185+
kernel_rgx_aliases: dict[str, str] = dc.field(
182186
default_factory=dict,
183187
metadata={
184188
"validator": deep_mapping(instance_of(str), instance_of(str)),
@@ -376,7 +380,7 @@ def __post_init__(self):
376380
},
377381
repr=False,
378382
)
379-
mime_priority_overrides: Sequence[Tuple[str, str, Optional[int]]] = dc.field(
383+
mime_priority_overrides: Sequence[tuple[str, str, int | None]] = dc.field(
380384
default=(),
381385
metadata={
382386
"validator": deep_iterable(
@@ -448,7 +452,7 @@ def __post_init__(self):
448452
),
449453
},
450454
)
451-
render_image_options: Dict[str, str] = dc.field(
455+
render_image_options: dict[str, str] = dc.field(
452456
default_factory=dict,
453457
# see https://docutils.sourceforge.io/docs/ref/rst/directives.html#image
454458
metadata={
@@ -465,7 +469,7 @@ def __post_init__(self):
465469
),
466470
},
467471
)
468-
render_figure_options: Dict[str, str] = dc.field(
472+
render_figure_options: dict[str, str] = dc.field(
469473
default_factory=dict,
470474
# see https://docutils.sourceforge.io/docs/ref/rst/directives.html#figure
471475
metadata={
@@ -498,7 +502,7 @@ def __post_init__(self):
498502
# TODO jupyter_sphinx_require_url and jupyter_sphinx_embed_url (undocumented),
499503
# are no longer used by this package, replaced by ipywidgets_js
500504
# do we add any deprecation warnings?
501-
ipywidgets_js: Dict[str, Dict[str, str]] = dc.field(
505+
ipywidgets_js: dict[str, dict[str, str]] = dc.field(
502506
default_factory=ipywidgets_js_factory,
503507
metadata={
504508
"validator": deep_mapping(
@@ -538,19 +542,19 @@ def __post_init__(self):
538542
)
539543

540544
@classmethod
541-
def get_fields(cls) -> Tuple[dc.Field, ...]:
545+
def get_fields(cls) -> tuple[dc.Field, ...]:
542546
return dc.fields(cls)
543547

544548
def as_dict(self, dict_factory=dict) -> dict:
545549
return dc.asdict(self, dict_factory=dict_factory)
546550

547-
def as_triple(self) -> Iterable[Tuple[str, Any, dc.Field]]:
551+
def as_triple(self) -> Iterable[tuple[str, Any, dc.Field]]:
548552
"""Yield triples of (name, value, field)."""
549553
fields = {f.name: f for f in dc.fields(self.__class__)}
550554
for name, value in dc.asdict(self).items():
551555
yield name, value, fields[name]
552556

553-
def copy(self, **changes) -> "NbParserConfig":
557+
def copy(self, **changes) -> NbParserConfig:
554558
"""Return a copy of the configuration with optional changes applied."""
555559
return dc.replace(self, **changes)
556560

@@ -566,8 +570,8 @@ def __getitem__(self, field: str) -> Any:
566570
def get_cell_level_config(
567571
self,
568572
field_name: str,
569-
cell_metadata: Dict[str, Any],
570-
warning_callback: Callable[[str, str], Any],
573+
cell_metadata: dict[str, Any],
574+
warning_callback: Callable[[str, MystNBWarnings], Any],
571575
) -> Any:
572576
"""Get a configuration value at the cell level.
573577
@@ -593,7 +597,7 @@ def get_cell_level_config(
593597
warning_callback(
594598
f"Deprecated `cell_metadata_key` 'render' "
595599
f"found, replace with {self.cell_metadata_key!r}",
596-
"cell_metadata_key",
600+
MystNBWarnings.CELL_METADATA_KEY,
597601
)
598602
cell_meta = cell_metadata["render"]
599603
else:
@@ -611,7 +615,10 @@ def get_cell_level_config(
611615
field.metadata["validator"](self, field, value)
612616
return value
613617
except Exception as exc:
614-
warning_callback(f"Cell metadata invalid: {exc}", "cell_config")
618+
warning_callback(
619+
f"Cell metadata invalid: {exc}",
620+
MystNBWarnings.CELL_CONFIG,
621+
)
615622

616623
# default/global/file level should have already been merged
617624
return getattr(self, field.name)

myst_nb/warnings_.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ class MystNBWarnings(Enum):
2929
OUTPUT_TYPE = "output_type"
3030
"""Issue resolving Output type"""
3131

32+
CELL_METADATA_KEY = "cell_metadata_key"
33+
"""Issue with a key in a cell's `metadata` dictionary."""
34+
CELL_CONFIG = "cell_config"
35+
"""Issue with a cell's configuration or metadata."""
36+
3237

3338
def _is_suppressed_warning(
3439
type: str, subtype: str, suppress_warnings: Sequence[str]

0 commit comments

Comments
 (0)