7
7
8
8
import abc
9
9
import ast
10
+ import builtins as bltns
10
11
import collections
11
12
import contextlib
12
13
import copy
26
27
import traceback
27
28
import types
28
29
30
+ from collections .abc import Callable
29
31
from types import *
32
+ from typing import Any , NamedTuple
30
33
31
34
# TODO:
32
35
#
@@ -78,19 +81,26 @@ def __repr__(self):
78
81
79
82
sig_end_marker = '--'
80
83
84
+ Appender = Callable [[str ], None ]
85
+ Outputter = Callable [[None ], str ]
81
86
82
- _text_accumulator_nt = collections .namedtuple ("_text_accumulator" , "text append output" )
87
+ class _TextAccumulator (NamedTuple ):
88
+ text : list [str ]
89
+ append : Appender
90
+ output : Outputter
83
91
84
92
def _text_accumulator ():
85
93
text = []
86
94
def output ():
87
95
s = '' .join (text )
88
96
text .clear ()
89
97
return s
90
- return _text_accumulator_nt (text , text .append , output )
98
+ return _TextAccumulator (text , text .append , output )
91
99
92
100
93
- text_accumulator_nt = collections .namedtuple ("text_accumulator" , "text append" )
101
+ class TextAccumulator (NamedTuple ):
102
+ text : list [str ]
103
+ append : Appender
94
104
95
105
def text_accumulator ():
96
106
"""
@@ -104,7 +114,7 @@ def text_accumulator():
104
114
empties the accumulator.
105
115
"""
106
116
text , append , output = _text_accumulator ()
107
- return text_accumulator_nt (append , output )
117
+ return TextAccumulator (append , output )
108
118
109
119
110
120
def warn_or_fail (fail = False , * args , filename = None , line_number = None ):
@@ -1925,8 +1935,10 @@ def dump(self):
1925
1935
# maps strings to Language objects.
1926
1936
# "languages" maps the name of the language ("C", "Python").
1927
1937
# "extensions" maps the file extension ("c", "py").
1938
+ LangDict = dict [str , Callable [[str ], Language ]]
1939
+
1928
1940
languages = { 'C' : CLanguage , 'Python' : PythonLanguage }
1929
- extensions = { name : CLanguage for name in "c cc cpp cxx h hh hpp hxx" .split () }
1941
+ extensions : LangDict = { name : CLanguage for name in "c cc cpp cxx h hh hpp hxx" .split () }
1930
1942
extensions ['py' ] = PythonLanguage
1931
1943
1932
1944
@@ -2558,15 +2570,15 @@ class CConverter(metaclass=CConverterAutoRegister):
2558
2570
"""
2559
2571
2560
2572
# The C name to use for this variable.
2561
- name = None
2573
+ name : str | None = None
2562
2574
2563
2575
# The Python name to use for this variable.
2564
- py_name = None
2576
+ py_name : str | None = None
2565
2577
2566
2578
# The C type to use for this variable.
2567
2579
# 'type' should be a Python string specifying the type, e.g. "int".
2568
2580
# If this is a pointer type, the type string should end with ' *'.
2569
- type = None
2581
+ type : str | None = None
2570
2582
2571
2583
# The Python default value for this parameter, as a Python value.
2572
2584
# Or the magic value "unspecified" if there is no default.
@@ -2577,15 +2589,15 @@ class CConverter(metaclass=CConverterAutoRegister):
2577
2589
2578
2590
# If not None, default must be isinstance() of this type.
2579
2591
# (You can also specify a tuple of types.)
2580
- default_type = None
2592
+ default_type : bltns . type [ Any ] | tuple [ bltns . type [ Any ], ...] | None = None
2581
2593
2582
2594
# "default" converted into a C value, as a string.
2583
2595
# Or None if there is no default.
2584
- c_default = None
2596
+ c_default : str | None = None
2585
2597
2586
2598
# "default" converted into a Python value, as a string.
2587
2599
# Or None if there is no default.
2588
- py_default = None
2600
+ py_default : str | None = None
2589
2601
2590
2602
# The default value used to initialize the C variable when
2591
2603
# there is no default, but not specifying a default may
@@ -2597,14 +2609,14 @@ class CConverter(metaclass=CConverterAutoRegister):
2597
2609
#
2598
2610
# This value is specified as a string.
2599
2611
# Every non-abstract subclass should supply a valid value.
2600
- c_ignored_default = 'NULL'
2612
+ c_ignored_default : str = 'NULL'
2601
2613
2602
2614
# If true, wrap with Py_UNUSED.
2603
2615
unused = False
2604
2616
2605
2617
# The C converter *function* to be used, if any.
2606
2618
# (If this is not None, format_unit must be 'O&'.)
2607
- converter = None
2619
+ converter : str | None = None
2608
2620
2609
2621
# Should Argument Clinic add a '&' before the name of
2610
2622
# the variable when passing it into the _impl function?
@@ -3432,7 +3444,7 @@ class robuffer: pass
3432
3444
def str_converter_key (types , encoding , zeroes ):
3433
3445
return (frozenset (types ), bool (encoding ), bool (zeroes ))
3434
3446
3435
- str_converter_argument_map = {}
3447
+ str_converter_argument_map : dict [ str , str ] = {}
3436
3448
3437
3449
class str_converter (CConverter ):
3438
3450
type = 'const char *'
0 commit comments