diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2020-12-14 23:56:58 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-14 23:56:58 (GMT) |
commit | aba12b67c18b17bb727a0d50dd0653e38cb64dc8 (patch) | |
tree | 204b0a9d0f5805607faf03a248445c60e1c20a36 /Lib/enum.py | |
parent | 6b2ed385094839c1920b934f07d946bf049a3770 (diff) | |
download | cpython-aba12b67c18b17bb727a0d50dd0653e38cb64dc8.zip cpython-aba12b67c18b17bb727a0d50dd0653e38cb64dc8.tar.gz cpython-aba12b67c18b17bb727a0d50dd0653e38cb64dc8.tar.bz2 |
[3.9] bpo-42517: [Enum] deprecate private name members (GH-23722) (GH-23748)
private names will raise a DeprecationWarning; in 3.10 they will become normal attributes
Diffstat (limited to 'Lib/enum.py')
-rw-r--r-- | Lib/enum.py | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/Lib/enum.py b/Lib/enum.py index 35210c9..46b5435 100644 --- a/Lib/enum.py +++ b/Lib/enum.py @@ -41,6 +41,19 @@ def _is_sunder(name): name[-2:-1] != '_' ) +def _is_private(cls_name, name): + # do not use `re` as `re` imports `enum` + pattern = '_%s__' % (cls_name, ) + if ( + len(name) >= 5 + and name.startswith(pattern) + and name[len(pattern)] != '_' + and (name[-1] != '_' or name[-2] != '_') + ): + return True + else: + return False + def _make_class_unpicklable(cls): """ Make the given class un-picklable. @@ -81,6 +94,14 @@ class _EnumDict(dict): Single underscore (sunder) names are reserved. """ + if _is_private(self._cls_name, key): + import warnings + warnings.warn( + "private variables, such as %r, will be normal attributes in 3.10" + % (key, ), + DeprecationWarning, + stacklevel=2, + ) if _is_sunder(key): if key not in ( '_order_', '_create_pseudo_member_', @@ -146,6 +167,7 @@ class EnumMeta(type): metacls._check_for_existing_members(cls, bases) # create the namespace dict enum_dict = _EnumDict() + enum_dict._cls_name = cls # inherit previous flags and _generate_next_value_ function member_type, first_enum = metacls._get_mixins_(cls, bases) if first_enum is not None: |