Skip to content

Commit

Permalink
config.types: better ValidatedAttribute(parse=bool) deprecation warnings
Browse files Browse the repository at this point in the history
This will fire only once, when the setting object is initialized.

Attaching the deprecation to the boolean helper functions seemed to fire
every time a ValidatedAttribute in bool mode was accessed. Very spammy.
  • Loading branch information
dgw committed Mar 26, 2021
1 parent 0654371 commit 556ec25
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions sopel/config/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@
import os.path
import re
import sys
import traceback

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

if sys.version_info.major >= 3:
unicode = str
Expand Down Expand Up @@ -264,11 +265,6 @@ def __delete__(self, instance):
instance._parser.remove_option(instance._section_name, self.name)


@deprecated(
reason="Use BooleanAttribute instead of ValidatedAttribute with bool as parser",
version='7.1',
removed_in='9.0',
)
def _parse_boolean(value):
if value is True or value == 1:
return value
Expand All @@ -277,11 +273,6 @@ def _parse_boolean(value):
return bool(value)


@deprecated(
reason="Use BooleanAttribute instead of ValidatedAttribute with bool as parser",
version='7.1',
removed_in='9.0',
)
def _serialize_boolean(value):
return 'true' if _parse_boolean(value) else 'false'

Expand Down Expand Up @@ -309,10 +300,24 @@ def __init__(self,
is_secret=False):
super(ValidatedAttribute, self).__init__(
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])

self.parse = parse or self.parse
self.serialize = serialize or self.serialize

Expand Down

0 comments on commit 556ec25

Please sign in to comment.