Repository: fossasia/open-event-server

Issue:
empty_string() needs a rewrite
**Describe the bug**
<!-- A clear and concise description of what the bug is. -->

https://github.com/fossasia/open-event-server/blob/development/app/api/helpers/utilities.py#L28-L34 is exceedingly convoluted, does `__type(x)__` compares contrary to PEP8, and contains several logic errors.  A code review of this function would be a worthwhile effort.  Some `__doctests__` would really be helpful here.

**To Reproduce**
Steps to reproduce the behavior:
1. Go to '...'
2. Click on '....'
3. Scroll down to '....'
4. See error

**Expected behavior**
<!-- A clear and concise description of what you expected to happen. -->

**Stacktrace**
<!-- If applicable, add stacktrace to help explain your problem. -->

**Additional details (please complete the following information):**
 - OS: [e.g. MacOS, Ubuntu, CentOS]
 - Python Version [e.g. `3.5`, `3.6`]
 - `HEAD` Commit hash [e.g. `4629c62`]

**Additional context**
<!-- Add any other context about the problem here. -->



Discussion:
Issue-Label Bot is automatically applying the label `bug` to this issue, with a confidence of 0.71. Please mark this comment with :thumbsup: or :thumbsdown: to give our bot feedback! 

 Links: [app homepage](https://github.com/marketplace/issue-label-bot), [dashboard](https://mlbot.net/data/fossasia/open-event-server) and [code](https://github.com/hamelsmu/MLapp) for this bot.
Yup. it was created when Python 2 to 3 migration/compatibility was needed

There are no doctests, but I think it has some unit tests. If not, they'll be added
There are unit tests. The behaviour is flaky though. None should return True, it is returning False

https://github.com/fossasia/open-event-server/blob/ff408e557f6ff12e37e72a5201d110d589ce50e3/tests/all/integration/api/helpers/test_utilities.py#L32
Perhaps add a few more tests for clarity:
```python
self.assertTrue(string_empty('  '))  # <space><space>
self.assertTrue(string_empty(' ' * 20))  # <space> * 20
self.assertFalse(string_empty(0))  # an int
self.assertFalse(string_empty(0.0))  # a float
self.assertFalse(string_empty([]))  # a list
self.assertFalse(string_empty(False))  # a bool
```
```python
self.assertTrue(string_empty('\t  \n'))  # <tab><space><return>
```
Does this codebase still support Python 2 or not?
From README.md it looks like legacy Python can be dropped so:
```python
def string_empty(value: str) -> bool:
    if isinstance(value, str):
        return not value.strip()
    return False
```
```python
def string_empty(value: str) -> bool:
    return isinstance(value, str) and not value.strip()
```

Resolving patch:
diff --git a/app/api/helpers/utilities.py b/app/api/helpers/utilities.py
--- a/app/api/helpers/utilities.py
+++ b/app/api/helpers/utilities.py
@@ -26,12 +26,7 @@ def require_relationship(resource_list, data):
 
 
 def string_empty(value):
-    is_not_str_type = type(value) is not str
-    if sys.version_info[0] < 3:
-        is_not_str_type = is_not_str_type and type(value) is not unicode
-    if type(value) is not value and is_not_str_type:
-        return False
-    return not (value and value.strip() and value != u'' and value != u' ')
+    return isinstance(value, str) and not value.strip()
 
 
 def strip_tags(html):


Test patch:
diff --git a/tests/all/integration/api/helpers/test_utilities.py b/tests/all/integration/api/helpers/test_utilities.py
--- a/tests/all/integration/api/helpers/test_utilities.py
+++ b/tests/all/integration/api/helpers/test_utilities.py
@@ -29,18 +29,6 @@ def test_require_relationship(self):
             data = ['event']
             require_relationship(['sponsor', 'event'], data)
 
-    def test_string_empty(self):
-        """Method to test whether an empty string is correctly identified."""
-
-        with app.test_request_context():
-            self.assertTrue(string_empty(''))
-            self.assertTrue(string_empty(' '))
-            self.assertFalse(string_empty('some value'))
-            self.assertFalse(string_empty('  some   value '))
-            self.assertFalse(string_empty(str))
-            self.assertFalse(string_empty(int))
-            self.assertFalse(string_empty(None))
-
     def test_monthdelta(self):
         """Method to test difference in months result"""
 
diff --git a/tests/all/unit/api/helpers/test_utilities.py b/tests/all/unit/api/helpers/test_utilities.py
--- a/tests/all/unit/api/helpers/test_utilities.py
+++ b/tests/all/unit/api/helpers/test_utilities.py
@@ -1,6 +1,6 @@
 import unittest
 from app.api.helpers.utilities import get_filename_from_cd
-
+from app.api.helpers.utilities import string_empty
 
 class TestUtilitiesHelperValidation(unittest.TestCase):
     def test_get_filename_from_cd(self):
@@ -17,6 +17,20 @@ def test_get_filename_from_cd(self):
         self.assertEqual(expected_response_first, response_first)
         self.assertEqual(expected_response_none, response_none)
 
+    def test_string_empty(self):
+        """Method to test whether an empty string is correctly identified."""
+
+        self.assertTrue(string_empty(''))
+        self.assertTrue(string_empty(' '))
+        self.assertTrue(string_empty('\t'))
+        self.assertTrue(string_empty('\n'))
+        self.assertFalse(string_empty(None))
+        self.assertFalse(string_empty('some value'))
+        self.assertFalse(string_empty('  some   value '))
+        self.assertFalse(string_empty(0))
+        self.assertFalse(string_empty([]))
+        self.assertFalse(string_empty(False))
+
 
 if __name__ == '__main__':
     unittest.main()