Skip to content

Commit 82094c1

Browse files
committed
Fix __signature__ support if object has a __file__
1 parent 26fc2b5 commit 82094c1

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

bpython/inspection.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,14 @@ def getfuncprops(func: str, f: Callable) -> Optional[FuncProps]:
289289
return None
290290
try:
291291
argspec = _get_argspec_from_signature(f)
292+
try:
293+
argspec = _fix_default_values(f, argspec)
294+
except KeyError as ex:
295+
# Parsing of the source failed. If f has a __signature__, we trust it.
296+
if not hasattr(f, "__signature__"):
297+
raise ex
292298
fprops = FuncProps(
293-
func, _fix_default_values(f, argspec), is_bound_method
299+
func, argspec, is_bound_method
294300
)
295301
except (TypeError, KeyError, ValueError):
296302
argspec_pydoc = _getpydocspec(f)

bpython/test/test_repl.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import collections
22
import inspect
3+
import os
34
import socket
45
import sys
56
import tempfile
@@ -523,13 +524,14 @@ def __init__(self, *args, **kwargs):
523524
inspect.Parameter("pinetree", inspect.Parameter.KEYWORD_ONLY),
524525
])
525526
"""
526-
for line in code.split("\n"):
527-
print(line[8:])
528-
self.repl.push(line[8:])
527+
code = [x[8:] for x in code.split("\n")]
528+
for line in code:
529+
self.repl.push(line)
529530

530-
self.assertTrue(self.repl.complete())
531-
self.assertTrue(hasattr(self.repl.matches_iter, "matches"))
532-
self.assertEqual(self.repl.matches_iter.matches, ["apple2=", "apple="])
531+
with mock.patch("bpython.inspection.inspect.getsourcelines", return_value=(code, None)):
532+
self.assertTrue(self.repl.complete())
533+
self.assertTrue(hasattr(self.repl.matches_iter, "matches"))
534+
self.assertEqual(self.repl.matches_iter.matches, ["apple2=", "apple="])
533535

534536

535537
if __name__ == "__main__":

0 commit comments

Comments
 (0)