Skip to content

Commit 28cab71

Browse files
pythongh-104504: Run mypy on cases_generator in CI (and blacken the code) (pythongh-108090)
Co-authored-by: Alex Waygood <[email protected]>
1 parent fd19509 commit 28cab71

File tree

11 files changed

+314
-195
lines changed

11 files changed

+314
-195
lines changed

.github/dependabot.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ updates:
1313
- "version-update:semver-minor"
1414
- "version-update:semver-patch"
1515
- package-ecosystem: "pip"
16-
directory: "/Tools/clinic/"
16+
directory: "/Tools/"
1717
schedule:
1818
interval: "monthly"
1919
labels:

.github/workflows/mypy.yml

+9-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ on:
88
pull_request:
99
paths:
1010
- "Tools/clinic/**"
11+
- "Tools/cases_generator/**"
1112
- ".github/workflows/mypy.yml"
1213
workflow_dispatch:
1314

@@ -25,15 +26,18 @@ concurrency:
2526

2627
jobs:
2728
mypy:
28-
name: Run mypy on Tools/clinic/
29+
strategy:
30+
matrix:
31+
target: ["Tools/cases_generator", "Tools/clinic"]
32+
name: Run mypy on ${{ matrix.target }}
2933
runs-on: ubuntu-latest
3034
timeout-minutes: 10
3135
steps:
3236
- uses: actions/checkout@v3
3337
- uses: actions/setup-python@v4
3438
with:
35-
python-version: "3.x"
39+
python-version: "3.11"
3640
cache: pip
37-
cache-dependency-path: Tools/clinic/requirements-dev.txt
38-
- run: pip install -r Tools/clinic/requirements-dev.txt
39-
- run: mypy --config-file Tools/clinic/mypy.ini
41+
cache-dependency-path: Tools/requirements-dev.txt
42+
- run: pip install -r Tools/requirements-dev.txt
43+
- run: mypy --config-file ${{ matrix.target }}/mypy.ini

Tools/cases_generator/flags.py

+8-9
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,11 @@ class InstructionFlags:
1616
HAS_FREE_FLAG: bool
1717
HAS_LOCAL_FLAG: bool
1818

19-
def __post_init__(self):
19+
def __post_init__(self) -> None:
2020
self.bitmask = {name: (1 << i) for i, name in enumerate(self.names())}
2121

2222
@staticmethod
23-
def fromInstruction(instr: parsing.Node):
24-
23+
def fromInstruction(instr: parsing.Node) -> "InstructionFlags":
2524
has_free = (
2625
variable_used(instr, "PyCell_New")
2726
or variable_used(instr, "PyCell_GET")
@@ -41,15 +40,15 @@ def fromInstruction(instr: parsing.Node):
4140
)
4241

4342
@staticmethod
44-
def newEmpty():
43+
def newEmpty() -> "InstructionFlags":
4544
return InstructionFlags(False, False, False, False, False, False)
4645

4746
def add(self, other: "InstructionFlags") -> None:
4847
for name, value in dataclasses.asdict(other).items():
4948
if value:
5049
setattr(self, name, value)
5150

52-
def names(self, value=None) -> list[str]:
51+
def names(self, value: bool | None = None) -> list[str]:
5352
if value is None:
5453
return list(dataclasses.asdict(self).keys())
5554
return [n for n, v in dataclasses.asdict(self).items() if v == value]
@@ -62,7 +61,7 @@ def bitmap(self) -> int:
6261
return flags
6362

6463
@classmethod
65-
def emit_macros(cls, out: Formatter):
64+
def emit_macros(cls, out: Formatter) -> None:
6665
flags = cls.newEmpty()
6766
for name, value in flags.bitmask.items():
6867
out.emit(f"#define {name} ({value})")
@@ -90,9 +89,9 @@ def variable_used_unspecialized(node: parsing.Node, name: str) -> bool:
9089
text = "".join(token.text.split())
9190
# TODO: Handle nested #if
9291
if text == "#if":
93-
if (
94-
i + 1 < len(node.tokens)
95-
and node.tokens[i + 1].text in ("ENABLE_SPECIALIZATION", "TIER_ONE")
92+
if i + 1 < len(node.tokens) and node.tokens[i + 1].text in (
93+
"ENABLE_SPECIALIZATION",
94+
"TIER_ONE",
9695
):
9796
skipping = True
9897
elif text in ("#else", "#endif"):

Tools/cases_generator/formatting.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import contextlib
22
import re
33
import typing
4+
from collections.abc import Iterator
45

56
from parsing import StackEffect, Family
67

@@ -58,13 +59,13 @@ def reset_lineno(self) -> None:
5859
self.set_lineno(self.lineno + 1, self.filename)
5960

6061
@contextlib.contextmanager
61-
def indent(self):
62+
def indent(self) -> Iterator[None]:
6263
self.prefix += " "
6364
yield
6465
self.prefix = self.prefix[:-4]
6566

6667
@contextlib.contextmanager
67-
def block(self, head: str, tail: str = ""):
68+
def block(self, head: str, tail: str = "") -> Iterator[None]:
6869
if head:
6970
self.emit(head + " {")
7071
else:
@@ -77,7 +78,7 @@ def stack_adjust(
7778
self,
7879
input_effects: list[StackEffect],
7980
output_effects: list[StackEffect],
80-
):
81+
) -> None:
8182
shrink, isym = list_effect_size(input_effects)
8283
grow, osym = list_effect_size(output_effects)
8384
diff = grow - shrink
@@ -90,7 +91,7 @@ def stack_adjust(
9091
if osym and osym != isym:
9192
self.emit(f"STACK_GROW({osym});")
9293

93-
def declare(self, dst: StackEffect, src: StackEffect | None):
94+
def declare(self, dst: StackEffect, src: StackEffect | None) -> None:
9495
if dst.name == UNUSED or dst.cond == "0":
9596
return
9697
typ = f"{dst.type}" if dst.type else "PyObject *"
@@ -107,7 +108,7 @@ def declare(self, dst: StackEffect, src: StackEffect | None):
107108
sepa = "" if typ.endswith("*") else " "
108109
self.emit(f"{typ}{sepa}{dst.name}{init};")
109110

110-
def assign(self, dst: StackEffect, src: StackEffect):
111+
def assign(self, dst: StackEffect, src: StackEffect) -> None:
111112
if src.name == UNUSED or dst.name == UNUSED:
112113
return
113114
cast = self.cast(dst, src)

0 commit comments

Comments
 (0)