From 8311660ce7e62e3d6ef74b35080441fecba255b4 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Sat, 12 Aug 2023 00:30:36 +0200 Subject: [PATCH 1/5] gh-107883: Argument Clinic: Handle full module/class path in Function.fulldisplayname --- Lib/test/test_clinic.py | 20 ++++++++++++++++++++ Tools/clinic/clinic.py | 15 ++++++++++++--- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_clinic.py b/Lib/test/test_clinic.py index a649b5fe2201c8..2d6557c138b538 100644 --- a/Lib/test/test_clinic.py +++ b/Lib/test/test_clinic.py @@ -1513,6 +1513,26 @@ def test_parameters_required_after_star(self): with self.subTest(block=block): self.expect_failure(block, err) + def test_fulldisplayname_class(self): + block = self.parse(""" + module m + class m.T "void *" "" + class m.T.C "void *" "" + m.T.C.__init__ + """) + func = block.signatures[-1] + self.assertEqual(func.fulldisplayname, "m.T.C") + + def test_fulldisplayname_meth(self): + block = self.parse(""" + module m + class m.T "" "" + class m.T.C "" "" + m.T.C.func + """) + func = block.signatures[-1] + self.assertEqual(func.fulldisplayname, "m.T.C.func") + def test_depr_star_invalid_format_1(self): block = """ module foo diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index 70b066cce82fae..6fdf70ed1dd026 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -2682,9 +2682,18 @@ def displayname(self) -> str: @functools.cached_property def fulldisplayname(self) -> str: - if isinstance(self.module, Module): - return f"{self.module.name}.{self.displayname}" - return self.displayname + name = self.displayname + try: + if self.kind.new_or_init: + parent = self.cls.parent + else: + parent = self.parent + while parent: + name = f"{parent.name}.{name}" + parent = parent.parent + except AttributeError: + pass + return name @property def render_parameters(self) -> list[Parameter]: From bfc72df7248a065caf4f698ce67d0c68d038b0c8 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Sat, 12 Aug 2023 21:42:52 +0200 Subject: [PATCH 2/5] Address review --- Tools/clinic/clinic.py | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index 6fdf70ed1dd026..2d23f9d12875ff 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -2682,17 +2682,15 @@ def displayname(self) -> str: @functools.cached_property def fulldisplayname(self) -> str: + parent: Class | Module | Clinic | None + if self.kind.new_or_init: + parent = getattr(self.cls, "parent", None) + else: + parent = self.parent name = self.displayname - try: - if self.kind.new_or_init: - parent = self.cls.parent - else: - parent = self.parent - while parent: - name = f"{parent.name}.{name}" - parent = parent.parent - except AttributeError: - pass + while isinstance(parent, (Module, Class)): + name = f"{parent.name}.{name}" + parent = parent.parent return name @property From 26a216e92d4fa450ccd2c674f4a2f53f8bd6a228 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Sun, 13 Aug 2023 01:13:34 +0200 Subject: [PATCH 3/5] Add more tests --- Lib/test/test_clinic.py | 63 ++++++++++++++++++++++++++++++----------- 1 file changed, 47 insertions(+), 16 deletions(-) diff --git a/Lib/test/test_clinic.py b/Lib/test/test_clinic.py index 2d6557c138b538..33b9c2f3d1f2cd 100644 --- a/Lib/test/test_clinic.py +++ b/Lib/test/test_clinic.py @@ -1514,24 +1514,55 @@ def test_parameters_required_after_star(self): self.expect_failure(block, err) def test_fulldisplayname_class(self): - block = self.parse(""" - module m - class m.T "void *" "" - class m.T.C "void *" "" - m.T.C.__init__ - """) - func = block.signatures[-1] - self.assertEqual(func.fulldisplayname, "m.T.C") + dataset = ( + ("T", """ + module m + class T "void *" "" + T.__init__ + """), + ("m.T", """ + module m + class m.T "void *" "" + @classmethod + m.T.__new__ + """), + ("m.T.C", """ + module m + class m.T "void *" "" + class m.T.C "void *" "" + m.T.C.__init__ + """), + ) + for name, code in dataset: + with self.subTest(name=name, code=code): + block = self.parse(code) + func = block.signatures[-1] + self.assertEqual(func.fulldisplayname, name) def test_fulldisplayname_meth(self): - block = self.parse(""" - module m - class m.T "" "" - class m.T.C "" "" - m.T.C.func - """) - func = block.signatures[-1] - self.assertEqual(func.fulldisplayname, "m.T.C.func") + dataset = ( + ("func", "func"), + ("m.func", """ + module m + m.func + """), + ("m.T.meth", """ + module m + class m.T "void *" "" + m.T.meth + """), + ("m.T.C.meth", """ + module m + class m.T "void *" "" + class m.T.C "void *" "" + m.T.C.meth + """), + ) + for name, code in dataset: + with self.subTest(name=name, code=code): + block = self.parse(code) + func = block.signatures[-1] + self.assertEqual(func.fulldisplayname, name) def test_depr_star_invalid_format_1(self): block = """ From ba1559824e932aac8adbe084600d94f0de73cec4 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Sun, 13 Aug 2023 01:15:20 +0200 Subject: [PATCH 4/5] Update Lib/test/test_clinic.py --- Lib/test/test_clinic.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Lib/test/test_clinic.py b/Lib/test/test_clinic.py index 33b9c2f3d1f2cd..98d1735670e225 100644 --- a/Lib/test/test_clinic.py +++ b/Lib/test/test_clinic.py @@ -1516,7 +1516,6 @@ def test_parameters_required_after_star(self): def test_fulldisplayname_class(self): dataset = ( ("T", """ - module m class T "void *" "" T.__init__ """), From c8fcaa45f98228fd730d3cf40c0a0134c2a03aec Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Sun, 13 Aug 2023 01:17:02 +0200 Subject: [PATCH 5/5] Update Lib/test/test_clinic.py --- Lib/test/test_clinic.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Lib/test/test_clinic.py b/Lib/test/test_clinic.py index 98d1735670e225..55251b516d2b2f 100644 --- a/Lib/test/test_clinic.py +++ b/Lib/test/test_clinic.py @@ -1545,6 +1545,10 @@ def test_fulldisplayname_meth(self): module m m.func """), + ("T.meth", """ + class T "void *" "" + T.meth + """), ("m.T.meth", """ module m class m.T "void *" ""