mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-10 21:34:18 +00:00
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
This commit is contained in:
parent
72ad6dc953
commit
f4cd1ba0d6
813 changed files with 66015 additions and 58839 deletions
|
|
@ -1,27 +1,29 @@
|
|||
# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
|
||||
# For details: https://github.com/nedbat/coveragepy/blob/master/NOTICE.txt
|
||||
|
||||
"""TOML configuration support for coverage.py"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import re
|
||||
|
||||
from typing import Any, Callable, Iterable, TypeVar
|
||||
from typing import Any
|
||||
from typing import Callable
|
||||
from typing import Iterable
|
||||
from typing import TypeVar
|
||||
|
||||
from coverage import env
|
||||
from coverage.exceptions import ConfigError
|
||||
from coverage.misc import import_third_party, substitute_variables
|
||||
from coverage.types import TConfigSectionOut, TConfigValueOut
|
||||
from coverage.misc import import_third_party
|
||||
from coverage.misc import substitute_variables
|
||||
from coverage.types import TConfigSectionOut
|
||||
from coverage.types import TConfigValueOut
|
||||
|
||||
|
||||
if env.PYVERSION >= (3, 11, 0, "alpha", 7):
|
||||
if env.PYVERSION >= (3, 11, 0, 'alpha', 7):
|
||||
import tomllib # pylint: disable=import-error
|
||||
has_tomllib = True
|
||||
else:
|
||||
# TOML support on Python 3.10 and below is an install-time extra option.
|
||||
tomllib, has_tomllib = import_third_party("tomli")
|
||||
tomllib, has_tomllib = import_third_party('tomli')
|
||||
|
||||
|
||||
class TomlDecodeError(Exception):
|
||||
|
|
@ -29,7 +31,8 @@ class TomlDecodeError(Exception):
|
|||
pass
|
||||
|
||||
|
||||
TWant = TypeVar("TWant")
|
||||
TWant = TypeVar('TWant')
|
||||
|
||||
|
||||
class TomlConfigParser:
|
||||
"""TOML file reading with the interface of HandyConfigParser."""
|
||||
|
|
@ -60,7 +63,7 @@ class TomlConfigParser:
|
|||
raise TomlDecodeError(str(err)) from err
|
||||
return [filename]
|
||||
else:
|
||||
has_toml = re.search(r"^\[tool\.coverage(\.|])", toml_text, flags=re.MULTILINE)
|
||||
has_toml = re.search(r'^\[tool\.coverage(\.|])', toml_text, flags=re.MULTILINE)
|
||||
if self.our_file or has_toml:
|
||||
# Looks like they meant to read TOML, but we can't read it.
|
||||
msg = "Can't read {!r} without TOML support. Install with [toml] extra"
|
||||
|
|
@ -79,10 +82,10 @@ class TomlConfigParser:
|
|||
data (str): the dict of data in the section, or None if not found.
|
||||
|
||||
"""
|
||||
prefixes = ["tool.coverage."]
|
||||
prefixes = ['tool.coverage.']
|
||||
for prefix in prefixes:
|
||||
real_section = prefix + section
|
||||
parts = real_section.split(".")
|
||||
parts = real_section.split('.')
|
||||
try:
|
||||
data = self.data[parts[0]]
|
||||
for part in parts[1:]:
|
||||
|
|
@ -98,12 +101,12 @@ class TomlConfigParser:
|
|||
"""Like .get, but returns the real section name and the value."""
|
||||
name, data = self._get_section(section)
|
||||
if data is None:
|
||||
raise ConfigError(f"No section: {section!r}")
|
||||
raise ConfigError(f'No section: {section!r}')
|
||||
assert name is not None
|
||||
try:
|
||||
value = data[option]
|
||||
except KeyError:
|
||||
raise ConfigError(f"No option {option!r} in section: {name!r}") from None
|
||||
raise ConfigError(f'No option {option!r} in section: {name!r}') from None
|
||||
return name, value
|
||||
|
||||
def _get_single(self, section: str, option: str) -> Any:
|
||||
|
|
@ -134,7 +137,7 @@ class TomlConfigParser:
|
|||
def options(self, section: str) -> list[str]:
|
||||
_, data = self._get_section(section)
|
||||
if data is None:
|
||||
raise ConfigError(f"No section: {section!r}")
|
||||
raise ConfigError(f'No section: {section!r}')
|
||||
return list(data.keys())
|
||||
|
||||
def get_section(self, section: str) -> TConfigSectionOut:
|
||||
|
|
@ -168,18 +171,18 @@ class TomlConfigParser:
|
|||
f"Option [{section}]{option} couldn't convert to {type_desc}: {value!r}",
|
||||
) from e
|
||||
raise ValueError(
|
||||
f"Option [{section}]{option} is not {type_desc}: {value!r}",
|
||||
f'Option [{section}]{option} is not {type_desc}: {value!r}',
|
||||
)
|
||||
|
||||
def getboolean(self, section: str, option: str) -> bool:
|
||||
name, value = self._get_single(section, option)
|
||||
bool_strings = {"true": True, "false": False}
|
||||
return self._check_type(name, option, value, bool, bool_strings.__getitem__, "a boolean")
|
||||
bool_strings = {'true': True, 'false': False}
|
||||
return self._check_type(name, option, value, bool, bool_strings.__getitem__, 'a boolean')
|
||||
|
||||
def _get_list(self, section: str, option: str) -> tuple[str, list[str]]:
|
||||
"""Get a list of strings, substituting environment variables in the elements."""
|
||||
name, values = self._get(section, option)
|
||||
values = self._check_type(name, option, values, list, None, "a list")
|
||||
values = self._check_type(name, option, values, list, None, 'a list')
|
||||
values = [substitute_variables(value, os.environ) for value in values]
|
||||
return name, values
|
||||
|
||||
|
|
@ -194,15 +197,15 @@ class TomlConfigParser:
|
|||
try:
|
||||
re.compile(value)
|
||||
except re.error as e:
|
||||
raise ConfigError(f"Invalid [{name}].{option} value {value!r}: {e}") from e
|
||||
raise ConfigError(f'Invalid [{name}].{option} value {value!r}: {e}') from e
|
||||
return values
|
||||
|
||||
def getint(self, section: str, option: str) -> int:
|
||||
name, value = self._get_single(section, option)
|
||||
return self._check_type(name, option, value, int, int, "an integer")
|
||||
return self._check_type(name, option, value, int, int, 'an integer')
|
||||
|
||||
def getfloat(self, section: str, option: str) -> float:
|
||||
name, value = self._get_single(section, option)
|
||||
if isinstance(value, int):
|
||||
value = float(value)
|
||||
return self._check_type(name, option, value, float, float, "a float")
|
||||
return self._check_type(name, option, value, float, float, 'a float')
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue