summaryrefslogtreecommitdiffstats
path: root/Tools/clinic/cpp.py
diff options
context:
space:
mode:
authorAlex Waygood <Alex.Waygood@Gmail.com>2023-05-15 08:49:28 (GMT)
committerGitHub <noreply@github.com>2023-05-15 08:49:28 (GMT)
commit9d41f83c58e6dc2fc6eb4b91f803551850b0adeb (patch)
treea9c76753a97335100c60e7712f9b886669db9367 /Tools/clinic/cpp.py
parenta6bcc8fb92ffb75bb1907cc568ba9fff516979c3 (diff)
downloadcpython-9d41f83c58e6dc2fc6eb4b91f803551850b0adeb.zip
cpython-9d41f83c58e6dc2fc6eb4b91f803551850b0adeb.tar.gz
cpython-9d41f83c58e6dc2fc6eb4b91f803551850b0adeb.tar.bz2
gh-104050: Run mypy on `clinic.py` in CI (#104421)
* Add basic mypy workflow to CI * Make the type check pass --------- Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com> Co-authored-by: Nikita Sobolev <mail@sobolevn.me> Co-authored-by: Hugo van Kemenade <hugovk@users.noreply.github.com>
Diffstat (limited to 'Tools/clinic/cpp.py')
-rw-r--r--Tools/clinic/cpp.py26
1 files changed, 16 insertions, 10 deletions
diff --git a/Tools/clinic/cpp.py b/Tools/clinic/cpp.py
index 77f5f96..bc2cc71 100644
--- a/Tools/clinic/cpp.py
+++ b/Tools/clinic/cpp.py
@@ -1,7 +1,12 @@
import re
import sys
+from collections.abc import Callable
-def negate(condition):
+
+TokenAndCondition = tuple[str, str]
+TokenStack = list[TokenAndCondition]
+
+def negate(condition: str) -> str:
"""
Returns a CPP conditional that is the opposite of the conditional passed in.
"""
@@ -22,17 +27,18 @@ class Monitor:
Anyway this implementation seems to work well enough for the CPython sources.
"""
+ is_a_simple_defined: Callable[[str], re.Match[str] | None]
is_a_simple_defined = re.compile(r'^defined\s*\(\s*[A-Za-z0-9_]+\s*\)$').match
- def __init__(self, filename=None, *, verbose=False):
- self.stack = []
+ def __init__(self, filename=None, *, verbose: bool = False):
+ self.stack: TokenStack = []
self.in_comment = False
- self.continuation = None
+ self.continuation: str | None = None
self.line_number = 0
self.filename = filename
self.verbose = verbose
- def __repr__(self):
+ def __repr__(self) -> str:
return ''.join((
'<Monitor ',
str(id(self)),
@@ -40,10 +46,10 @@ class Monitor:
" condition=", repr(self.condition()),
">"))
- def status(self):
+ def status(self) -> str:
return str(self.line_number).rjust(4) + ": " + self.condition()
- def condition(self):
+ def condition(self) -> str:
"""
Returns the current preprocessor state, as a single #if condition.
"""
@@ -62,15 +68,15 @@ class Monitor:
if self.stack:
self.fail("Ended file while still in a preprocessor conditional block!")
- def write(self, s):
+ def write(self, s: str) -> None:
for line in s.split("\n"):
self.writeline(line)
- def writeline(self, line):
+ def writeline(self, line: str) -> None:
self.line_number += 1
line = line.strip()
- def pop_stack():
+ def pop_stack() -> TokenAndCondition:
if not self.stack:
self.fail("#" + token + " without matching #if / #ifdef / #ifndef!")
return self.stack.pop()