diff --git a/src/docformatter/configuration.py b/src/docformatter/configuration.py index 3fe45ee..fe6c74d 100644 --- a/src/docformatter/configuration.py +++ b/src/docformatter/configuration.py @@ -346,7 +346,14 @@ def _do_read_toml_configuration(self) -> None: if tomllib is None: # tomli/tomllib is not installed (Python < 3.11 without the tomli # backport); skip reading TOML configuration rather than crashing - # with a NameError. See #368. + # with a NameError, but say so instead of silently ignoring the + # user's settings. See #268 and #368. + print( + f"docformatter: {self.config_file} was not read because TOML " + "support is missing; on Python < 3.11 this needs the tomli " + 'package: pip install "docformatter[tomli]"', + file=sys.stderr, + ) return with open(self.config_file, "rb") as f: config = tomllib.load(f) diff --git a/tests/test_configuration_functions.py b/tests/test_configuration_functions.py index 0c63c39..e6740d0 100644 --- a/tests/test_configuration_functions.py +++ b/tests/test_configuration_functions.py @@ -699,3 +699,51 @@ def test_non_cap_from_setup_cfg( "diff": "true", "non-cap": '["qBittorrent", "iPad", "iOS", "eBay"]', } +class TestMissingTomlSupport: + """Class for testing behaviour when no TOML parser is installed. + + On Python < 3.11 the tomli backport is an optional extra, so a plain + ``pip install docformatter`` leaves docformatter unable to read + pyproject.toml. Issue #268 is that this happens without a word. + """ + + @pytest.mark.unit + def test_says_so_when_toml_support_is_missing(self, tmp_path, capsys): + """Warn on stderr instead of silently dropping the settings.""" + # Third Party Imports + import docformatter.configuration as configuration + + config_file = tmp_path / "pyproject.toml" + config_file.write_text( + '[tool.docformatter]\nwrap-summaries = "120"\n', encoding="utf-8" + ) + + saved = configuration.tomllib + configuration.tomllib = None + try: + uut = Configurater( + ["/path/to/docformatter", "--config", str(config_file), ""] + ) + uut.do_parse_arguments() + finally: + configuration.tomllib = saved + + stderr = capsys.readouterr().err + assert str(config_file) in stderr + assert "tomli" in stderr + # The settings are still dropped; that part is by design (#368). + assert uut.args.wrap_summaries == 79 + + @pytest.mark.unit + def test_stays_quiet_when_toml_support_is_present(self, tmp_path, capsys): + """Say nothing on the happy path.""" + config_file = tmp_path / "pyproject.toml" + config_file.write_text( + '[tool.docformatter]\nwrap-summaries = "120"\n', encoding="utf-8" + ) + + uut = Configurater(["/path/to/docformatter", "--config", str(config_file), ""]) + uut.do_parse_arguments() + + assert capsys.readouterr().err == "" + assert uut.args.wrap_summaries == 120