Skip to content

Commit

Permalink
config.types: use new stack_frame arg to tools.deprecated
Browse files Browse the repository at this point in the history
Exi provided the inspiration, and I made it work after adding a new
feature to Sopel's deprecation decorator. 🙃

Co-Authored-By: Exirel <florian.strzelecki@gmail.com>
  • Loading branch information
dgw and Exirel committed Mar 28, 2021
1 parent 556ec25 commit bed0471
Showing 1 changed file with 15 additions and 16 deletions.
31 changes: 15 additions & 16 deletions sopel/config/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import sys
import traceback

from sopel.tools import get_input, stderr
from sopel.tools import deprecated, get_input

if sys.version_info.major >= 3:
unicode = str
Expand Down Expand Up @@ -277,6 +277,19 @@ def _serialize_boolean(value):
return 'true' if _parse_boolean(value) else 'false'


@deprecated(
reason='Use BooleanAttribute instead of ValidatedAttribute with parse=bool',
version='7.1',
removed_in='9.0',
stack_frame=-2,
)
def _deprecated_special_bool_handling(serialize):
if not serialize or serialize == bool:
serialize = _serialize_boolean

return _parse_boolean, serialize


class ValidatedAttribute(BaseValidated):
"""A descriptor for settings in a :class:`StaticSection`.
Expand All @@ -302,21 +315,7 @@ def __init__(self,
name, default=default, is_secret=is_secret)

if parse == bool:
parse = _parse_boolean
if not serialize or serialize == bool:
serialize = _serialize_boolean

# deprecation warning
# can't use tools.deprecated in this case, unfortunately
# we need this to be conditional
msg = (
'Deprecated since 7.1, '
'will be removed in 9.0: '
'Use BooleanAttribute instead of ValidatedAttribute with parse=bool')
stderr(msg)
# Only display the last stack frame
trace = traceback.extract_stack()
stderr(traceback.format_list(trace[:-1])[-1][:-1])
parse, serialize = _deprecated_special_bool_handling(serialize)

self.parse = parse or self.parse
self.serialize = serialize or self.serialize
Expand Down

0 comments on commit bed0471

Please sign in to comment.