diff options
author | Shantanu <12621235+hauntsaninja@users.noreply.github.com> | 2022-05-15 15:45:55 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-15 15:45:55 -0700 |
commit | 0d67ae1b7938ada3fe5df680b643d69290f0c099 (patch) | |
tree | 8fa5d3325837a27cf950dec47883c4cd6c3a6215 | |
parent | e8973c5f50e9510c6ec7addf8fad70eceff6ab89 (diff) | |
download | python-coveragepy-git-0d67ae1b7938ada3fe5df680b643d69290f0c099.tar.gz |
Use tomllib on Python 3.11 (#1359)
Co-authored-by: hauntsaninja <>
-rw-r--r-- | coverage/tomlconfig.py | 23 | ||||
-rw-r--r-- | setup.py | 2 | ||||
-rw-r--r-- | tests/test_config.py | 11 | ||||
-rw-r--r-- | tests/test_testing.py | 8 |
4 files changed, 26 insertions, 18 deletions
diff --git a/coverage/tomlconfig.py b/coverage/tomlconfig.py index a06da65f..82f84ce9 100644 --- a/coverage/tomlconfig.py +++ b/coverage/tomlconfig.py @@ -6,16 +6,21 @@ import configparser import os import re +import sys from coverage.exceptions import ConfigError from coverage.misc import import_third_party, substitute_variables -# TOML support is an install-time extra option. (Import typing is here because -# import_third_party will unload any module that wasn't already imported. -# tomli imports typing, and if we unload it, later it's imported again, and on -# Python 3.6, this causes infinite recursion.) -import typing # pylint: disable=unused-import, wrong-import-order -tomli = import_third_party("tomli") +if sys.version_info >= (3, 11): + import tomllib +else: + # TOML support on Python 3.10 and below is an install-time extra option. + # (Import typing is here because import_third_party will unload any module + # that wasn't already imported. tomli imports typing, and if we unload it, + # later it's imported again, and on Python 3.6, this causes infinite + # recursion.) + import typing # pylint: disable=unused-import, wrong-import-order + tomllib = import_third_party("tomli") class TomlDecodeError(Exception): @@ -45,11 +50,11 @@ class TomlConfigParser: toml_text = fp.read() except OSError: return [] - if tomli is not None: + if tomllib is not None: toml_text = substitute_variables(toml_text, os.environ) try: - self.data = tomli.loads(toml_text) - except tomli.TOMLDecodeError as err: + self.data = tomllib.loads(toml_text) + except tomllib.TOMLDecodeError as err: raise TomlDecodeError(str(err)) from err return [filename] else: @@ -108,7 +108,7 @@ setup_args = dict( extras_require={ # Enable pyproject.toml support. - 'toml': ['tomli'], + 'toml': ['tomli; python_version < "3.11"'], }, # We need to get HTML assets from our htmlfiles directory. diff --git a/tests/test_config.py b/tests/test_config.py index d85dfc7b..6aa43511 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -4,6 +4,7 @@ """Test the config file handling for coverage.py""" import math +import sys from collections import OrderedDict from unittest import mock @@ -706,19 +707,21 @@ class ConfigFileTest(UsingModulesMixin, CoverageTest): def test_no_toml_installed_no_toml(self): # Can't read a toml file that doesn't exist. - with without_module(coverage.tomlconfig, 'tomli'): + with without_module(coverage.tomlconfig, 'tomllib'): msg = "Couldn't read 'cov.toml' as a config file" with pytest.raises(ConfigError, match=msg): coverage.Coverage(config_file="cov.toml") + @pytest.mark.skipif(sys.version_info >= (3, 11), reason="Python 3.11 has toml in stdlib") def test_no_toml_installed_explicit_toml(self): # Can't specify a toml config file if toml isn't installed. self.make_file("cov.toml", "# A toml file!") - with without_module(coverage.tomlconfig, 'tomli'): + with without_module(coverage.tomlconfig, 'tomllib'): msg = "Can't read 'cov.toml' without TOML support" with pytest.raises(ConfigError, match=msg): coverage.Coverage(config_file="cov.toml") + @pytest.mark.skipif(sys.version_info >= (3, 11), reason="Python 3.11 has toml in stdlib") def test_no_toml_installed_pyproject_toml(self): # Can't have coverage config in pyproject.toml without toml installed. self.make_file("pyproject.toml", """\ @@ -726,7 +729,7 @@ class ConfigFileTest(UsingModulesMixin, CoverageTest): [tool.coverage.run] xyzzy = 17 """) - with without_module(coverage.tomlconfig, 'tomli'): + with without_module(coverage.tomlconfig, 'tomllib'): msg = "Can't read 'pyproject.toml' without TOML support" with pytest.raises(ConfigError, match=msg): coverage.Coverage() @@ -738,7 +741,7 @@ class ConfigFileTest(UsingModulesMixin, CoverageTest): [tool.something] xyzzy = 17 """) - with without_module(coverage.tomlconfig, 'tomli'): + with without_module(coverage.tomlconfig, 'tomllib'): cov = coverage.Coverage() # We get default settings: assert not cov.config.timid diff --git a/tests/test_testing.py b/tests/test_testing.py index e1d3a345..43c682cd 100644 --- a/tests/test_testing.py +++ b/tests/test_testing.py @@ -357,10 +357,10 @@ def _same_python_executable(e1, e2): def test_without_module(): - toml1 = tomlconfig.tomli - with without_module(tomlconfig, 'tomli'): - toml2 = tomlconfig.tomli - toml3 = tomlconfig.tomli + toml1 = tomlconfig.tomllib + with without_module(tomlconfig, 'tomllib'): + toml2 = tomlconfig.tomllib + toml3 = tomlconfig.tomllib assert toml1 is toml3 is not None assert toml2 is None |