From 1f41486af49b2d65ec058d2bec1447632278f1fc Mon Sep 17 00:00:00 2001 From: Guillaume Ayoub Date: Wed, 3 Jul 2024 10:18:46 +0200 Subject: [PATCH] Log warning when unknown rendering option is given --- tests/test_api.py | 9 +++++++++ weasyprint/__init__.py | 2 ++ weasyprint/__main__.py | 3 ++- 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/tests/test_api.py b/tests/test_api.py index 86aacf46..009b3b71 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -305,6 +305,15 @@ def test_python_render(assert_pixels_equal, tmp_path): ).write_png() == rotated_png_bytes +@assert_no_logs +def test_unknown_options(): + with capture_logs() as logs: + pdf_bytes = FakeHTML(string='test').write_pdf(zoom=2, unknown=True) + assert len(logs) == 1 + assert 'unknown' in logs[0] + assert pdf_bytes + + @assert_no_logs def test_command_line_render(tmp_path): css = b''' diff --git a/weasyprint/__init__.py b/weasyprint/__init__.py index 64e125ef..2908524e 100644 --- a/weasyprint/__init__.py +++ b/weasyprint/__init__.py @@ -210,6 +210,8 @@ class HTML: :returns: A :class:`document.Document` object. """ + for unknown in set(options) - set(DEFAULT_OPTIONS): + LOGGER.warning('Unknown rendering option: %s.', unknown) new_options = DEFAULT_OPTIONS.copy() new_options.update(options) options = new_options diff --git a/weasyprint/__main__.py b/weasyprint/__main__.py index 970df1af..fcb3ff05 100644 --- a/weasyprint/__main__.py +++ b/weasyprint/__main__.py @@ -165,7 +165,8 @@ def main(argv=None, stdout=None, stdin=None, HTML=HTML): # noqa: N803 if args.timeout is not None: url_fetcher = partial(default_url_fetcher, timeout=args.timeout) - options = vars(args) + options = { + key: value for key, value in vars(args).items() if key in DEFAULT_OPTIONS} # Default to logging to stderr. if args.debug: