Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Doc/library/types.rst
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,14 @@ Standard names are defined for the following types:
.. versionadded:: 3.7


.. data:: ClassMethodDescriptorType

The type of *unbound* class methods of some built-in data types such as
``dict.__dict__['fromkeys']``.

.. versionadded:: 3.7


.. class:: ModuleType(name, doc=None)

The type of :term:`modules <module>`. Constructor takes the name of the
Expand Down
4 changes: 2 additions & 2 deletions Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,10 +457,10 @@ def classify_class_attrs(cls):
continue
obj = get_obj if get_obj is not None else dict_obj
# Classify the object or its descriptor.
if isinstance(dict_obj, staticmethod):
if isinstance(dict_obj, (staticmethod, types.BuiltinMethodType)):
kind = "static method"
obj = dict_obj
elif isinstance(dict_obj, classmethod):
elif isinstance(dict_obj, (classmethod, types.ClassMethodDescriptorType)):
kind = "class method"
obj = dict_obj
elif isinstance(dict_obj, property):
Expand Down
15 changes: 14 additions & 1 deletion Lib/test/test_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -858,7 +858,8 @@ def m1(self): pass

attrs = attrs_wo_objs(A)

self.assertIn(('__new__', 'method', object), attrs, 'missing __new__')
self.assertIn(('__new__', 'static method', object), attrs,
'missing __new__')
self.assertIn(('__init__', 'method', object), attrs, 'missing __init__')

self.assertIn(('s', 'static method', A), attrs, 'missing static method')
Expand Down Expand Up @@ -923,6 +924,18 @@ def test_classify_builtin_types(self):
if isinstance(builtin, type):
inspect.classify_class_attrs(builtin)

attrs = attrs_wo_objs(bool)
self.assertIn(('__new__', 'static method', bool), attrs,
'missing __new__')
self.assertIn(('from_bytes', 'class method', int), attrs,
'missing class method')
self.assertIn(('to_bytes', 'method', int), attrs,
'missing plain method')
self.assertIn(('__add__', 'method', int), attrs,
'missing plain method')
self.assertIn(('__and__', 'method', bool), attrs,
'missing plain method')

def test_classify_DynamicClassAttribute(self):
class Meta(type):
def __getattr__(self, name):
Expand Down
4 changes: 4 additions & 0 deletions Lib/test/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,10 @@ def test_method_descriptor_types(self):
self.assertIsInstance(''.join, types.BuiltinMethodType)
self.assertIsInstance([].append, types.BuiltinMethodType)

self.assertIsInstance(int.__dict__['from_bytes'], types.ClassMethodDescriptorType)
self.assertIsInstance(int.from_bytes, types.BuiltinMethodType)
self.assertIsInstance(int.__new__, types.BuiltinMethodType)


class MappingProxyTests(unittest.TestCase):
mappingproxy = types.MappingProxyType
Expand Down
1 change: 1 addition & 0 deletions Lib/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def _m(self): pass
WrapperDescriptorType = type(object.__init__)
MethodWrapperType = type(object().__str__)
MethodDescriptorType = type(str.join)
ClassMethodDescriptorType = type(dict.__dict__['fromkeys'])

ModuleType = type(sys)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
All class and static methods of builtin types now are correctly classified
by inspect.classify_class_attrs() and grouped in pydoc ouput. Added
types.ClassMethodDescriptorType for unbound class methods of builtin types.