From c22a966db71724b1130c9451787cb57436610e33 Mon Sep 17 00:00:00 2001 From: AlexWaygood Date: Fri, 7 Jul 2023 16:20:06 +0100 Subject: [PATCH 1/4] gh-104050: Argument Clinic: Annotate the `Block` class --- Tools/clinic/clinic.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index 19c5f573920cb9..39e2bf9f695f98 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -759,7 +759,7 @@ def parse_line(self, line: str) -> None: def render( self, clinic: Clinic | None, - signatures: Iterable[Function] + signatures: Iterable[Module | Class | Function] ) -> str: function = None for o in signatures: @@ -1633,6 +1633,7 @@ def create_regex( return re.compile(pattern) +@dc.dataclass(slots=True, repr=False) class Block: r""" Represents a single block of text embedded in @@ -1679,14 +1680,12 @@ class Block: "preindent" would be "____" and "indent" would be "__". """ - def __init__(self, input, dsl_name=None, signatures=None, output=None, indent='', preindent=''): - assert isinstance(input, str) - self.input = input - self.dsl_name = dsl_name - self.signatures = signatures or [] - self.output = output - self.indent = indent - self.preindent = preindent + input: str + dsl_name: str | None = None + signatures: list[Module | Class | Function] = dc.field(default_factory=list) + output: Any = None # TODO: Very dynamic; probably untypeable in its current form? + indent: str = '' + preindent: str = '' def __repr__(self): dsl_name = self.dsl_name or "text" @@ -4620,7 +4619,7 @@ def state_modulename_name(self, line: str | None) -> None: if not (existing_function.kind == self.kind and existing_function.coexist == self.coexist): fail("'kind' of function and cloned function don't match! (@classmethod/@staticmethod/@coexist)") self.function = existing_function.copy(name=function_name, full_name=full_name, module=module, cls=cls, c_basename=c_basename, docstring='') - + assert self.function is not None self.block.signatures.append(self.function) (cls or module).functions.append(self.function) self.next(self.state_function_docstring) From a2968dcb706740fc2aae81965f1823ed9ae23809 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Fri, 7 Jul 2023 16:27:24 +0100 Subject: [PATCH 2/4] Update Tools/clinic/clinic.py --- Tools/clinic/clinic.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index 39e2bf9f695f98..3f1f8b95d89064 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -1687,7 +1687,7 @@ class Block: indent: str = '' preindent: str = '' - def __repr__(self): + def __repr__(self) -> str: dsl_name = self.dsl_name or "text" def summarize(s): s = repr(s) From 5de514536f5fc48423efc16648f959147577826c Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Fri, 7 Jul 2023 16:28:51 +0100 Subject: [PATCH 3/4] Update Tools/clinic/clinic.py --- Tools/clinic/clinic.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index 3f1f8b95d89064..3c4234b05ed732 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -1689,7 +1689,7 @@ class Block: def __repr__(self) -> str: dsl_name = self.dsl_name or "text" - def summarize(s): + def summarize(s: object) -> str: s = repr(s) if len(s) > 30: return s[:26] + "..." + s[0] From d506cb4e97c0ab1b9a1d1bf6d0343c2492619c6c Mon Sep 17 00:00:00 2001 From: AlexWaygood Date: Tue, 11 Jul 2023 21:40:40 +0100 Subject: [PATCH 4/4] Remove redundant assertion --- Tools/clinic/clinic.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index 3c4234b05ed732..12ab633388485e 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -4618,10 +4618,13 @@ def state_modulename_name(self, line: str | None) -> None: if not (existing_function.kind == self.kind and existing_function.coexist == self.coexist): fail("'kind' of function and cloned function don't match! (@classmethod/@staticmethod/@coexist)") - self.function = existing_function.copy(name=function_name, full_name=full_name, module=module, cls=cls, c_basename=c_basename, docstring='') - assert self.function is not None - self.block.signatures.append(self.function) - (cls or module).functions.append(self.function) + function = existing_function.copy( + name=function_name, full_name=full_name, module=module, + cls=cls, c_basename=c_basename, docstring='' + ) + self.function = function + self.block.signatures.append(function) + (cls or module).functions.append(function) self.next(self.state_function_docstring) return