From d90973340bf5ac35e0b35e99239cd37c46a30910 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Mon, 28 Aug 2023 14:41:05 +0200 Subject: gh-104683: Argument Clinic: Refactor the module and class resolver (#108552) --- Lib/test/test_clinic.py | 4 ++-- Tools/clinic/clinic.py | 26 ++++++++++---------------- 2 files changed, 12 insertions(+), 18 deletions(-) diff --git a/Lib/test/test_clinic.py b/Lib/test/test_clinic.py index e7039d5..fcccf98 100644 --- a/Lib/test/test_clinic.py +++ b/Lib/test/test_clinic.py @@ -2324,10 +2324,10 @@ class ClinicParserTest(TestCase): self.expect_failure(block, err, lineno=1) def test_parent_class_or_module_does_not_exist(self): - err = "Parent class or module 'z' does not exist" + err = "Parent class or module 'baz' does not exist" block = """ module m - z.func + baz.func """ self.expect_failure(block, err, lineno=1) diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index 70ec18f..1fb53c3 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -2554,7 +2554,7 @@ impl_definition block return printer.f.getvalue() def _module_and_class( - self, fields: Iterable[str] + self, fields: Sequence[str] ) -> tuple[Module | Clinic, Class | None]: """ fields should be an iterable of field names. @@ -2563,26 +2563,20 @@ impl_definition block this function is only ever used to find the parent of where a new class/module should go. """ - parent: Clinic | Module | Class - child: Module | Class | None - module: Clinic | Module + parent: Clinic | Module | Class = self + module: Clinic | Module = self cls: Class | None = None - so_far: list[str] = [] - parent = module = self - - for field in fields: - so_far.append(field) + for idx, field in enumerate(fields): if not isinstance(parent, Class): - child = parent.modules.get(field) - if child: - parent = module = child + if field in parent.modules: + parent = module = parent.modules[field] continue - child = parent.classes.get(field) - if not child: - fullname = ".".join(so_far) + if field in parent.classes: + parent = cls = parent.classes[field] + else: + fullname = ".".join(fields[idx:]) fail(f"Parent class or module {fullname!r} does not exist.") - cls = parent = child return module, cls -- cgit v0.12