Skip to content

Commit

Permalink
Merge pull request #139 from HalfWhitt/deprecate-default
Browse files Browse the repository at this point in the history
Deprecating Choices.default
  • Loading branch information
freakboy3742 committed Feb 27, 2024
2 parents aae5eda + e55fe1e commit 7672a97
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
1 change: 1 addition & 0 deletions changes/139.removal.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The 'default' parameter for Choice has been deprecated.
19 changes: 14 additions & 5 deletions src/travertino/declaration.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
from warnings import filterwarnings, warn

from .colors import color

# Make sure deprecation warnings are shown by default
filterwarnings("default", category=DeprecationWarning)


class Choices:
"A class to define allowable data types for a property"

def __init__(
self,
*constants,
default=False,
default=None, # DEPRECATED
string=False,
integer=False,
number=False,
color=False,
):
if default is not None:
warn(
"The `default` argument to Choices.__init__ is deprecated. "
"Providing no initial value to a property using it is sufficient.",
DeprecationWarning,
stacklevel=2,
)

self.constants = set(constants)
self.default = default

self.string = string
self.integer = integer
Expand All @@ -32,9 +44,6 @@ def __init__(
self._options.append("<color>")

def validate(self, value):
if self.default:
if value is None:
return None
if self.string:
try:
return value.strip()
Expand Down
4 changes: 4 additions & 0 deletions tests/test_choices.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,3 +396,7 @@ def __init__(self):
# Both equality and instance checking should work.
self.assertEqual(obj.prop, TOP)
self.assertIs(obj.prop, TOP)

def test_deprecated_default(self):
with self.assertWarns(DeprecationWarning):
Choices(default=True)
2 changes: 1 addition & 1 deletion tests/test_declaration.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
VALUE2 = "value2"
VALUE3 = "value3"
VALUE_CHOICES = Choices(VALUE1, VALUE2, VALUE3, None, integer=True)
DEFAULT_VALUE_CHOICES = Choices(VALUE1, VALUE2, VALUE3, integer=True, default=True)
DEFAULT_VALUE_CHOICES = Choices(VALUE1, VALUE2, VALUE3, integer=True)


class Style(BaseStyle):
Expand Down

0 comments on commit 7672a97

Please sign in to comment.