10000 bpo-29418: Implement inspect.ismethodwrapper and fix inspect.isroutine for cases where methodwrapper is given by hakancelikdev · Pull Request #19261 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-29418: Implement inspect.ismethodwrapper and fix inspect.isroutine for cases where methodwrapper is given #19261

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Feb 16, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
More detail & test
  • Loading branch information
hakancelikdev authored and Hakan Çelik committed Feb 13, 2022
commit cc71878bc42fde9d4519348f78d8b3725d85fb44
2 changes: 1 addition & 1 deletion Doc/library/inspect.rst
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ attributes:

.. function:: ismethodwrapper(object)

Return ``True`` if the object is a method-wrapper.
Return ``True`` if the type of object is a :class:`~types.MethodWrapperType`.


.. function:: isroutine(object)
Expand Down
5 changes: 5 additions & 0 deletions Doc/whatsnew/3.10.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1223,6 +1223,9 @@ now call :func:`inspect.get_annotations` to retrieve annotations. This means
also now un-stringize stringized annotations.
(Contributed by Larry Hastings in :issue:`43817`.)

Add :func:`inspect.ismethodwrapper` for checking if the type of object is a
:class:`~types.MethodWrapperType`. (Contributed by Hakan Çelik in :issue:`29418`.)

linecache
---------

Expand Down Expand Up @@ -1511,6 +1514,7 @@ Add a :class:`~xml.sax.handler.LexicalHandler` class to the
:mod:`xml.sax.handler` module.
(Contributed by Jonathan Gossage and Zackery Spytz in :issue:`35018`.)


zipimport
---------
Add methods related to :pep:`451`: :meth:`~zipimport.zipimporter.find_spec`,
Expand All @@ -1520,6 +1524,7 @@ Add methods related to :pep:`451`: :meth:`~zipimport.zipimporter.find_spec`,

Add :meth:`~zipimport.zipimporter.invalidate_caches` method.
(Contributed by Desmond Cheong in :issue:`14678`.)
=======


Optimizations
Expand Down
3 changes: 0 additions & 3 deletions Doc/whatsnew/3.9.rst
Original file line number Diff line number Diff line change
Expand Up @@ -545,9 +545,6 @@ inspect
:attr:`inspect.BoundArguments.arguments` is changed from ``OrderedDict`` to regular
dict. (Contributed by Inada Naoki in :issue:`36350` and :issue:`39775`.)

Add :func:`inspect.ismethodwrapper` for checking if the object is a method wrapper.
(Contributed by Hakan Çelik in :issue:`29418`.)

ipaddress
---------

Expand Down
7 changes: 6 additions & 1 deletion Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,12 @@ def isbuiltin(object):
return isinstance(object, types.BuiltinFunctionType)

def ismethodwrapper(object):
"""return True if the object is a method-wrapper."""
"""Return true if the type of object is a types.MethodWrapperType.

For example;

>>> ismethodwrapper(object().__str__)
... True"""
return isinstance(object, types.MethodWrapperType)

def isroutine(object):
Expand Down
24 changes: 15 additions & 9 deletions Lib/test/test_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
# isbuiltin, isroutine, isgenerator, isgeneratorfunction, getmembers,
# getdoc, getfile, getmodule, getsourcefile, getcomments, getsource,
# getclasstree, getargvalues, formatargvalues,
# currentframe, stack, trace, isdatadescriptor
# currentframe, stack, trace, isdatadescriptor,
# ismethodwrapper

# NOTE: There are some additional tests relating to interaction with
# zipimport in the test_zipimport_support test module.
Expand Down Expand Up @@ -93,7 +94,8 @@ class IsTestBase(unittest.TestCase):
inspect.ismodule, inspect.istraceback,
inspect.isgenerator, inspect.isgeneratorfunction,
inspect.iscoroutine, inspect.iscoroutinefunction,
inspect.isasyncgen, inspect.isasyncgenfunction])
inspect.isasyncgen, inspect.isasyncgenfunction,
inspect.ismethodwrapper])

def istest(self, predicate, exp):
obj = eval(exp)
Expand Down Expand Up @@ -169,6 +171,10 @@ def test_excluding_predicates(self):
self.istest(inspect.ismemberdescriptor, 'datetime.timedelta.days')
else:
self.assertFalse(inspect.ismemberdescriptor(datetime.timedelta.days))
self.istest(inspect.ismethodwrapper, "object().__str__")
self.istest(inspect.ismethodwrapper, "object().__eq__")
self.istest(inspect.ismethodwrapper, "object().__repr__")


def test_iscoroutine(self):
async_gen_coro = async_generator_function_example(1)
Expand Down Expand Up @@ -241,33 +247,33 @@ class NotFuture: pass
coro.close(); gen_coro.close() # silence warnings

def test_isroutine(self):
# to method
# method
self.assertTrue(inspect.isroutine(git.argue))
self.assertTrue(inspect.isroutine(mod.custom_method))
self.assertTrue(inspect.isroutine([].count))
# to function
# function
self.assertTrue(inspect.isroutine(mod.spam))
self.assertTrue(inspect.isroutine(mod.StupidGit.abuse))
# to slot-wrapper
# slot-wrapper
self.assertTrue(inspect.isroutine(object.__init__))
self.assertTrue(inspect.isroutine(object.__str__))
self.assertTrue(inspect.isroutine(object.__lt__))
self.assertTrue(inspect.isroutine(int.__lt__))
# to method-wrapper
# method-wrapper
self.assertTrue(inspect.isroutine(object().__init__))
self.assertTrue(inspect.isroutine(object().__str__))
self.assertTrue(inspect.isroutine(object().__lt__))
self.assertTrue(inspect.isroutine((42).__lt__))
# to method-descriptor
# method-descriptor
self.assertTrue(inspect.isroutine(str.join))
self.assertTrue(inspect.isroutine(list.append))
self.assertTrue(inspect.isroutine(''.join))
self.assertTrue(inspect.isroutine([].append))
# to object
# object
self.assertFalse(inspect.isroutine(object))
self.assertFalse(inspect.isroutine(object()))
self.assertFalse(inspect.isroutine(str()))
# to module
# module
self.assertFalse(inspect.isroutine(mod))


Expand Down
0