PEP 702: Marking deprecations using the type system

I already stated this earlier, but Flask etc. probably won’t use this decorator since we need to deprecate other things, like arguments and attributes. We also want precise control over what triggers the warning and other behavior if deprecated stuff is used.

I wish we could find another solution to annotating deprecations that was more flexible, because I do like the general idea of type checking this. Perhaps something similar to the Annotated[type, info] construct? A real annotation has the advantage of no runtime cost at all compared to a decorator.

# The class is deprecated
class Example(typing.Deprecated): ...

# The function or method is deprecated
def example() -> Deprecated[int]: ...

# An argument is deprecated
def example(old: Deprecated[str]): ...

# An attribute is deprecated
class Example:
    old: Deprecated[str]

Perhaps it could take a second message parameter, but I’d also be fine leaving runtime warnings to the library, which can usually target them better than a general annotation/decorator could.

# Maybe an optional message?
def example() -> Deprecated[
    str,
    "The 'example' function will be removed in Library 2.0. Use 'other' instead."
]:
    ...
6 Likes