GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — bpo-38364: unwrap partialmethods just like we unwrap partials by mjpieters · Pull Request #16600 · python/cpython

bpo-38364: unwrap partialmethods just like we unwrap partials #16600

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.

Sign up for GitHub

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
encukou merged 5 commits into python:main from mjpieters:bpo38364_partialmethod_inspect
Feb 15, 2024

Uh oh!

There was an error while loading. Please reload this page.

Merged

bpo-38364: unwrap partialmethods just like we unwrap partials #16600

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
5 commits
Select commit Hold shift + click to select a range
9cb267a
bpo-38364: unwrap partialmethods just like we unwrap partials
mjpieters Oct 5, 2019
243cb00
📜🤖 Added by blurb_it.
mjpieters Oct 5, 2019
2a9c535
Merge in the main branch
encukou Jan 25, 2024
d20e069
Update versionchanged directives
encukou Jan 25, 2024
fa36941
Rename _partialmethod to __partialmethod__
encukou Jan 25, 2024
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

10 changes: 10 additions & 0 deletions 10 Doc/library/inspect.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,9 @@ attributes (see :ref:`import-mod-attrs` for module attributes):
Functions wrapped in :func:`functools.partial` now return ``True`` if the
wrapped function is a Python generator function.

.. versionchanged:: 3.13
Functions wrapped in :func:`functools.partialmethod` now return ``True``
if the wrapped function is a Python generator function.

.. function:: isgenerator(object)

Expand All @@ -363,6 +366,10 @@ attributes (see :ref:`import-mod-attrs` for module attributes):
Sync functions marked with :func:`markcoroutinefunction` now return
``True``.

.. versionchanged:: 3.13
Functions wrapped in :func:`functools.partialmethod` now return ``True``
if the wrapped function is a :term:`coroutine function`.


.. function:: markcoroutinefunction(func)

Expand Down Expand Up @@ -429,6 +436,9 @@ attributes (see :ref:`import-mod-attrs` for module attributes):
Functions wrapped in :func:`functools.partial` now return ``True`` if the
wrapped function is a :term:`asynchronous generator` function.

.. versionchanged:: 3.13
Functions wrapped in :func:`functools.partialmethod` now return ``True``
if the wrapped function is a :term:`coroutine function`.

.. function:: isasyncgen(object)

Expand Down
13 changes: 12 additions & 1 deletion 13 Lib/functools.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
_method.__isabstractmethod__ = self.__isabstractmethod__
_method._partialmethod = self
_method.__partialmethod__ = self
return _method

def __get__(self, obj, cls=None):
Expand Down Expand Up @@ -424,6 +424,17 @@ def _unwrap_partial(func):
func = func.func
return func

def _unwrap_partialmethod(func):
prev = None
while func is not prev:
prev = func
while isinstance(getattr(func, "__partialmethod__", None), partialmethod):
func = func.__partialmethod__
while isinstance(func, partialmethod):
func = getattr(func, 'func')
func = _unwrap_partial(func)
return func

################################################################################
### LRU Cache function decorator
################################################################################
Expand Down
6 changes: 4 additions & 2 deletions 6 Lib/inspect.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -383,8 +383,10 @@ def isfunction(object):

