From cce842a03bf0b331370bbffbd2da4222ef417af9 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Tue, 16 May 2023 13:02:39 +0200 Subject: [PATCH 1/5] gh-104050: Add typing to Argument Clinic converters --- Tools/clinic/clinic.py | 237 +++++++++++++++++++++-------------------- 1 file changed, 120 insertions(+), 117 deletions(-) diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index 0ef5deb586d6ee..016b6d73d44f48 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -82,6 +82,7 @@ def __repr__(self): Appender = Callable[[str], None] Outputter = Callable[[None], str] +TemplateDict = dict[str, str] class _TextAccumulator(NamedTuple): text: list[str] @@ -1941,20 +1942,6 @@ def dump(self): extensions['py'] = PythonLanguage -# maps strings to callables. -# these callables must be of the form: -# def foo(name, default, *, ...) -# The callable may have any number of keyword-only parameters. -# The callable must return a CConverter object. -# The callable should not call builtins.print. -converters = {} - -# maps strings to callables. -# these callables follow the same rules as those for "converters" above. -# note however that they will never be called with keyword-only parameters. -legacy_converters = {} - - # maps strings to callables. # these callables must be of the form: # def foo(*, ...) @@ -2928,7 +2915,7 @@ def parse_arg(self, argname, displayname): """.format(argname=argname, paramname=self.parser_name, cast=cast) return None - def set_template_dict(self, template_dict: dict[str, str]): + def set_template_dict(self, template_dict: TemplateDict): pass @property @@ -2951,11 +2938,27 @@ def parser_name(self): } +ConverterDict = dict[str, CConverter] + +# maps strings to callables. +# these callables must be of the form: +# def foo(name, default, *, ...) +# The callable may have any number of keyword-only parameters. +# The callable must return a CConverter object. +# The callable should not call builtins.print. +converters: ConverterDict = {} + +# maps strings to callables. +# these callables follow the same rules as those for "converters" above. +# note however that they will never be called with keyword-only parameters. +legacy_converters: ConverterDict = {} + + class bool_converter(CConverter): - type = 'int' + type: str = 'int' default_type = bool - format_unit = 'p' - c_ignored_default = '0' + format_unit: str = 'p' + c_ignored_default: str = '0' def converter_init(self, *, accept={object}): if accept == {int}: @@ -2966,7 +2969,7 @@ def converter_init(self, *, accept={object}): self.default = bool(self.default) self.c_default = str(int(self.default)) - def parse_arg(self, argname, displayname): + def parse_arg(self, argname: str, displayname: str) -> str: if self.format_unit == 'i': return """ {paramname} = _PyLong_AsInt({argname}); @@ -2988,14 +2991,14 @@ class defining_class_converter(CConverter): A special-case converter: this is the default converter used for the defining class. """ - type = 'PyTypeObject *' - format_unit = '' - show_in_signature = False + type: str = 'PyTypeObject *' + format_unit: str = '' + show_in_signature: bool = False - def converter_init(self, *, type=None): + def converter_init(self, *, type=None) -> None: self.specified_type = type - def render(self, parameter, data): + def render(self, parameter, data) -> None: self._render_self(parameter, data) def set_template_dict(self, template_dict): @@ -3003,12 +3006,12 @@ def set_template_dict(self, template_dict): class char_converter(CConverter): - type = 'char' + type: str = 'char' default_type = (bytes, bytearray) - format_unit = 'c' - c_ignored_default = "'\0'" + format_unit: str = 'c' + c_ignored_default: str = "'\0'" - def converter_init(self): + def converter_init(self) -> None: if isinstance(self.default, self.default_type): if len(self.default) != 1: fail("char_converter: illegal default value " + repr(self.default)) @@ -3017,7 +3020,7 @@ def converter_init(self): if self.c_default == '"\'"': self.c_default = r"'\''" - def parse_arg(self, argname, displayname): + def parse_arg(self, argname: str, displayname: str) -> str: if self.format_unit == 'c': return """ if (PyBytes_Check({argname}) && PyBytes_GET_SIZE({argname}) == 1) {{{{ @@ -3037,16 +3040,16 @@ def parse_arg(self, argname, displayname): @add_legacy_c_converter('B', bitwise=True) class unsigned_char_converter(CConverter): - type = 'unsigned char' + type: str = 'unsigned char' default_type = int - format_unit = 'b' - c_ignored_default = "'\0'" + format_unit: str = 'b' + c_ignored_default: str = "'\0'" - def converter_init(self, *, bitwise=False): + def converter_init(self, *, bitwise=False) -> None: if bitwise: self.format_unit = 'B' - def parse_arg(self, argname, displayname): + def parse_arg(self, argname: str, displayname: str) -> str: if self.format_unit == 'b': return """ {{{{ @@ -3086,12 +3089,12 @@ def parse_arg(self, argname, displayname): class byte_converter(unsigned_char_converter): pass class short_converter(CConverter): - type = 'short' + type: str = 'short' default_type = int - format_unit = 'h' - c_ignored_default = "0" + format_unit: str = 'h' + c_ignored_default: str = "0" - def parse_arg(self, argname, displayname): + def parse_arg(self, argname: str, displayname: str) -> str: if self.format_unit == 'h': return """ {{{{ @@ -3117,17 +3120,17 @@ def parse_arg(self, argname, displayname): return super().parse_arg(argname, displayname) class unsigned_short_converter(CConverter): - type = 'unsigned short' + type: str = 'unsigned short' default_type = int - c_ignored_default = "0" + c_ignored_default: str = "0" - def converter_init(self, *, bitwise=False): + def converter_init(self, *, bitwise: bool = False) -> None: if bitwise: self.format_unit = 'H' else: self.converter = '_PyLong_UnsignedShort_Converter' - def parse_arg(self, argname, displayname): + def parse_arg(self, argname: str, displayname: str) -> str: if self.format_unit == 'H': return """ {paramname} = (unsigned short)PyLong_AsUnsignedLongMask({argname}); @@ -3139,12 +3142,12 @@ def parse_arg(self, argname, displayname): @add_legacy_c_converter('C', accept={str}) class int_converter(CConverter): - type = 'int' + type: str = 'int' default_type = int - format_unit = 'i' - c_ignored_default = "0" + format_unit: str = 'i' + c_ignored_default: str = "0" - def converter_init(self, *, accept={int}, type=None): + def converter_init(self, *, accept={int}, type=None) -> None: if accept == {str}: self.format_unit = 'C' elif accept != {int}: @@ -3152,7 +3155,7 @@ def converter_init(self, *, accept={int}, type=None): if type is not None: self.type = type - def parse_arg(self, argname, displayname): + def parse_arg(self, argname: str, displayname: str) -> str: if self.format_unit == 'i': return """ {paramname} = _PyLong_AsInt({argname}); @@ -3179,17 +3182,17 @@ def parse_arg(self, argname, displayname): return super().parse_arg(argname, displayname) class unsigned_int_converter(CConverter): - type = 'unsigned int' + type: str = 'unsigned int' default_type = int - c_ignored_default = "0" + c_ignored_default: str = "0" - def converter_init(self, *, bitwise=False): + def converter_init(self, *, bitwise: bool = False) -> None: if bitwise: self.format_unit = 'I' else: self.converter = '_PyLong_UnsignedInt_Converter' - def parse_arg(self, argname, displayname): + def parse_arg(self, argname: str, displayname: str) -> str: if self.format_unit == 'I': return """ {paramname} = (unsigned int)PyLong_AsUnsignedLongMask({argname}); @@ -3200,12 +3203,12 @@ def parse_arg(self, argname, displayname): return super().parse_arg(argname, displayname) class long_converter(CConverter): - type = 'long' + type: str = 'long' default_type = int - format_unit = 'l' - c_ignored_default = "0" + format_unit: str = 'l' + c_ignored_default: str = "0" - def parse_arg(self, argname, displayname): + def parse_arg(self, argname: str, displayname: str) -> str: if self.format_unit == 'l': return """ {paramname} = PyLong_AsLong({argname}); @@ -3216,17 +3219,17 @@ def parse_arg(self, argname, displayname): return super().parse_arg(argname, displayname) class unsigned_long_converter(CConverter): - type = 'unsigned long' + type: str = 'unsigned long' default_type = int - c_ignored_default = "0" + c_ignored_default: str = "0" - def converter_init(self, *, bitwise=False): + def converter_init(self, *, bitwise: bool = False) -> None: if bitwise: self.format_unit = 'k' else: self.converter = '_PyLong_UnsignedLong_Converter' - def parse_arg(self, argname, displayname): + def parse_arg(self, argname: str, displayname: str) -> str: if self.format_unit == 'k': return """ if (!PyLong_Check({argname})) {{{{ @@ -3239,12 +3242,12 @@ def parse_arg(self, argname, displayname): return super().parse_arg(argname, displayname) class long_long_converter(CConverter): - type = 'long long' + type: str = 'long long' default_type = int - format_unit = 'L' - c_ignored_default = "0" + format_unit: str = 'L' + c_ignored_default: str = "0" - def parse_arg(self, argname, displayname): + def parse_arg(self, argname: str, displayname: str) -> str: if self.format_unit == 'L': return """ {paramname} = PyLong_AsLongLong({argname}); @@ -3255,17 +3258,17 @@ def parse_arg(self, argname, displayname): return super().parse_arg(argname, displayname) class unsigned_long_long_converter(CConverter): - type = 'unsigned long long' + type: str = 'unsigned long long' default_type = int - c_ignored_default = "0" + c_ignored_default: str = "0" - def converter_init(self, *, bitwise=False): + def converter_init(self, *, bitwise: bool = False) -> None: if bitwise: self.format_unit = 'K' else: self.converter = '_PyLong_UnsignedLongLong_Converter' - def parse_arg(self, argname, displayname): + def parse_arg(self, argname: str, displayname: str) -> str: if self.format_unit == 'K': return """ if (!PyLong_Check({argname})) {{{{ @@ -3278,10 +3281,10 @@ def parse_arg(self, argname, displayname): return super().parse_arg(argname, displayname) class Py_ssize_t_converter(CConverter): - type = 'Py_ssize_t' - c_ignored_default = "0" + type: str = 'Py_ssize_t' + c_ignored_default: str = "0" - def converter_init(self, *, accept={int}): + def converter_init(self, *, accept={int}) -> None: if accept == {int}: self.format_unit = 'n' self.default_type = int @@ -3290,7 +3293,7 @@ def converter_init(self, *, accept={int}): else: fail("Py_ssize_t_converter: illegal 'accept' argument " + repr(accept)) - def parse_arg(self, argname, displayname): + def parse_arg(self, argname: str, displayname: str) -> str: if self.format_unit == 'n': return """ {{{{ @@ -3310,9 +3313,9 @@ def parse_arg(self, argname, displayname): class slice_index_converter(CConverter): - type = 'Py_ssize_t' + type: str = 'Py_ssize_t' - def converter_init(self, *, accept={int, NoneType}): + def converter_init(self, *, accept={int, NoneType}) -> None: if accept == {int}: self.converter = '_PyEval_SliceIndexNotNone' elif accept == {int, NoneType}: @@ -3321,11 +3324,11 @@ def converter_init(self, *, accept={int, NoneType}): fail("slice_index_converter: illegal 'accept' argument " + repr(accept)) class size_t_converter(CConverter): - type = 'size_t' - converter = '_PyLong_Size_t_Converter' - c_ignored_default = "0" + type: str = 'size_t' + converter: str = '_PyLong_Size_t_Converter' + c_ignored_default: str = "0" - def parse_arg(self, argname, displayname): + def parse_arg(self, argname: str, displayname: str) -> str: if self.format_unit == 'n': return """ {paramname} = PyNumber_AsSsize_t({argname}, PyExc_OverflowError); @@ -3337,10 +3340,10 @@ def parse_arg(self, argname, displayname): class fildes_converter(CConverter): - type = 'int' - converter = '_PyLong_FileDescriptor_Converter' + type: str = 'int' + converter: str = '_PyLong_FileDescriptor_Converter' - def _parse_arg(self, argname, displayname): + def _parse_arg(self, argname: str, displayname: str) -> str: return """ {paramname} = PyObject_AsFileDescriptor({argname}); if ({paramname} == -1) {{{{ @@ -3350,12 +3353,12 @@ def _parse_arg(self, argname, displayname): class float_converter(CConverter): - type = 'float' + type: str = 'float' default_type = float - format_unit = 'f' - c_ignored_default = "0.0" + format_unit: str = 'f' + c_ignored_default: str = "0.0" - def parse_arg(self, argname, displayname): + def parse_arg(self, argname: str, displayname: str) -> str: if self.format_unit == 'f': return """ if (PyFloat_CheckExact({argname})) {{{{ @@ -3372,12 +3375,12 @@ def parse_arg(self, argname, displayname): return super().parse_arg(argname, displayname) class double_converter(CConverter): - type = 'double' + type: str = 'double' default_type = float - format_unit = 'd' - c_ignored_default = "0.0" + format_unit: str = 'd' + c_ignored_default: str = "0.0" - def parse_arg(self, argname, displayname): + def parse_arg(self, argname: str, displayname: str) -> str: if self.format_unit == 'd': return """ if (PyFloat_CheckExact({argname})) {{{{ @@ -3395,12 +3398,12 @@ def parse_arg(self, argname, displayname): class Py_complex_converter(CConverter): - type = 'Py_complex' + type: str = 'Py_complex' default_type = complex - format_unit = 'D' - c_ignored_default = "{0.0, 0.0}" + format_unit: str = 'D' + c_ignored_default: str = "{0.0, 0.0}" - def parse_arg(self, argname, displayname): + def parse_arg(self, argname: str, displayname: str) -> str: if self.format_unit == 'D': return """ {paramname} = PyComplex_AsCComplex({argname}); @@ -3412,8 +3415,8 @@ def parse_arg(self, argname, displayname): class object_converter(CConverter): - type = 'PyObject *' - format_unit = 'O' + type: str = 'PyObject *' + format_unit: str = 'O' def converter_init(self, *, converter=None, type=None, subclass_of=None): if converter: @@ -3447,9 +3450,9 @@ def str_converter_key(types, encoding, zeroes): str_converter_argument_map: dict[str, str] = {} class str_converter(CConverter): - type = 'const char *' + type: str = 'const char *' default_type = (str, Null, NoneType) - format_unit = 's' + format_unit: str = 's' def converter_init(self, *, accept={str}, encoding=None, zeroes=False): @@ -3476,7 +3479,7 @@ def post_parsing(self): name = self.name return f"PyMem_FREE({name});\n" - def parse_arg(self, argname, displayname): + def parse_arg(self, argname: str, displayname: str) -> str: if self.format_unit == 's': return """ if (!PyUnicode_Check({argname})) {{{{ @@ -3567,11 +3570,11 @@ def r(format_unit, *, accept, encoding=False, zeroes=False): class PyBytesObject_converter(CConverter): - type = 'PyBytesObject *' - format_unit = 'S' + type: str = 'PyBytesObject *' + format_unit: str = 'S' # accept = {bytes} - def parse_arg(self, argname, displayname): + def parse_arg(self, argname: str, displayname: str) -> str: if self.format_unit == 'S': return """ if (!PyBytes_Check({argname})) {{{{ @@ -3584,11 +3587,11 @@ def parse_arg(self, argname, displayname): return super().parse_arg(argname, displayname) class PyByteArrayObject_converter(CConverter): - type = 'PyByteArrayObject *' - format_unit = 'Y' + type: str = 'PyByteArrayObject *' + format_unit: str = 'Y' # accept = {bytearray} - def parse_arg(self, argname, displayname): + def parse_arg(self, argname: str, displayname: str) -> str: if self.format_unit == 'Y': return """ if (!PyByteArray_Check({argname})) {{{{ @@ -3601,11 +3604,11 @@ def parse_arg(self, argname, displayname): return super().parse_arg(argname, displayname) class unicode_converter(CConverter): - type = 'PyObject *' + type: str = 'PyObject *' default_type = (str, Null, NoneType) - format_unit = 'U' + format_unit: str = 'U' - def parse_arg(self, argname, displayname): + def parse_arg(self, argname: str, displayname: str) -> str: if self.format_unit == 'U': return """ if (!PyUnicode_Check({argname})) {{{{ @@ -3625,10 +3628,10 @@ def parse_arg(self, argname, displayname): @add_legacy_c_converter('Z', accept={str, NoneType}) @add_legacy_c_converter('Z#', accept={str, NoneType}, zeroes=True) class Py_UNICODE_converter(CConverter): - type = 'const Py_UNICODE *' + type: str = 'const Py_UNICODE *' default_type = (str, Null, NoneType) - def converter_init(self, *, accept={str}, zeroes=False): + def converter_init(self, *, accept={str}, zeroes: bool = False) -> None: format_unit = 'Z' if accept=={str, NoneType} else 'u' if zeroes: format_unit += '#' @@ -3650,7 +3653,7 @@ def cleanup(self): PyMem_Free((void *){name}); """.format(name=self.name) - def parse_arg(self, argname, argnum): + def parse_arg(self, argname: str, argnum: str) -> str: if not self.length: if self.accept == {str}: return """ @@ -3685,12 +3688,12 @@ def parse_arg(self, argname, argnum): @add_legacy_c_converter('z*', accept={str, buffer, NoneType}) @add_legacy_c_converter('w*', accept={rwbuffer}) class Py_buffer_converter(CConverter): - type = 'Py_buffer' - format_unit = 'y*' + type: str = 'Py_buffer' + format_unit: str = 'y*' impl_by_reference = True - c_ignored_default = "{NULL, NULL}" + c_ignored_default: str = "{NULL, NULL}" - def converter_init(self, *, accept={buffer}): + def converter_init(self, *, accept={buffer}) -> None: if self.default not in (unspecified, None): fail("The only legal default value for Py_buffer is None.") @@ -3713,7 +3716,7 @@ def cleanup(self): name = self.name return "".join(["if (", name, ".obj) {\n PyBuffer_Release(&", name, ");\n}\n"]) - def parse_arg(self, argname, displayname): + def parse_arg(self, argname: str, displayname: str) -> str: if self.format_unit == 'y*': return """ if (PyObject_GetBuffer({argname}, &{paramname}, PyBUF_SIMPLE) != 0) {{{{ @@ -3762,7 +3765,7 @@ def parse_arg(self, argname, displayname): return super().parse_arg(argname, displayname) -def correct_name_for_self(f): +def correct_name_for_self(f) -> tuple[str, str]: if f.kind in (CALLABLE, METHOD_INIT): if f.cls: return "PyObject *", "self" @@ -3786,9 +3789,9 @@ class self_converter(CConverter): this is the default converter used for "self". """ type = None - format_unit = '' + format_unit: str = '' - def converter_init(self, *, type=None): + def converter_init(self, *, type=None) -> None: self.specified_type = type def pre_render(self): From 81911831e8cd7679e11e63912e285108a64953a6 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Tue, 16 May 2023 14:49:12 +0200 Subject: [PATCH 2/5] Update Tools/clinic/clinic.py --- Tools/clinic/clinic.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index 016b6d73d44f48..a35890a4482830 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -2938,7 +2938,8 @@ def parser_name(self): } -ConverterDict = dict[str, CConverter] +ConverterType = Callable[..., CConverter] +ConverterDict = dict[str, ConverterType] # maps strings to callables. # these callables must be of the form: From dd1c773bf42521d40477e05561335922b4ba42b6 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Tue, 16 May 2023 15:02:02 +0200 Subject: [PATCH 3/5] Also add types to 'default_type' --- Tools/clinic/clinic.py | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index a35890a4482830..44f540bdad9cce 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -2957,7 +2957,7 @@ def parser_name(self): class bool_converter(CConverter): type: str = 'int' - default_type = bool + default_type: bltns.type[bool] = bool format_unit: str = 'p' c_ignored_default: str = '0' @@ -3008,7 +3008,7 @@ def set_template_dict(self, template_dict): class char_converter(CConverter): type: str = 'char' - default_type = (bytes, bytearray) + default_type: tuple[bltns.type[bytes], bltns.type[bytearray]] = (bytes, bytearray) format_unit: str = 'c' c_ignored_default: str = "'\0'" @@ -3042,7 +3042,7 @@ def parse_arg(self, argname: str, displayname: str) -> str: @add_legacy_c_converter('B', bitwise=True) class unsigned_char_converter(CConverter): type: str = 'unsigned char' - default_type = int + default_type: bltns.type[int] = int format_unit: str = 'b' c_ignored_default: str = "'\0'" @@ -3091,7 +3091,7 @@ class byte_converter(unsigned_char_converter): pass class short_converter(CConverter): type: str = 'short' - default_type = int + default_type: bltns.type[int] = int format_unit: str = 'h' c_ignored_default: str = "0" @@ -3122,7 +3122,7 @@ def parse_arg(self, argname: str, displayname: str) -> str: class unsigned_short_converter(CConverter): type: str = 'unsigned short' - default_type = int + default_type: bltns.type[int] = int c_ignored_default: str = "0" def converter_init(self, *, bitwise: bool = False) -> None: @@ -3144,7 +3144,7 @@ def parse_arg(self, argname: str, displayname: str) -> str: @add_legacy_c_converter('C', accept={str}) class int_converter(CConverter): type: str = 'int' - default_type = int + default_type: bltns.type[int] = int format_unit: str = 'i' c_ignored_default: str = "0" @@ -3184,7 +3184,7 @@ def parse_arg(self, argname: str, displayname: str) -> str: class unsigned_int_converter(CConverter): type: str = 'unsigned int' - default_type = int + default_type: bltns.type[int] = int c_ignored_default: str = "0" def converter_init(self, *, bitwise: bool = False) -> None: @@ -3205,7 +3205,7 @@ def parse_arg(self, argname: str, displayname: str) -> str: class long_converter(CConverter): type: str = 'long' - default_type = int + default_type: bltns.type[int] = int format_unit: str = 'l' c_ignored_default: str = "0" @@ -3221,7 +3221,7 @@ def parse_arg(self, argname: str, displayname: str) -> str: class unsigned_long_converter(CConverter): type: str = 'unsigned long' - default_type = int + default_type: bltns.type[int] = int c_ignored_default: str = "0" def converter_init(self, *, bitwise: bool = False) -> None: @@ -3244,7 +3244,7 @@ def parse_arg(self, argname: str, displayname: str) -> str: class long_long_converter(CConverter): type: str = 'long long' - default_type = int + default_type: bltns.type[int] = int format_unit: str = 'L' c_ignored_default: str = "0" @@ -3260,7 +3260,7 @@ def parse_arg(self, argname: str, displayname: str) -> str: class unsigned_long_long_converter(CConverter): type: str = 'unsigned long long' - default_type = int + default_type: bltns.type[int] = int c_ignored_default: str = "0" def converter_init(self, *, bitwise: bool = False) -> None: @@ -3355,7 +3355,7 @@ def _parse_arg(self, argname: str, displayname: str) -> str: class float_converter(CConverter): type: str = 'float' - default_type = float + default_type: bltns.type[float] = float format_unit: str = 'f' c_ignored_default: str = "0.0" @@ -3377,7 +3377,7 @@ def parse_arg(self, argname: str, displayname: str) -> str: class double_converter(CConverter): type: str = 'double' - default_type = float + default_type: bltns.type[float] = float format_unit: str = 'd' c_ignored_default: str = "0.0" @@ -3400,7 +3400,7 @@ def parse_arg(self, argname: str, displayname: str) -> str: class Py_complex_converter(CConverter): type: str = 'Py_complex' - default_type = complex + default_type: bltns.type[complex] = complex format_unit: str = 'D' c_ignored_default: str = "{0.0, 0.0}" @@ -3691,7 +3691,7 @@ def parse_arg(self, argname: str, argnum: str) -> str: class Py_buffer_converter(CConverter): type: str = 'Py_buffer' format_unit: str = 'y*' - impl_by_reference = True + impl_by_reference: bool = True c_ignored_default: str = "{NULL, NULL}" def converter_init(self, *, accept={buffer}) -> None: @@ -3789,7 +3789,7 @@ class self_converter(CConverter): A special-case converter: this is the default converter used for "self". """ - type = None + type: str | None = None format_unit: str = '' def converter_init(self, *, type=None) -> None: From bdc130d22374479ce85562d68f86cf1461a5d1e8 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Tue, 16 May 2023 15:15:33 +0200 Subject: [PATCH 4/5] Remove annotations of simple assignments --- Tools/clinic/clinic.py | 160 ++++++++++++++++++++--------------------- 1 file changed, 80 insertions(+), 80 deletions(-) diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index 44f540bdad9cce..be5990bfcfb1ba 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -2956,10 +2956,10 @@ def parser_name(self): class bool_converter(CConverter): - type: str = 'int' - default_type: bltns.type[bool] = bool - format_unit: str = 'p' - c_ignored_default: str = '0' + type = 'int' + default_type = bool + format_unit = 'p' + c_ignored_default = '0' def converter_init(self, *, accept={object}): if accept == {int}: @@ -2992,9 +2992,9 @@ class defining_class_converter(CConverter): A special-case converter: this is the default converter used for the defining class. """ - type: str = 'PyTypeObject *' - format_unit: str = '' - show_in_signature: bool = False + type = 'PyTypeObject *' + format_unit = '' + show_in_signature = False def converter_init(self, *, type=None) -> None: self.specified_type = type @@ -3007,10 +3007,10 @@ def set_template_dict(self, template_dict): class char_converter(CConverter): - type: str = 'char' - default_type: tuple[bltns.type[bytes], bltns.type[bytearray]] = (bytes, bytearray) - format_unit: str = 'c' - c_ignored_default: str = "'\0'" + type = 'char' + default_type = (bytes, bytearray) + format_unit = 'c' + c_ignored_default = "'\0'" def converter_init(self) -> None: if isinstance(self.default, self.default_type): @@ -3041,10 +3041,10 @@ def parse_arg(self, argname: str, displayname: str) -> str: @add_legacy_c_converter('B', bitwise=True) class unsigned_char_converter(CConverter): - type: str = 'unsigned char' - default_type: bltns.type[int] = int - format_unit: str = 'b' - c_ignored_default: str = "'\0'" + type = 'unsigned char' + default_type = int + format_unit = 'b' + c_ignored_default = "'\0'" def converter_init(self, *, bitwise=False) -> None: if bitwise: @@ -3090,10 +3090,10 @@ def parse_arg(self, argname: str, displayname: str) -> str: class byte_converter(unsigned_char_converter): pass class short_converter(CConverter): - type: str = 'short' - default_type: bltns.type[int] = int - format_unit: str = 'h' - c_ignored_default: str = "0" + type = 'short' + default_type = int + format_unit = 'h' + c_ignored_default = "0" def parse_arg(self, argname: str, displayname: str) -> str: if self.format_unit == 'h': @@ -3121,9 +3121,9 @@ def parse_arg(self, argname: str, displayname: str) -> str: return super().parse_arg(argname, displayname) class unsigned_short_converter(CConverter): - type: str = 'unsigned short' - default_type: bltns.type[int] = int - c_ignored_default: str = "0" + type = 'unsigned short' + default_type = int + c_ignored_default = "0" def converter_init(self, *, bitwise: bool = False) -> None: if bitwise: @@ -3143,10 +3143,10 @@ def parse_arg(self, argname: str, displayname: str) -> str: @add_legacy_c_converter('C', accept={str}) class int_converter(CConverter): - type: str = 'int' - default_type: bltns.type[int] = int - format_unit: str = 'i' - c_ignored_default: str = "0" + type = 'int' + default_type = int + format_unit = 'i' + c_ignored_default = "0" def converter_init(self, *, accept={int}, type=None) -> None: if accept == {str}: @@ -3183,9 +3183,9 @@ def parse_arg(self, argname: str, displayname: str) -> str: return super().parse_arg(argname, displayname) class unsigned_int_converter(CConverter): - type: str = 'unsigned int' - default_type: bltns.type[int] = int - c_ignored_default: str = "0" + type = 'unsigned int' + default_type = int + c_ignored_default = "0" def converter_init(self, *, bitwise: bool = False) -> None: if bitwise: @@ -3204,10 +3204,10 @@ def parse_arg(self, argname: str, displayname: str) -> str: return super().parse_arg(argname, displayname) class long_converter(CConverter): - type: str = 'long' - default_type: bltns.type[int] = int - format_unit: str = 'l' - c_ignored_default: str = "0" + type = 'long' + default_type = int + format_unit = 'l' + c_ignored_default = "0" def parse_arg(self, argname: str, displayname: str) -> str: if self.format_unit == 'l': @@ -3220,9 +3220,9 @@ def parse_arg(self, argname: str, displayname: str) -> str: return super().parse_arg(argname, displayname) class unsigned_long_converter(CConverter): - type: str = 'unsigned long' - default_type: bltns.type[int] = int - c_ignored_default: str = "0" + type = 'unsigned long' + default_type = int + c_ignored_default = "0" def converter_init(self, *, bitwise: bool = False) -> None: if bitwise: @@ -3243,10 +3243,10 @@ def parse_arg(self, argname: str, displayname: str) -> str: return super().parse_arg(argname, displayname) class long_long_converter(CConverter): - type: str = 'long long' - default_type: bltns.type[int] = int - format_unit: str = 'L' - c_ignored_default: str = "0" + type = 'long long' + default_type = int + format_unit = 'L' + c_ignored_default = "0" def parse_arg(self, argname: str, displayname: str) -> str: if self.format_unit == 'L': @@ -3259,9 +3259,9 @@ def parse_arg(self, argname: str, displayname: str) -> str: return super().parse_arg(argname, displayname) class unsigned_long_long_converter(CConverter): - type: str = 'unsigned long long' - default_type: bltns.type[int] = int - c_ignored_default: str = "0" + type = 'unsigned long long' + default_type = int + c_ignored_default = "0" def converter_init(self, *, bitwise: bool = False) -> None: if bitwise: @@ -3282,8 +3282,8 @@ def parse_arg(self, argname: str, displayname: str) -> str: return super().parse_arg(argname, displayname) class Py_ssize_t_converter(CConverter): - type: str = 'Py_ssize_t' - c_ignored_default: str = "0" + type = 'Py_ssize_t' + c_ignored_default = "0" def converter_init(self, *, accept={int}) -> None: if accept == {int}: @@ -3314,7 +3314,7 @@ def parse_arg(self, argname: str, displayname: str) -> str: class slice_index_converter(CConverter): - type: str = 'Py_ssize_t' + type = 'Py_ssize_t' def converter_init(self, *, accept={int, NoneType}) -> None: if accept == {int}: @@ -3325,9 +3325,9 @@ def converter_init(self, *, accept={int, NoneType}) -> None: fail("slice_index_converter: illegal 'accept' argument " + repr(accept)) class size_t_converter(CConverter): - type: str = 'size_t' - converter: str = '_PyLong_Size_t_Converter' - c_ignored_default: str = "0" + type = 'size_t' + converter = '_PyLong_Size_t_Converter' + c_ignored_default = "0" def parse_arg(self, argname: str, displayname: str) -> str: if self.format_unit == 'n': @@ -3341,8 +3341,8 @@ def parse_arg(self, argname: str, displayname: str) -> str: class fildes_converter(CConverter): - type: str = 'int' - converter: str = '_PyLong_FileDescriptor_Converter' + type = 'int' + converter = '_PyLong_FileDescriptor_Converter' def _parse_arg(self, argname: str, displayname: str) -> str: return """ @@ -3354,10 +3354,10 @@ def _parse_arg(self, argname: str, displayname: str) -> str: class float_converter(CConverter): - type: str = 'float' - default_type: bltns.type[float] = float - format_unit: str = 'f' - c_ignored_default: str = "0.0" + type = 'float' + default_type = float + format_unit = 'f' + c_ignored_default = "0.0" def parse_arg(self, argname: str, displayname: str) -> str: if self.format_unit == 'f': @@ -3376,10 +3376,10 @@ def parse_arg(self, argname: str, displayname: str) -> str: return super().parse_arg(argname, displayname) class double_converter(CConverter): - type: str = 'double' - default_type: bltns.type[float] = float - format_unit: str = 'd' - c_ignored_default: str = "0.0" + type = 'double' + default_type = float + format_unit = 'd' + c_ignored_default = "0.0" def parse_arg(self, argname: str, displayname: str) -> str: if self.format_unit == 'd': @@ -3399,10 +3399,10 @@ def parse_arg(self, argname: str, displayname: str) -> str: class Py_complex_converter(CConverter): - type: str = 'Py_complex' - default_type: bltns.type[complex] = complex - format_unit: str = 'D' - c_ignored_default: str = "{0.0, 0.0}" + type = 'Py_complex' + default_type = complex + format_unit = 'D' + c_ignored_default = "{0.0, 0.0}" def parse_arg(self, argname: str, displayname: str) -> str: if self.format_unit == 'D': @@ -3416,8 +3416,8 @@ def parse_arg(self, argname: str, displayname: str) -> str: class object_converter(CConverter): - type: str = 'PyObject *' - format_unit: str = 'O' + type = 'PyObject *' + format_unit = 'O' def converter_init(self, *, converter=None, type=None, subclass_of=None): if converter: @@ -3451,9 +3451,9 @@ def str_converter_key(types, encoding, zeroes): str_converter_argument_map: dict[str, str] = {} class str_converter(CConverter): - type: str = 'const char *' + type = 'const char *' default_type = (str, Null, NoneType) - format_unit: str = 's' + format_unit = 's' def converter_init(self, *, accept={str}, encoding=None, zeroes=False): @@ -3571,8 +3571,8 @@ def r(format_unit, *, accept, encoding=False, zeroes=False): class PyBytesObject_converter(CConverter): - type: str = 'PyBytesObject *' - format_unit: str = 'S' + type = 'PyBytesObject *' + format_unit = 'S' # accept = {bytes} def parse_arg(self, argname: str, displayname: str) -> str: @@ -3588,8 +3588,8 @@ def parse_arg(self, argname: str, displayname: str) -> str: return super().parse_arg(argname, displayname) class PyByteArrayObject_converter(CConverter): - type: str = 'PyByteArrayObject *' - format_unit: str = 'Y' + type = 'PyByteArrayObject *' + format_unit = 'Y' # accept = {bytearray} def parse_arg(self, argname: str, displayname: str) -> str: @@ -3605,9 +3605,9 @@ def parse_arg(self, argname: str, displayname: str) -> str: return super().parse_arg(argname, displayname) class unicode_converter(CConverter): - type: str = 'PyObject *' + type = 'PyObject *' default_type = (str, Null, NoneType) - format_unit: str = 'U' + format_unit = 'U' def parse_arg(self, argname: str, displayname: str) -> str: if self.format_unit == 'U': @@ -3629,7 +3629,7 @@ def parse_arg(self, argname: str, displayname: str) -> str: @add_legacy_c_converter('Z', accept={str, NoneType}) @add_legacy_c_converter('Z#', accept={str, NoneType}, zeroes=True) class Py_UNICODE_converter(CConverter): - type: str = 'const Py_UNICODE *' + type = 'const Py_UNICODE *' default_type = (str, Null, NoneType) def converter_init(self, *, accept={str}, zeroes: bool = False) -> None: @@ -3689,10 +3689,10 @@ def parse_arg(self, argname: str, argnum: str) -> str: @add_legacy_c_converter('z*', accept={str, buffer, NoneType}) @add_legacy_c_converter('w*', accept={rwbuffer}) class Py_buffer_converter(CConverter): - type: str = 'Py_buffer' - format_unit: str = 'y*' - impl_by_reference: bool = True - c_ignored_default: str = "{NULL, NULL}" + type = 'Py_buffer' + format_unit = 'y*' + impl_by_reference = True + c_ignored_default = "{NULL, NULL}" def converter_init(self, *, accept={buffer}) -> None: if self.default not in (unspecified, None): @@ -3789,8 +3789,8 @@ class self_converter(CConverter): A special-case converter: this is the default converter used for "self". """ - type: str | None = None - format_unit: str = '' + type = None + format_unit = '' def converter_init(self, *, type=None) -> None: self.specified_type = type From 2e5b92c7acdb9c33ad4526f195fb37bdce1e88fa Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Wed, 17 May 2023 00:04:52 +0200 Subject: [PATCH 5/5] Address reviews --- Tools/clinic/clinic.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index 04fc0fd1cf604c..3e54ba03d7a57c 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -2943,7 +2943,7 @@ def parse_arg(self, argname, displayname): """.format(argname=argname, paramname=self.parser_name, cast=cast) return None - def set_template_dict(self, template_dict: TemplateDict): + def set_template_dict(self, template_dict: TemplateDict) -> None: pass @property @@ -3074,7 +3074,7 @@ class unsigned_char_converter(CConverter): format_unit = 'b' c_ignored_default = "'\0'" - def converter_init(self, *, bitwise=False) -> None: + def converter_init(self, *, bitwise: bool = False) -> None: if bitwise: self.format_unit = 'B'