From 1380090843f7e832fc8f87790916e0387c459c00 Mon Sep 17 00:00:00 2001 From: AlexWaygood Date: Wed, 12 Jul 2023 00:25:09 +0100 Subject: [PATCH 1/2] gh-104050: Argument clinic: Annotate the `Destination` class --- Tools/clinic/clinic.py | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index a0cf50b29c978d..c0b712ac88df30 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -1954,27 +1954,32 @@ def dump(self): return "".join(texts) +@dc.dataclass(slots=True, repr=False) class Destination: - def __init__(self, name, type, clinic, *args): - self.name = name - self.type = type - self.clinic = clinic - self.buffers = BufferSeries() + name: str + type: str + clinic: Clinic + buffers: BufferSeries = dc.field(init=False, default_factory=BufferSeries) + filename: str = dc.field(init=False) # set in __post_init__ + args: dc.InitVar[tuple[str, ...]] = () + + def __post_init__(self, args: tuple[str, ...] = ()) -> None: valid_types = ('buffer', 'file', 'suppress') - if type not in valid_types: + if self.type not in valid_types: fail( - f"Invalid destination type {type!r} for {name}, " + f"Invalid destination type {self.type!r} for {self.name}, " f"must be {', '.join(valid_types)}" ) - extra_arguments = 1 if type == "file" else 0 + extra_arguments = 1 if self.type == "file" else 0 if len(args) < extra_arguments: - fail(f"Not enough arguments for destination {name} new {type}") + fail(f"Not enough arguments for destination {self.name} new {self.type}") if len(args) > extra_arguments: - fail(f"Too many arguments for destination {name} new {type}") - if type =='file': + fail(f"Too many arguments for destination {self.name} new {self.type}") + if self.type =='file': d = {} - filename = clinic.filename + filename = self.clinic.filename + assert filename is not None d['path'] = filename dirname, basename = os.path.split(filename) if not dirname: @@ -1984,19 +1989,19 @@ def __init__(self, name, type, clinic, *args): d['basename_root'], d['basename_extension'] = os.path.splitext(filename) self.filename = args[0].format_map(d) - def __repr__(self): + def __repr__(self) -> str: if self.type == 'file': file_repr = " " + repr(self.filename) else: file_repr = '' return "".join(("")) - def clear(self): + def clear(self) -> None: if self.type != 'buffer': fail("Can't clear destination" + self.name + " , it's not of type buffer") self.buffers.clear() - def dump(self): + def dump(self) -> str: return self.buffers.dump() @@ -2164,11 +2169,11 @@ def add_destination( self, name: str, type: str, - *args + *args: str ) -> None: if name in self.destinations: fail("Destination already exists: " + repr(name)) - self.destinations[name] = Destination(name, type, self, *args) + self.destinations[name] = Destination(name, type, self, args) def get_destination(self, name: str) -> Destination: d = self.destinations.get(name) From 36df87ac7dd28acd7972a57a7603e65bc5cd7aca Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Wed, 12 Jul 2023 17:45:03 +0100 Subject: [PATCH 2/2] Update Tools/clinic/clinic.py Co-authored-by: Nikita Sobolev --- 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 c0b712ac88df30..ce3039cdd8bff4 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -1964,7 +1964,7 @@ class Destination: args: dc.InitVar[tuple[str, ...]] = () - def __post_init__(self, args: tuple[str, ...] = ()) -> None: + def __post_init__(self, args: tuple[str, ...]) -> None: valid_types = ('buffer', 'file', 'suppress') if self.type not in valid_types: fail(