def _has_code_flag(f, flag):
"""Return true if ``f`` is a function (or a method or functools.partial
wrapper wrapping a function) whose code object has the given ``flag``
wrapper wrapping a function or a functools.partialmethod wrapping a
function) whose code object has the given ``flag``
Comment thread
mjpieters marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.

set in its flags."""
f = functools._unwrap_partialmethod(f)
while ismethod(f):
f = f.__func__
f = functools._unwrap_partial(f)
Expand Down Expand Up @@ -2561,7 +2563,7 @@ def _signature_from_callable(obj, *,
return sig

try:
partialmethod = obj._partialmethod
partialmethod = obj.__partialmethod__
except AttributeError:
pass
else:
Expand Down
31 changes: 30 additions & 1 deletion 31 Lib/test/test_inspect/test_inspect.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,33 @@ def test_iscoroutine(self):
gen_coro = gen_coroutine_function_example(1)
coro = coroutine_function_example(1)

class PMClass:
async_generator_partialmethod_example = functools.partialmethod(
async_generator_function_example)
coroutine_partialmethod_example = functools.partialmethod(
coroutine_function_example)
gen_coroutine_partialmethod_example = functools.partialmethod(
gen_coroutine_function_example)

# partialmethods on the class, bound to an instance
pm_instance = PMClass()
async_gen_coro_pmi = pm_instance.async_generator_partialmethod_example
gen_coro_pmi = pm_instance.gen_coroutine_partialmethod_example
coro_pmi = pm_instance.coroutine_partialmethod_example

# partialmethods on the class, unbound but accessed via the class
async_gen_coro_pmc = PMClass.async_generator_partialmethod_example
gen_coro_pmc = PMClass.gen_coroutine_partialmethod_example
coro_pmc = PMClass.coroutine_partialmethod_example

self.assertFalse(
inspect.iscoroutinefunction(gen_coroutine_function_example))
self.assertFalse(
inspect.iscoroutinefunction(
functools.partial(functools.partial(
gen_coroutine_function_example))))
self.assertFalse(inspect.iscoroutinefunction(gen_coro_pmi))
self.assertFalse(inspect.iscoroutinefunction(gen_coro_pmc))
self.assertFalse(inspect.iscoroutine(gen_coro))

self.assertTrue(
Expand All @@ -220,6 +241,8 @@ def test_iscoroutine(self):
inspect.isgeneratorfunction(
functools.partial(functools.partial(
gen_coroutine_function_example))))
self.assertTrue(inspect.isgeneratorfunction(gen_coro_pmi))
self.assertTrue(inspect.isgeneratorfunction(gen_coro_pmc))
self.assertTrue(inspect.isgenerator(gen_coro))

async def _fn3():
Expand Down Expand Up @@ -285,6 +308,8 @@ def do_something_static():
inspect.iscoroutinefunction(
functools.partial(functools.partial(
coroutine_function_example))))
self.assertTrue(inspect.iscoroutinefunction(coro_pmi))
self.assertTrue(inspect.iscoroutinefunction(coro_pmc))
self.assertTrue(inspect.iscoroutine(coro))

self.assertFalse(
Expand All @@ -297,6 +322,8 @@ def do_something_static():
inspect.isgeneratorfunction(
functools.partial(functools.partial(
coroutine_function_example))))
self.assertFalse(inspect.isgeneratorfunction(coro_pmi))
self.assertFalse(inspect.isgeneratorfunction(coro_pmc))
self.assertFalse(inspect.isgenerator(coro))

self.assertFalse(
Expand All @@ -311,6 +338,8 @@ def do_something_static():
inspect.isasyncgenfunction(
functools.partial(functools.partial(
async_generator_function_example))))
self.assertTrue(inspect.isasyncgenfunction(async_gen_coro_pmi))
self.assertTrue(inspect.isasyncgenfunction(async_gen_coro_pmc))
self.assertTrue(inspect.isasyncgen(async_gen_coro))

coro.close(); gen_coro.close(); # silence warnings
Expand Down Expand Up @@ -3389,7 +3418,7 @@ def test(self: 'anno', x):

def test_signature_on_fake_partialmethod(self):
def foo(a): pass
foo._partialmethod = 'spam'
foo.__partialmethod__ = 'spam'
self.assertEqual(str(inspect.signature(foo)), '(a)')

def test_signature_on_decorated(self):
Expand Down
1 change: 1 addition & 0 deletions 1 Misc/NEWS.d/next/Library/2019-10-05-22-56-50.bpo-38364.sYTCWF.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The ``inspect`` functions ``isgeneratorfunction``, ``iscoroutinefunction``, ``isasyncgenfunction`` now support ``functools.partialmethod`` wrapped functions the same way they support ``functools.partial``.
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.