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

Conversation

@mjpieters

@mjpieters mjpieters commented Oct 5, 2019 •
edited by bedevere-bot
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor

The inspect.isgeneratorfunction, inspect.iscoroutinefunction and inspect.isasyncgenfunction already unwrap functools.partial objects, this patch adds support for partialmethod objects as well.

https://bugs.python.org/issue38364

Sorry, something went wrong.

Uh oh!

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

florimondmanca, jmurrayufo, jalaziz, nzig, and lukany reacted with thumbs up emoji
All reactions
  • 👍 5 reactions
@mjpieters
mjpieters requested a review from rhettinger as a code owner October 5, 2019 22:51
@the-knights-who-say-ni the-knights-who-say-ni added the CLA signed label Oct 5, 2019
@bedevere-bot bedevere-bot added the awaiting review label Oct 5, 2019
@mjpieters
mjpieters force-pushed the bpo38364_partialmethod_inspect branch from 3e49547 to 4fecb83 Compare October 5, 2019 23:08
taleinat
taleinat reviewed Nov 13, 2019
View reviewed changes
Comment thread Lib/inspect.py Resolved

Uh oh!

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

taleinat
taleinat reviewed Nov 13, 2019
View reviewed changes
Comment thread Lib/test/test_inspect.py Resolved

Uh oh!

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

@florimondmanca

florimondmanca commented Dec 21, 2019 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown

Coincidently, this would also resolve another situation, i.e. when passing the partial of a method directly…

import functools, inspect

class Dog:
    async def abark(self): pass

inspect.iscoroutinefunction(dog.abark)  # True
inspect.iscoroutinefunction(functools.partial(dog.abark))  # False (should probably be True?)

This would be because the partial would be unwrapped (via the inner call to _unwrap_partial() here), yielding a method object that will be correctly handled by the method unwrapping.

All reactions

Sorry, something went wrong.

Uh oh!

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

@florimondmanca florimondmanca mentioned this pull request Dec 21, 2019
Rename 'next' to 'anext' on Response encode/httpx#676
Merged
@thedrow thedrow mentioned this pull request Apr 6, 2020
Using partial functions in async stories incorrectly raises a StoryDefinitionError proofit404/stories#348
Closed
@csabella
csabella requested review from 1st1 and encukou May 28, 2020 23:09
@JayH5 JayH5 mentioned this pull request Feb 2, 2021
Asynchronous classmethod handlers not recognized as coroutines if wrapped in functools.partial Kludex/starlette#1107
Closed
@jalaziz

jalaziz commented Mar 5, 2022 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown

This is also an issue for unittest mocking. patch will return MagicMock for partial async methods and not AsyncMock. In our case, the underlying code is using calling partial against a method, but as @florimondmanca pointed out, this PR should fix it.

All reactions

Sorry, something went wrong.

Uh oh!

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

@rhettinger
rhettinger removed their request for review May 3, 2022 06:34
@ezio-melotti ezio-melotti removed the CLA signed label Jul 13, 2022
@mjpieters mjpieters mannequin mentioned this pull request Jul 16, 2022
inspect.iscoroutinefunction / isgeneratorfunction / isasyncgenfunction can't handle partialmethod objects #82545
Open
kumaraditya303
kumaraditya303 requested changes Dec 30, 2022
View reviewed changes

@kumaraditya303 kumaraditya303 left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are merge conflicts.

Sorry, something went wrong.

Uh oh!

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

All reactions
@bedevere-bot bedevere-bot added awaiting changes and removed awaiting review labels Dec 30, 2022
@bedevere-bot

bedevere-bot commented Dec 30, 2022

Copy link
Copy Markdown

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

All reactions

Sorry, something went wrong.

Uh oh!

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

@mjpieters
mjpieters force-pushed the bpo38364_partialmethod_inspect branch from 03ca24c to 84dd702 Compare December 30, 2022 14:14
@mjpieters

mjpieters commented Dec 30, 2022

Copy link
Copy Markdown
Contributor Author

I have made the requested changes; please review again.

All reactions

Sorry, something went wrong.

Uh oh!

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

@bedevere-bot bedevere-bot added awaiting change review and removed awaiting changes labels Dec 30, 2022
@bedevere-bot

bedevere-bot commented Dec 30, 2022

Copy link
Copy Markdown

Thanks for making the requested changes!

@kumaraditya303: please review the changes made to this pull request.

All reactions

Sorry, something went wrong.

Uh oh!

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

@bedevere-bot
bedevere-bot requested a review from kumaraditya303 December 30, 2022 14:39
@mjpieters
mjpieters force-pushed the bpo38364_partialmethod_inspect branch from 84dd702 to 35b8c19 Compare December 30, 2022 14:45
kumaraditya303
kumaraditya303 previously requested changes Dec 31, 2022
View reviewed changes
Comment thread Doc/library/inspect.rst Resolved

Uh oh!

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

Comment thread Doc/library/inspect.rst Resolved

Uh oh!

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

Comment thread Lib/functools.py Outdated
prev = None
while func is not prev:
prev = func
while isinstance(getattr(func, "_partialmethod", None), partialmethod):

@kumaraditya303 kumaraditya303 Dec 31, 2022

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why getattr? What objects have _partialmethod?

Sorry, something went wrong.

Uh oh!

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

All reactions

@mjpieters mjpieters Dec 31, 2022

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

partialmethod(...).__get__() can return a closure with _partialmethod pointing to the instance. This happens if the wrapped callable is not a descriptor, or if the descriptor returned itself again.

See the partialmethod._make_unbound_method() implementation.

getattr() is used because the attribute is optional, and you'd not want this to break even if the supplied object does have a _partialmethod attribute that is not actually a partialmethod instance.

Sorry, something went wrong.

Uh oh!

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

All reactions
@bedevere-bot bedevere-bot added awaiting changes and removed awaiting change review labels Dec 31, 2022
@mjpieters

mjpieters commented Dec 31, 2022

Copy link
Copy Markdown
Contributor Author

I have made the requested changes; please review again.

All reactions

Sorry, something went wrong.

Uh oh!

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

@bedevere-bot bedevere-bot added awaiting change review and removed awaiting changes labels Dec 31, 2022
@bedevere-bot

bedevere-bot commented Dec 31, 2022

Copy link
Copy Markdown

Thanks for making the requested changes!

@kumaraditya303: please review the changes made to this pull request.

All reactions

Sorry, something went wrong.

Uh oh!

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

@bedevere-bot
bedevere-bot requested a review from kumaraditya303 December 31, 2022 11:34
mjpieters added 2 commits December 31, 2022 11:35
@mjpieters
bpo-38364: unwrap partialmethods just like we unwrap partials
9cb267a
The inspect.isgeneratorfunction, inspect.iscoroutinefunction and inspect.isasyncgenfunction already unwrap functools.partial objects, this patch adds support for partialmethod objects as well.
@mjpieters
📜🤖 Added by blurb_it.
243cb00
@mjpieters
mjpieters force-pushed the bpo38364_partialmethod_inspect branch from 2fb40c2 to 243cb00 Compare December 31, 2022 11:35
@kumaraditya303
kumaraditya303 removed their request for review May 30, 2023 12:31
encukou added 3 commits January 25, 2024 14:51
@encukou
Merge in the main branch
2a9c535
@encukou
Update versionchanged directives
d20e069
@encukou
Rename _partialmethod to __partialmethod__
fa36941
Since we'rs checking this attribute on arbitrary function-like objects,
we should use the namespace reserved for core Python.
@encukou

encukou commented Jan 25, 2024

Copy link
Copy Markdown
Member

Hi! Just getting to this, sorry for the delay.
Since we're accessing _partialmethod on arbitrary objects, it would be best to rename it to __partialmethod__ -- make it part of the namespace reserved for Python.

I hope you don't mind me pushing directly to the PR.

All reactions

Sorry, something went wrong.

Uh oh!

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

encukou
encukou approved these changes Jan 29, 2024
View reviewed changes
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting change review labels Jan 29, 2024
@encukou
encukou dismissed kumaraditya303’s stale review February 1, 2024 12:53

All concerns from the review should be resolved now.

@encukou
encukou merged commit edb59d5 into python:main Feb 15, 2024
@bedevere-app bedevere-app Bot removed the awaiting merge label Feb 15, 2024
@mjpieters
mjpieters deleted the bpo38364_partialmethod_inspect branch February 20, 2024 12:57
rominf added a commit to rominf/sentry-python that referenced this pull request Jul 10, 2024
@rominf
fix(utils): Handle partialmethod in qualname_from_function (CPyth…
a193ad4
…on 3.13)

The `_partialmethod` attribute of methods wrapped with `partialmethod()`
was renamed to `__partialmethod__` in CPython 3.13:
python/cpython#16600
@rominf rominf mentioned this pull request Jul 10, 2024
fix(utils): Handle partialmethod in qualname_from_function (CPython 3.13) getsentry/sentry-python#3272
Merged
rominf added a commit to rominf/sentry-python that referenced this pull request Jul 10, 2024
@rominf
fix(utils): Handle partialmethod in qualname_from_function (CPyth…
6f24f61
…on 3.13)

The `_partialmethod` attribute of methods wrapped with `partialmethod()`
was renamed to `__partialmethod__` in CPython 3.13:
python/cpython#16600
sentrivana pushed a commit to getsentry/sentry-python that referenced this pull request Jul 16, 2024
@rominf
fix(utils): Handle partialmethod in qualname_from_function (CPyth…
b9d1e3e
…on 3.13) (#3272)

The `_partialmethod` attribute of methods wrapped with `partialmethod()`
was renamed to `__partialmethod__` in CPython 3.13:
python/cpython#16600
sentrivana added a commit to getsentry/sentry-python that referenced this pull request Jul 16, 2024
@sentrivana @rominf
feat: Preliminary support for Python 3.13 (#3200)
a98f660
Adding preliminary support for Python 3.13.

The `_partialmethod` attribute of methods wrapped with `partialmethod()`
was renamed to `__partialmethod__` in CPython 3.13:
python/cpython#16600

Starting from Python 3.13, `frame.f_locals` is not `dict` anymore, but
`FrameLocalsProxy`, that cannot be copied using `copy.copy()`. In Python
3.13 and later, it should be copied using a method `.copy()`. The new way
of copying works the same as the old one for versions of Python prior to
3.13, according to the documentation (both copying methods produce a
shallow copy).

Since Python 3.13, `FrameLocalsProxy` skips items of `locals()` that have
non-`str` keys; this is a CPython implementation detail, so we hence
disable `test_non_string_variables` test on Python 3.13.

See:
https://peps.python.org/pep-0667/
python/cpython#118921
python/cpython#118923
https://docs.python.org/3.13/whatsnew/3.13.html#porting-to-python-3-13
https://docs.python.org/3/library/copy.html
https://github.com/python/cpython/blame/7b413952e817ae87bfda2ac85dd84d30a6ce743b/Objects/frameobject.c#L148

---------

Co-authored-by: Roman Inflianskas <[email protected]>
arjennienhuis pushed a commit to arjennienhuis/sentry-python that referenced this pull request Sep 30, 2024
@sentrivana @rominf @arjenzorgdoc
feat: Preliminary support for Python 3.13 (getsentry#3200)
869e366
Adding preliminary support for Python 3.13.

The `_partialmethod` attribute of methods wrapped with `partialmethod()`
was renamed to `__partialmethod__` in CPython 3.13:
python/cpython#16600

Starting from Python 3.13, `frame.f_locals` is not `dict` anymore, but
`FrameLocalsProxy`, that cannot be copied using `copy.copy()`. In Python
3.13 and later, it should be copied using a method `.copy()`. The new way
of copying works the same as the old one for versions of Python prior to
3.13, according to the documentation (both copying methods produce a
shallow copy).

Since Python 3.13, `FrameLocalsProxy` skips items of `locals()` that have
non-`str` keys; this is a CPython implementation detail, so we hence
disable `test_non_string_variables` test on Python 3.13.

See:
https://peps.python.org/pep-0667/
python/cpython#118921
python/cpython#118923
https://docs.python.org/3.13/whatsnew/3.13.html#porting-to-python-3-13
https://docs.python.org/3/library/copy.html
https://github.com/python/cpython/blame/7b413952e817ae87bfda2ac85dd84d30a6ce743b/Objects/frameobject.c#L148

---------

Co-authored-by: Roman Inflianskas <[email protected]>
turicas added a commit to turicas/sentry-python that referenced this pull request Jun 6, 2025
@turicas
feat: Preliminary support for Python 3.13 (getsentry#3200)
298a192
Partial cherry-pick of: a98f660

Adding preliminary support for Python 3.13.

The `_partialmethod` attribute of methods wrapped with `partialmethod()`
was renamed to `__partialmethod__` in CPython 3.13:
python/cpython#16600

Starting from Python 3.13, `frame.f_locals` is not `dict` anymore, but
`FrameLocalsProxy`, that cannot be copied using `copy.copy()`. In Python
3.13 and later, it should be copied using a method `.copy()`. The new way
of copying works the same as the old one for versions of Python prior to
3.13, according to the documentation (both copying methods produce a
shallow copy).

Since Python 3.13, `FrameLocalsProxy` skips items of `locals()` that have
non-`str` keys; this is a CPython implementation detail, so we hence
disable `test_non_string_variables` test on Python 3.13.

See:
https://peps.python.org/pep-0667/
python/cpython#118921
python/cpython#118923
https://docs.python.org/3.13/whatsnew/3.13.html#porting-to-python-3-13
https://docs.python.org/3/library/copy.html
https://github.com/python/cpython/blame/7b413952e817ae87bfda2ac85dd84d30a6ce743b/Objects/frameobject.c#L148
turicas added a commit to turicas/sentry-python that referenced this pull request Jun 6, 2025
@turicas
feat: Preliminary support for Python 3.13 (getsentry#3200)
3b4a05f
Partial cherry-pick of: a98f660

Adding preliminary support for Python 3.13.

The `_partialmethod` attribute of methods wrapped with `partialmethod()`
was renamed to `__partialmethod__` in CPython 3.13:
python/cpython#16600

Starting from Python 3.13, `frame.f_locals` is not `dict` anymore, but
`FrameLocalsProxy`, that cannot be copied using `copy.copy()`. In Python
3.13 and later, it should be copied using a method `.copy()`. The new way
of copying works the same as the old one for versions of Python prior to
3.13, according to the documentation (both copying methods produce a
shallow copy).

Since Python 3.13, `FrameLocalsProxy` skips items of `locals()` that have
non-`str` keys; this is a CPython implementation detail, so we hence
disable `test_non_string_variables` test on Python 3.13.

See:
https://peps.python.org/pep-0667/
python/cpython#118921
python/cpython#118923
https://docs.python.org/3.13/whatsnew/3.13.html#porting-to-python-3-13
https://docs.python.org/3/library/copy.html
https://github.com/python/cpython/blame/7b413952e817ae87bfda2ac85dd84d30a6ce743b/Objects/frameobject.c#L148
turicas added a commit to turicas/sentry-python that referenced this pull request Jun 6, 2025
@turicas
feat: Preliminary support for Python 3.13 (getsentry#3200)
09787da
Partial cherry-pick of: a98f660

Adding preliminary support for Python 3.13.

The `_partialmethod` attribute of methods wrapped with `partialmethod()`
was renamed to `__partialmethod__` in CPython 3.13:
python/cpython#16600

Starting from Python 3.13, `frame.f_locals` is not `dict` anymore, but
`FrameLocalsProxy`, that cannot be copied using `copy.copy()`. In Python
3.13 and later, it should be copied using a method `.copy()`. The new way
of copying works the same as the old one for versions of Python prior to
3.13, according to the documentation (both copying methods produce a
shallow copy).

Since Python 3.13, `FrameLocalsProxy` skips items of `locals()` that have
non-`str` keys; this is a CPython implementation detail, so we hence
disable `test_non_string_variables` test on Python 3.13.

See:
https://peps.python.org/pep-0667/
python/cpython#118921
python/cpython#118923
https://docs.python.org/3.13/whatsnew/3.13.html#porting-to-python-3-13
https://docs.python.org/3/library/copy.html
https://github.com/python/cpython/blame/7b413952e817ae87bfda2ac85dd84d30a6ce743b/Objects/frameobject.c#L148
@x42005e1f x42005e1f mentioned this pull request Dec 8, 2025
inspect.iscoroutinefunction() does not detect marked partial objects #142418
Open
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@encukou encukou encukou approved these changes
@kumaraditya303 kumaraditya303 kumaraditya303 left review comments
@1st1 1st1 Awaiting requested review from 1st1
+1 more reviewer
@taleinat taleinat taleinat left review comments
Reviewers whose approvals may not affect merge requirements

Uh oh!

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

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

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

9 participants

@mjpieters @florimondmanca @jalaziz @bedevere-bot @encukou @taleinat @kumaraditya303 @the-knights-who-say-ni @ezio-melotti
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.