Skip to content

Commit 5668309

Browse files
miss-islingtonsobolevnAlexWaygood
authored
[3.12] gh-107805: Fix signatures of module-level generated functions in turtle (GH-107807) (#108749)
gh-107805: Fix signatures of module-level generated functions in `turtle` (GH-107807) (cherry picked from commit 044b8b3) Co-authored-by: Nikita Sobolev <mail@sobolevn.me> Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
1 parent 1e15c15 commit 5668309

File tree

3 files changed

+45
-19
lines changed

3 files changed

+45
-19
lines changed

Lib/test/test_turtle.py

+20
Original file line numberDiff line numberDiff line change
@@ -461,5 +461,25 @@ def test_teleport(self):
461461
self.assertTrue(tpen.isdown())
462462

463463

464+
class TestModuleLevel(unittest.TestCase):
465+
def test_all_signatures(self):
466+
import inspect
467+
468+
known_signatures = {
469+
'teleport':
470+
'(x=None, y=None, *, fill_gap: bool = False) -> None',
471+
'undo': '()',
472+
'goto': '(x, y=None)',
473+
'bgcolor': '(*args)',
474+
'pen': '(pen=None, **pendict)',
475+
}
476+
477+
for name in known_signatures:
478+
with self.subTest(name=name):
479+
obj = getattr(turtle, name)
480+
sig = inspect.signature(obj)
481+
self.assertEqual(str(sig), known_signatures[name])
482+
483+
464484
if __name__ == '__main__':
465485
unittest.main()

Lib/turtle.py

+24-19
Original file line numberDiff line numberDiff line change
@@ -3951,28 +3951,33 @@ def getmethparlist(ob):
39513951
function definition and the second is suitable for use in function
39523952
call. The "self" parameter is not included.
39533953
"""
3954-
defText = callText = ""
3954+
orig_sig = inspect.signature(ob)
39553955
# bit of a hack for methods - turn it into a function
39563956
# but we drop the "self" param.
39573957
# Try and build one for Python defined functions
3958-
args, varargs, varkw = inspect.getargs(ob.__code__)
3959-
items2 = args[1:]
3960-
realArgs = args[1:]
3961-
defaults = ob.__defaults__ or []
3962-
defaults = ["=%r" % (value,) for value in defaults]
3963-
defaults = [""] * (len(realArgs)-len(defaults)) + defaults
3964-
items1 = [arg + dflt for arg, dflt in zip(realArgs, defaults)]
3965-
if varargs is not None:
3966-
items1.append("*" + varargs)
3967-
items2.append("*" + varargs)
3968-
if varkw is not None:
3969-
items1.append("**" + varkw)
3970-
items2.append("**" + varkw)
3971-
defText = ", ".join(items1)
3972-
defText = "(%s)" % defText
3973-
callText = ", ".join(items2)
3974-
callText = "(%s)" % callText
3975-
return defText, callText
3958+
func_sig = orig_sig.replace(
3959+
parameters=list(orig_sig.parameters.values())[1:],
3960+
)
3961+
3962+
call_args = []
3963+
for param in func_sig.parameters.values():
3964+
match param.kind:
3965+
case (
3966+
inspect.Parameter.POSITIONAL_ONLY
3967+
| inspect.Parameter.POSITIONAL_OR_KEYWORD
3968+
):
3969+
call_args.append(param.name)
3970+
case inspect.Parameter.VAR_POSITIONAL:
3971+
call_args.append(f'*{param.name}')
3972+
case inspect.Parameter.KEYWORD_ONLY:
3973+
call_args.append(f'{param.name}={param.name}')
3974+
case inspect.Parameter.VAR_KEYWORD:
3975+
call_args.append(f'**{param.name}')
3976+
case _:
3977+
raise RuntimeError('Unsupported parameter kind', param.kind)
3978+
call_text = f'({', '.join(call_args)})'
3979+
3980+
return str(func_sig), call_text
39763981

39773982
def _turtle_docrevise(docstr):
39783983
"""To reduce docstrings from RawTurtle class for functions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix signatures of module-level generated functions in :mod:`turtle`.

0 commit comments

Comments
 (0)