fix inspect.formatargspec on functions with keyword-only arguments without defaults #4959
diff --git a/Lib/inspect.py b/Lib/inspect.py
index b84aec0..45515fc 100644
--- a/Lib/inspect.py
+++ b/Lib/inspect.py
@@ -894,7 +894,7 @@
     if kwonlyargs:
         for kwonlyarg in kwonlyargs:
             spec = formatargandannotation(kwonlyarg)
-            if kwonlyarg in kwonlydefaults:
+            if kwonlydefaults and kwonlyarg in kwonlydefaults:
                 spec += formatvalue(kwonlydefaults[kwonlyarg])
             specs.append(spec)
     if varkw is not None:
diff --git a/Lib/test/inspect_fodder2.py b/Lib/test/inspect_fodder2.py
index d244935..bd7106f 100644
--- a/Lib/test/inspect_fodder2.py
+++ b/Lib/test/inspect_fodder2.py
@@ -105,3 +105,7 @@
 #line 105
 def annotated(arg1: list):
     pass
+
+#line 109
+def keyword_only_arg(*, arg):
+    pass
diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py
index ac9fcd7..b3aa28c 100644
--- a/Lib/test/test_inspect.py
+++ b/Lib/test/test_inspect.py
@@ -386,6 +386,9 @@
 
         self.assertRaises(ValueError, self.assertArgSpecEquals,
                           mod2.annotated, [])
+        self.assertRaises(ValueError, self.assertArgSpecEquals,
+                          mod2.keyword_only_arg, [])
+
 
     def test_getfullargspec(self):
         self.assertFullArgSpecEquals(mod2.keyworded, [], varargs_e='arg1',
@@ -396,6 +399,10 @@
         self.assertFullArgSpecEquals(mod2.annotated, ['arg1'],
                                      ann_e={'arg1' : list},
                                      formatted='(arg1: list)')
+        self.assertFullArgSpecEquals(mod2.keyword_only_arg, [],
+                                     kwonlyargs_e=['arg'],
+                                     formatted='(*, arg)')
+
 
     def test_getargspec_method(self):
         class A(object):
diff --git a/Misc/NEWS b/Misc/NEWS
index 7588f46..28cd6b6 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -126,6 +126,9 @@
 Library
 -------
 
+- Issue #4959: inspect.formatargspec now works for keyword only arguments
+  without defaults.
+
 - Issue #3826 and #4791: The socket module now closes the underlying socket
   appropriately when it is being used via socket.makefile() objects
   rather than delaying the close by waiting for garbage collection to do it.