Skip to content

Commit 3823cc6

Browse files
committed
fix: [tools.coverage] is valid for settings in a toml file. #1516
1 parent 012a687 commit 3823cc6

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

CHANGES.rst

+9
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,18 @@ development at the same time, such as 4.5.x and 5.0.
2020
Unreleased
2121
----------
2222

23+
- Fix: if Python doesn't provide tomllib, then TOML configuration files can
24+
only be read if coverage.py is installed with the ``[toml]`` extra.
25+
Coverage.py will raise an error if toml support is not installed when it sees
26+
your settings are in a .toml file. But it didn't understand that
27+
``[tools.coverage]`` was a valid section header, so the error wasn't
28+
reported, and settings were silently ignored. This is now fixed, closing
29+
`issue 1516`_.
30+
2331
- Fix: adjusted how decorators are traced on PyPy 7.3.10, fixing `issue 1515`_.
2432

2533
.. _issue 1515: https://github.com/nedbat/coveragepy/issues/1515
34+
.. _issue 1516: https://github.com/nedbat/coveragepy/issues/1516
2635

2736

2837
.. _changes_7-0-1:

coverage/tomlconfig.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def read(self, filenames):
5252
raise TomlDecodeError(str(err)) from err
5353
return [filename]
5454
else:
55-
has_toml = re.search(r"^\[tool\.coverage\.", toml_text, flags=re.MULTILINE)
55+
has_toml = re.search(r"^\[tool\.coverage(\.|])", toml_text, flags=re.MULTILINE)
5656
if self.our_file or has_toml:
5757
# Looks like they meant to read TOML, but we can't read it.
5858
msg = "Can't read {!r} without TOML support. Install with [toml] extra"

tests/test_config.py

+13
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,19 @@ def test_no_toml_installed_pyproject_toml(self):
739739
with pytest.raises(ConfigError, match=msg):
740740
coverage.Coverage()
741741

742+
@pytest.mark.skipif(sys.version_info >= (3, 11), reason="Python 3.11 has toml in stdlib")
743+
def test_no_toml_installed_pyproject_toml_shorter_syntax(self):
744+
# Can't have coverage config in pyproject.toml without toml installed.
745+
self.make_file("pyproject.toml", """\
746+
# A toml file!
747+
[tool.coverage]
748+
run.parallel = true
749+
""")
750+
with without_module(coverage.tomlconfig, 'tomllib'):
751+
msg = "Can't read 'pyproject.toml' without TOML support"
752+
with pytest.raises(ConfigError, match=msg):
753+
coverage.Coverage()
754+
742755
def test_no_toml_installed_pyproject_no_coverage(self):
743756
# It's ok to have non-coverage pyproject.toml without toml installed.
744757
self.make_file("pyproject.toml", """\

0 commit comments

Comments
 (0)