diff --git a/constyle/_style.py b/constyle/_style.py index e41a2ac..d34b499 100644 --- a/constyle/_style.py +++ b/constyle/_style.py @@ -39,13 +39,15 @@ def __init__( self._prefix = f"\033[{';'.join(str(p) for p in self._params)}m" self._end = end - def __call__(self, string: str) -> str: + def __call__(self, string: object) -> str: """Apply this style to the given string. + If the given object isn't a string it will be converted into one. + Args: string: The string to apply the style to. """ - return f"{self}{string}{self._end or Style()}" if string else string + return f"{self}{string}{self._end or Style()}" if string != "" else string def __add__(self, other: "Style") -> "Style": """Add the attributes of the left and right `Style` operands, returning a new `Style`. @@ -72,9 +74,11 @@ def __eq__(self, __o: object) -> bool: ) -def style(string: str, *attrs: "Style", end: "Optional[Style]" = None) -> str: +def style(string: object, *attrs: "Style", end: "Optional[Style]" = None) -> str: """Apply the given attributes to the given string. + If the given object isn't a string it will be converted into one. + Args: string: The string to style. *attrs: The attributes/styles to apply. diff --git a/tests/test_constyle.py b/tests/test_constyle.py index b5b5d33..1987b09 100644 --- a/tests/test_constyle.py +++ b/tests/test_constyle.py @@ -72,3 +72,7 @@ def test_add() -> None: def test_attribute_call() -> None: assert Attributes.BOLD("Hello World") == style("Hello World", Attributes.BOLD) + + +def test_non_string() -> None: + assert style(None, Attributes.BOLD) == style("None", Attributes.BOLD) \ No newline at end of file