Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-104003: Implement PEP 702#104004

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
JelleZijlstra merged 25 commits intopython:mainfromJelleZijlstra:pep702
Nov 29, 2023
Merged

Conversation

JelleZijlstra
Copy link
Member

@JelleZijlstraJelleZijlstra commentedApr 30, 2023
edited by bedevere-bot
Loading

@JelleZijlstra
Copy link
MemberAuthor

This is copied frompython/typing_extensions#105.

Copy link
Member

@gvanrossumgvanrossum left a comment

Choose a reason for hiding this comment

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

LGTM.

Copy link
Member

@gvanrossumgvanrossum left a comment

Choose a reason for hiding this comment

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

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

@AlexWaygood
Copy link
Member

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

The PEP hasn't been accepted yet!

erlend-aasland reacted with thumbs up emojigvanrossum reacted with laugh emoji

@JelleZijlstra
Copy link
MemberAuthor

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

@JelleZijlstra
Copy link
MemberAuthor

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

AlexWaygood reacted with thumbs up emoji

Co-authored-by: Hugo van Kemenade <hugovk@users.noreply.github.com>
@AlexWaygoodAlexWaygood self-requested a reviewNovember 26, 2023 18:55
@Viicos
Copy link
Contributor

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 usingdeprecated 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)

AlexWaygood reacted with thumbs up emoji

@AlexWaygood
Copy link
Member

AlexWaygood commentedNov 26, 2023
edited
Loading

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 usingdeprecated 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.pyindex 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:

fromtypingimportget_originfromwarningsimportdecoratedifget_origin(obj)isdeprecated:    ...# it was a wrapper function returned by `warnings.deprecated("message")`
Zac-HD reacted with thumbs up emoji

@Viicos
Copy link
Contributor

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

@AlexWaygood
Copy link
Member

AlexWaygood commentedNov 26, 2023
edited
Loading

Doesn't seem to beget_origin's primary use

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

@Zac-HD
Copy link
Contributor

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

@JelleZijlstra
Copy link
MemberAuthor

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.

Zac-HD, AlexWaygood, and Viicos reacted with thumbs up emoji

Comment on lines 338 to 340
* The new :func:`warnings.deprecated` decorator provides a way to communicate
deprecations to :term:`static type checkers <static type checker>` and
to warn on usage of deprecated classes and functions.
Copy link
Member

Choose a reason for hiding this comment

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

Despite static typing being the original motivation for the feature, I would actually put the runtime effect first here:

Suggested change
* The new:func:`warnings.deprecated` decorator provides a way to communicate
deprecations to:term:`static type checkers <static type checker>` and
to warn on usage of deprecated classes and functions.
* The new:func:`warnings.deprecated` decorator provides an ergonomic way to
mark a class or function as deprecated. A deprecation warning will be
emitted whenever a decorated function or class is used at runtime. The
decorator is also understood by
:term:`static type checkers <static type checker>`, which will emit warnings
if they identify a decorated function or class being used.

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

I still want to put the type checker effect first because that's the unique part. You can write a decorator that generates runtime warnings yourself; the new and exciting part is that this decorator is also understood by static type checkers.

Copy link
Member

Choose a reason for hiding this comment

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

You can do, sure, butdo people? It was possible to reimplementitertools.batched in a few lines of code before Python 3.12, but lots of people were still very excited by its inclusion in the stdlib in Python 3.12.

I think this new decorator here could prove pretty popular with people who don't use static typing! But, I don't feel strongly; it looks fine to me now :)

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

Sure, there are lots of third-party deprecation decorators. TheDeprecated library, I think Flask has one internally (David Lord mentioned it in PEP 702 discussions), I know at Quora we have one internally.

Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure if that's meant to be a point in agreement or in disagreement with the point I'm making that this decorator could prove pretty popular with people who don't use static typing. Anyway, as I say, I'm happy with the docs now!

@JelleZijlstra
Copy link
MemberAuthor

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

Copy link
Member

@AlexWaygoodAlexWaygood left a comment

Choose a reason for hiding this comment

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

LGTM

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
@JelleZijlstraJelleZijlstra merged commitd4a6229 intopython:mainNov 29, 2023
@JelleZijlstraJelleZijlstra deleted the pep702 branchNovember 29, 2023 17:38
aisk pushed a commit to aisk/cpython that referenced this pull requestFeb 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 requestSep 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 freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@hugovkhugovkhugovk left review comments

@hauntsaninjahauntsaninjahauntsaninja left review comments

@AlexWaygoodAlexWaygoodAlexWaygood approved these changes

@gvanrossumgvanrossumgvanrossum approved these changes

@Fidget-SpinnerFidget-SpinnerAwaiting requested review from Fidget-Spinner

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

8 participants
@JelleZijlstra@AlexWaygood@Zac-HD@Viicos@hugovk@gvanrossum@hauntsaninja@bedevere-bot

[8]ページ先頭

©2009-2025 Movatter.jp