GH-103899: Provide a hint when accidentally calling a module (GH-103900)

This commit is contained in:
Brandt Bucher 2023-05-04 15:07:42 -07:00 committed by GitHub
parent f5c38382f9
commit 7d35c3121a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 73 additions and 6 deletions

View file

@ -10,6 +10,7 @@ import itertools
import gc
import contextlib
import sys
import types
class BadStr(str):
@ -202,6 +203,37 @@ class CFunctionCallsErrorMessages(unittest.TestCase):
msg = r"count\(\) takes no keyword arguments"
self.assertRaisesRegex(TypeError, msg, [].count, x=2, y=2)
def test_object_not_callable(self):
msg = r"^'object' object is not callable$"
self.assertRaisesRegex(TypeError, msg, object())
def test_module_not_callable_no_suggestion_0(self):
msg = r"^'module' object is not callable$"
self.assertRaisesRegex(TypeError, msg, types.ModuleType("mod"))
def test_module_not_callable_no_suggestion_1(self):
msg = r"^'module' object is not callable$"
mod = types.ModuleType("mod")
mod.mod = 42
self.assertRaisesRegex(TypeError, msg, mod)
def test_module_not_callable_no_suggestion_2(self):
msg = r"^'module' object is not callable$"
mod = types.ModuleType("mod")
del mod.__name__
self.assertRaisesRegex(TypeError, msg, mod)
def test_module_not_callable_no_suggestion_3(self):
msg = r"^'module' object is not callable$"
mod = types.ModuleType("mod")
mod.__name__ = 42
self.assertRaisesRegex(TypeError, msg, mod)
def test_module_not_callable_suggestion(self):
msg = r"^'module' object is not callable\. Did you mean: 'mod\.mod\(\.\.\.\)'\?$"
mod = types.ModuleType("mod")
mod.mod = lambda: ...
self.assertRaisesRegex(TypeError, msg, mod)
class TestCallingConventions(unittest.TestCase):