Kaydet (Commit) 875df20e authored tarafından Yury Selivanov's avatar Yury Selivanov

inspect: Fix getcallargs() to raise correct TypeError

... for missing keyword-only arguments. Patch by Jeremiah Lowin.
Closes #20816.
üst 374375dd
...@@ -1210,7 +1210,7 @@ def getcallargs(*func_and_positional, **named): ...@@ -1210,7 +1210,7 @@ def getcallargs(*func_and_positional, **named):
missing = 0 missing = 0
for kwarg in kwonlyargs: for kwarg in kwonlyargs:
if kwarg not in arg2value: if kwarg not in arg2value:
if kwarg in kwonlydefaults: if kwonlydefaults and kwarg in kwonlydefaults:
arg2value[kwarg] = kwonlydefaults[kwarg] arg2value[kwarg] = kwonlydefaults[kwarg]
else: else:
missing += 1 missing += 1
......
...@@ -1208,6 +1208,14 @@ class TestGetcallargsFunctions(unittest.TestCase): ...@@ -1208,6 +1208,14 @@ class TestGetcallargsFunctions(unittest.TestCase):
self.assertEqualException(f3, '1, 2') self.assertEqualException(f3, '1, 2')
self.assertEqualException(f3, '1, 2, a=1, b=2') self.assertEqualException(f3, '1, 2, a=1, b=2')
# issue #20816: getcallargs() fails to iterate over non-existent
# kwonlydefaults and raises a wrong TypeError
def f5(*, a): pass
with self.assertRaisesRegex(TypeError,
'missing 1 required keyword-only'):
inspect.getcallargs(f5)
class TestGetcallargsMethods(TestGetcallargsFunctions): class TestGetcallargsMethods(TestGetcallargsFunctions):
def setUp(self): def setUp(self):
......
...@@ -113,6 +113,9 @@ Library ...@@ -113,6 +113,9 @@ Library
- Issue #20378: Improve repr of inspect.Signature and inspect.Parameter. - Issue #20378: Improve repr of inspect.Signature and inspect.Parameter.
- Issue #20816: Fix inspect.getcallargs() to raise correct TypeError for
missing keyword-only arguments. Patch by Jeremiah Lowin.
Documentation Documentation
------------- -------------
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment