From 98e6c3acb36b58bfb6e14b0d1ae2c11e3ebcc9c5 Mon Sep 17 00:00:00 2001 From: AlexWaygood Date: Sat, 5 Aug 2023 17:13:46 +0100 Subject: [PATCH 1/2] Improve consistency and test coverage of argument-clinic `__repr__` functions --- Lib/test/test_clinic.py | 98 +++++++++++++++++++++++++++++++++++++++-- Tools/clinic/clinic.py | 20 +++++---- Tools/clinic/cpp.py | 7 ++- 3 files changed, 112 insertions(+), 13 deletions(-) diff --git a/Lib/test/test_clinic.py b/Lib/test/test_clinic.py index 84b6a193ecf914..29770880aa1ea4 100644 --- a/Lib/test/test_clinic.py +++ b/Lib/test/test_clinic.py @@ -69,7 +69,7 @@ def __init__(self): self.converters = FakeConvertersDict() self.legacy_converters = FakeConvertersDict() self.language = clinic.CLanguage(None) - self.filename = None + self.filename = "clinic_tests" self.destination_buffers = {} self.block_parser = clinic.BlockParser('', self.language) self.modules = collections.OrderedDict() @@ -1849,10 +1849,10 @@ def test_non_ascii_character_in_docstring(self): self.parse(block) # The line numbers are off; this is a known limitation. expected = dedent("""\ - Warning on line 0: + Warning in file 'clinic_tests' on line 0: Non-ascii characters are not allowed in docstrings: 'á' - Warning on line 0: + Warning in file 'clinic_tests' on line 0: Non-ascii characters are not allowed in docstrings: 'ü', 'á', 'ß' """) @@ -3030,5 +3030,97 @@ def test_suffix_all_lines(self): self.assertEqual(out, expected) +class ClinicReprTests(unittest.TestCase): + def test_Block_repr(self): + block = clinic.Block("foo") + expected_repr = "" + self.assertEqual(repr(block), expected_repr) + + block2 = clinic.Block("bar", "baz", [], "eggs", "spam") + expected_repr_2 = "" + self.assertEqual(repr(block2), expected_repr_2) + + block3 = clinic.Block( + input="longboi_" * 100, + dsl_name="wow_so_long", + signatures=[], + output="very_long_" * 100, + indent="" + ) + expected_repr_3 = ( + "" + ) + self.assertEqual(repr(block3), expected_repr_3) + + def test_Destination_repr(self): + destination = clinic.Destination( + "foo", type="file", clinic=FakeClinic(), args=("eggs",) + ) + self.assertEqual( + repr(destination), "" + ) + + destination2 = clinic.Destination("bar", type="buffer", clinic=FakeClinic()) + self.assertEqual(repr(destination2), "") + + def test_Module_repr(self): + module = clinic.Module("foo", FakeClinic()) + self.assertRegex(repr(module), r"") + + def test_Class_repr(self): + cls = clinic.Class("foo", FakeClinic(), None, 'some_typedef', 'some_type_object') + self.assertRegex(repr(cls), r"") + + def test_FunctionKind_repr(self): + self.assertEqual( + repr(clinic.FunctionKind.INVALID), "" + ) + self.assertEqual( + repr(clinic.FunctionKind.CLASS_METHOD), "" + ) + + def test_Function_and_Parameter_reprs(self): + function = clinic.Function( + name='foo', + module=FakeClinic(), + cls=None, + c_basename=None, + full_name='foofoo', + return_converter=clinic.init_return_converter(), + kind=clinic.FunctionKind.METHOD_INIT, + coexist=False + ) + self.assertEqual(repr(function), "") + + converter = clinic.self_converter('bar', 'bar', function) + parameter = clinic.Parameter( + "bar", + kind=inspect.Parameter.POSITIONAL_OR_KEYWORD, + function=function, + converter=converter + ) + self.assertEqual(repr(parameter), "") + + def test_Monitor_repr(self): + monitor = clinic.cpp.Monitor() + self.assertRegex( + repr(monitor), + r"" + ) + + monitor.line_number = 42 + monitor.stack.append(("token1", "condition1")) + self.assertRegex( + repr(monitor), + r"" + ) + + monitor.stack.append(("token2", "condition2")) + self.assertRegex( + repr(monitor), + r"" + ) + + if __name__ == "__main__": unittest.main() diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index 7fbae1e0d870ad..423cdfb939c161 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -1728,8 +1728,12 @@ def summarize(s: object) -> str: if len(s) > 30: return s[:26] + "..." + s[0] return s - return "".join(( - "")) + parts = ( + repr(dsl_name), + f"input={summarize(self.input)}", + f"output={summarize(self.output)}" + ) + return f"" class BlockParser: @@ -2037,10 +2041,10 @@ def __post_init__(self, args: tuple[str, ...]) -> None: def __repr__(self) -> str: if self.type == 'file': - file_repr = " " + repr(self.filename) + type_repr = f"type='file' file={self.filename!r}" else: - file_repr = '' - return "".join(("")) + type_repr = f"type={self.type!r}" + return f"" def clear(self) -> None: if self.type != 'buffer': @@ -2500,7 +2504,7 @@ def new_or_init(self) -> bool: return self in {FunctionKind.METHOD_INIT, FunctionKind.METHOD_NEW} def __repr__(self) -> str: - return f"" + return f"" INVALID: Final = FunctionKind.INVALID @@ -2577,7 +2581,7 @@ def methoddef_flags(self) -> str | None: return '|'.join(flags) def __repr__(self) -> str: - return '' + return f'' def copy(self, **overrides: Any) -> Function: f = dc.replace(self, **overrides) @@ -2605,7 +2609,7 @@ class Parameter: right_bracket_count: int = dc.field(init=False, default=0) def __repr__(self) -> str: - return '' + return f'' def is_keyword_only(self) -> bool: return self.kind == inspect.Parameter.KEYWORD_ONLY diff --git a/Tools/clinic/cpp.py b/Tools/clinic/cpp.py index 21a1b02e862c1c..876105120c97f2 100644 --- a/Tools/clinic/cpp.py +++ b/Tools/clinic/cpp.py @@ -43,9 +43,12 @@ def __post_init__(self) -> None: self.line_number = 0 def __repr__(self) -> str: - return ( - f"" + parts = ( + str(id(self)), + f"line={self.line_number}", + f"condition={self.condition()!r}" ) + return f"" def status(self) -> str: return str(self.line_number).rjust(4) + ": " + self.condition() From d2f6bbd3405ff7b2f1a082b92e63059a774f08ad Mon Sep 17 00:00:00 2001 From: AlexWaygood Date: Sat, 5 Aug 2023 18:21:19 +0100 Subject: [PATCH 2/2] Simpler regexes --- Lib/test/test_clinic.py | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/Lib/test/test_clinic.py b/Lib/test/test_clinic.py index 29770880aa1ea4..f30fad2126940b 100644 --- a/Lib/test/test_clinic.py +++ b/Lib/test/test_clinic.py @@ -3065,11 +3065,11 @@ def test_Destination_repr(self): def test_Module_repr(self): module = clinic.Module("foo", FakeClinic()) - self.assertRegex(repr(module), r"") + self.assertRegex(repr(module), r"") def test_Class_repr(self): cls = clinic.Class("foo", FakeClinic(), None, 'some_typedef', 'some_type_object') - self.assertRegex(repr(cls), r"") + self.assertRegex(repr(cls), r"") def test_FunctionKind_repr(self): self.assertEqual( @@ -3103,22 +3103,18 @@ def test_Function_and_Parameter_reprs(self): def test_Monitor_repr(self): monitor = clinic.cpp.Monitor() - self.assertRegex( - repr(monitor), - r"" - ) + self.assertRegex(repr(monitor), r"") monitor.line_number = 42 monitor.stack.append(("token1", "condition1")) self.assertRegex( - repr(monitor), - r"" + repr(monitor), r"" ) monitor.stack.append(("token2", "condition2")) self.assertRegex( repr(monitor), - r"" + r"" )