Skip to content
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

Copy the coroutine status in deprecated #438

Merged
merged 8 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
- Add `typing_extensions.get_annotations`, a backport of
`inspect.get_annotations` that adds features specified
by PEP 649. Patches by Jelle Zijlstra and Alex Waygood.
- Copy the coroutine status of functions and methods wrapped
with `@typing_extensions.deprecated`. Patch by Sebastian Rittau.

# Release 4.12.2 (June 7, 2024)

Expand Down
50 changes: 50 additions & 0 deletions src/test_typing_extensions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import abc
import asyncio
import collections
import collections.abc
import contextlib
Expand Down Expand Up @@ -115,9 +116,15 @@
# and adds PEP 695 to CPython's grammar
TYPING_3_12_0 = sys.version_info[:3] >= (3, 12, 0)

# @deprecated works differently in Python 3.12
TYPING_3_12_ONLY = (3, 12) <= sys.version_info < (3, 13)

# 3.13 drops support for the keyword argument syntax of TypedDict
TYPING_3_13_0 = sys.version_info[:3] >= (3, 13, 0)

# 3.13.0.rc1 fixes a problem with @deprecated
TYPING_3_13_0_RC = sys.version_info[:3] >= (3, 13, 0) and sys.version_info[:4] != (3, 13, 0, 'beta')
srittau marked this conversation as resolved.
Show resolved Hide resolved

# https://github.com/python/cpython/pull/27017 was backported into some 3.9 and 3.10
# versions, but not all
HAS_FORWARD_MODULE = "module" in inspect.signature(typing._type_check).parameters
Expand Down Expand Up @@ -850,6 +857,49 @@ def d(): pass
isinstance(cell.cell_contents, deprecated) for cell in d.__closure__
))

@deprecated("depr")
def func():
pass

@deprecated("depr")
async def coro():
pass

class Cls:
@deprecated("depr")
def func(self):
pass

@deprecated("depr")
async def coro(self):
pass

class DeprecatedCoroTests(BaseTestCase):
@skipUnless(TYPING_3_12_ONLY or TYPING_3_13_0_RC, "inspect.iscoroutinefunction works differently on Python < 3.12")
def test_inspect_py313(self):
self.assertFalse(inspect.iscoroutinefunction(func))
self.assertFalse(inspect.iscoroutinefunction(Cls.func))
self.assertTrue(inspect.iscoroutinefunction(coro))
self.assertTrue(inspect.iscoroutinefunction(Cls.coro))

self.assertFalse(asyncio.coroutines.iscoroutinefunction(func))
self.assertFalse(asyncio.coroutines.iscoroutinefunction(Cls.func))
self.assertTrue(asyncio.coroutines.iscoroutinefunction(coro))
self.assertTrue(asyncio.coroutines.iscoroutinefunction(Cls.coro))

@skipIf(TYPING_3_12_0, "inspect.iscoroutinefunction works differently on Python 3.12+")
def test_inspect_py311(self):
# This doesn't work in Python < 3.12.
# self.assertTrue(inspect.iscoroutinefunction(func))
# self.assertTrue(inspect.iscoroutinefunction(Cls.func))
self.assertFalse(inspect.iscoroutinefunction(coro))
self.assertFalse(inspect.iscoroutinefunction(Cls.coro))

self.assertFalse(asyncio.coroutines.iscoroutinefunction(func))
self.assertFalse(asyncio.coroutines.iscoroutinefunction(Cls.func))
self.assertTrue(asyncio.coroutines.iscoroutinefunction(coro))
self.assertTrue(asyncio.coroutines.iscoroutinefunction(Cls.coro))
srittau marked this conversation as resolved.
Show resolved Hide resolved


class AnyTests(BaseTestCase):
def test_can_subclass(self):
Expand Down
8 changes: 8 additions & 0 deletions src/typing_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2898,13 +2898,21 @@ def __init_subclass__(*args, **kwargs):
__init_subclass__.__deprecated__ = msg
return arg
elif callable(arg):
import asyncio.coroutines
import functools
import inspect

@functools.wraps(arg)
def wrapper(*args, **kwargs):
warnings.warn(msg, category=category, stacklevel=stacklevel + 1)
return arg(*args, **kwargs)

if asyncio.coroutines.iscoroutinefunction(arg):
if sys.version_info >= (3, 12):
wrapper = inspect.markcoroutinefunction(wrapper)
else:
wrapper._is_coroutine = asyncio.coroutines._is_coroutine
Comment on lines +2911 to +2914
Copy link
Member

Choose a reason for hiding this comment

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

It seems like the _is_coroutine thing also exists on the CPython main branch. I think it would be nice for @deprecated to work consistently across Python versions as much as possible, so maybe we could just add the _is_coroutine marker unconditionally, even though it isn't really necessary on py312+:

Suggested change
if sys.version_info >= (3, 12):
wrapper = inspect.markcoroutinefunction(wrapper)
else:
wrapper._is_coroutine = asyncio.coroutines._is_coroutine
wrapper._is_coroutine = asyncio.coroutines._is_coroutine
if sys.version_info >= (3, 12):
wrapper = inspect.markcoroutinefunction(wrapper)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'd be wary about this. Why access a private item when not necessary? Especially since it gets overwritten immediately after?

Copy link
Member

Choose a reason for hiding this comment

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

Fair enough, we can leave this.

Copy link
Member

@AlexWaygood AlexWaygood Jul 23, 2024

Choose a reason for hiding this comment

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

Ah, well, it would fix the asyncio.coroutines.iscoroutinefunction() test that's currently failing on Python 3.13.0b4. But I suppose that test could easily just be skipped only if we're on Python >=3.13 and <3.13.0rc1


arg.__deprecated__ = wrapper.__deprecated__ = msg
return wrapper
else:
Expand Down
Loading