Voice et bot modif

This commit is contained in:
pi 2026-06-16 17:09:34 +00:00
parent 189d56026b
commit 7333a22bcd
10774 changed files with 634644 additions and 933308 deletions

View file

@ -1,21 +0,0 @@
MIT License
Copyright (c) 2021 Taneli Hukkinen
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View file

@ -3,6 +3,9 @@
# Licensed to PSF under a Contributor Agreement.
__all__ = ("loads", "load", "TOMLDecodeError")
__version__ = "2.3.0" # DO NOT EDIT THIS LINE MANUALLY. LET bump2version UTILITY DO IT
__version__ = "2.0.1" # DO NOT EDIT THIS LINE MANUALLY. LET bump2version UTILITY DO IT
from ._parser import TOMLDecodeError, load, loads
# Pretend this exception was created here.
TOMLDecodeError.__module__ = __name__

View file

@ -4,8 +4,10 @@
from __future__ import annotations
import sys
from collections.abc import Iterable
import string
from types import MappingProxyType
from typing import Any, BinaryIO, NamedTuple
from ._re import (
RE_DATETIME,
@ -15,125 +17,44 @@ from ._re import (
match_to_localtime,
match_to_number,
)
from ._types import Key, ParseFloat, Pos
TYPE_CHECKING = False
if TYPE_CHECKING:
from collections.abc import Iterable
from typing import IO, Any, Final
from ._types import Key, ParseFloat, Pos
# Inline tables/arrays are implemented using recursion. Pathologically
# nested documents cause pure Python to raise RecursionError (which is OK),
# but mypyc binary wheels will crash unrecoverably (not OK). According to
# mypyc docs this will be fixed in the future:
# https://mypyc.readthedocs.io/en/latest/differences_from_python.html#stack-overflows
# Before mypyc's fix is in, recursion needs to be limited by this library.
# Choosing `sys.getrecursionlimit()` as maximum inline table/array nesting
# level, as it allows more nesting than pure Python, but still seems a far
# lower number than where mypyc binaries crash.
MAX_INLINE_NESTING: Final = sys.getrecursionlimit()
ASCII_CTRL: Final = frozenset(chr(i) for i in range(32)) | frozenset(chr(127))
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: Final = ASCII_CTRL - frozenset("\t")
ILLEGAL_MULTILINE_BASIC_STR_CHARS: Final = ASCII_CTRL - frozenset("\t\n")
ILLEGAL_BASIC_STR_CHARS = ASCII_CTRL - frozenset("\t")
ILLEGAL_MULTILINE_BASIC_STR_CHARS = ASCII_CTRL - frozenset("\t\n")
ILLEGAL_LITERAL_STR_CHARS: Final = ILLEGAL_BASIC_STR_CHARS
ILLEGAL_MULTILINE_LITERAL_STR_CHARS: Final = ILLEGAL_MULTILINE_BASIC_STR_CHARS
ILLEGAL_LITERAL_STR_CHARS = ILLEGAL_BASIC_STR_CHARS
ILLEGAL_MULTILINE_LITERAL_STR_CHARS = ILLEGAL_MULTILINE_BASIC_STR_CHARS
ILLEGAL_COMMENT_CHARS: Final = ILLEGAL_BASIC_STR_CHARS
ILLEGAL_COMMENT_CHARS = ILLEGAL_BASIC_STR_CHARS
TOML_WS: Final = frozenset(" \t")
TOML_WS_AND_NEWLINE: Final = TOML_WS | frozenset("\n")
BARE_KEY_CHARS: Final = frozenset(
"abcdefghijklmnopqrstuvwxyz" "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "0123456789" "-_"
)
KEY_INITIAL_CHARS: Final = BARE_KEY_CHARS | frozenset("\"'")
HEXDIGIT_CHARS: Final = frozenset("abcdef" "ABCDEF" "0123456789")
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("\"'")
HEXDIGIT_CHARS = frozenset(string.hexdigits)
BASIC_STR_ESCAPE_REPLACEMENTS: Final = MappingProxyType(
BASIC_STR_ESCAPE_REPLACEMENTS = MappingProxyType(
{
"\\b": "\u0008", # backspace
"\\t": "\u0009", # tab
"\\n": "\u000a", # linefeed
"\\f": "\u000c", # form feed
"\\r": "\u000d", # carriage return
"\\n": "\u000A", # linefeed
"\\f": "\u000C", # form feed
"\\r": "\u000D", # carriage return
'\\"': "\u0022", # quote
"\\\\": "\u005c", # backslash
"\\\\": "\u005C", # backslash
}
)
class DEPRECATED_DEFAULT:
"""Sentinel to be used as default arg during deprecation
period of TOMLDecodeError's free-form arguments."""
class TOMLDecodeError(ValueError):
"""An error raised if a document is not valid TOML.
Adds the following attributes to ValueError:
msg: The unformatted error message
doc: The TOML document being parsed
pos: The index of doc where parsing failed
lineno: The line corresponding to pos
colno: The column corresponding to pos
"""
def __init__(
self,
msg: str | type[DEPRECATED_DEFAULT] = DEPRECATED_DEFAULT,
doc: str | type[DEPRECATED_DEFAULT] = DEPRECATED_DEFAULT,
pos: Pos | type[DEPRECATED_DEFAULT] = DEPRECATED_DEFAULT,
*args: Any,
):
if (
args
or not isinstance(msg, str)
or not isinstance(doc, str)
or not isinstance(pos, int)
):
import warnings
warnings.warn(
"Free-form arguments for TOMLDecodeError are deprecated. "
"Please set 'msg' (str), 'doc' (str) and 'pos' (int) arguments only.",
DeprecationWarning,
stacklevel=2,
)
if pos is not DEPRECATED_DEFAULT:
args = pos, *args
if doc is not DEPRECATED_DEFAULT:
args = doc, *args
if msg is not DEPRECATED_DEFAULT:
args = msg, *args
ValueError.__init__(self, *args)
return
lineno = doc.count("\n", 0, pos) + 1
if lineno == 1:
colno = pos + 1
else:
colno = pos - doc.rindex("\n", 0, pos)
if pos >= len(doc):
coord_repr = "end of document"
else:
coord_repr = f"line {lineno}, column {colno}"
errmsg = f"{msg} (at {coord_repr})"
ValueError.__init__(self, errmsg)
self.msg = msg
self.doc = doc
self.pos = pos
self.lineno = lineno
self.colno = colno
"""An error raised if a document is not valid TOML."""
def load(__fp: IO[bytes], *, parse_float: ParseFloat = float) -> dict[str, Any]:
def load(__fp: BinaryIO, *, parse_float: ParseFloat = float) -> dict[str, Any]:
"""Parse TOML from a binary file object."""
b = __fp.read()
try:
@ -150,14 +71,9 @@ def loads(__s: str, *, parse_float: ParseFloat = float) -> dict[str, Any]: # no
# The spec allows converting "\r\n" to "\n", even in string
# literals. Let's do so to simplify parsing.
try:
src = __s.replace("\r\n", "\n")
except (AttributeError, TypeError):
raise TypeError(
f"Expected str object, not '{type(__s).__qualname__}'"
) from None
src = __s.replace("\r\n", "\n")
pos = 0
out = Output()
out = Output(NestedDict(), Flags())
header: Key = ()
parse_float = make_safe_parse_float(parse_float)
@ -197,7 +113,7 @@ def loads(__s: str, *, parse_float: ParseFloat = float) -> dict[str, Any]: # no
pos, header = create_dict_rule(src, pos, out)
pos = skip_chars(src, pos, TOML_WS)
elif char != "#":
raise TOMLDecodeError("Invalid statement", src, pos)
raise suffixed_err(src, pos, "Invalid statement")
# 3. Skip comment
pos = skip_comment(src, pos)
@ -208,8 +124,8 @@ def loads(__s: str, *, parse_float: ParseFloat = float) -> dict[str, Any]: # no
except IndexError:
break
if char != "\n":
raise TOMLDecodeError(
"Expected newline or end of document after a statement", src, pos
raise suffixed_err(
src, pos, "Expected newline or end of document after a statement"
)
pos += 1
@ -220,13 +136,13 @@ class Flags:
"""Flags that map to parsed keys/namespaces."""
# Marks an immutable namespace (inline array or inline table).
FROZEN: Final = 0
FROZEN = 0
# Marks a nest that has been explicitly created and can no longer
# be opened using the "[table]" syntax.
EXPLICIT_NEST: Final = 1
EXPLICIT_NEST = 1
def __init__(self) -> None:
self._flags: dict[str, dict[Any, Any]] = {}
self._flags: dict[str, dict] = {}
self._pending_flags: set[tuple[Key, int]] = set()
def add_pending(self, key: Key, flag: int) -> None:
@ -269,8 +185,8 @@ class Flags:
cont = inner_cont["nested"]
key_stem = key[-1]
if key_stem in cont:
inner_cont = cont[key_stem]
return flag in inner_cont["flags"] or flag in inner_cont["recursive_flags"]
cont = cont[key_stem]
return flag in cont["flags"] or flag in cont["recursive_flags"]
return False
@ -284,7 +200,7 @@ class NestedDict:
key: Key,
*,
access_lists: bool = True,
) -> dict[str, Any]:
) -> dict:
cont: Any = self.dict
for k in key:
if k not in cont:
@ -294,7 +210,7 @@ class NestedDict:
cont = cont[-1]
if not isinstance(cont, dict):
raise KeyError("There is no nest behind this key")
return cont # type: ignore[no-any-return]
return cont
def append_nest_to_list(self, key: Key) -> None:
cont = self.get_or_create_nest(key[:-1])
@ -308,10 +224,9 @@ class NestedDict:
cont[last_key] = [{}]
class Output:
def __init__(self) -> None:
self.data = NestedDict()
self.flags = Flags()
class Output(NamedTuple):
data: NestedDict
flags: Flags
def skip_chars(src: str, pos: Pos, chars: Iterable[str]) -> Pos:
@ -336,12 +251,12 @@ def skip_until(
except ValueError:
new_pos = len(src)
if error_on_eof:
raise TOMLDecodeError(f"Expected {expect!r}", src, new_pos) from None
raise suffixed_err(src, new_pos, f"Expected {expect!r}") from None
if not error_on.isdisjoint(src[pos:new_pos]):
while src[pos] not in error_on:
pos += 1
raise TOMLDecodeError(f"Found invalid character {src[pos]!r}", src, pos)
raise suffixed_err(src, pos, f"Found invalid character {src[pos]!r}")
return new_pos
@ -372,17 +287,15 @@ def create_dict_rule(src: str, pos: Pos, out: Output) -> tuple[Pos, Key]:
pos, key = parse_key(src, pos)
if out.flags.is_(key, Flags.EXPLICIT_NEST) or out.flags.is_(key, Flags.FROZEN):
raise TOMLDecodeError(f"Cannot declare {key} twice", src, pos)
raise suffixed_err(src, pos, f"Cannot declare {key} twice")
out.flags.set(key, Flags.EXPLICIT_NEST, recursive=False)
try:
out.data.get_or_create_nest(key)
except KeyError:
raise TOMLDecodeError("Cannot overwrite a value", src, pos) from None
raise suffixed_err(src, pos, "Cannot overwrite a value") from None
if not src.startswith("]", pos):
raise TOMLDecodeError(
"Expected ']' at the end of a table declaration", src, pos
)
raise suffixed_err(src, pos, "Expected ']' at the end of a table declaration")
return pos + 1, key
@ -392,7 +305,7 @@ def create_list_rule(src: str, pos: Pos, out: Output) -> tuple[Pos, Key]:
pos, key = parse_key(src, pos)
if out.flags.is_(key, Flags.FROZEN):
raise TOMLDecodeError(f"Cannot mutate immutable namespace {key}", src, pos)
raise suffixed_err(src, pos, f"Cannot mutate immutable namespace {key}")
# Free the namespace now that it points to another empty list item...
out.flags.unset_all(key)
# ...but this key precisely is still prohibited from table declaration
@ -400,19 +313,17 @@ def create_list_rule(src: str, pos: Pos, out: Output) -> tuple[Pos, Key]:
try:
out.data.append_nest_to_list(key)
except KeyError:
raise TOMLDecodeError("Cannot overwrite a value", src, pos) from None
raise suffixed_err(src, pos, "Cannot overwrite a value") from None
if not src.startswith("]]", pos):
raise TOMLDecodeError(
"Expected ']]' at the end of an array declaration", src, pos
)
raise suffixed_err(src, pos, "Expected ']]' at the end of an array declaration")
return pos + 2, key
def key_value_rule(
src: str, pos: Pos, out: Output, header: Key, parse_float: ParseFloat
) -> Pos:
pos, key, value = parse_key_value_pair(src, pos, parse_float, nest_lvl=0)
pos, key, value = parse_key_value_pair(src, pos, parse_float)
key_parent, key_stem = key[:-1], key[-1]
abs_key_parent = header + key_parent
@ -420,22 +331,22 @@ def key_value_rule(
for cont_key in relative_path_cont_keys:
# Check that dotted key syntax does not redefine an existing table
if out.flags.is_(cont_key, Flags.EXPLICIT_NEST):
raise TOMLDecodeError(f"Cannot redefine namespace {cont_key}", src, pos)
raise suffixed_err(src, pos, f"Cannot redefine namespace {cont_key}")
# Containers in the relative path can't be opened with the table syntax or
# dotted key/value syntax in following table sections.
out.flags.add_pending(cont_key, Flags.EXPLICIT_NEST)
if out.flags.is_(abs_key_parent, Flags.FROZEN):
raise TOMLDecodeError(
f"Cannot mutate immutable namespace {abs_key_parent}", src, pos
raise suffixed_err(
src, pos, f"Cannot mutate immutable namespace {abs_key_parent}"
)
try:
nest = out.data.get_or_create_nest(abs_key_parent)
except KeyError:
raise TOMLDecodeError("Cannot overwrite a value", src, pos) from None
raise suffixed_err(src, pos, "Cannot overwrite a value") from None
if key_stem in nest:
raise TOMLDecodeError("Cannot overwrite a value", src, pos)
raise suffixed_err(src, pos, "Cannot overwrite a value")
# Mark inline table and array namespaces recursively immutable
if isinstance(value, (dict, list)):
out.flags.set(header + key, Flags.FROZEN, recursive=True)
@ -444,7 +355,7 @@ def key_value_rule(
def parse_key_value_pair(
src: str, pos: Pos, parse_float: ParseFloat, nest_lvl: int
src: str, pos: Pos, parse_float: ParseFloat
) -> tuple[Pos, Key, Any]:
pos, key = parse_key(src, pos)
try:
@ -452,10 +363,10 @@ def parse_key_value_pair(
except IndexError:
char = None
if char != "=":
raise TOMLDecodeError("Expected '=' after a key in a key/value pair", src, pos)
raise suffixed_err(src, pos, "Expected '=' after a key in a key/value pair")
pos += 1
pos = skip_chars(src, pos, TOML_WS)
pos, value = parse_value(src, pos, parse_float, nest_lvl)
pos, value = parse_value(src, pos, parse_float)
return pos, key, value
@ -490,7 +401,7 @@ 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 TOMLDecodeError("Invalid initial character for a key part", src, pos)
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]:
@ -498,17 +409,15 @@ def parse_one_line_basic_str(src: str, pos: Pos) -> tuple[Pos, str]:
return parse_basic_str(src, pos, multiline=False)
def parse_array(
src: str, pos: Pos, parse_float: ParseFloat, nest_lvl: int
) -> tuple[Pos, list[Any]]:
def parse_array(src: str, pos: Pos, parse_float: ParseFloat) -> tuple[Pos, list]:
pos += 1
array: list[Any] = []
array: list = []
pos = skip_comments_and_array_ws(src, pos)
if src.startswith("]", pos):
return pos + 1, array
while True:
pos, val = parse_value(src, pos, parse_float, nest_lvl)
pos, val = parse_value(src, pos, parse_float)
array.append(val)
pos = skip_comments_and_array_ws(src, pos)
@ -516,7 +425,7 @@ def parse_array(
if c == "]":
return pos + 1, array
if c != ",":
raise TOMLDecodeError("Unclosed array", src, pos)
raise suffixed_err(src, pos, "Unclosed array")
pos += 1
pos = skip_comments_and_array_ws(src, pos)
@ -524,9 +433,7 @@ def parse_array(
return pos + 1, array
def parse_inline_table(
src: str, pos: Pos, parse_float: ParseFloat, nest_lvl: int
) -> tuple[Pos, dict[str, Any]]:
def parse_inline_table(src: str, pos: Pos, parse_float: ParseFloat) -> tuple[Pos, dict]:
pos += 1
nested_dict = NestedDict()
flags = Flags()
@ -535,23 +442,23 @@ def parse_inline_table(
if src.startswith("}", pos):
return pos + 1, nested_dict.dict
while True:
pos, key, value = parse_key_value_pair(src, pos, parse_float, nest_lvl)
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 TOMLDecodeError(f"Cannot mutate immutable namespace {key}", src, pos)
raise suffixed_err(src, pos, f"Cannot mutate immutable namespace {key}")
try:
nest = nested_dict.get_or_create_nest(key_parent, access_lists=False)
except KeyError:
raise TOMLDecodeError("Cannot overwrite a value", src, pos) from None
raise suffixed_err(src, pos, "Cannot overwrite a value") from None
if key_stem in nest:
raise TOMLDecodeError(f"Duplicate inline table key {key_stem!r}", src, pos)
raise suffixed_err(src, pos, f"Duplicate inline table key {key_stem!r}")
nest[key_stem] = value
pos = skip_chars(src, pos, TOML_WS)
c = src[pos : pos + 1]
if c == "}":
return pos + 1, nested_dict.dict
if c != ",":
raise TOMLDecodeError("Unclosed inline table", src, pos)
raise suffixed_err(src, pos, "Unclosed inline table")
if isinstance(value, (dict, list)):
flags.set(key, Flags.FROZEN, recursive=True)
pos += 1
@ -573,7 +480,7 @@ def parse_basic_str_escape(
except IndexError:
return pos, ""
if char != "\n":
raise TOMLDecodeError("Unescaped '\\' in a string", src, pos)
raise suffixed_err(src, pos, "Unescaped '\\' in a string")
pos += 1
pos = skip_chars(src, pos, TOML_WS_AND_NEWLINE)
return pos, ""
@ -584,7 +491,7 @@ def parse_basic_str_escape(
try:
return pos, BASIC_STR_ESCAPE_REPLACEMENTS[escape_id]
except KeyError:
raise TOMLDecodeError("Unescaped '\\' in a string", src, pos) from None
raise suffixed_err(src, pos, "Unescaped '\\' in a string") from None
def parse_basic_str_escape_multiline(src: str, pos: Pos) -> tuple[Pos, str]:
@ -594,13 +501,11 @@ def parse_basic_str_escape_multiline(src: str, pos: Pos) -> tuple[Pos, str]:
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 not HEXDIGIT_CHARS.issuperset(hex_str):
raise TOMLDecodeError("Invalid hex value", src, pos)
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 TOMLDecodeError(
"Escaped character is not a Unicode scalar value", src, pos
)
raise suffixed_err(src, pos, "Escaped character is not a Unicode scalar value")
return pos, chr(hex_int)
@ -657,7 +562,7 @@ def parse_basic_str(src: str, pos: Pos, *, multiline: bool) -> tuple[Pos, str]:
try:
char = src[pos]
except IndexError:
raise TOMLDecodeError("Unterminated string", src, pos) from None
raise suffixed_err(src, pos, "Unterminated string") from None
if char == '"':
if not multiline:
return pos + 1, result + src[start_pos:pos]
@ -672,21 +577,13 @@ def parse_basic_str(src: str, pos: Pos, *, multiline: bool) -> tuple[Pos, str]:
start_pos = pos
continue
if char in error_on:
raise TOMLDecodeError(f"Illegal character {char!r}", src, pos)
raise suffixed_err(src, pos, f"Illegal character {char!r}")
pos += 1
def parse_value( # noqa: C901
src: str, pos: Pos, parse_float: ParseFloat, nest_lvl: int
src: str, pos: Pos, parse_float: ParseFloat
) -> tuple[Pos, Any]:
if nest_lvl > MAX_INLINE_NESTING:
# Pure Python should have raised RecursionError already.
# This ensures mypyc binaries eventually do the same.
raise RecursionError( # pragma: no cover
"TOML inline arrays/tables are nested more than the allowed"
f" {MAX_INLINE_NESTING} levels"
)
try:
char: str | None = src[pos]
except IndexError:
@ -716,11 +613,11 @@ def parse_value( # noqa: C901
# Arrays
if char == "[":
return parse_array(src, pos, parse_float, nest_lvl + 1)
return parse_array(src, pos, parse_float)
# Inline tables
if char == "{":
return parse_inline_table(src, pos, parse_float, nest_lvl + 1)
return parse_inline_table(src, pos, parse_float)
# Dates and times
datetime_match = RE_DATETIME.match(src, pos)
@ -728,7 +625,7 @@ def parse_value( # noqa: C901
try:
datetime_obj = match_to_datetime(datetime_match)
except ValueError as e:
raise TOMLDecodeError("Invalid date or datetime", src, pos) from e
raise suffixed_err(src, pos, "Invalid date or datetime") from e
return datetime_match.end(), datetime_obj
localtime_match = RE_LOCALTIME.match(src, pos)
if localtime_match:
@ -749,7 +646,24 @@ def parse_value( # noqa: C901
if first_four in {"-inf", "+inf", "-nan", "+nan"}:
return pos + 4, parse_float(first_four)
raise TOMLDecodeError("Invalid value", src, pos)
raise suffixed_err(src, pos, "Invalid value")
def suffixed_err(src: str, pos: Pos, msg: str) -> TOMLDecodeError:
"""Return a `TOMLDecodeError` where error message is suffixed with
coordinates in source."""
def coord_repr(src: str, pos: Pos) -> str:
if pos >= len(src):
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}"
return TOMLDecodeError(f"{msg} (at {coord_repr(src, pos)})")
def is_unicode_scalar_value(codepoint: int) -> bool:
@ -765,7 +679,7 @@ def make_safe_parse_float(parse_float: ParseFloat) -> ParseFloat:
instead of returning illegal types.
"""
# The default `float` callable never returns illegal types. Optimize it.
if parse_float is float:
if parse_float is float: # type: ignore[comparison-overlap]
return float
def safe_parse_float(float_str: str) -> Any:

View file

@ -7,21 +7,16 @@ from __future__ import annotations
from datetime import date, datetime, time, timedelta, timezone, tzinfo
from functools import lru_cache
import re
from typing import Any
TYPE_CHECKING = False
if TYPE_CHECKING:
from typing import Any, Final
from ._types import ParseFloat
from ._types import ParseFloat
# E.g.
# - 00:32:00.999999
# - 00:32:00
_TIME_RE_STR: Final = (
r"([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])(?:\.([0-9]{1,6})[0-9]*)?"
)
_TIME_RE_STR = r"([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])(?:\.([0-9]{1,6})[0-9]*)?"
RE_NUMBER: Final = re.compile(
RE_NUMBER = re.compile(
r"""
0
(?:
@ -40,8 +35,8 @@ RE_NUMBER: Final = re.compile(
""",
flags=re.VERBOSE,
)
RE_LOCALTIME: Final = re.compile(_TIME_RE_STR)
RE_DATETIME: Final = re.compile(
RE_LOCALTIME = re.compile(_TIME_RE_STR)
RE_DATETIME = re.compile(
rf"""
([0-9]{{4}})-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01]) # date, e.g. 1988-10-27
(?:
@ -54,7 +49,7 @@ RE_DATETIME: Final = re.compile(
)
def match_to_datetime(match: re.Match[str]) -> datetime | date:
def match_to_datetime(match: re.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
@ -89,9 +84,6 @@ def match_to_datetime(match: re.Match[str]) -> datetime | date:
return datetime(year, month, day, hour, minute, sec, micros, tzinfo=tz)
# No need to limit cache size. This is only ever called on input
# that matched RE_DATETIME, so there is an implicit bound of
# 24 (hours) * 60 (minutes) * 2 (offset direction) = 2880.
@lru_cache(maxsize=None)
def cached_tz(hour_str: str, minute_str: str, sign_str: str) -> timezone:
sign = 1 if sign_str == "+" else -1
@ -103,13 +95,13 @@ def cached_tz(hour_str: str, minute_str: str, sign_str: str) -> timezone:
)
def match_to_localtime(match: re.Match[str]) -> time:
def match_to_localtime(match: re.Match) -> time:
hour_str, minute_str, sec_str, micros_str = match.groups()
micros = int(micros_str.ljust(6, "0")) if micros_str else 0
return time(int(hour_str), int(minute_str), int(sec_str), micros)
def match_to_number(match: re.Match[str], parse_float: ParseFloat) -> Any:
def match_to_number(match: re.Match, parse_float: ParseFloat) -> Any:
if match.group("floatpart"):
return parse_float(match.group())
return int(match.group(), 0)

View file

@ -1 +0,0 @@
# Marker file for PEP 561