Skip to content

gh-104683: Argument Clinic: Refactor the module and class resolver #108552

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Lib/test/test_clinic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2324,10 +2324,10 @@ def test_invalid_legacy_converter(self):
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)

Expand Down
26 changes: 10 additions & 16 deletions Tools/clinic/clinic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2554,7 +2554,7 @@ def parse(self, input: str) -> str:
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.
Expand All @@ -2563,26 +2563,20 @@ def _module_and_class(
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

Expand Down