Skip to content

Commit 8eb317c

Browse files
committed
gh-152692: Fix annotationlib.type_repr() for bound built-in methods
type_repr() returned a string naming a nonexistent module None -- e.g. type_repr([].append) == 'None.list.append' -- for bound built-in methods, whose __module__ is None. Return the qualified name when __module__ is None, as already done for the "builtins" module.
1 parent ecdef17 commit 8eb317c

3 files changed

Lines changed: 10 additions & 1 deletion

File tree

Lib/annotationlib.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1090,7 +1090,8 @@ def type_repr(value):
10901090
10911091
"""
10921092
if isinstance(value, (type, types.FunctionType, types.BuiltinFunctionType)):
1093-
if value.__module__ == "builtins":
1093+
# Built-in methods have __module__ set to None.
1094+
if value.__module__ is None or value.__module__ == "builtins":
10941095
return value.__qualname__
10951096
return f"{value.__module__}.{value.__qualname__}"
10961097
elif isinstance(value, _Template):

Lib/test/test_annotationlib.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1850,6 +1850,10 @@ def nested():
18501850
type_repr(nested), f"{__name__}.TestTypeRepr.test_type_repr.<locals>.nested"
18511851
)
18521852
self.assertEqual(type_repr(len), "len")
1853+
# gh-152692: built-in methods have __module__ set to None
1854+
self.assertEqual(type_repr([].append), "list.append")
1855+
self.assertEqual(type_repr((1).bit_length), "int.bit_length")
1856+
self.assertEqual(type_repr(dict.fromkeys), "dict.fromkeys")
18531857
self.assertEqual(type_repr(type_repr), "annotationlib.type_repr")
18541858
self.assertEqual(type_repr(times_three), f"{__name__}.times_three")
18551859
self.assertEqual(type_repr(...), "...")
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Fix :func:`annotationlib.type_repr` returning a string such as
2+
``None.list.append`` for bound built-in methods, whose ``__module__`` is
3+
``None``. It now returns the qualified name, as it already did for other
4+
builtins.

0 commit comments

Comments
 (0)