Repository: pavdmyt/yaspin

Issue:
Allow write() to print non-string objects
As I have to use `write()` in a yaspin context instead of `print()` I tend to use it in a similar way. This often includes printing non-string objects. `print()` calls `repr()` automatically on anything that is not a string, whereas yaspin runs into an assertion.

Would you be open to replace that assertion with something just calling `repr()` on the given text? This coud also be implemented for the spinner text itself. I'd also volunteer to bring up a PR for this.

Discussion:
pavdmyt: Hi @sebageek 

Thanks for the PR! 
I'll try to review it during this week (pretty busy with other stuff right now).

Patch:
diff --git a/pyproject.toml b/pyproject.toml
index dd4a80d..c18179d 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -73,5 +73,5 @@ pytest-sugar = "^0.9"
 "Bug Tracker" = "https://github.com/pavdmyt/yaspin/issues"
 
 [build-system]
-requires = ["poetry_core>=1.0.0"]
-build-backend = "poetry.core.masonry.api"
+requires = ["poetry>=0.12"]
+build-backend = "poetry.masonry.api"
diff --git a/yaspin/core.py b/yaspin/core.py
index a773ac5..5e8bd9a 100644
--- a/yaspin/core.py
+++ b/yaspin/core.py
@@ -319,9 +319,12 @@ class Yaspin(object):
             sys.stdout.write("\r")
             self._clear_line()
 
-            _text = to_unicode(text)
-            if PY2:
-                _text = _text.encode(ENCODING)
+            if isinstance(text, (builtin_str, str, bytes)):
+                _text = to_unicode(text)
+                if PY2:
+                    _text = _text.encode(ENCODING)
+            else:
+                _text = builtin_str(text)
 
             # Ensure output is bytes for Py2 and Unicode for Py3
             assert isinstance(_text, builtin_str)


Test patch:
diff --git a/tests/test_in_out.py b/tests/test_in_out.py
index 8c1ba45..50c34c5 100644
--- a/tests/test_in_out.py
+++ b/tests/test_in_out.py
@@ -253,3 +253,20 @@ def test_spinner_hiding_with_context_manager_and_exception():
     assert not sp._hide_spin.is_set()
 
     sp.stop()
+
+
+@pytest.mark.parametrize(
+    "obj,obj_str",
+    [
+        ("foo", "foo"),
+        (dict(cat="meow"), "{'cat': 'meow'}"),
+        (23, "23"),
+        (["foo", "bar", "'", 23], """['foo', 'bar', "'", 23]"""),
+    ]
+)
+def test_write_non_str_objects(capsys, obj, obj_str):
+    sp = yaspin()
+    capsys.readouterr()
+    sp.write(obj)
+    out, _ = capsys.readouterr()
+    assert out == "\r\033[K{}\n".format(obj_str)