Skip to content

Conversation

s3bw
Copy link
Owner

@s3bw s3bw commented Feb 1, 2020

Add deprecation annotations to Enums

In some cases we wish to deprecate members of an enum. This will allow users of a library or of an enum class to receive warning that an enum member will be deprecated ahead of time.

We can achieve deprecation of an enum by adding it to ignore, however this doesn't allow us to warn users ahead of time.

I've introduced a method for user's of python Enum to annotate the enum's members:

from enum import Enum

class Colour(Enum):
    _ignore_ = ["red"]
    _deprecated_ = ["yellow"]

    red = 1
    blue = 2
    green = 3
    yellow = 4

In the above example, red will raise an ValueError due to being ignored. While yellow will raise a warning to the user of yellow. As shown below:

$ ./python.exe concept.py
/cpython/concept.py:15: DeprecationWarning: 'yellow' is a deprecated Colour.
  Colour(4)
/cpython/concept.py:16: DeprecationWarning: 'yellow' is a deprecated Colour.
  Colour["yellow"]
example.py:13: DeprecationWarning: 'yellow' is a deprecated Colour.
  Colour.yellow

Another example:

from enum import Enum

class NotificationType(Enum):
    _deprecated_ = ["offers"]

    marketing = 1
    budgeting = 2
    # Offer has been deprecated in favour of more granular
    # notification types: 'awards' and 'promotions'
    offers = 3
    milestones = 4
    awards = 5
    promotions = 6

Inspired from Java, which allows the use of an @deprecated annotation:

enum Status {
    OK,
    ERROR,

    @Deprecated
    PROBLEM
}

s3bw pushed a commit that referenced this pull request Jul 29, 2020
```
Direct leak of 8 byte(s) in 1 object(s) allocated from:
    #0 0x7f008bf19667 in __interceptor_malloc (/lib64/libasan.so.6+0xb0667)
    #1 0x7f007a0bee4a in subprocess_fork_exec /home/heimes/dev/python/cpython/Modules/_posixsubprocess.c:774
    #2 0xe0305b in cfunction_call Objects/methodobject.c:546
```

Signed-off-by: Christian Heimes <[email protected]>
@github-actions
Copy link

github-actions bot commented Dec 8, 2021

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions bot added the stale label Dec 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant