diff options
-rw-r--r-- | .pre-commit-config.yaml | 3 | ||||
-rw-r--r-- | ChangeLog | 4 | ||||
-rw-r--r-- | doc/whatsnew/2.13.rst | 4 | ||||
-rw-r--r-- | pylint/config/find_default_config_files.py | 13 | ||||
-rw-r--r-- | pylint/config/option_manager_mixin.py | 13 | ||||
-rw-r--r-- | requirements_test.txt | 1 | ||||
-rw-r--r-- | setup.cfg | 5 | ||||
-rw-r--r-- | tests/config/functional/toml/issue_3181/toml_decode_error.1.out | 2 |
8 files changed, 27 insertions, 18 deletions
diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index e3a1ad58f..c440a0ca3 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -86,8 +86,7 @@ repos: types: [python] args: [] require_serial: true - additional_dependencies: - ["platformdirs==2.2.0", "types-pkg_resources==0.1.3", "types-toml==0.1.3"] + additional_dependencies: ["platformdirs==2.2.0", "types-pkg_resources==0.1.3"] exclude: tests/functional/|tests/input|tests(/.*)*/data|tests/regrtest_data/|tests/data/|tests(/.*)+/conftest.py|doc/|bin/ - repo: https://github.com/pre-commit/mirrors-prettier rev: v2.5.1 @@ -83,6 +83,10 @@ Release date: TBA Closes #5281 +* Use the ``tomli`` package instead of ``toml`` to parse ``.toml`` files. + + Closes #5885 + * Fix false positive - Allow unpacking of ``self`` in a subclass of ``typing.NamedTuple``. Closes #5312 diff --git a/doc/whatsnew/2.13.rst b/doc/whatsnew/2.13.rst index a0e5273db..0830fd561 100644 --- a/doc/whatsnew/2.13.rst +++ b/doc/whatsnew/2.13.rst @@ -129,6 +129,10 @@ Other Changes Closes #5614 +* Use the ``tomli`` package instead of ``toml`` to parse ``.toml`` files. + + Closes #5885 + * Fixed false positive ``consider-using-dict-comprehension`` when creating a dict using a list of tuples where key AND value vary depending on the same condition. diff --git a/pylint/config/find_default_config_files.py b/pylint/config/find_default_config_files.py index 10c31345d..73a7f0f8a 100644 --- a/pylint/config/find_default_config_files.py +++ b/pylint/config/find_default_config_files.py @@ -3,17 +3,20 @@ import configparser import os +import sys from typing import Iterator, Optional -import toml -from toml import TomlDecodeError +if sys.version_info >= (3, 11): + import tomllib +else: + import tomli as tomllib def _toml_has_config(path): - with open(path, encoding="utf-8") as toml_handle: + with open(path, mode="rb") as toml_handle: try: - content = toml.load(toml_handle) - except TomlDecodeError as error: + content = tomllib.load(toml_handle) + except tomllib.TOMLDecodeError as error: print(f"Failed to load '{path}': {error}") return False diff --git a/pylint/config/option_manager_mixin.py b/pylint/config/option_manager_mixin.py index 9260f7593..387c62823 100644 --- a/pylint/config/option_manager_mixin.py +++ b/pylint/config/option_manager_mixin.py @@ -14,13 +14,16 @@ from pathlib import Path from types import ModuleType from typing import Dict, List, Optional, TextIO, Tuple, Union -import toml - from pylint import utils from pylint.config.man_help_formatter import _ManHelpFormatter from pylint.config.option import Option from pylint.config.option_parser import OptionParser +if sys.version_info >= (3, 11): + import tomllib +else: + import tomli as tomllib + def _expand_default(self, option): """Patch OptionParser.expand_default with custom behaviour. @@ -276,7 +279,7 @@ class OptionsManagerMixIn: if config_file.endswith(".toml"): try: self._parse_toml(config_file, parser) - except toml.TomlDecodeError as e: + except tomllib.TOMLDecodeError as e: self.add_message("config-parse-error", line=0, args=str(e)) else: # Use this encoding in order to strip the BOM marker, if any. @@ -300,8 +303,8 @@ class OptionsManagerMixIn: self, config_file: Union[Path, str], parser: configparser.ConfigParser ) -> None: """Parse and handle errors of a toml configuration file.""" - with open(config_file, encoding="utf-8") as fp: - content = toml.load(fp) + with open(config_file, mode="rb") as fp: + content = tomllib.load(fp) try: sections_values = content["tool"]["pylint"] except KeyError: diff --git a/requirements_test.txt b/requirements_test.txt index 052ac74d5..bcc6311fc 100644 --- a/requirements_test.txt +++ b/requirements_test.txt @@ -10,4 +10,3 @@ pytest-profiling~=1.7 pytest-xdist~=2.5 # Type packages for mypy types-pkg_resources==0.1.3 -types-toml==0.10.4 @@ -51,7 +51,7 @@ install_requires = astroid>=2.9.2,<=2.10.0-dev0 isort>=4.2.5,<6 mccabe>=0.6,<0.7 - toml>=0.9.2 + tomli>=1.1.0;python_version<"3.11" colorama;sys_platform=="win32" typing-extensions>=3.10.0;python_version<"3.10" python_requires = >=3.6.2 @@ -125,9 +125,6 @@ ignore_missing_imports = True [mypy-_string] ignore_missing_imports = True -[mypy-toml.decoder] -ignore_missing_imports = True - [mypy-git.*] ignore_missing_imports = True diff --git a/tests/config/functional/toml/issue_3181/toml_decode_error.1.out b/tests/config/functional/toml/issue_3181/toml_decode_error.1.out index 545acbd2d..4511afc5c 100644 --- a/tests/config/functional/toml/issue_3181/toml_decode_error.1.out +++ b/tests/config/functional/toml/issue_3181/toml_decode_error.1.out @@ -1,2 +1,2 @@ ************* Module {abspath} -{relpath}:1:0: F0011: error while parsing the configuration: Found invalid character in key name: '*'. Try quoting the key name. (line 3 column 2 char 48) (config-parse-error) +{relpath}:1:0: F0011: error while parsing the configuration: Invalid statement (at line 3, column 1) (config-parse-error) |