Skip to content

Commit

Permalink
Tweak exporter default_config merging behavior (#1981)
Browse files Browse the repository at this point in the history
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
tuncbkose and pre-commit-ci[bot] authored May 6, 2023
1 parent f0d86bc commit d113388
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 7 deletions.
5 changes: 4 additions & 1 deletion nbconvert/exporters/asciidoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,8 @@ def default_config(self):
"HighlightMagicsPreprocessor": {"enabled": True},
}
)
c.merge(super().default_config)
if super().default_config:
c2 = super().default_config.copy()
c2.merge(c)
c = c2
return c
5 changes: 4 additions & 1 deletion nbconvert/exporters/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,10 @@ def default_config(self):
"HighlightMagicsPreprocessor": {"enabled": True},
}
)
c.merge(super().default_config)
if super().default_config:
c2 = super().default_config.copy()
c2.merge(c)
c = c2
return c

@contextfilter
Expand Down
5 changes: 4 additions & 1 deletion nbconvert/exporters/latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ def default_config(self):
"HighlightMagicsPreprocessor": {"enabled": True},
}
)
c.merge(super().default_config)
if super().default_config:
c2 = super().default_config.copy()
c2.merge(c)
c = c2
return c

def from_notebook_node(self, nb, resources=None, **kw):
Expand Down
5 changes: 4 additions & 1 deletion nbconvert/exporters/markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,8 @@ def default_config(self):
"HighlightMagicsPreprocessor": {"enabled": True},
}
)
c.merge(super().default_config)
if super().default_config:
c2 = super().default_config.copy()
c2.merge(c)
c = c2
return c
5 changes: 4 additions & 1 deletion nbconvert/exporters/rst.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,8 @@ def default_config(self):
"HighlightMagicsPreprocessor": {"enabled": True},
}
)
c.merge(super().default_config)
if super().default_config:
c2 = super().default_config.copy()
c2.merge(c)
c = c2
return c
5 changes: 4 additions & 1 deletion nbconvert/exporters/templateexporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,10 @@ def default_config(self):
"TagRemovePreprocessor": {"enabled": True},
}
)
c.merge(super().default_config)
if super().default_config:
c2 = super().default_config.copy()
c2.merge(c)
c = c2
return c

template_name = Unicode(help="Name of the template to use").tag(
Expand Down
26 changes: 25 additions & 1 deletion nbconvert/exporters/tests/test_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
from traitlets.config import Config

from ...preprocessors.base import Preprocessor
from .. import Exporter, TemplateExporter
from ..base import get_export_names
from ..exporter import Exporter
from .base import ExportersTestsBase

# -----------------------------------------------------------------------------
Expand All @@ -38,6 +38,21 @@ def preprocess(self, nb, resources):
return nb, resources


class DummyExporter(TemplateExporter):
"""
Dummy exporter to check that parent default_config gets overwritten properly
"""

@property
def default_config(self):
c = Config({"TagRemovePreprocessor": {"enabled": False}})
if super().default_config:
c2 = super().default_config.copy()
c2.merge(c)
c = c2
return c


class TestExporter(ExportersTestsBase):
"""Contains test functions for exporter.py"""

Expand Down Expand Up @@ -87,3 +102,12 @@ def test_get_exporter_disable_config_exporters(self):
del os.environ["NBCONVERT_DISABLE_CONFIG_EXPORTERS"]
export_names = get_export_names(config=config)
self.assertEqual(export_names, ["notebook"])

def test_default_config_merge(self):
"""
Do default_configs merge properly?
Class config should overwrite parent config
"""
e = DummyExporter()
self.assertFalse(e.default_config["TagRemovePreprocessor"]["enabled"])
self.assertTrue(e.default_config["RegexRemovePreprocessor"]["enabled"])

0 comments on commit d113388

Please sign in to comment.