Skip to content

Commit d0051ac

Browse files
committed
fix: say when a config file is skipped for lack of TOML support (#268)
#371 stopped docformatter crashing with a NameError when neither tomllib nor the tomli backport is importable, by returning early from _do_read_toml_configuration. That fixed #368. What is left is #268: the early return is completely silent, so a pyproject.toml -- including one passed explicitly with --config -- is ignored without a word, and the run looks like it simply chose to reformat differently. This is reachable on a supported configuration. pyproject.toml declares python = "^3.10", tomli is an optional extra for python < 3.11, and the CI matrix still runs 3.10 and pypy3.9. A plain `pip install docformatter` on 3.10 therefore reads no TOML configuration at all. Print one line to stderr naming the file that was not read and how to get TOML support, and leave the behaviour itself alone -- whether the settings should be honoured some other way is a separate question.
1 parent d5c7b77 commit d0051ac

2 files changed

Lines changed: 56 additions & 1 deletion

File tree

src/docformatter/configuration.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,14 @@ def _do_read_toml_configuration(self) -> None:
346346
if tomllib is None:
347347
# tomli/tomllib is not installed (Python < 3.11 without the tomli
348348
# backport); skip reading TOML configuration rather than crashing
349-
# with a NameError. See #368.
349+
# with a NameError, but say so instead of silently ignoring the
350+
# user's settings. See #268 and #368.
351+
print(
352+
f"docformatter: {self.config_file} was not read because TOML "
353+
"support is missing; on Python < 3.11 this needs the tomli "
354+
'package: pip install "docformatter[tomli]"',
355+
file=sys.stderr,
356+
)
350357
return
351358
with open(self.config_file, "rb") as f:
352359
config = tomllib.load(f)

tests/test_configuration_functions.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,3 +699,51 @@ def test_non_cap_from_setup_cfg(
699699
"diff": "true",
700700
"non-cap": '["qBittorrent", "iPad", "iOS", "eBay"]',
701701
}
702+
class TestMissingTomlSupport:
703+
"""Class for testing behaviour when no TOML parser is installed.
704+
705+
On Python < 3.11 the tomli backport is an optional extra, so a plain
706+
``pip install docformatter`` leaves docformatter unable to read
707+
pyproject.toml. Issue #268 is that this happens without a word.
708+
"""
709+
710+
@pytest.mark.unit
711+
def test_says_so_when_toml_support_is_missing(self, tmp_path, capsys):
712+
"""Warn on stderr instead of silently dropping the settings."""
713+
# Third Party Imports
714+
import docformatter.configuration as configuration
715+
716+
config_file = tmp_path / "pyproject.toml"
717+
config_file.write_text(
718+
'[tool.docformatter]\nwrap-summaries = "120"\n', encoding="utf-8"
719+
)
720+
721+
saved = configuration.tomllib
722+
configuration.tomllib = None
723+
try:
724+
uut = Configurater(
725+
["/path/to/docformatter", "--config", str(config_file), ""]
726+
)
727+
uut.do_parse_arguments()
728+
finally:
729+
configuration.tomllib = saved
730+
731+
stderr = capsys.readouterr().err
732+
assert str(config_file) in stderr
733+
assert "tomli" in stderr
734+
# The settings are still dropped; that part is by design (#368).
735+
assert uut.args.wrap_summaries == 79
736+
737+
@pytest.mark.unit
738+
def test_stays_quiet_when_toml_support_is_present(self, tmp_path, capsys):
739+
"""Say nothing on the happy path."""
740+
config_file = tmp_path / "pyproject.toml"
741+
config_file.write_text(
742+
'[tool.docformatter]\nwrap-summaries = "120"\n', encoding="utf-8"
743+
)
744+
745+
uut = Configurater(["/path/to/docformatter", "--config", str(config_file), ""])
746+
uut.do_parse_arguments()
747+
748+
assert capsys.readouterr().err == ""
749+
assert uut.args.wrap_summaries == 120

0 commit comments

Comments
 (0)