Skip to content

Commit 4c8738f

Browse files
committed
gh-133117: Run mypy on tomllib in CI
1 parent 68a7376 commit 4c8738f

File tree

6 files changed

+30
-10
lines changed

6 files changed

+30
-10
lines changed

.github/workflows/mypy.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ on:
99
paths:
1010
- ".github/workflows/mypy.yml"
1111
- "Lib/_pyrepl/**"
12+
- "Lib/tomllib/**"
1213
- "Lib/test/libregrtest/**"
1314
- "Tools/build/generate_sbom.py"
1415
- "Tools/cases_generator/**"
@@ -41,6 +42,7 @@ jobs:
4142
matrix:
4243
target: [
4344
"Lib/_pyrepl",
45+
"Lib/tomllib",
4446
"Lib/test/libregrtest",
4547
"Tools/build",
4648
"Tools/cases_generator",

Lib/tomllib/_parser.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ class Flags:
214214
EXPLICIT_NEST = 1
215215

216216
def __init__(self) -> None:
217-
self._flags: dict[str, dict] = {}
217+
self._flags: dict[str, dict[Any, Any]] = {}
218218
self._pending_flags: set[tuple[Key, int]] = set()
219219

220220
def add_pending(self, key: Key, flag: int) -> None:
@@ -272,8 +272,8 @@ def get_or_create_nest(
272272
key: Key,
273273
*,
274274
access_lists: bool = True,
275-
) -> dict:
276-
cont: Any = self.dict
275+
) -> dict[str, Any]:
276+
cont = self.dict
277277
for k in key:
278278
if k not in cont:
279279
cont[k] = {}
@@ -486,9 +486,9 @@ def parse_one_line_basic_str(src: str, pos: Pos) -> tuple[Pos, str]:
486486
return parse_basic_str(src, pos, multiline=False)
487487

488488

489-
def parse_array(src: str, pos: Pos, parse_float: ParseFloat) -> tuple[Pos, list]:
489+
def parse_array(src: str, pos: Pos, parse_float: ParseFloat) -> tuple[Pos, list[Any]]:
490490
pos += 1
491-
array: list = []
491+
array: list[Any] = []
492492

493493
pos = skip_comments_and_array_ws(src, pos)
494494
if src.startswith("]", pos):
@@ -510,7 +510,7 @@ def parse_array(src: str, pos: Pos, parse_float: ParseFloat) -> tuple[Pos, list]
510510
return pos + 1, array
511511

512512

513-
def parse_inline_table(src: str, pos: Pos, parse_float: ParseFloat) -> tuple[Pos, dict]:
513+
def parse_inline_table(src: str, pos: Pos, parse_float: ParseFloat) -> tuple[Pos, dict[str, Any]]:
514514
pos += 1
515515
nested_dict = NestedDict()
516516
flags = Flags()
@@ -741,7 +741,7 @@ def make_safe_parse_float(parse_float: ParseFloat) -> ParseFloat:
741741
instead of returning illegal types.
742742
"""
743743
# The default `float` callable never returns illegal types. Optimize it.
744-
if parse_float is float: # type: ignore[comparison-overlap]
744+
if parse_float is float:
745745
return float
746746

747747
def safe_parse_float(float_str: str) -> Any:

Lib/tomllib/_re.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
)
5353

5454

55-
def match_to_datetime(match: re.Match) -> datetime | date:
55+
def match_to_datetime(match: re.Match[str]) -> datetime | date:
5656
"""Convert a `RE_DATETIME` match to `datetime.datetime` or `datetime.date`.
5757
5858
Raises ValueError if the match does not correspond to a valid date
@@ -101,13 +101,13 @@ def cached_tz(hour_str: str, minute_str: str, sign_str: str) -> timezone:
101101
)
102102

103103

104-
def match_to_localtime(match: re.Match) -> time:
104+
def match_to_localtime(match: re.Match[str]) -> time:
105105
hour_str, minute_str, sec_str, micros_str = match.groups()
106106
micros = int(micros_str.ljust(6, "0")) if micros_str else 0
107107
return time(int(hour_str), int(minute_str), int(sec_str), micros)
108108

109109

110-
def match_to_number(match: re.Match, parse_float: ParseFloat) -> Any:
110+
def match_to_number(match: re.Match[str], parse_float: ParseFloat) -> Any:
111111
if match.group("floatpart"):
112112
return parse_float(match.group())
113113
return int(match.group(), 0)

Lib/tomllib/mypy.ini

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Config file for running mypy on tomllib.
2+
# Run mypy by invoking `mypy --config-file Lib/tomllib/mypy.ini`
3+
# on the command-line from the repo root
4+
5+
[mypy]
6+
files = Lib/tomllib
7+
mypy_path = $MYPY_CONFIG_FILE_DIR/../../Misc/mypy
8+
explicit_package_bases = True
9+
python_version = 3.12
10+
pretty = True
11+
12+
# Enable most stricter settings
13+
enable_error_code = ignore-without-code
14+
strict = True
15+
strict_bytes = True
16+
local_partial_types = True

Misc/mypy/tomllib

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../Lib/tomllib

Misc/mypy/typed-stdlib.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22

33
_colorize.py
44
_pyrepl
5+
tomllib

0 commit comments

Comments
 (0)