mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-19 09:59:59 +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,6 +1,7 @@
|
|||
"""A lil' TOML parser."""
|
||||
from __future__ import annotations
|
||||
|
||||
__all__ = ("loads", "load", "TOMLDecodeError")
|
||||
__version__ = "1.0.3" # DO NOT EDIT THIS LINE MANUALLY. LET bump2version UTILITY DO IT
|
||||
__all__ = ('loads', 'load', 'TOMLDecodeError')
|
||||
__version__ = '1.0.3' # DO NOT EDIT THIS LINE MANUALLY. LET bump2version UTILITY DO IT
|
||||
|
||||
from pip._vendor.tomli._parser import TOMLDecodeError, load, loads
|
||||
|
|
|
|||
|
|
@ -1,28 +1,26 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import string
|
||||
from types import MappingProxyType
|
||||
from typing import (
|
||||
TYPE_CHECKING,
|
||||
Any,
|
||||
Callable,
|
||||
Dict,
|
||||
FrozenSet,
|
||||
Iterable,
|
||||
Optional,
|
||||
TextIO,
|
||||
Tuple,
|
||||
)
|
||||
from typing import Any
|
||||
from typing import Callable
|
||||
from typing import Dict
|
||||
from typing import FrozenSet
|
||||
from typing import Iterable
|
||||
from typing import Optional
|
||||
from typing import TextIO
|
||||
from typing import Tuple
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from pip._vendor.tomli._re import (
|
||||
RE_BIN,
|
||||
RE_DATETIME,
|
||||
RE_HEX,
|
||||
RE_LOCALTIME,
|
||||
RE_NUMBER,
|
||||
RE_OCT,
|
||||
match_to_datetime,
|
||||
match_to_localtime,
|
||||
match_to_number,
|
||||
)
|
||||
from pip._vendor.tomli._re import match_to_datetime
|
||||
from pip._vendor.tomli._re import match_to_localtime
|
||||
from pip._vendor.tomli._re import match_to_number
|
||||
from pip._vendor.tomli._re import RE_BIN
|
||||
from pip._vendor.tomli._re import RE_DATETIME
|
||||
from pip._vendor.tomli._re import RE_HEX
|
||||
from pip._vendor.tomli._re import RE_LOCALTIME
|
||||
from pip._vendor.tomli._re import RE_NUMBER
|
||||
from pip._vendor.tomli._re import RE_OCT
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from re import Pattern
|
||||
|
|
@ -32,29 +30,29 @@ ASCII_CTRL = frozenset(chr(i) for i in range(32)) | frozenset(chr(127))
|
|||
|
||||
# Neither of these sets include quotation mark or backslash. They are
|
||||
# currently handled as separate cases in the parser functions.
|
||||
ILLEGAL_BASIC_STR_CHARS = ASCII_CTRL - frozenset("\t")
|
||||
ILLEGAL_MULTILINE_BASIC_STR_CHARS = ASCII_CTRL - frozenset("\t\n\r")
|
||||
ILLEGAL_BASIC_STR_CHARS = ASCII_CTRL - frozenset('\t')
|
||||
ILLEGAL_MULTILINE_BASIC_STR_CHARS = ASCII_CTRL - frozenset('\t\n\r')
|
||||
|
||||
ILLEGAL_LITERAL_STR_CHARS = ILLEGAL_BASIC_STR_CHARS
|
||||
ILLEGAL_MULTILINE_LITERAL_STR_CHARS = ASCII_CTRL - frozenset("\t\n")
|
||||
ILLEGAL_MULTILINE_LITERAL_STR_CHARS = ASCII_CTRL - frozenset('\t\n')
|
||||
|
||||
ILLEGAL_COMMENT_CHARS = ILLEGAL_BASIC_STR_CHARS
|
||||
|
||||
TOML_WS = frozenset(" \t")
|
||||
TOML_WS_AND_NEWLINE = TOML_WS | frozenset("\n")
|
||||
BARE_KEY_CHARS = frozenset(string.ascii_letters + string.digits + "-_")
|
||||
TOML_WS = frozenset(' \t')
|
||||
TOML_WS_AND_NEWLINE = TOML_WS | frozenset('\n')
|
||||
BARE_KEY_CHARS = frozenset(string.ascii_letters + string.digits + '-_')
|
||||
KEY_INITIAL_CHARS = BARE_KEY_CHARS | frozenset("\"'")
|
||||
|
||||
BASIC_STR_ESCAPE_REPLACEMENTS = MappingProxyType(
|
||||
{
|
||||
"\\b": "\u0008", # backspace
|
||||
"\\t": "\u0009", # tab
|
||||
"\\n": "\u000A", # linefeed
|
||||
"\\f": "\u000C", # form feed
|
||||
"\\r": "\u000D", # carriage return
|
||||
'\\"': "\u0022", # quote
|
||||
"\\\\": "\u005C", # backslash
|
||||
}
|
||||
'\\b': '\u0008', # backspace
|
||||
'\\t': '\u0009', # tab
|
||||
'\\n': '\u000A', # linefeed
|
||||
'\\f': '\u000C', # form feed
|
||||
'\\r': '\u000D', # carriage return
|
||||
'\\"': '\u0022', # quote
|
||||
'\\\\': '\u005C', # backslash
|
||||
},
|
||||
)
|
||||
|
||||
# Type annotations
|
||||
|
|
@ -67,18 +65,18 @@ class TOMLDecodeError(ValueError):
|
|||
"""An error raised if a document is not valid TOML."""
|
||||
|
||||
|
||||
def load(fp: TextIO, *, parse_float: ParseFloat = float) -> Dict[str, Any]:
|
||||
def load(fp: TextIO, *, parse_float: ParseFloat = float) -> dict[str, Any]:
|
||||
"""Parse TOML from a file object."""
|
||||
s = fp.read()
|
||||
return loads(s, parse_float=parse_float)
|
||||
|
||||
|
||||
def loads(s: str, *, parse_float: ParseFloat = float) -> Dict[str, Any]: # noqa: C901
|
||||
def loads(s: str, *, parse_float: ParseFloat = float) -> dict[str, Any]: # noqa: C901
|
||||
"""Parse TOML from a string."""
|
||||
|
||||
# The spec allows converting "\r\n" to "\n", even in string
|
||||
# literals. Let's do so to simplify parsing.
|
||||
src = s.replace("\r\n", "\n")
|
||||
src = s.replace('\r\n', '\n')
|
||||
pos = 0
|
||||
state = State()
|
||||
|
||||
|
|
@ -100,24 +98,24 @@ def loads(s: str, *, parse_float: ParseFloat = float) -> Dict[str, Any]: # noqa
|
|||
char = src[pos]
|
||||
except IndexError:
|
||||
break
|
||||
if char == "\n":
|
||||
if char == '\n':
|
||||
pos += 1
|
||||
continue
|
||||
if char in KEY_INITIAL_CHARS:
|
||||
pos = key_value_rule(src, pos, state, parse_float)
|
||||
pos = skip_chars(src, pos, TOML_WS)
|
||||
elif char == "[":
|
||||
elif char == '[':
|
||||
try:
|
||||
second_char: Optional[str] = src[pos + 1]
|
||||
second_char: str | None = src[pos + 1]
|
||||
except IndexError:
|
||||
second_char = None
|
||||
if second_char == "[":
|
||||
if second_char == '[':
|
||||
pos = create_list_rule(src, pos, state)
|
||||
else:
|
||||
pos = create_dict_rule(src, pos, state)
|
||||
pos = skip_chars(src, pos, TOML_WS)
|
||||
elif char != "#":
|
||||
raise suffixed_err(src, pos, "Invalid statement")
|
||||
elif char != '#':
|
||||
raise suffixed_err(src, pos, 'Invalid statement')
|
||||
|
||||
# 3. Skip comment
|
||||
pos = skip_comment(src, pos)
|
||||
|
|
@ -127,9 +125,9 @@ def loads(s: str, *, parse_float: ParseFloat = float) -> Dict[str, Any]: # noqa
|
|||
char = src[pos]
|
||||
except IndexError:
|
||||
break
|
||||
if char != "\n":
|
||||
if char != '\n':
|
||||
raise suffixed_err(
|
||||
src, pos, "Expected newline or end of document after a statement"
|
||||
src, pos, 'Expected newline or end of document after a statement',
|
||||
)
|
||||
pos += 1
|
||||
|
||||
|
|
@ -156,39 +154,39 @@ class Flags:
|
|||
EXPLICIT_NEST = 1
|
||||
|
||||
def __init__(self) -> None:
|
||||
self._flags: Dict[str, dict] = {}
|
||||
self._flags: dict[str, dict] = {}
|
||||
|
||||
def unset_all(self, key: Key) -> None:
|
||||
cont = self._flags
|
||||
for k in key[:-1]:
|
||||
if k not in cont:
|
||||
return
|
||||
cont = cont[k]["nested"]
|
||||
cont = cont[k]['nested']
|
||||
cont.pop(key[-1], None)
|
||||
|
||||
def set_for_relative_key(self, head_key: Key, rel_key: Key, flag: int) -> None:
|
||||
cont = self._flags
|
||||
for k in head_key:
|
||||
if k not in cont:
|
||||
cont[k] = {"flags": set(), "recursive_flags": set(), "nested": {}}
|
||||
cont = cont[k]["nested"]
|
||||
cont[k] = {'flags': set(), 'recursive_flags': set(), 'nested': {}}
|
||||
cont = cont[k]['nested']
|
||||
for k in rel_key:
|
||||
if k in cont:
|
||||
cont[k]["flags"].add(flag)
|
||||
cont[k]['flags'].add(flag)
|
||||
else:
|
||||
cont[k] = {"flags": {flag}, "recursive_flags": set(), "nested": {}}
|
||||
cont = cont[k]["nested"]
|
||||
cont[k] = {'flags': {flag}, 'recursive_flags': set(), 'nested': {}}
|
||||
cont = cont[k]['nested']
|
||||
|
||||
def set(self, key: Key, flag: int, *, recursive: bool) -> None: # noqa: A003
|
||||
cont = self._flags
|
||||
key_parent, key_stem = key[:-1], key[-1]
|
||||
for k in key_parent:
|
||||
if k not in cont:
|
||||
cont[k] = {"flags": set(), "recursive_flags": set(), "nested": {}}
|
||||
cont = cont[k]["nested"]
|
||||
cont[k] = {'flags': set(), 'recursive_flags': set(), 'nested': {}}
|
||||
cont = cont[k]['nested']
|
||||
if key_stem not in cont:
|
||||
cont[key_stem] = {"flags": set(), "recursive_flags": set(), "nested": {}}
|
||||
cont[key_stem]["recursive_flags" if recursive else "flags"].add(flag)
|
||||
cont[key_stem] = {'flags': set(), 'recursive_flags': set(), 'nested': {}}
|
||||
cont[key_stem]['recursive_flags' if recursive else 'flags'].add(flag)
|
||||
|
||||
def is_(self, key: Key, flag: int) -> bool:
|
||||
if not key:
|
||||
|
|
@ -198,20 +196,20 @@ class Flags:
|
|||
if k not in cont:
|
||||
return False
|
||||
inner_cont = cont[k]
|
||||
if flag in inner_cont["recursive_flags"]:
|
||||
if flag in inner_cont['recursive_flags']:
|
||||
return True
|
||||
cont = inner_cont["nested"]
|
||||
cont = inner_cont['nested']
|
||||
key_stem = key[-1]
|
||||
if key_stem in cont:
|
||||
cont = cont[key_stem]
|
||||
return flag in cont["flags"] or flag in cont["recursive_flags"]
|
||||
return flag in cont['flags'] or flag in cont['recursive_flags']
|
||||
return False
|
||||
|
||||
|
||||
class NestedDict:
|
||||
def __init__(self) -> None:
|
||||
# The parsed content of the TOML document
|
||||
self.dict: Dict[str, Any] = {}
|
||||
self.dict: dict[str, Any] = {}
|
||||
|
||||
def get_or_create_nest(
|
||||
self,
|
||||
|
|
@ -227,7 +225,7 @@ class NestedDict:
|
|||
if access_lists and isinstance(cont, list):
|
||||
cont = cont[-1]
|
||||
if not isinstance(cont, dict):
|
||||
raise KeyError("There is no nest behind this key")
|
||||
raise KeyError('There is no nest behind this key')
|
||||
return cont
|
||||
|
||||
def append_nest_to_list(self, key: Key) -> None:
|
||||
|
|
@ -236,7 +234,7 @@ class NestedDict:
|
|||
if last_key in cont:
|
||||
list_ = cont[last_key]
|
||||
if not isinstance(list_, list):
|
||||
raise KeyError("An object other than list found behind this key")
|
||||
raise KeyError('An object other than list found behind this key')
|
||||
list_.append({})
|
||||
else:
|
||||
cont[last_key] = [{}]
|
||||
|
|
@ -256,7 +254,7 @@ def skip_until(
|
|||
pos: Pos,
|
||||
expect: str,
|
||||
*,
|
||||
error_on: FrozenSet[str],
|
||||
error_on: frozenset[str],
|
||||
error_on_eof: bool,
|
||||
) -> Pos:
|
||||
try:
|
||||
|
|
@ -276,12 +274,12 @@ def skip_until(
|
|||
|
||||
def skip_comment(src: str, pos: Pos) -> Pos:
|
||||
try:
|
||||
char: Optional[str] = src[pos]
|
||||
char: str | None = src[pos]
|
||||
except IndexError:
|
||||
char = None
|
||||
if char == "#":
|
||||
if char == '#':
|
||||
return skip_until(
|
||||
src, pos + 1, "\n", error_on=ILLEGAL_COMMENT_CHARS, error_on_eof=False
|
||||
src, pos + 1, '\n', error_on=ILLEGAL_COMMENT_CHARS, error_on_eof=False,
|
||||
)
|
||||
return pos
|
||||
|
||||
|
|
@ -301,15 +299,15 @@ def create_dict_rule(src: str, pos: Pos, state: State) -> Pos:
|
|||
pos, key = parse_key(src, pos)
|
||||
|
||||
if state.flags.is_(key, Flags.EXPLICIT_NEST) or state.flags.is_(key, Flags.FROZEN):
|
||||
raise suffixed_err(src, pos, f"Can not declare {key} twice")
|
||||
raise suffixed_err(src, pos, f'Can not declare {key} twice')
|
||||
state.flags.set(key, Flags.EXPLICIT_NEST, recursive=False)
|
||||
try:
|
||||
state.out.get_or_create_nest(key)
|
||||
except KeyError:
|
||||
raise suffixed_err(src, pos, "Can not overwrite a value")
|
||||
raise suffixed_err(src, pos, 'Can not overwrite a value')
|
||||
state.header_namespace = key
|
||||
|
||||
if src[pos : pos + 1] != "]":
|
||||
if src[pos: pos + 1] != ']':
|
||||
raise suffixed_err(src, pos, 'Expected "]" at the end of a table declaration')
|
||||
return pos + 1
|
||||
|
||||
|
|
@ -320,7 +318,7 @@ def create_list_rule(src: str, pos: Pos, state: State) -> Pos:
|
|||
pos, key = parse_key(src, pos)
|
||||
|
||||
if state.flags.is_(key, Flags.FROZEN):
|
||||
raise suffixed_err(src, pos, f"Can not mutate immutable namespace {key}")
|
||||
raise suffixed_err(src, pos, f'Can not mutate immutable namespace {key}')
|
||||
# Free the namespace now that it points to another empty list item...
|
||||
state.flags.unset_all(key)
|
||||
# ...but this key precisely is still prohibited from table declaration
|
||||
|
|
@ -328,11 +326,11 @@ def create_list_rule(src: str, pos: Pos, state: State) -> Pos:
|
|||
try:
|
||||
state.out.append_nest_to_list(key)
|
||||
except KeyError:
|
||||
raise suffixed_err(src, pos, "Can not overwrite a value")
|
||||
raise suffixed_err(src, pos, 'Can not overwrite a value')
|
||||
state.header_namespace = key
|
||||
|
||||
end_marker = src[pos : pos + 2]
|
||||
if end_marker != "]]":
|
||||
end_marker = src[pos: pos + 2]
|
||||
if end_marker != ']]':
|
||||
raise suffixed_err(
|
||||
src,
|
||||
pos,
|
||||
|
|
@ -349,16 +347,16 @@ def key_value_rule(src: str, pos: Pos, state: State, parse_float: ParseFloat) ->
|
|||
|
||||
if state.flags.is_(abs_key_parent, Flags.FROZEN):
|
||||
raise suffixed_err(
|
||||
src, pos, f"Can not mutate immutable namespace {abs_key_parent}"
|
||||
src, pos, f'Can not mutate immutable namespace {abs_key_parent}',
|
||||
)
|
||||
# Containers in the relative path can't be opened with the table syntax after this
|
||||
state.flags.set_for_relative_key(state.header_namespace, key, Flags.EXPLICIT_NEST)
|
||||
try:
|
||||
nest = state.out.get_or_create_nest(abs_key_parent)
|
||||
except KeyError:
|
||||
raise suffixed_err(src, pos, "Can not overwrite a value")
|
||||
raise suffixed_err(src, pos, 'Can not overwrite a value')
|
||||
if key_stem in nest:
|
||||
raise suffixed_err(src, pos, "Can not overwrite a value")
|
||||
raise suffixed_err(src, pos, 'Can not overwrite a value')
|
||||
# Mark inline table and array namespaces recursively immutable
|
||||
if isinstance(value, (dict, list)):
|
||||
abs_key = state.header_namespace + key
|
||||
|
|
@ -368,14 +366,14 @@ def key_value_rule(src: str, pos: Pos, state: State, parse_float: ParseFloat) ->
|
|||
|
||||
|
||||
def parse_key_value_pair(
|
||||
src: str, pos: Pos, parse_float: ParseFloat
|
||||
) -> Tuple[Pos, Key, Any]:
|
||||
src: str, pos: Pos, parse_float: ParseFloat,
|
||||
) -> tuple[Pos, Key, Any]:
|
||||
pos, key = parse_key(src, pos)
|
||||
try:
|
||||
char: Optional[str] = src[pos]
|
||||
char: str | None = src[pos]
|
||||
except IndexError:
|
||||
char = None
|
||||
if char != "=":
|
||||
if char != '=':
|
||||
raise suffixed_err(src, pos, 'Expected "=" after a key in a key/value pair')
|
||||
pos += 1
|
||||
pos = skip_chars(src, pos, TOML_WS)
|
||||
|
|
@ -383,16 +381,16 @@ def parse_key_value_pair(
|
|||
return pos, key, value
|
||||
|
||||
|
||||
def parse_key(src: str, pos: Pos) -> Tuple[Pos, Key]:
|
||||
def parse_key(src: str, pos: Pos) -> tuple[Pos, Key]:
|
||||
pos, key_part = parse_key_part(src, pos)
|
||||
key = [key_part]
|
||||
pos = skip_chars(src, pos, TOML_WS)
|
||||
while True:
|
||||
try:
|
||||
char: Optional[str] = src[pos]
|
||||
char: str | None = src[pos]
|
||||
except IndexError:
|
||||
char = None
|
||||
if char != ".":
|
||||
if char != '.':
|
||||
return pos, tuple(key)
|
||||
pos += 1
|
||||
pos = skip_chars(src, pos, TOML_WS)
|
||||
|
|
@ -401,9 +399,9 @@ def parse_key(src: str, pos: Pos) -> Tuple[Pos, Key]:
|
|||
pos = skip_chars(src, pos, TOML_WS)
|
||||
|
||||
|
||||
def parse_key_part(src: str, pos: Pos) -> Tuple[Pos, str]:
|
||||
def parse_key_part(src: str, pos: Pos) -> tuple[Pos, str]:
|
||||
try:
|
||||
char: Optional[str] = src[pos]
|
||||
char: str | None = src[pos]
|
||||
except IndexError:
|
||||
char = None
|
||||
if char in BARE_KEY_CHARS:
|
||||
|
|
@ -414,64 +412,64 @@ def parse_key_part(src: str, pos: Pos) -> Tuple[Pos, str]:
|
|||
return parse_literal_str(src, pos)
|
||||
if char == '"':
|
||||
return parse_one_line_basic_str(src, pos)
|
||||
raise suffixed_err(src, pos, "Invalid initial character for a key part")
|
||||
raise suffixed_err(src, pos, 'Invalid initial character for a key part')
|
||||
|
||||
|
||||
def parse_one_line_basic_str(src: str, pos: Pos) -> Tuple[Pos, str]:
|
||||
def parse_one_line_basic_str(src: str, pos: Pos) -> tuple[Pos, str]:
|
||||
pos += 1
|
||||
return parse_basic_str(src, pos, multiline=False)
|
||||
|
||||
|
||||
def parse_array(src: str, pos: Pos, parse_float: ParseFloat) -> Tuple[Pos, list]:
|
||||
def parse_array(src: str, pos: Pos, parse_float: ParseFloat) -> tuple[Pos, list]:
|
||||
pos += 1
|
||||
array: list = []
|
||||
|
||||
pos = skip_comments_and_array_ws(src, pos)
|
||||
if src[pos : pos + 1] == "]":
|
||||
if src[pos: pos + 1] == ']':
|
||||
return pos + 1, array
|
||||
while True:
|
||||
pos, val = parse_value(src, pos, parse_float)
|
||||
array.append(val)
|
||||
pos = skip_comments_and_array_ws(src, pos)
|
||||
|
||||
c = src[pos : pos + 1]
|
||||
if c == "]":
|
||||
c = src[pos: pos + 1]
|
||||
if c == ']':
|
||||
return pos + 1, array
|
||||
if c != ",":
|
||||
raise suffixed_err(src, pos, "Unclosed array")
|
||||
if c != ',':
|
||||
raise suffixed_err(src, pos, 'Unclosed array')
|
||||
pos += 1
|
||||
|
||||
pos = skip_comments_and_array_ws(src, pos)
|
||||
if src[pos : pos + 1] == "]":
|
||||
if src[pos: pos + 1] == ']':
|
||||
return pos + 1, array
|
||||
|
||||
|
||||
def parse_inline_table(src: str, pos: Pos, parse_float: ParseFloat) -> Tuple[Pos, dict]:
|
||||
def parse_inline_table(src: str, pos: Pos, parse_float: ParseFloat) -> tuple[Pos, dict]:
|
||||
pos += 1
|
||||
nested_dict = NestedDict()
|
||||
flags = Flags()
|
||||
|
||||
pos = skip_chars(src, pos, TOML_WS)
|
||||
if src[pos : pos + 1] == "}":
|
||||
if src[pos: pos + 1] == '}':
|
||||
return pos + 1, nested_dict.dict
|
||||
while True:
|
||||
pos, key, value = parse_key_value_pair(src, pos, parse_float)
|
||||
key_parent, key_stem = key[:-1], key[-1]
|
||||
if flags.is_(key, Flags.FROZEN):
|
||||
raise suffixed_err(src, pos, f"Can not mutate immutable namespace {key}")
|
||||
raise suffixed_err(src, pos, f'Can not mutate immutable namespace {key}')
|
||||
try:
|
||||
nest = nested_dict.get_or_create_nest(key_parent, access_lists=False)
|
||||
except KeyError:
|
||||
raise suffixed_err(src, pos, "Can not overwrite a value")
|
||||
raise suffixed_err(src, pos, 'Can not overwrite a value')
|
||||
if key_stem in nest:
|
||||
raise suffixed_err(src, pos, f'Duplicate inline table key "{key_stem}"')
|
||||
nest[key_stem] = value
|
||||
pos = skip_chars(src, pos, TOML_WS)
|
||||
c = src[pos : pos + 1]
|
||||
if c == "}":
|
||||
c = src[pos: pos + 1]
|
||||
if c == '}':
|
||||
return pos + 1, nested_dict.dict
|
||||
if c != ",":
|
||||
raise suffixed_err(src, pos, "Unclosed inline table")
|
||||
if c != ',':
|
||||
raise suffixed_err(src, pos, 'Unclosed inline table')
|
||||
if isinstance(value, (dict, list)):
|
||||
flags.set(key, Flags.FROZEN, recursive=True)
|
||||
pos += 1
|
||||
|
|
@ -479,62 +477,62 @@ def parse_inline_table(src: str, pos: Pos, parse_float: ParseFloat) -> Tuple[Pos
|
|||
|
||||
|
||||
def parse_basic_str_escape(
|
||||
src: str, pos: Pos, *, multiline: bool = False
|
||||
) -> Tuple[Pos, str]:
|
||||
escape_id = src[pos : pos + 2]
|
||||
src: str, pos: Pos, *, multiline: bool = False,
|
||||
) -> tuple[Pos, str]:
|
||||
escape_id = src[pos: pos + 2]
|
||||
pos += 2
|
||||
if multiline and escape_id in {"\\ ", "\\\t", "\\\n"}:
|
||||
if multiline and escape_id in {'\\ ', '\\\t', '\\\n'}:
|
||||
# Skip whitespace until next non-whitespace character or end of
|
||||
# the doc. Error if non-whitespace is found before newline.
|
||||
if escape_id != "\\\n":
|
||||
if escape_id != '\\\n':
|
||||
pos = skip_chars(src, pos, TOML_WS)
|
||||
char = src[pos : pos + 1]
|
||||
char = src[pos: pos + 1]
|
||||
if not char:
|
||||
return pos, ""
|
||||
if char != "\n":
|
||||
return pos, ''
|
||||
if char != '\n':
|
||||
raise suffixed_err(src, pos, 'Unescaped "\\" in a string')
|
||||
pos += 1
|
||||
pos = skip_chars(src, pos, TOML_WS_AND_NEWLINE)
|
||||
return pos, ""
|
||||
if escape_id == "\\u":
|
||||
return pos, ''
|
||||
if escape_id == '\\u':
|
||||
return parse_hex_char(src, pos, 4)
|
||||
if escape_id == "\\U":
|
||||
if escape_id == '\\U':
|
||||
return parse_hex_char(src, pos, 8)
|
||||
try:
|
||||
return pos, BASIC_STR_ESCAPE_REPLACEMENTS[escape_id]
|
||||
except KeyError:
|
||||
if len(escape_id) != 2:
|
||||
raise suffixed_err(src, pos, "Unterminated string")
|
||||
raise suffixed_err(src, pos, 'Unterminated string')
|
||||
raise suffixed_err(src, pos, 'Unescaped "\\" in a string')
|
||||
|
||||
|
||||
def parse_basic_str_escape_multiline(src: str, pos: Pos) -> Tuple[Pos, str]:
|
||||
def parse_basic_str_escape_multiline(src: str, pos: Pos) -> tuple[Pos, str]:
|
||||
return parse_basic_str_escape(src, pos, multiline=True)
|
||||
|
||||
|
||||
def parse_hex_char(src: str, pos: Pos, hex_len: int) -> Tuple[Pos, str]:
|
||||
hex_str = src[pos : pos + hex_len]
|
||||
def parse_hex_char(src: str, pos: Pos, hex_len: int) -> tuple[Pos, str]:
|
||||
hex_str = src[pos: pos + hex_len]
|
||||
if len(hex_str) != hex_len or any(c not in string.hexdigits for c in hex_str):
|
||||
raise suffixed_err(src, pos, "Invalid hex value")
|
||||
raise suffixed_err(src, pos, 'Invalid hex value')
|
||||
pos += hex_len
|
||||
hex_int = int(hex_str, 16)
|
||||
if not is_unicode_scalar_value(hex_int):
|
||||
raise suffixed_err(src, pos, "Escaped character is not a Unicode scalar value")
|
||||
raise suffixed_err(src, pos, 'Escaped character is not a Unicode scalar value')
|
||||
return pos, chr(hex_int)
|
||||
|
||||
|
||||
def parse_literal_str(src: str, pos: Pos) -> Tuple[Pos, str]:
|
||||
def parse_literal_str(src: str, pos: Pos) -> tuple[Pos, str]:
|
||||
pos += 1 # Skip starting apostrophe
|
||||
start_pos = pos
|
||||
pos = skip_until(
|
||||
src, pos, "'", error_on=ILLEGAL_LITERAL_STR_CHARS, error_on_eof=True
|
||||
src, pos, "'", error_on=ILLEGAL_LITERAL_STR_CHARS, error_on_eof=True,
|
||||
)
|
||||
return pos + 1, src[start_pos:pos] # Skip ending apostrophe
|
||||
|
||||
|
||||
def parse_multiline_str(src: str, pos: Pos, *, literal: bool) -> Tuple[Pos, str]:
|
||||
def parse_multiline_str(src: str, pos: Pos, *, literal: bool) -> tuple[Pos, str]:
|
||||
pos += 3
|
||||
if src[pos : pos + 1] == "\n":
|
||||
if src[pos: pos + 1] == '\n':
|
||||
pos += 1
|
||||
|
||||
if literal:
|
||||
|
|
@ -554,37 +552,37 @@ def parse_multiline_str(src: str, pos: Pos, *, literal: bool) -> Tuple[Pos, str]
|
|||
|
||||
# Add at maximum two extra apostrophes/quotes if the end sequence
|
||||
# is 4 or 5 chars long instead of just 3.
|
||||
if src[pos : pos + 1] != delim:
|
||||
if src[pos: pos + 1] != delim:
|
||||
return pos, result
|
||||
pos += 1
|
||||
if src[pos : pos + 1] != delim:
|
||||
if src[pos: pos + 1] != delim:
|
||||
return pos, result + delim
|
||||
pos += 1
|
||||
return pos, result + (delim * 2)
|
||||
|
||||
|
||||
def parse_basic_str(src: str, pos: Pos, *, multiline: bool) -> Tuple[Pos, str]:
|
||||
def parse_basic_str(src: str, pos: Pos, *, multiline: bool) -> tuple[Pos, str]:
|
||||
if multiline:
|
||||
error_on = ILLEGAL_MULTILINE_BASIC_STR_CHARS
|
||||
parse_escapes = parse_basic_str_escape_multiline
|
||||
else:
|
||||
error_on = ILLEGAL_BASIC_STR_CHARS
|
||||
parse_escapes = parse_basic_str_escape
|
||||
result = ""
|
||||
result = ''
|
||||
start_pos = pos
|
||||
while True:
|
||||
try:
|
||||
char = src[pos]
|
||||
except IndexError:
|
||||
raise suffixed_err(src, pos, "Unterminated string")
|
||||
raise suffixed_err(src, pos, 'Unterminated string')
|
||||
if char == '"':
|
||||
if not multiline:
|
||||
return pos + 1, result + src[start_pos:pos]
|
||||
if src[pos + 1 : pos + 3] == '""':
|
||||
if src[pos + 1: pos + 3] == '""':
|
||||
return pos + 3, result + src[start_pos:pos]
|
||||
pos += 1
|
||||
continue
|
||||
if char == "\\":
|
||||
if char == '\\':
|
||||
result += src[start_pos:pos]
|
||||
pos, parsed_escape = parse_escapes(src, pos)
|
||||
result += parsed_escape
|
||||
|
|
@ -595,39 +593,39 @@ def parse_basic_str(src: str, pos: Pos, *, multiline: bool) -> Tuple[Pos, str]:
|
|||
pos += 1
|
||||
|
||||
|
||||
def parse_regex(src: str, pos: Pos, regex: "Pattern") -> Tuple[Pos, str]:
|
||||
def parse_regex(src: str, pos: Pos, regex: Pattern) -> tuple[Pos, str]:
|
||||
match = regex.match(src, pos)
|
||||
if not match:
|
||||
raise suffixed_err(src, pos, "Unexpected sequence")
|
||||
raise suffixed_err(src, pos, 'Unexpected sequence')
|
||||
return match.end(), match.group()
|
||||
|
||||
|
||||
def parse_value( # noqa: C901
|
||||
src: str, pos: Pos, parse_float: ParseFloat
|
||||
) -> Tuple[Pos, Any]:
|
||||
src: str, pos: Pos, parse_float: ParseFloat,
|
||||
) -> tuple[Pos, Any]:
|
||||
try:
|
||||
char: Optional[str] = src[pos]
|
||||
char: str | None = src[pos]
|
||||
except IndexError:
|
||||
char = None
|
||||
|
||||
# Basic strings
|
||||
if char == '"':
|
||||
if src[pos + 1 : pos + 3] == '""':
|
||||
if src[pos + 1: pos + 3] == '""':
|
||||
return parse_multiline_str(src, pos, literal=False)
|
||||
return parse_one_line_basic_str(src, pos)
|
||||
|
||||
# Literal strings
|
||||
if char == "'":
|
||||
if src[pos + 1 : pos + 3] == "''":
|
||||
if src[pos + 1: pos + 3] == "''":
|
||||
return parse_multiline_str(src, pos, literal=True)
|
||||
return parse_literal_str(src, pos)
|
||||
|
||||
# Booleans
|
||||
if char == "t":
|
||||
if src[pos + 1 : pos + 4] == "rue":
|
||||
if char == 't':
|
||||
if src[pos + 1: pos + 4] == 'rue':
|
||||
return pos + 4, True
|
||||
if char == "f":
|
||||
if src[pos + 1 : pos + 5] == "alse":
|
||||
if char == 'f':
|
||||
if src[pos + 1: pos + 5] == 'alse':
|
||||
return pos + 5, False
|
||||
|
||||
# Dates and times
|
||||
|
|
@ -636,22 +634,22 @@ def parse_value( # noqa: C901
|
|||
try:
|
||||
datetime_obj = match_to_datetime(datetime_match)
|
||||
except ValueError:
|
||||
raise suffixed_err(src, pos, "Invalid date or datetime")
|
||||
raise suffixed_err(src, pos, 'Invalid date or datetime')
|
||||
return datetime_match.end(), datetime_obj
|
||||
localtime_match = RE_LOCALTIME.match(src, pos)
|
||||
if localtime_match:
|
||||
return localtime_match.end(), match_to_localtime(localtime_match)
|
||||
|
||||
# Non-decimal integers
|
||||
if char == "0":
|
||||
second_char = src[pos + 1 : pos + 2]
|
||||
if second_char == "x":
|
||||
if char == '0':
|
||||
second_char = src[pos + 1: pos + 2]
|
||||
if second_char == 'x':
|
||||
pos, hex_str = parse_regex(src, pos + 2, RE_HEX)
|
||||
return pos, int(hex_str, 16)
|
||||
if second_char == "o":
|
||||
if second_char == 'o':
|
||||
pos, oct_str = parse_regex(src, pos + 2, RE_OCT)
|
||||
return pos, int(oct_str, 8)
|
||||
if second_char == "b":
|
||||
if second_char == 'b':
|
||||
pos, bin_str = parse_regex(src, pos + 2, RE_BIN)
|
||||
return pos, int(bin_str, 2)
|
||||
|
||||
|
|
@ -664,22 +662,22 @@ def parse_value( # noqa: C901
|
|||
return number_match.end(), match_to_number(number_match, parse_float)
|
||||
|
||||
# Arrays
|
||||
if char == "[":
|
||||
if char == '[':
|
||||
return parse_array(src, pos, parse_float)
|
||||
|
||||
# Inline tables
|
||||
if char == "{":
|
||||
if char == '{':
|
||||
return parse_inline_table(src, pos, parse_float)
|
||||
|
||||
# Special floats
|
||||
first_three = src[pos : pos + 3]
|
||||
if first_three in {"inf", "nan"}:
|
||||
first_three = src[pos: pos + 3]
|
||||
if first_three in {'inf', 'nan'}:
|
||||
return pos + 3, parse_float(first_three)
|
||||
first_four = src[pos : pos + 4]
|
||||
if first_four in {"-inf", "+inf", "-nan", "+nan"}:
|
||||
first_four = src[pos: pos + 4]
|
||||
if first_four in {'-inf', '+inf', '-nan', '+nan'}:
|
||||
return pos + 4, parse_float(first_four)
|
||||
|
||||
raise suffixed_err(src, pos, "Invalid value")
|
||||
raise suffixed_err(src, pos, 'Invalid value')
|
||||
|
||||
|
||||
def suffixed_err(src: str, pos: Pos, msg: str) -> TOMLDecodeError:
|
||||
|
|
@ -688,15 +686,15 @@ def suffixed_err(src: str, pos: Pos, msg: str) -> TOMLDecodeError:
|
|||
|
||||
def coord_repr(src: str, pos: Pos) -> str:
|
||||
if pos >= len(src):
|
||||
return "end of document"
|
||||
line = src.count("\n", 0, pos) + 1
|
||||
return 'end of document'
|
||||
line = src.count('\n', 0, pos) + 1
|
||||
if line == 1:
|
||||
column = pos + 1
|
||||
else:
|
||||
column = pos - src.rindex("\n", 0, pos)
|
||||
return f"line {line}, column {column}"
|
||||
column = pos - src.rindex('\n', 0, pos)
|
||||
return f'line {line}, column {column}'
|
||||
|
||||
return TOMLDecodeError(f"{msg} (at {coord_repr(src, pos)})")
|
||||
return TOMLDecodeError(f'{msg} (at {coord_repr(src, pos)})')
|
||||
|
||||
|
||||
def is_unicode_scalar_value(codepoint: int) -> bool:
|
||||
|
|
|
|||
|
|
@ -1,6 +1,16 @@
|
|||
from datetime import date, datetime, time, timedelta, timezone, tzinfo
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
from typing import TYPE_CHECKING, Any, Optional, Union
|
||||
from datetime import date
|
||||
from datetime import datetime
|
||||
from datetime import time
|
||||
from datetime import timedelta
|
||||
from datetime import timezone
|
||||
from datetime import tzinfo
|
||||
from typing import Any
|
||||
from typing import Optional
|
||||
from typing import TYPE_CHECKING
|
||||
from typing import Union
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from re import Match
|
||||
|
|
@ -10,28 +20,28 @@ if TYPE_CHECKING:
|
|||
# E.g.
|
||||
# - 00:32:00.999999
|
||||
# - 00:32:00
|
||||
_TIME_RE_STR = r"([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])(\.[0-9]+)?"
|
||||
_TIME_RE_STR = r'([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])(\.[0-9]+)?'
|
||||
|
||||
RE_HEX = re.compile(r"[0-9A-Fa-f](?:_?[0-9A-Fa-f])*")
|
||||
RE_BIN = re.compile(r"[01](?:_?[01])*")
|
||||
RE_OCT = re.compile(r"[0-7](?:_?[0-7])*")
|
||||
RE_HEX = re.compile(r'[0-9A-Fa-f](?:_?[0-9A-Fa-f])*')
|
||||
RE_BIN = re.compile(r'[01](?:_?[01])*')
|
||||
RE_OCT = re.compile(r'[0-7](?:_?[0-7])*')
|
||||
RE_NUMBER = re.compile(
|
||||
r"[+-]?(?:0|[1-9](?:_?[0-9])*)" # integer
|
||||
+ r"(?:\.[0-9](?:_?[0-9])*)?" # optional fractional part
|
||||
+ r"(?:[eE][+-]?[0-9](?:_?[0-9])*)?" # optional exponent part
|
||||
r'[+-]?(?:0|[1-9](?:_?[0-9])*)' + # integer
|
||||
r'(?:\.[0-9](?:_?[0-9])*)?' + # optional fractional part
|
||||
r'(?:[eE][+-]?[0-9](?:_?[0-9])*)?', # optional exponent part
|
||||
)
|
||||
RE_LOCALTIME = re.compile(_TIME_RE_STR)
|
||||
RE_DATETIME = re.compile(
|
||||
r"([0-9]{4})-(0[1-9]|1[0-2])-(0[1-9]|1[0-9]|2[0-9]|3[01])" # date, e.g. 1988-10-27
|
||||
+ r"(?:"
|
||||
+ r"[T ]"
|
||||
+ _TIME_RE_STR
|
||||
+ r"(?:(Z)|([+-])([01][0-9]|2[0-3]):([0-5][0-9]))?" # time offset
|
||||
+ r")?"
|
||||
r'([0-9]{4})-(0[1-9]|1[0-2])-(0[1-9]|1[0-9]|2[0-9]|3[01])' + # date, e.g. 1988-10-27
|
||||
r'(?:' +
|
||||
r'[T ]' +
|
||||
_TIME_RE_STR +
|
||||
r'(?:(Z)|([+-])([01][0-9]|2[0-3]):([0-5][0-9]))?' + # time offset
|
||||
r')?',
|
||||
)
|
||||
|
||||
|
||||
def match_to_datetime(match: "Match") -> Union[datetime, date]:
|
||||
def match_to_datetime(match: Match) -> datetime | date:
|
||||
"""Convert a `RE_DATETIME` match to `datetime.datetime` or `datetime.date`.
|
||||
|
||||
Raises ValueError if the match does not correspond to a valid date
|
||||
|
|
@ -54,14 +64,14 @@ def match_to_datetime(match: "Match") -> Union[datetime, date]:
|
|||
if hour_str is None:
|
||||
return date(year, month, day)
|
||||
hour, minute, sec = int(hour_str), int(minute_str), int(sec_str)
|
||||
micros = int(micros_str[1:].ljust(6, "0")[:6]) if micros_str else 0
|
||||
micros = int(micros_str[1:].ljust(6, '0')[:6]) if micros_str else 0
|
||||
if offset_dir_str:
|
||||
offset_dir = 1 if offset_dir_str == "+" else -1
|
||||
tz: Optional[tzinfo] = timezone(
|
||||
offset_dir = 1 if offset_dir_str == '+' else -1
|
||||
tz: tzinfo | None = timezone(
|
||||
timedelta(
|
||||
hours=offset_dir * int(offset_hour_str),
|
||||
minutes=offset_dir * int(offset_minute_str),
|
||||
)
|
||||
),
|
||||
)
|
||||
elif zulu_time:
|
||||
tz = timezone.utc
|
||||
|
|
@ -70,14 +80,14 @@ def match_to_datetime(match: "Match") -> Union[datetime, date]:
|
|||
return datetime(year, month, day, hour, minute, sec, micros, tzinfo=tz)
|
||||
|
||||
|
||||
def match_to_localtime(match: "Match") -> time:
|
||||
def match_to_localtime(match: Match) -> time:
|
||||
hour_str, minute_str, sec_str, micros_str = match.groups()
|
||||
micros = int(micros_str[1:].ljust(6, "0")[:6]) if micros_str else 0
|
||||
micros = int(micros_str[1:].ljust(6, '0')[:6]) if micros_str else 0
|
||||
return time(int(hour_str), int(minute_str), int(sec_str), micros)
|
||||
|
||||
|
||||
def match_to_number(match: "Match", parse_float: "ParseFloat") -> Any:
|
||||
def match_to_number(match: Match, parse_float: ParseFloat) -> Any:
|
||||
match_str = match.group()
|
||||
if "." in match_str or "e" in match_str or "E" in match_str:
|
||||
if '.' in match_str or 'e' in match_str or 'E' in match_str:
|
||||
return parse_float(match_str)
|
||||
return int(match_str)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue