From 875df20e8a2a9bad07a42f0175be019d915e0845 Mon Sep 17 00:00:00 2001 From: Yury Selivanov Date: Thu, 27 Mar 2014 18:23:03 -0400 Subject: [PATCH] inspect: Fix getcallargs() to raise correct TypeError ... for missing keyword-only arguments. Patch by Jeremiah Lowin. Closes #20816. --- Lib/inspect.py | 2 +- Lib/test/test_inspect.py | 8 ++++++++ Misc/NEWS | 3 +++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/Lib/inspect.py b/Lib/inspect.py index 05d67fce54e..fdb5e2a45a2 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -1210,7 +1210,7 @@ def getcallargs(*func_and_positional, **named): missing = 0 for kwarg in kwonlyargs: if kwarg not in arg2value: - if kwarg in kwonlydefaults: + if kwonlydefaults and kwarg in kwonlydefaults: arg2value[kwarg] = kwonlydefaults[kwarg] else: missing += 1 diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py index 95b88779f98..b94353031bd 100644 --- a/Lib/test/test_inspect.py +++ b/Lib/test/test_inspect.py @@ -1208,6 +1208,14 @@ class TestGetcallargsFunctions(unittest.TestCase): self.assertEqualException(f3, '1, 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): def setUp(self): diff --git a/Misc/NEWS b/Misc/NEWS index c8384f9df37..fa774ba50d9 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -113,6 +113,9 @@ Library - 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 -------------