mirror of
https://github.com/python/cpython.git
synced 2025-09-13 20:27:05 +00:00
gh-104683: Argument Clinic: Refactor the module and class resolver (#108552)
This commit is contained in:
parent
0e8b3fc718
commit
d90973340b
2 changed files with 12 additions and 18 deletions
|
@ -2324,10 +2324,10 @@ class ClinicParserTest(TestCase):
|
||||||
self.expect_failure(block, err, lineno=1)
|
self.expect_failure(block, err, lineno=1)
|
||||||
|
|
||||||
def test_parent_class_or_module_does_not_exist(self):
|
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 = """
|
block = """
|
||||||
module m
|
module m
|
||||||
z.func
|
baz.func
|
||||||
"""
|
"""
|
||||||
self.expect_failure(block, err, lineno=1)
|
self.expect_failure(block, err, lineno=1)
|
||||||
|
|
||||||
|
|
|
@ -2554,7 +2554,7 @@ impl_definition block
|
||||||
return printer.f.getvalue()
|
return printer.f.getvalue()
|
||||||
|
|
||||||
def _module_and_class(
|
def _module_and_class(
|
||||||
self, fields: Iterable[str]
|
self, fields: Sequence[str]
|
||||||
) -> tuple[Module | Clinic, Class | None]:
|
) -> tuple[Module | Clinic, Class | None]:
|
||||||
"""
|
"""
|
||||||
fields should be an iterable of field names.
|
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
|
this function is only ever used to find the parent of where
|
||||||
a new class/module should go.
|
a new class/module should go.
|
||||||
"""
|
"""
|
||||||
parent: Clinic | Module | Class
|
parent: Clinic | Module | Class = self
|
||||||
child: Module | Class | None
|
module: Clinic | Module = self
|
||||||
module: Clinic | Module
|
|
||||||
cls: Class | None = None
|
cls: Class | None = None
|
||||||
so_far: list[str] = []
|
|
||||||
|
|
||||||
parent = module = self
|
for idx, field in enumerate(fields):
|
||||||
|
|
||||||
for field in fields:
|
|
||||||
so_far.append(field)
|
|
||||||
if not isinstance(parent, Class):
|
if not isinstance(parent, Class):
|
||||||
child = parent.modules.get(field)
|
if field in parent.modules:
|
||||||
if child:
|
parent = module = parent.modules[field]
|
||||||
parent = module = child
|
|
||||||
continue
|
continue
|
||||||
child = parent.classes.get(field)
|
if field in parent.classes:
|
||||||
if not child:
|
parent = cls = parent.classes[field]
|
||||||
fullname = ".".join(so_far)
|
else:
|
||||||
|
fullname = ".".join(fields[idx:])
|
||||||
fail(f"Parent class or module {fullname!r} does not exist.")
|
fail(f"Parent class or module {fullname!r} does not exist.")
|
||||||
cls = parent = child
|
|
||||||
|
|
||||||
return module, cls
|
return module, cls
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue