10000 gh-104504: Run mypy on cases_generator in CI (and blacken the code) by corona10 · Pull Request #108090 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-104504: Run mypy on cases_generator in CI (and blacken the code) #108090

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Aug 18, 2023
Merged
2 changes: 1 addition & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ updates:
- "version-update:semver-minor"
- "version-update:semver-patch"
- package-ecosystem: "pip"
directory: "/Tools/clinic/"
directory: "/Tools/"
schedule:
interval: "monthly"
labels:
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ jobs:
with:
python-version: "3.x"
- uses: pre-commit/action@v3.0.0
- run: pip install black
- run: black Tools/cases_generator
21 changes: 18 additions & 3 deletions .github/workflows/mypy.yml
< 10000 /tr>
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ on:
pull_request:
paths:
- "Tools/clinic/**"
- "Tools/cases_generator/**"
- ".github/workflows/mypy.yml"
workflow_dispatch:

Expand All @@ -24,7 +25,21 @@ concurrency:
cancel-in-progress: true

jobs:
mypy:
mypy-cases-generator:
name: Run mypy on Tools/cases_generator/
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: "3.x"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there constraints on x? The cases generator uses match/case so it's got to be at least Python 3.10.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use 3.11. We need dog fooding

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3.x is "the latest stable version of Python":

https://github.com/actions/setup-python/blob/main/docs/advanced-usage.md#specifying-a-python-version

That's 3.11 now, and will be 3.12 shortly after its release.

So 3.x would be fine to minimise churn on this line (for next time).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So 3.x would be fine to minimise churn on this line (for next time).

Good!

cache: pip
cache-dependency-path: Tools/requirements-dev.txt
- run: pip install -r Tools/requirements-dev.txt
- run: mypy --config-file Tools/cases_generator/mypy.ini

mypy-clinic:
name: Run mypy on Tools/clinic/
runs-on: ubuntu-latest
timeout-minutes: 10
Expand All @@ -34,6 +49,6 @@ jobs:
with:
python-version: "3.x"
cache: pip
cache-dependency-path: Tools/clinic/requirements-dev.txt
- run: pip install -r Tools/clinic/requirements-dev.txt
cache-dependency-path: Tools/requirements-dev.txt
- run: pip install -r Tools/requirements-dev.txt
- run: mypy --config-file Tools/clinic/mypy.ini
16 changes: 8 additions & 8 deletions Tools/cases_generator/flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ class InstructionFlags:
HAS_FREE_FLAG: bool
HAS_LOCAL_FLAG: bool

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

@staticmethod
def fromInstruction(instr: parsing.Node):
def fromInstruction(instr: parsing.Node) -> "InstructionFlags":

has_free = (
variable_used(instr, "PyCell_New")
Expand All @@ -41,15 +41,15 @@ def fromInstruction(instr: parsing.Node):
)

@staticmethod
def newEmpty():
def newEmpty() -> "InstructionFlags":
return InstructionFlags(False, False, False, False, False, False)

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

def names(self, value=None) -> list[str]:
def names(self, value: bool | None = None) -> list[str]:
if value is None:
return list(dataclasses.asdict(self).keys())
return [n for n, v in dataclasses.asdict(self).items() if v == value]
Expand All @@ -62,7 +62,7 @@ def bitmap(self) -> int:
return flags

@classmethod
def emit_macros(cls, out: Formatter):
def emit_macros(cls, out: Formatter) -> None:
flags = cls.newEmpty()
for name, value in flags.bitmask.items():
out.emit(f"#define {name} ({value})")
Expand Down Expand Up @@ -90,9 +90,9 @@ def variable_used_unspecialized(node: parsing.Node, name: str) -> bool:
text = "".join(token.text.split())
# TODO: Handle nested #if
if text == "#if":
if (
i + 1 < len(node.tokens)
and node.tokens[i + 1].text in ("ENABLE_SPECIALIZATION", "TIER_ONE")
if i + 1 < len(node.tokens) and node.tokens[i + 1].text in (
"ENABLE_SPECIALIZATION",
"TIER_ONE",
):
skipping = True
elif text in ("#else", "#endif"):
Expand Down
11 changes: 6 additions & 5 deletions Tools/cases_generator/formatting.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import collections
import contextlib
import re
import typing
Expand Down Expand Up @@ -58,13 +59,13 @@ def reset_lineno(self) -> None:
self.set_lineno(self.lineno + 1, self.filename)

@contextlib.contextmanager
def indent(self):
def indent(self) -> collections.abc.Iterator[None]:
self.prefix += " "
yield
self.prefix = self.prefix[:-4]

@contextlib.contextmanager
def block(self, head: str, tail: str = ""):
def block(self, head: str, tail: str = "") -> collections.abc.Iterator[None]:
if head:
self.emit(head + " {")
else:
Expand All @@ -77,7 +78,7 @@ def stack_adjust(
self,
input_effects: list[StackEffect],
output_effects: list[StackEffect],
):
) -> None:
shrink, isym = list_effect_size(input_effects)
grow, osym = list_effect_size(output_effects)
diff = grow - shrink
Expand All @@ -90,7 +91,7 @@ def stack_adjust(
if osym and osym != isym:
self.emit(f"STACK_GROW({osym});")

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

def assign(self, dst: StackEffect, src: StackEffect):
def assign(self, dst: StackEffect, src: StackEffect) -> None:
if src.name == UNUSED or dst.name == UNUSED:
return
cast = self.cast(dst, src)
Expand Down
Loading
0