Skip to content

Commit

Permalink
[3.11] pythongh-106368: Increase Argument Clinic test coverage (pytho…
Browse files Browse the repository at this point in the history
…n#106369)

Add tests for 'self' and 'defining_class' converter requirements.
(cherry picked from commit 7f4c812)

Co-authored-by: Erlend E. Aasland <[email protected]>
  • Loading branch information
erlend-aasland committed Jul 3, 2023
1 parent e0d951d commit 2899599
Showing 1 changed file with 111 additions and 0 deletions.
111 changes: 111 additions & 0 deletions Lib/test/test_clinic.py
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,117 @@ def test_other_bizarre_things_in_annotations_fail(self):
)
self.assertEqual(s, expected_failure_message)

def test_kwarg_splats_disallowed_in_function_call_annotations(self):
expected_error_msg = (
"Error on line 0:\n"
"Cannot use a kwarg splat in a function-call annotation\n"
)
dataset = (
'module fo\nfo.barbaz\n o: bool(**{None: "bang!"})',
'module fo\nfo.barbaz -> bool(**{None: "bang!"})',
'module fo\nfo.barbaz -> bool(**{"bang": 42})',
'module fo\nfo.barbaz\n o: bool(**{"bang": None})',
)
for fn in dataset:
with self.subTest(fn=fn):
out = self.parse_function_should_fail(fn)
self.assertEqual(out, expected_error_msg)

def test_self_param_placement(self):
expected_error_msg = (
"Error on line 0:\n"
"A 'self' parameter, if specified, must be the very first thing "
"in the parameter block.\n"
)
block = """
module foo
foo.func
a: int
self: self(type="PyObject *")
"""
out = self.parse_function_should_fail(block)
self.assertEqual(out, expected_error_msg)

def test_self_param_cannot_be_optional(self):
expected_error_msg = (
"Error on line 0:\n"
"A 'self' parameter cannot be marked optional.\n"
)
block = """
module foo
foo.func
self: self(type="PyObject *") = None
"""
out = self.parse_function_should_fail(block)
self.assertEqual(out, expected_error_msg)

def test_defining_class_param_placement(self):
expected_error_msg = (
"Error on line 0:\n"
"A 'defining_class' parameter, if specified, must either be the "
"first thing in the parameter block, or come just after 'self'.\n"
)
block = """
module foo
foo.func
self: self(type="PyObject *")
a: int
cls: defining_class
"""
out = self.parse_function_should_fail(block)
self.assertEqual(out, expected_error_msg)

def test_defining_class_param_cannot_be_optional(self):
expected_error_msg = (
"Error on line 0:\n"
"A 'defining_class' parameter cannot be marked optional.\n"
)
block = """
module foo
foo.func
cls: defining_class(type="PyObject *") = None
"""
out = self.parse_function_should_fail(block)
self.assertEqual(out, expected_error_msg)

def test_unused_param(self):
block = self.parse("""
module foo
foo.func
fn: object
k: float
i: float(unused=True)
/
*
flag: bool(unused=True) = False
""")
sig = block.signatures[1] # Function index == 1
params = sig.parameters
conv = lambda fn: params[fn].converter
dataset = (
{"name": "fn", "unused": False},
{"name": "k", "unused": False},
{"name": "i", "unused": True},
{"name": "flag", "unused": True},
)
for param in dataset:
name, unused = param.values()
with self.subTest(name=name, unused=unused):
p = conv(name)
# Verify that the unused flag is parsed correctly.
self.assertEqual(unused, p.unused)

# Now, check that we'll produce correct code.
decl = p.simple_declaration(in_parser=False)
if unused:
self.assertIn("Py_UNUSED", decl)
else:
self.assertNotIn("Py_UNUSED", decl)

# Make sure the Py_UNUSED macro is not used in the parser body.
parser_decl = p.simple_declaration(in_parser=True)
self.assertNotIn("Py_UNUSED", parser_decl)

def parse(self, text):
c = FakeClinic()
parser = DSLParser(c)
Expand Down

0 comments on commit 2899599

Please sign in to comment.