◐ Shell
reader mode source ↗
Skip to content

gh-104003: Implement PEP 702#104004

Merged
JelleZijlstra merged 25 commits into
python:mainfrom
JelleZijlstra:pep702
Nov 29, 2023
Merged

gh-104003: Implement PEP 702#104004
JelleZijlstra merged 25 commits into
python:mainfrom
JelleZijlstra:pep702

Conversation

@JelleZijlstra

@JelleZijlstra JelleZijlstra commented Apr 30, 2023

Copy link
Copy Markdown
Member

@JelleZijlstra

Copy link
Copy Markdown
Member Author

This is copied from python/typing_extensions#105.

@gvanrossum gvanrossum left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hide comment

LGTM.

@gvanrossum gvanrossum left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hide comment

Waiting for the other reviewers to pipe in? This seems pretty straightforward.

@AlexWaygood

Copy link
Copy Markdown
Member

Waiting for the other reviewers to pipe in? This seems pretty straightforward.

The PEP hasn't been accepted yet!

@JelleZijlstra

Copy link
Copy Markdown
Member Author

Planning to hit the merge button the moment the PEP is accepted :)

@JelleZijlstra

Copy link
Copy Markdown
Member Author

PEP 702 has been accepted, but it's changed since this PR. I'll update the PR soon to put the decorator in warnings and sync the implementation from typing-extensions.

Co-authored-by: Hugo van Kemenade <hugovk@users.noreply.github.com>
24 hidden items Load more…
@AlexWaygood

AlexWaygood commented Nov 26, 2023

Copy link
Copy Markdown
Member

The purpose of that is supposed to be to allow third-party runtime tools to detect that the original object was decorated with @deprecated. Is that insufficient?

The issue is when using deprecated the way Zac-HD wrote (Annotated[T, warnings.deprecated("message")]), no object is being decorated here (in other words, deprecated("message") returns another function yet to be called with the decorated object)

Got it. What if we did something like this?

diff --git a/Lib/warnings.py b/Lib/warnings.py
index 36da0e75c6..fc8a10e71f 100644
--- a/Lib/warnings.py
+++ b/Lib/warnings.py
@@ -621,6 +621,7 @@ def wrapper(*args, **kwargs):
                 f"a class or callable, not {arg!r}"
             )

+    decorator.__origin__ = deprecated
     return decorator

Then third-party code could do checks like this:

from typing import get_origin
from warnings import decorated

if get_origin(obj) is deprecated:
    ...  # it was a wrapper function returned by `warnings.deprecated("message")`

@Viicos

Viicos commented Nov 26, 2023

Copy link
Copy Markdown
Contributor

Doesn't seem to be get_origin's primary use (maybe docs should be updated), but that would avoid completely refactoring the current implementation!

@AlexWaygood

AlexWaygood commented Nov 26, 2023

Copy link
Copy Markdown
Member

Doesn't seem to be get_origin's primary use

It's already sorta awkwardly overloaded in what it means tbh — the __origin__ attribute of ParamSpecArgs instances doesn't really have the same semantics as the __origin__ attribute of generic aliases :)

@Zac-HD

Zac-HD commented Nov 26, 2023

Copy link
Copy Markdown
Contributor

A class seems somewhat more elegant to me, but yeah, that would work.

@JelleZijlstra

Copy link
Copy Markdown
Member Author

I'm OK with making it a class. It's not what the object is meant for, but it's a reasonable extension.

Using __origin__ feels hacky and wouldn't provide an easy way to get the deprecation message out.

@JelleZijlstra

Copy link
Copy Markdown
Member Author

Thanks @AlexWaygood for the review! I pushed some changes.

@AlexWaygood AlexWaygood left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hide comment

LGTM

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
@JelleZijlstra JelleZijlstra merged commit d4a6229 into python:main Nov 29, 2023
@JelleZijlstra JelleZijlstra deleted the pep702 branch November 29, 2023 17:38
aisk pushed a commit to aisk/cpython that referenced this pull request Feb 11, 2024
Co-authored-by: Hugo van Kemenade <hugovk@users.noreply.github.com>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Glyphack pushed a commit to Glyphack/cpython that referenced this pull request Sep 2, 2024
Co-authored-by: Hugo van Kemenade <hugovk@users.noreply.github.com>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.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

Successfully merging this pull request may close these issues.

8 participants