gh-107805: Fix signatures of module-level generated functions in turtle (#107807)

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
This commit is contained in:
Nikita Sobolev 2023-09-01 11:18:15 +03:00 committed by GitHub
parent 3edcf743e8
commit 044b8b3b6a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 19 deletions

View file

@ -461,5 +461,25 @@ class TestTPen(unittest.TestCase):
self.assertTrue(tpen.isdown()) self.assertTrue(tpen.isdown())
class TestModuleLevel(unittest.TestCase):
def test_all_signatures(self):
import inspect
known_signatures = {
'teleport':
'(x=None, y=None, *, fill_gap: bool = False) -> None',
'undo': '()',
'goto': '(x, y=None)',
'bgcolor': '(*args)',
'pen': '(pen=None, **pendict)',
}
for name in known_signatures:
with self.subTest(name=name):
obj = getattr(turtle, name)
sig = inspect.signature(obj)
self.assertEqual(str(sig), known_signatures[name])
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()

View file

@ -3920,28 +3920,33 @@ def getmethparlist(ob):
function definition and the second is suitable for use in function function definition and the second is suitable for use in function
call. The "self" parameter is not included. call. The "self" parameter is not included.
""" """
defText = callText = "" orig_sig = inspect.signature(ob)
# bit of a hack for methods - turn it into a function # bit of a hack for methods - turn it into a function
# but we drop the "self" param. # but we drop the "self" param.
# Try and build one for Python defined functions # Try and build one for Python defined functions
args, varargs, varkw = inspect.getargs(ob.__code__) func_sig = orig_sig.replace(
items2 = args[1:] parameters=list(orig_sig.parameters.values())[1:],
realArgs = args[1:] )
defaults = ob.__defaults__ or []
defaults = ["=%r" % (value,) for value in defaults] call_args = []
defaults = [""] * (len(realArgs)-len(defaults)) + defaults for param in func_sig.parameters.values():
items1 = [arg + dflt for arg, dflt in zip(realArgs, defaults)] match param.kind:
if varargs is not None: case (
items1.append("*" + varargs) inspect.Parameter.POSITIONAL_ONLY
items2.append("*" + varargs) | inspect.Parameter.POSITIONAL_OR_KEYWORD
if varkw is not None: ):
items1.append("**" + varkw) call_args.append(param.name)
items2.append("**" + varkw) case inspect.Parameter.VAR_POSITIONAL:
defText = ", ".join(items1) call_args.append(f'*{param.name}')
defText = "(%s)" % defText case inspect.Parameter.KEYWORD_ONLY:
callText = ", ".join(items2) call_args.append(f'{param.name}={param.name}')
callText = "(%s)" % callText case inspect.Parameter.VAR_KEYWORD:
return defText, callText call_args.append(f'**{param.name}')
case _:
raise RuntimeError('Unsupported parameter kind', param.kind)
call_text = f'({', '.join(call_args)})'
return str(func_sig), call_text
def _turtle_docrevise(docstr): def _turtle_docrevise(docstr):
"""To reduce docstrings from RawTurtle class for functions """To reduce docstrings from RawTurtle class for functions

View file

@ -0,0 +1 @@
Fix signatures of module-level generated functions in :mod:`turtle`.