Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-107805: Fix signatures of module-level generated functions inturtle#107807

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
gpshead merged 7 commits intopython:mainfromsobolevn:issue-107805
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
NextNext commit
gh-107805: Fix signatures of module-level generated functions in `tur…
…tle`
  • Loading branch information
@sobolevn
sobolevn committedAug 9, 2023
commit40ee61d8570a9a90f72b941121c062a03cccd0da
27 changes: 27 additions & 0 deletionsLib/test/test_turtle.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -461,5 +461,32 @@ def test_teleport(self):
self.assertTrue(tpen.isdown())


class TestModuleLevel(unittest.TestCase):
def test_all_signatures(self):
import inspect
import types

known_signatures = {
'teleport':
'(x=None, y=None, fill_gap: bool = False) -> None',
'clear': '()',
'reset': '(canvwidth=None, canvheight=None, bg=None)',
'bgcolor': '(*args)',
'pen': '(pen=None, **pendict)',
}

for name in turtle.__all__:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

The purpose of this test is only to verify known signatures, correct?

In that case, looping overknown_signatures.items() rather thanturtle.__all__ makes more sense. Otherwise a typo in a known_signatures name key would go silently unnoticed and we're otherwise creating subtests for all names in turtle even though most of them don't test anything.

obj = getattr(turtle, name)
if not isinstance(obj, types.FunctionType):
continue

with self.subTest(name=name):
# All functions must produce correct signatures:
sig = inspect.signature(obj)

if name in known_signatures:
self.assertEqual(str(sig), known_signatures[name])


if __name__ == '__main__':
unittest.main()
41 changes: 22 additions & 19 deletionsLib/turtle.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -3920,28 +3920,31 @@ def getmethparlist(ob):
function definition and the second is suitable for use in function
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
# but we drop the "self" param.
# Try and build one for Python defined functions
args, varargs, varkw = inspect.getargs(ob.__code__)
items2 = args[1:]
realArgs = args[1:]
defaults = ob.__defaults__ or []
defaults = ["=%r" % (value,) for value in defaults]
defaults = [""] * (len(realArgs)-len(defaults)) + defaults
items1 = [arg + dflt for arg, dflt in zip(realArgs, defaults)]
if varargs is not None:
items1.append("*" + varargs)
items2.append("*" + varargs)
if varkw is not None:
items1.append("**" + varkw)
items2.append("**" + varkw)
defText = ", ".join(items1)
defText = "(%s)" % defText
callText = ", ".join(items2)
callText = "(%s)" % callText
return defText, callText
func_sig = orig_sig.replace(
parameters=list(orig_sig.parameters.values())[1:],
)

call_args = []
for param in func_sig.parameters.values():
kind = param.kind
if kind in (
inspect.Parameter.POSITIONAL_ONLY,
inspect.Parameter.POSITIONAL_OR_KEYWORD,
):
call_args.append(param.name)
if kind == inspect.Parameter.VAR_POSITIONAL:
call_args.append(f'*{param.name}')
if kind == inspect.Parameter.KEYWORD_ONLY:
call_args.append(f'{param.name}={param.name}')
if kind == inspect.Parameter.VAR_KEYWORD:
call_args.append(f'**{param.name}')
call_text = f'({', '.join(call_args)})'

return str(func_sig), call_text

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

[8]ページ先頭

©2009-2025 Movatter.jp