Skip to content

Commit 036bb73

Browse files
gh-104050: Improve Argument Clinic type annotation coverage (#106810)
Add various missing annotations in the following classes: - BlockPrinter - CConverter - CLanguage - FormatCounterFormatter - Language - _TextAccumulator Co-authored-by: Alex Waygood <[email protected]>
1 parent 5ecedbd commit 036bb73

File tree

1 file changed

+31
-18
lines changed

1 file changed

+31
-18
lines changed

Tools/clinic/clinic.py

+31-18
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class _TextAccumulator(NamedTuple):
109109

110110
def _text_accumulator() -> _TextAccumulator:
111111
text: list[str] = []
112-
def output():
112+
def output() -> str:
113113
s = ''.join(text)
114114
text.clear()
115115
return s
@@ -433,10 +433,10 @@ class FormatCounterFormatter(string.Formatter):
433433
the counts dict would now look like
434434
{'a': 2, 'b': 1, 'c': 1}
435435
"""
436-
def __init__(self):
437-
self.counts = collections.Counter()
436+
def __init__(self) -> None:
437+
self.counts = collections.Counter[str]()
438438

439-
def get_value(self, key, args, kwargs):
439+
def get_value(self, key: str, args, kwargs) -> str: # type: ignore[override]
440440
self.counts[key] += 1
441441
return ''
442442

@@ -447,18 +447,25 @@ class Language(metaclass=abc.ABCMeta):
447447
stop_line = ""
448448
checksum_line = ""
449449

450-
def __init__(self, filename):
450+
def __init__(self, filename: str) -> None:
451451
pass
452452

453453
@abc.abstractmethod
454-
def render(self, clinic, signatures):
454+
def render(
455+
self,
456+
clinic: Clinic | None,
457+
signatures: Iterable[Module | Class | Function]
458+
) -> str:
455459
pass
456460

457-
def parse_line(self, line):
461+
def parse_line(self, line: str) -> None:
458462
pass
459463

460-
def validate(self):
461-
def assert_only_one(attr, *additional_fields):
464+
def validate(self) -> None:
465+
def assert_only_one(
466+
attr: str,
467+
*additional_fields: str
468+
) -> None:
462469
"""
463470
Ensures that the string found at getattr(self, attr)
464471
contains exactly one formatter replacement string for
@@ -485,10 +492,10 @@ def assert_only_one(attr, *additional_fields):
485492
"""
486493
fields = ['dsl_name']
487494
fields.extend(additional_fields)
488-
line = getattr(self, attr)
495+
line: str = getattr(self, attr)
489496
fcf = FormatCounterFormatter()
490497
fcf.format(line)
491-
def local_fail(should_be_there_but_isnt):
498+
def local_fail(should_be_there_but_isnt: bool) -> None:
492499
if should_be_there_but_isnt:
493500
fail("{} {} must contain {{{}}} exactly once!".format(
494501
self.__class__.__name__, attr, name))
@@ -749,10 +756,10 @@ class CLanguage(Language):
749756
stop_line = "[{dsl_name} start generated code]*/"
750757
checksum_line = "/*[{dsl_name} end generated code: {arguments}]*/"
751758

752-
def __init__(self, filename):
759+
def __init__(self, filename: str) -> None:
753760
super().__init__(filename)
754761
self.cpp = cpp.Monitor(filename)
755-
self.cpp.fail = fail
762+
self.cpp.fail = fail # type: ignore[method-assign]
756763

757764
def parse_line(self, line: str) -> None:
758765
self.cpp.writeline(line)
@@ -935,6 +942,7 @@ def parser_body(
935942
add(field)
936943
return linear_format(output(), parser_declarations=declarations)
937944

945+
parsearg: str | None
938946
if not parameters:
939947
parser_code: list[str] | None
940948
if not requires_defining_class:
@@ -1880,7 +1888,12 @@ class BlockPrinter:
18801888
language: Language
18811889
f: io.StringIO = dc.field(default_factory=io.StringIO)
18821890

1883-
def print_block(self, block, *, core_includes=False):
1891+
def print_block(
1892+
self,
1893+
block: Block,
1894+
*,
1895+
core_includes: bool = False
1896+
) -> None:
18841897
input = block.input
18851898
output = block.output
18861899
dsl_name = block.dsl_name
@@ -1931,7 +1944,7 @@ def print_block(self, block, *, core_includes=False):
19311944
write(self.language.checksum_line.format(dsl_name=dsl_name, arguments=arguments))
19321945
write("\n")
19331946

1934-
def write(self, text):
1947+
def write(self, text: str) -> None:
19351948
self.f.write(text)
19361949

19371950

@@ -2755,7 +2768,7 @@ class CConverter(metaclass=CConverterAutoRegister):
27552768
# If not None, should be a string representing a pointer to a
27562769
# PyTypeObject (e.g. "&PyUnicode_Type").
27572770
# Only used by the 'O!' format unit (and the "object" converter).
2758-
subclass_of = None
2771+
subclass_of: str | None = None
27592772

27602773
# Do we want an adjacent '_length' variable for this variable?
27612774
# Only used by format units ending with '#'.
@@ -2948,7 +2961,7 @@ def simple_declaration(self, by_reference=False, *, in_parser=False):
29482961
prototype.append(name)
29492962
return "".join(prototype)
29502963

2951-
def declaration(self, *, in_parser=False):
2964+
def declaration(self, *, in_parser=False) -> str:
29522965
"""
29532966
The C statement to declare this variable.
29542967
"""
@@ -3006,7 +3019,7 @@ def pre_render(self):
30063019
"""
30073020
pass
30083021

3009-
def parse_arg(self, argname, displayname):
3022+
def parse_arg(self, argname: str, displayname: str):
30103023
if self.format_unit == 'O&':
30113024
return """
30123025
if (!{converter}({argname}, &{paramname})) {{{{

0 commit comments

Comments
 (0)