From 3b12493e01d29daccff0f06aa037f77080fa77a4 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Wed, 17 May 2023 00:41:43 +0200 Subject: [PATCH 1/4] gh-104050: Add type annotations to sentinels in Argument Clinic --- Tools/clinic/clinic.py | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index 3e54ba03d7a57c..a80521d9a041e2 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -12,6 +12,7 @@ import contextlib import copy import cpp +import enum import functools import hashlib import inspect @@ -28,7 +29,7 @@ from collections.abc import Callable from types import FunctionType, NoneType -from typing import Any, NamedTuple, NoReturn, Literal, overload +from typing import Any, Final, NamedTuple, NoReturn, Literal, overload # TODO: # @@ -58,25 +59,19 @@ "return_value", } -class Unspecified: - def __repr__(self) -> str: - return '' - -unspecified = Unspecified() +class Sentinels(enum.Enum): + unspecified = "unspecified" + NULL = "null" + unknown = "unknown" -class Null: def __repr__(self) -> str: - return '' - -NULL = Null() + return f"<{self.value.capitalize()}>" -class Unknown: - def __repr__(self) -> str: - return '' - -unknown = Unknown() +unspecified: Final = Sentinels.unspecified +NULL: Final = Sentinels.NULL +unknown: Final = Sentinels.unknown sig_end_marker = '--' @@ -2690,7 +2685,7 @@ def __init__(self, *, # Keyword only args: c_default: str | None = None, py_default: str | None = None, - annotation: str | Unspecified = unspecified, + annotation: str | Literal[Sentinels.unspecified] = unspecified, unused: bool = False, **kwargs ): From d145ff881c48d7a7c77cf59d975911100d1f3508 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Thu, 18 May 2023 15:00:17 +0200 Subject: [PATCH 2/4] Fix stuff - Fix variable names - Add NullType - Fix instance check --- Tools/clinic/clinic.py | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index d6e48afd201712..48f38d84325443 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -29,7 +29,15 @@ from collections.abc import Callable from types import FunctionType, NoneType -from typing import Any, Final, NamedTuple, NoReturn, Literal, overload +from typing import ( + Any, + Final, + Literal, + NamedTuple, + NoReturn, + Type, + overload, +) # TODO: # @@ -73,6 +81,8 @@ def __repr__(self) -> str: NULL: Final = Sentinels.NULL unknown: Final = Sentinels.unknown +NullType = type(Sentinels.NULL) + sig_end_marker = '--' Appender = Callable[[str], None] @@ -2694,7 +2704,12 @@ def __init__(self, self.unused = unused if default is not unspecified: - if self.default_type and not isinstance(default, (self.default_type, Unknown)): + if (self.default_type + and not ( + isinstance(default, self.default_type) + or default is unknown + ) + ): if isinstance(self.default_type, type): types_str = self.default_type.__name__ else: @@ -3482,7 +3497,7 @@ def str_converter_key(types, encoding, zeroes): class str_converter(CConverter): type = 'const char *' - default_type = (str, Null, NoneType) + default_type = (str, NullType, NoneType) format_unit = 's' def converter_init( @@ -3501,7 +3516,7 @@ def converter_init( self.format_unit = format_unit self.length = bool(zeroes) if encoding: - if self.default not in (Null, None, unspecified): + if self.default not in (NULL, None, unspecified): fail("str_converter: Argument Clinic doesn't support default values for encoded strings") self.encoding = encoding self.type = 'char *' @@ -3649,7 +3664,7 @@ def parse_arg(self, argname: str, displayname: str) -> str: class unicode_converter(CConverter): type = 'PyObject *' - default_type = (str, Null, NoneType) + default_type = (str, NullType, NoneType) format_unit = 'U' def parse_arg(self, argname: str, displayname: str) -> str: @@ -3673,7 +3688,7 @@ def parse_arg(self, argname: str, displayname: str) -> str: @add_legacy_c_converter('Z#', accept={str, NoneType}, zeroes=True) class Py_UNICODE_converter(CConverter): type = 'const Py_UNICODE *' - default_type = (str, Null, NoneType) + default_type = (str, NullType, NoneType) def converter_init( self, *, From 5833421ec2bc0f10d3c99e82053c25edb7254baf Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Thu, 18 May 2023 21:45:23 +0200 Subject: [PATCH 3/4] Revert unneeded change --- Tools/clinic/clinic.py | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index 48f38d84325443..332592ca0fe1c8 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -29,15 +29,7 @@ from collections.abc import Callable from types import FunctionType, NoneType -from typing import ( - Any, - Final, - Literal, - NamedTuple, - NoReturn, - Type, - overload, -) +from typing import Any, NamedTuple, NoReturn, Literal, overload # TODO: # From 703c4177637d6eb8afd2d538c91ec868afe352c3 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Thu, 18 May 2023 21:54:33 +0200 Subject: [PATCH 4/4] Fix local rebase and write nicer if for line 2700 --- Tools/clinic/clinic.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index 332592ca0fe1c8..f455da597f2441 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -29,7 +29,7 @@ from collections.abc import Callable from types import FunctionType, NoneType -from typing import Any, NamedTuple, NoReturn, Literal, overload +from typing import Any, Final, NamedTuple, NoReturn, Literal, overload # TODO: # @@ -75,6 +75,7 @@ def __repr__(self) -> str: NullType = type(Sentinels.NULL) + sig_end_marker = '--' Appender = Callable[[str], None] @@ -2597,7 +2598,7 @@ class CConverter(metaclass=CConverterAutoRegister): # Or the magic value "unknown" if this value is a cannot be evaluated # at Argument-Clinic-preprocessing time (but is presumed to be valid # at runtime). - default: bool | Unspecified = unspecified + default: bool | Literal[Sentinels.unspecified] = unspecified # If not None, default must be isinstance() of this type. # (You can also specify a tuple of types.) @@ -2697,10 +2698,8 @@ def __init__(self, if default is not unspecified: if (self.default_type - and not ( - isinstance(default, self.default_type) - or default is unknown - ) + and default is not unknown + and not isinstance(default, self.default_type) ): if isinstance(self.default_type, type): types_str = self.default_type.__name__