Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/docformatter/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
48 changes: 48 additions & 0 deletions tests/test_configuration_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading