diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 9890e3d..b5995ec 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -1,6 +1,15 @@ Changelog ========= +Upcoming release +---------------- + +- Prevent ``AttributeError`` when trying to humanize ``None`` value (`#6`_), + thanks to `@earlbread`_ for the report. + +.. _#6: https://github.com/vitalk/flask-humanize/pull/6 +.. _@earlbread: https://github.com/earlbread + 0.3.0 (2016-03-26) ------------------ diff --git a/flask_humanize/__init__.py b/flask_humanize/__init__.py index 3a74565..1be8aa5 100644 --- a/flask_humanize/__init__.py +++ b/flask_humanize/__init__.py @@ -16,15 +16,18 @@ from flask import current_app from werkzeug.datastructures import ImmutableDict -from . import compat +from .compat import ( + text_type, string_types +) __version__ = '0.3.0' def force_unicode(value): - if not isinstance(value, compat.text_type): - return value.decode('utf-8') + if isinstance(value, string_types): + if not isinstance(value, text_type): + return value.decode('utf-8') return value diff --git a/flask_humanize/compat.py b/flask_humanize/compat.py index 99bc9a4..b3ed5af 100644 --- a/flask_humanize/compat.py +++ b/flask_humanize/compat.py @@ -7,5 +7,7 @@ if PY2: text_type = unicode + string_types = basestring, else: text_type = str + string_types = str, diff --git a/tests/test_humanize.py b/tests/test_humanize.py index 5480ee6..6360aff 100644 --- a/tests/test_humanize.py +++ b/tests/test_humanize.py @@ -43,6 +43,9 @@ def test_defaults(self, config, h): @pytest.mark.usefixtures('app') class TestHumanize: + def test_none(self, h): + assert h._humanize(None) is None + def test_naturalday(self, h): assert h._humanize(today, 'naturalday') == 'today' assert h._humanize(today + one_day, 'naturalday') == 'tomorrow'