summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Lord <davidism@gmail.com>2020-01-28 22:33:45 -0800
committerDavid Lord <davidism@gmail.com>2020-01-28 22:33:45 -0800
commit58ef8298075485e84a86184d58e877bc527a7e55 (patch)
tree9205c3f3f7d3f71d56d18a994926fe08b5f03b7f
parent4b8d839e1ec7ac4c7e42241e9e4279826bcc3580 (diff)
downloadjinja2-isinstance-tests.tar.gz
type tests use isinstanceisinstance-tests
-rw-r--r--CHANGES.rst4
-rw-r--r--src/jinja2/tests.py17
-rw-r--r--tests/test_tests.py1
3 files changed, 9 insertions, 13 deletions
diff --git a/CHANGES.rst b/CHANGES.rst
index 9e26dff..4467887 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -8,6 +8,10 @@ Unreleased
- Drop support for Python 2.7 and 3.5.
- Bump MarkupSafe dependency to >=1.1.
- Bump Babel optional dependency to >=2.1.
+- Rather than checking for attributes, ``is sequence`` tests against
+ :class:`~collections.abc.Sequence`, ``is iterable`` tests against
+ :class:`~collections.abc.Iterable`, and ``is escaped`` tests against
+ :class:`~markupsafe.Markup`.
Version 2.11.0
diff --git a/src/jinja2/tests.py b/src/jinja2/tests.py
index fabd4ce..2f9d23f 100644
--- a/src/jinja2/tests.py
+++ b/src/jinja2/tests.py
@@ -4,6 +4,8 @@ import decimal
import operator
import re
+from markupsafe import Markup
+
from ._compat import abc
from ._compat import integer_types
from ._compat import string_types
@@ -131,12 +133,7 @@ def test_sequence(value):
"""Return true if the variable is a sequence. Sequences are variables
that are iterable.
"""
- try:
- len(value)
- value.__getitem__
- except Exception:
- return False
- return True
+ return isinstance(value, abc.Sequence)
def test_sameas(value, other):
@@ -154,16 +151,12 @@ def test_sameas(value, other):
def test_iterable(value):
"""Check if it's possible to iterate over an object."""
- try:
- iter(value)
- except TypeError:
- return False
- return True
+ return isinstance(value, abc.Iterable)
def test_escaped(value):
"""Check if the value is escaped."""
- return hasattr(value, "__html__")
+ return isinstance(value, Markup)
def test_in(value, seq):
diff --git a/tests/test_tests.py b/tests/test_tests.py
index 42595e8..84f9e2d 100644
--- a/tests/test_tests.py
+++ b/tests/test_tests.py
@@ -86,7 +86,6 @@ class TestTestsCase(object):
('"foo" is sequence', True),
("[] is sequence", True),
("[1, 2, 3] is sequence", True),
- ("{} is sequence", True),
("none is mapping", False),
("false is mapping", False),
("42 is mapping", False),