Skip to content

How to extend warnings.deprecated? #2256

Description

@flying-sheep

We need more metadata for our deprecation decorator, but the spec doesn’t say how deprecated marks an API. So the only way to mark something as deprecated it is by creating an API with the same exact signature and replace it with warnings.deprecated at the type level:

if TYPE_CHECKING:
    from warnings import deprecated
else:
    def deprecated[F](msg: str) -> Callable[[F], F]: ...

We need to define deprecated(msg: str, version: packaging.Version) and have it work with the type system. How?

Activity

  1. flying-sheep commented on Apr 15, 2026

    @flying-sheep
    Author

    Another issue: there seems to be no reason why it takes LiteralString only, so that should be changed to str in the stubs.

  2. srittau commented on Apr 15, 2026

    @srittau
    Collaborator

    Another issue: there seems to be no reason why it takes LiteralString only, so that should be changed to str in the stubs.

    LiteralString is used intentionally, see python/typeshed#11699.

  3. flying-sheep commented on Apr 15, 2026

    @flying-sheep
    Author

    This goes back to the original issue then:

    How do I keep the property of seeing the deprecation (strike-through function call) and its message, but can also add e.g. a mandatory version parameter and extra behavior to the decorator?

    The following hack is very unpythonic and easy to get wrong:

    @deprecated("some message")
    @deprecated_at("3.5")
    def f(...): ...
  4. flying-sheep commented on May 19, 2026

    @flying-sheep
    Author

    So deprecated is a class: python/cpython#150076

    I want to add keyword arguments. So how about we say this?

    in subclasses, msg should stay a position-only argument at position 0 so type checkers can display it

    cc @JelleZijlstra

  5. JelleZijlstra commented on May 19, 2026

    @JelleZijlstra
    Member

    Subclasses of deprecated have no special meaning in the type system.

  6. flying-sheep commented on May 19, 2026

    @flying-sheep
    Author

    let’s change that then

  7. flying-sheep commented on Jun 17, 2026

    @flying-sheep
    Author

    @JelleZijlstra what’s necessary to change that? A whole PEP?

  8. JelleZijlstra commented on Jun 18, 2026

    @JelleZijlstra
    Member

    At a minimum we should have a discussion on discuss.python.org. A small extension like the ones you suggested above shouldn't need a PEP.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions