Skip to content

Commit cf7ba83

Browse files
sobolevnhugovk
andauthored
gh-108455: Run mypy on Tools/peg_generator (#108456)
Co-authored-by: Hugo van Kemenade <[email protected]>
1 parent f75cefd commit cf7ba83

File tree

6 files changed

+23
-11
lines changed

6 files changed

+23
-11
lines changed

.github/workflows/mypy.yml

+6-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ on:
99
paths:
1010
- "Tools/clinic/**"
1111
- "Tools/cases_generator/**"
12+
- "Tools/peg_generator/**"
1213
- "Tools/requirements-dev.txt"
1314
- ".github/workflows/mypy.yml"
1415
workflow_dispatch:
@@ -29,7 +30,11 @@ jobs:
2930
mypy:
3031
strategy:
3132
matrix:
32-
target: ["Tools/cases_generator", "Tools/clinic"]
33+
target: [
34+
"Tools/cases_generator",
35+
"Tools/clinic",
36+
"Tools/peg_generator",
37+
]
3338
name: Run mypy on ${{ matrix.target }}
3439
runs-on: ubuntu-latest
3540
timeout-minutes: 10

Tools/peg_generator/mypy.ini

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[mypy]
2-
files = pegen
2+
files = Tools/peg_generator/pegen
3+
pretty = True
34

45
follow_imports = error
56
no_implicit_optional = True
@@ -24,3 +25,6 @@ show_error_codes = True
2425

2526
[mypy-pegen.grammar_parser]
2627
strict_optional = False
28+
29+
[mypy-setuptools.*]
30+
ignore_missing_imports = True

Tools/peg_generator/pegen/build.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import sysconfig
66
import tempfile
77
import tokenize
8-
from typing import IO, Dict, List, Optional, Set, Tuple
8+
from typing import IO, Any, Dict, List, Optional, Set, Tuple
99

1010
from pegen.c_generator import CParserGenerator
1111
from pegen.grammar import Grammar
@@ -18,6 +18,7 @@
1818
MOD_DIR = pathlib.Path(__file__).resolve().parent
1919

2020
TokenDefinitions = Tuple[Dict[int, str], Dict[str, int], Set[str]]
21+
Incomplete = Any # TODO: install `types-setuptools` and remove this alias
2122

2223

2324
def get_extra_flags(compiler_flags: str, compiler_py_flags_nodist: str) -> List[str]:
@@ -28,7 +29,7 @@ def get_extra_flags(compiler_flags: str, compiler_py_flags_nodist: str) -> List[
2829
return f"{flags} {py_flags_nodist}".split()
2930

3031

31-
def fixup_build_ext(cmd):
32+
def fixup_build_ext(cmd: Incomplete) -> None:
3233
"""Function needed to make build_ext tests pass.
3334
3435
When Python was built with --enable-shared on Unix, -L. is not enough to
@@ -74,7 +75,7 @@ def compile_c_extension(
7475
keep_asserts: bool = True,
7576
disable_optimization: bool = False,
7677
library_dir: Optional[str] = None,
77-
) -> str:
78+
) -> pathlib.Path:
7879
"""Compile the generated source for a parser generator into an extension module.
7980
8081
The extension module will be generated in the same directory as the provided path

Tools/peg_generator/pegen/keywordgen.py

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
issoftkeyword = frozenset(softkwlist).__contains__
3636
'''.lstrip()
3737

38+
3839
def main() -> None:
3940
parser = argparse.ArgumentParser(
4041
description="Generate the Lib/keywords.py file from the grammar."

Tools/peg_generator/pegen/parser.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from pegen.tokenizer import Mark, Tokenizer, exact_token_types
1111

1212
T = TypeVar("T")
13-
P = TypeVar("P", bound="Parser")
1413
F = TypeVar("F", bound=Callable[..., Any])
1514

1615

@@ -21,7 +20,7 @@ def logger(method: F) -> F:
2120
"""
2221
method_name = method.__name__
2322

24-
def logger_wrapper(self: P, *args: object) -> T:
23+
def logger_wrapper(self: "Parser", *args: object) -> Any:
2524
if not self._verbose:
2625
return method(self, *args)
2726
argsr = ",".join(repr(arg) for arg in args)
@@ -41,7 +40,7 @@ def memoize(method: F) -> F:
4140
"""Memoize a symbol method."""
4241
method_name = method.__name__
4342

44-
def memoize_wrapper(self: P, *args: object) -> T:
43+
def memoize_wrapper(self: "Parser", *args: object) -> Any:
4544
mark = self._mark()
4645
key = mark, method_name, args
4746
# Fast path: cache hit, and not verbose.
@@ -74,11 +73,13 @@ def memoize_wrapper(self: P, *args: object) -> T:
7473
return cast(F, memoize_wrapper)
7574

7675

77-
def memoize_left_rec(method: Callable[[P], Optional[T]]) -> Callable[[P], Optional[T]]:
76+
def memoize_left_rec(
77+
method: Callable[["Parser"], Optional[T]]
78+
) -> Callable[["Parser"], Optional[T]]:
7879
"""Memoize a left-recursive symbol method."""
7980
method_name = method.__name__
8081

81-
def memoize_left_rec_wrapper(self: P) -> Optional[T]:
82+
def memoize_left_rec_wrapper(self: "Parser") -> Optional[T]:
8283
mark = self._mark()
8384
key = mark, method_name, ()
8485
# Fast path: cache hit, and not verbose.

Tools/requirements-dev.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# Requirements file for external linters and checks we run on
2-
# Tools/clinic and Tools/cases_generator/ in CI
2+
# Tools/clinic, Tools/cases_generator/, and Tools/peg_generator/ in CI
33
mypy==1.5.1

0 commit comments

Comments
 (0)