|
18 | 18 | TYPE_CHECKING = False |
19 | 19 | if TYPE_CHECKING: |
20 | 20 | from collections.abc import Iterable |
21 | | - from typing import IO, Any |
| 21 | + from typing import IO, Any, Final |
22 | 22 |
|
23 | 23 | from ._types import Key, ParseFloat, Pos |
24 | 24 |
|
25 | | -ASCII_CTRL = frozenset(chr(i) for i in range(32)) | frozenset(chr(127)) |
| 25 | +ASCII_CTRL: Final = frozenset(chr(i) for i in range(32)) | frozenset(chr(127)) |
26 | 26 |
|
27 | 27 | # Neither of these sets include quotation mark or backslash. They are |
28 | 28 | # currently handled as separate cases in the parser functions. |
29 | | -ILLEGAL_BASIC_STR_CHARS = ASCII_CTRL - frozenset("\t") |
30 | | -ILLEGAL_MULTILINE_BASIC_STR_CHARS = ASCII_CTRL - frozenset("\t\n") |
| 29 | +ILLEGAL_BASIC_STR_CHARS: Final = ASCII_CTRL - frozenset("\t") |
| 30 | +ILLEGAL_MULTILINE_BASIC_STR_CHARS: Final = ASCII_CTRL - frozenset("\t\n") |
31 | 31 |
|
32 | | -ILLEGAL_LITERAL_STR_CHARS = ILLEGAL_BASIC_STR_CHARS |
33 | | -ILLEGAL_MULTILINE_LITERAL_STR_CHARS = ILLEGAL_MULTILINE_BASIC_STR_CHARS |
| 32 | +ILLEGAL_LITERAL_STR_CHARS: Final = ILLEGAL_BASIC_STR_CHARS |
| 33 | +ILLEGAL_MULTILINE_LITERAL_STR_CHARS: Final = ILLEGAL_MULTILINE_BASIC_STR_CHARS |
34 | 34 |
|
35 | | -ILLEGAL_COMMENT_CHARS = ILLEGAL_BASIC_STR_CHARS |
| 35 | +ILLEGAL_COMMENT_CHARS: Final = ILLEGAL_BASIC_STR_CHARS |
36 | 36 |
|
37 | | -TOML_WS = frozenset(" \t") |
38 | | -TOML_WS_AND_NEWLINE = TOML_WS | frozenset("\n") |
39 | | -BARE_KEY_CHARS = frozenset( |
| 37 | +TOML_WS: Final = frozenset(" \t") |
| 38 | +TOML_WS_AND_NEWLINE: Final = TOML_WS | frozenset("\n") |
| 39 | +BARE_KEY_CHARS: Final = frozenset( |
40 | 40 | "abcdefghijklmnopqrstuvwxyz" "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "0123456789" "-_" |
41 | 41 | ) |
42 | | -KEY_INITIAL_CHARS = BARE_KEY_CHARS | frozenset("\"'") |
43 | | -HEXDIGIT_CHARS = frozenset("abcdef" "ABCDEF" "0123456789") |
| 42 | +KEY_INITIAL_CHARS: Final = BARE_KEY_CHARS | frozenset("\"'") |
| 43 | +HEXDIGIT_CHARS: Final = frozenset("abcdef" "ABCDEF" "0123456789") |
44 | 44 |
|
45 | | -BASIC_STR_ESCAPE_REPLACEMENTS = MappingProxyType( |
| 45 | +BASIC_STR_ESCAPE_REPLACEMENTS: Final = MappingProxyType( |
46 | 46 | { |
47 | 47 | "\\b": "\u0008", # backspace |
48 | 48 | "\\t": "\u0009", # tab |
49 | | - "\\n": "\u000A", # linefeed |
50 | | - "\\f": "\u000C", # form feed |
51 | | - "\\r": "\u000D", # carriage return |
| 49 | + "\\n": "\u000a", # linefeed |
| 50 | + "\\f": "\u000c", # form feed |
| 51 | + "\\r": "\u000d", # carriage return |
52 | 52 | "\\e": "\u001b", # escape |
53 | 53 | '\\"': "\u0022", # quote |
54 | | - "\\\\": "\u005C", # backslash |
| 54 | + "\\\\": "\u005c", # backslash |
55 | 55 | } |
56 | 56 | ) |
57 | 57 |
|
@@ -134,7 +134,7 @@ def load(fp: IO[bytes], /, *, parse_float: ParseFloat = float) -> dict[str, Any] |
134 | 134 | return loads(s, parse_float=parse_float) |
135 | 135 |
|
136 | 136 |
|
137 | | -def loads(s: str, /, *, parse_float: ParseFloat = float) -> dict[str, Any]: # noqa: C901 |
| 137 | +def loads(s: str, /, *, parse_float: ParseFloat = float) -> dict[str, Any]: |
138 | 138 | """Parse TOML from a string.""" |
139 | 139 |
|
140 | 140 | # The spec allows converting "\r\n" to "\n", even in string |
@@ -209,10 +209,10 @@ class Flags: |
209 | 209 | """Flags that map to parsed keys/namespaces.""" |
210 | 210 |
|
211 | 211 | # Marks an immutable namespace (inline array or inline table). |
212 | | - FROZEN = 0 |
| 212 | + FROZEN: Final = 0 |
213 | 213 | # Marks a nest that has been explicitly created and can no longer |
214 | 214 | # be opened using the "[table]" syntax. |
215 | | - EXPLICIT_NEST = 1 |
| 215 | + EXPLICIT_NEST: Final = 1 |
216 | 216 |
|
217 | 217 | def __init__(self) -> None: |
218 | 218 | self._flags: dict[str, dict[Any, Any]] = {} |
@@ -258,8 +258,8 @@ def is_(self, key: Key, flag: int) -> bool: |
258 | 258 | cont = inner_cont["nested"] |
259 | 259 | key_stem = key[-1] |
260 | 260 | if key_stem in cont: |
261 | | - cont = cont[key_stem] |
262 | | - return flag in cont["flags"] or flag in cont["recursive_flags"] |
| 261 | + inner_cont = cont[key_stem] |
| 262 | + return flag in inner_cont["flags"] or flag in inner_cont["recursive_flags"] |
263 | 263 | return False |
264 | 264 |
|
265 | 265 |
|
@@ -665,7 +665,7 @@ def parse_basic_str(src: str, pos: Pos, *, multiline: bool) -> tuple[Pos, str]: |
665 | 665 | pos += 1 |
666 | 666 |
|
667 | 667 |
|
668 | | -def parse_value( # noqa: C901 |
| 668 | +def parse_value( |
669 | 669 | src: str, pos: Pos, parse_float: ParseFloat |
670 | 670 | ) -> tuple[Pos, Any]: |
671 | 671 | try: |
|
0 commit comments