Skip to content

Commit

Permalink
Import abstract base classes from collections.abc
Browse files Browse the repository at this point in the history
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
```
  • Loading branch information
The-Compiler authored and jbarreneche committed Jun 18, 2019
1 parent 4ce2449 commit 593ee1e
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 26 deletions.
4 changes: 2 additions & 2 deletions docs/jinjaext.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
:copyright: Copyright 2008 by Armin Ronacher.
:license: BSD.
"""
import collections
import os
import re
import inspect
Expand All @@ -26,6 +25,7 @@
from pygments.token import Keyword, Name, Comment, String, Error, \
Number, Operator, Generic
from jinja2 import Environment, FileSystemLoader
from jinja2._compat import abc


def parse_rst(state, content_offset, doc):
Expand Down Expand Up @@ -160,7 +160,7 @@ def walk(node, indent):
members = []
for key, name in node.__dict__.items():
if not key.startswith('_') and \
not hasattr(node.__base__, key) and isinstance(name, collections.Callable):
not hasattr(node.__base__, key) and isinstance(name, abc.Callable):
members.append(key)
if members:
members.sort()
Expand Down
6 changes: 6 additions & 0 deletions jinja2/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,9 @@ def __new__(cls, name, this_bases, d):
from urllib.parse import quote_from_bytes as url_quote
except ImportError:
from urllib import quote as url_quote


try:
from collections import abc
except ImportError:
import collections as abc
9 changes: 2 additions & 7 deletions jinja2/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
TemplateNotFound
from jinja2._compat import imap, text_type, iteritems, \
implements_iterator, implements_to_string, string_types, PY2, \
with_metaclass
with_metaclass, abc


# these variables are exported to the template runtime
Expand Down Expand Up @@ -313,12 +313,7 @@ def __repr__(self):
)


# register the context as mapping if possible
try:
from collections import Mapping
Mapping.register(Context)
except ImportError:
pass
abc.Mapping.register(Context)


class BlockReference(object):
Expand Down
12 changes: 5 additions & 7 deletions jinja2/sandbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@
"""
import types
import operator
from collections import Mapping
from jinja2.environment import Environment
from jinja2.exceptions import SecurityError
from jinja2._compat import string_types, PY2
from jinja2._compat import string_types, PY2, abc
from jinja2.utils import Markup

from markupsafe import EscapeFormatter
Expand Down Expand Up @@ -79,10 +78,9 @@
pass

#: register Python 2.6 abstract base classes
from collections import MutableSet, MutableMapping, MutableSequence
_mutable_set_types += (MutableSet,)
_mutable_mapping_types += (MutableMapping,)
_mutable_sequence_types += (MutableSequence,)
_mutable_set_types += (abc.MutableSet,)
_mutable_mapping_types += (abc.MutableMapping,)
_mutable_sequence_types += (abc.MutableSequence,)


_mutable_spec = (
Expand All @@ -103,7 +101,7 @@
)


class _MagicFormatMapping(Mapping):
class _MagicFormatMapping(abc.Mapping):
"""This class implements a dummy wrapper to fix a bug in the Python
standard library for string formatting.
Expand Down
5 changes: 2 additions & 3 deletions jinja2/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@
"""
import operator
import re
from collections import Mapping
from jinja2.runtime import Undefined
from jinja2._compat import text_type, string_types, integer_types
from jinja2._compat import text_type, string_types, integer_types, abc
import decimal

number_re = re.compile(r'^-?\d+(\.\d+)?$')
Expand Down Expand Up @@ -84,7 +83,7 @@ def test_mapping(value):
.. versionadded:: 2.6
"""
return isinstance(value, Mapping)
return isinstance(value, abc.Mapping)


def test_number(value):
Expand Down
9 changes: 2 additions & 7 deletions jinja2/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from collections import deque
from threading import Lock
from jinja2._compat import text_type, string_types, implements_iterator, \
url_quote
url_quote, abc


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


# register the LRU cache as mutable mapping if possible
try:
from collections import MutableMapping
MutableMapping.register(LRUCache)
except ImportError:
pass
abc.MutableMapping.register(LRUCache)


def select_autoescape(enabled_extensions=('html', 'htm', 'xml'),
Expand Down

0 comments on commit 593ee1e

Please sign in to comment.