Skip to content

Commit 593ee1e

Browse files
The-Compilerjbarreneche
authored andcommitted
Import abstract base classes from collections.abc
In Python 3.7, importing ABCs directly from the `collections` module shows a warning (and in Python 3.8 it will stop working) - see python/cpython@c66f9f8 This fixes various DeprecationWarnings such as those: ``` .../jinja2/utils.py:485: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working from collections import MutableMapping .../jinja2/runtime.py:318: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working from collections import Mapping ```
1 parent 4ce2449 commit 593ee1e

6 files changed

Lines changed: 19 additions & 26 deletions

File tree

‎docs/jinjaext.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
:copyright: Copyright 2008 by Armin Ronacher.
99
:license: BSD.
1010
"""
11-
import collections
1211
import os
1312
import re
1413
import inspect
@@ -26,6 +25,7 @@
2625
from pygments.token import Keyword, Name, Comment, String, Error, \
2726
Number, Operator, Generic
2827
from jinja2 import Environment, FileSystemLoader
28+
from jinja2._compat import abc
2929

3030

3131
def parse_rst(state, content_offset, doc):
@@ -160,7 +160,7 @@ def walk(node, indent):
160160
members = []
161161
for key, name in node.__dict__.items():
162162
if not key.startswith('_') and \
163-
not hasattr(node.__base__, key) and isinstance(name, collections.Callable):
163+
not hasattr(node.__base__, key) and isinstance(name, abc.Callable):
164164
members.append(key)
165165
if members:
166166
members.sort()

‎jinja2/_compat.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,9 @@ def __new__(cls, name, this_bases, d):
9797
from urllib.parse import quote_from_bytes as url_quote
9898
except ImportError:
9999
from urllib import quote as url_quote
100+
101+
102+
try:
103+
from collections import abc
104+
except ImportError:
105+
import collections as abc

‎jinja2/runtime.py‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
TemplateNotFound
2121
from jinja2._compat import imap, text_type, iteritems, \
2222
implements_iterator, implements_to_string, string_types, PY2, \
23-
with_metaclass
23+
with_metaclass, abc
2424

2525

2626
# these variables are exported to the template runtime
@@ -313,12 +313,7 @@ def __repr__(self):
313313
)
314314

315315

316-
# register the context as mapping if possible
317-
try:
318-
from collections import Mapping
319-
Mapping.register(Context)
320-
except ImportError:
321-
pass
316+
abc.Mapping.register(Context)
322317

323318

324319
class BlockReference(object):

‎jinja2/sandbox.py‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@
1414
"""
1515
import types
1616
import operator
17-
from collections import Mapping
1817
from jinja2.environment import Environment
1918
from jinja2.exceptions import SecurityError
20-
from jinja2._compat import string_types, PY2
19+
from jinja2._compat import string_types, PY2, abc
2120
from jinja2.utils import Markup
2221

2322
from markupsafe import EscapeFormatter
@@ -79,10 +78,9 @@
7978
pass
8079

8180
#: register Python 2.6 abstract base classes
82-
from collections import MutableSet, MutableMapping, MutableSequence
83-
_mutable_set_types += (MutableSet,)
84-
_mutable_mapping_types += (MutableMapping,)
85-
_mutable_sequence_types += (MutableSequence,)
81+
_mutable_set_types += (abc.MutableSet,)
82+
_mutable_mapping_types += (abc.MutableMapping,)
83+
_mutable_sequence_types += (abc.MutableSequence,)
8684

8785

8886
_mutable_spec = (
@@ -103,7 +101,7 @@
103101
)
104102

105103

106-
class _MagicFormatMapping(Mapping):
104+
class _MagicFormatMapping(abc.Mapping):
107105
"""This class implements a dummy wrapper to fix a bug in the Python
108106
standard library for string formatting.
109107

‎jinja2/tests.py‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010
"""
1111
import operator
1212
import re
13-
from collections import Mapping
1413
from jinja2.runtime import Undefined
15-
from jinja2._compat import text_type, string_types, integer_types
14+
from jinja2._compat import text_type, string_types, integer_types, abc
1615
import decimal
1716

1817
number_re = re.compile(r'^-?\d+(\.\d+)?$')
@@ -84,7 +83,7 @@ def test_mapping(value):
8483
8584
.. versionadded:: 2.6
8685
"""
87-
return isinstance(value, Mapping)
86+
return isinstance(value, abc.Mapping)
8887

8988

9089
def test_number(value):

‎jinja2/utils.py‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from collections import deque
1515
from threading import Lock
1616
from jinja2._compat import text_type, string_types, implements_iterator, \
17-
url_quote
17+
url_quote, abc
1818

1919

2020
_word_split_re = re.compile(r'(\s+)')
@@ -480,12 +480,7 @@ def __reversed__(self):
480480
__copy__ = copy
481481

482482

483-
# register the LRU cache as mutable mapping if possible
484-
try:
485-
from collections import MutableMapping
486-
MutableMapping.register(LRUCache)
487-
except ImportError:
488-
pass
483+
abc.MutableMapping.register(LRUCache)
489484

490485

491486
def select_autoescape(enabled_extensions=('html', 'htm', 'xml'),

0 commit comments

Comments
 (0)