diff --git a/changes/139.removal.rst b/changes/139.removal.rst new file mode 100644 index 0000000..db7e079 --- /dev/null +++ b/changes/139.removal.rst @@ -0,0 +1 @@ +The 'default' parameter for Choice has been deprecated. diff --git a/src/travertino/declaration.py b/src/travertino/declaration.py index 99c556f..b731239 100644 --- a/src/travertino/declaration.py +++ b/src/travertino/declaration.py @@ -1,5 +1,10 @@ +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" @@ -7,14 +12,21 @@ class Choices: 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 @@ -32,9 +44,6 @@ def __init__( self._options.append("") def validate(self, value): - if self.default: - if value is None: - return None if self.string: try: return value.strip() diff --git a/tests/test_choices.py b/tests/test_choices.py index 6e62842..4bb8f85 100644 --- a/tests/test_choices.py +++ b/tests/test_choices.py @@ -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) diff --git a/tests/test_declaration.py b/tests/test_declaration.py index 043d114..debdf49 100644 --- a/tests/test_declaration.py +++ b/tests/test_declaration.py @@ -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):