Skip to content

typing_extensions.deprecated and warnings.deprecated remove coroutine property; How to deprecate async functions? #122088

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

Closed
Kakadus opened this issue Jul 18, 2024 · 5 comments
Assignees

Comments

@Kakadus
Copy link

Kakadus commented Jul 18, 2024

When I annotate an async function with @deprecated(...), inspect.iscoroutinefunction no longer returns True. This is not what I expect to happen, as with partial from functools this does not happen.

Consider the following code:

import inspect

from functools import partial
from typing_extensions import deprecated


async def coroutinefunction_a():
    pass


@deprecated("deprecated coroutine")
async def coroutinefunction_b():
    pass


assert inspect.iscoroutinefunction(coroutinefunction_a) is True  # correct
assert inspect.iscoroutinefunction(partial(coroutinefunction_a)) is True  # correct
assert inspect.iscoroutinefunction(coroutinefunction_b) is True  # fail

With typing_extensions and python3.13, the last assertion fails.

This is especially annoying since e.g. libraries use inspect.iscoroutinefunction to detect the type of function.

I am not sure this is the correct place for this bug report, as this both happens with python 3.13 and typing_extensions on older python versions.

Linked PRs

@srittau
Copy link
Contributor

srittau commented Jul 18, 2024

inspect.iscoroutinefunction() has special handling for partial(). The ultimate culprit here is @functools.wraps, which @deprecated uses:

import inspect

from functools import wraps


async def coroutinefunction_a():
    pass

def wrapper(f):
    @wraps(f)
    def x():
        return f()

    return x

@wrapper
async def coroutinefunction_b():
    pass


assert inspect.iscoroutinefunction(coroutinefunction_a) is True  # correct
assert inspect.iscoroutinefunction(coroutinefunction_b) is True  # fail

@wraps (or rather update_wrapper) should probably copy the coroutine marker (_is_coroutine_marker) that iscoroutinefunction checks for.

@AlexWaygood
Copy link
Member

@wraps (or rather update_wrapper) should probably copy the coroutine marker (_is_coroutine_marker) that iscoroutinefunction checks for.

Previously discussed in:

@srittau
Copy link
Contributor

srittau commented Jul 18, 2024

In that case, I definitely see the point of copying the flag in @deprecated. (Maybe update_wrapper/@wraps should get a flag to copy it, but that's off-topic for this issue?)

@AlexWaygood
Copy link
Member

In that case, I definitely see the point of copying the flag in @deprecated. (Maybe update_wrapper/@wraps should get a flag to copy it, but that's off-topic for this issue?)

Yeah, that sounds good to me. Even if the flag is added to update_wrapper/@wraps, it won't be added to the stdlib before Python 3.14, so we'll need an intermediate solution here.

@srittau srittau self-assigned this Jul 21, 2024
@srittau
Copy link
Contributor

srittau commented Jul 21, 2024

@AlexWaygood @JelleZijlstra Could one of you move this issue over to the cpython repository? This should probably be fixed in CPython first.

@JelleZijlstra JelleZijlstra transferred this issue from python/typing_extensions Jul 21, 2024
AlexWaygood pushed a commit that referenced this issue Jul 23, 2024
…warnings.deprecated` (#122086)

Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jul 23, 2024
… in `@warnings.deprecated` (pythonGH-122086)

(cherry picked from commit 375c9f6)

Co-authored-by: Sebastian Rittau <srittau@rittau.biz>
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
AlexWaygood pushed a commit that referenced this issue Jul 23, 2024
…e in `@warnings.deprecated` (GH-122086) (#122156)

Co-authored-by: Sebastian Rittau <srittau@rittau.biz>
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
nohlson pushed a commit to nohlson/cpython that referenced this issue Jul 24, 2024
… in `@warnings.deprecated` (python#122086)

Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
nohlson pushed a commit to nohlson/cpython that referenced this issue Jul 24, 2024
… in `@warnings.deprecated` (python#122086)

Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants