Fix deprecation warnings under py36

This addresses two sources of deprecation warnings.

The collections package has moved ABC classes under collections.abc. Six
does not support this move yet, so this updates the code to try to
import from the newer locations, and if that fails, import from the old
location.

Py36 is also more strict about escape sequences in strings. This happens
move often with regex strings that are valid regex but not a valid
normal string escape sequence. This addresses those errors by switching
to raw strings.

Change-Id: I4c61df6b6432b135297f38c02b4538e4ba56be51
Signed-off-by: Sean McGinnis <sean.mcginnis@gmail.com>
This commit is contained in:
Sean McGinnis 2019-04-09 16:49:11 -05:00 committed by Stephen Finucane
parent 57333e902a
commit 3c50cb5cce
4 changed files with 25 additions and 13 deletions

1
.gitignore vendored
View File

@ -23,6 +23,7 @@ AUTHORS
.update-venv/ .update-venv/
ChangeLog ChangeLog
*.egg *.egg
*.eggs
.stestr/ .stestr/
oslo.db.egg-info/ oslo.db.egg-info/
doc/source/reference/api doc/source/reference/api

View File

@ -195,15 +195,16 @@ def _sqlite_dupe_key_error(integrity_error, match, engine_name, is_disconnect):
r"(?i).*foreign key constraint failed") r"(?i).*foreign key constraint failed")
@filters("postgresql", sqla_exc.IntegrityError, @filters("postgresql", sqla_exc.IntegrityError,
r".*on table \"(?P<table>[^\"]+)\" violates " r".*on table \"(?P<table>[^\"]+)\" violates "
"foreign key constraint \"(?P<constraint>[^\"]+)\".*\n" r"foreign key constraint \"(?P<constraint>[^\"]+)\".*\n"
"DETAIL: Key \((?P<key>.+)\)=\(.+\) " r"DETAIL: Key \((?P<key>.+)\)=\(.+\) "
"is (not present in|still referenced from) table " r"is (not present in|still referenced from) table "
"\"(?P<key_table>[^\"]+)\".") r"\"(?P<key_table>[^\"]+)\".")
@filters("mysql", sqla_exc.IntegrityError, @filters(
"mysql", sqla_exc.IntegrityError,
r".*Cannot (add|delete) or update a (child|parent) row: " r".*Cannot (add|delete) or update a (child|parent) row: "
'a foreign key constraint fails \([`"].+[`"]\.[`"](?P<table>.+)[`"], ' r'a foreign key constraint fails \([`"].+[`"]\.[`"](?P<table>.+)[`"], '
'CONSTRAINT [`"](?P<constraint>.+)[`"] FOREIGN KEY ' r'CONSTRAINT [`"](?P<constraint>.+)[`"] FOREIGN KEY '
'\([`"](?P<key>.+)[`"]\) REFERENCES [`"](?P<key_table>.+)[`"] ') r'\([`"](?P<key>.+)[`"]\) REFERENCES [`"](?P<key_table>.+)[`"] ')
def _foreign_key_error(integrity_error, match, engine_name, is_disconnect): def _foreign_key_error(integrity_error, match, engine_name, is_disconnect):
"""Filter for foreign key errors.""" """Filter for foreign key errors."""

View File

@ -17,6 +17,12 @@
# under the License. # under the License.
import collections import collections
# TODO(smcginnis) update this once six has support for collections.abc
# (https://github.com/benjaminp/six/pull/241) or clean up once we drop py2.7.
try:
from collections.abc import Iterable
except ImportError:
from collections import Iterable
import contextlib import contextlib
import inspect as pyinspect import inspect as pyinspect
import itertools import itertools
@ -280,8 +286,7 @@ def paginate_query(query, model, limit, sort_keys, marker=None,
def to_list(x, default=None): def to_list(x, default=None):
if x is None: if x is None:
return default return default
if not isinstance(x, collections.Iterable) or \ if not isinstance(x, Iterable) or isinstance(x, six.string_types):
isinstance(x, six.string_types):
return [x] return [x]
elif isinstance(x, list): elif isinstance(x, list):
return x return x

View File

@ -13,7 +13,12 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import collections # TODO(smcginnis) update this once six has support for collections.abc
# (https://github.com/benjaminp/six/pull/241) or clean up once we drop py2.7.
try:
from collections.abc import Iterable
except ImportError:
from collections import Iterable
import datetime import datetime
import mock import mock
@ -51,7 +56,7 @@ class ModelBaseTest(test_base._DbTestCase):
"Method %s() is not found" % method) "Method %s() is not found" % method)
def test_modelbase_is_iterable(self): def test_modelbase_is_iterable(self):
self.assertTrue(issubclass(models.ModelBase, collections.Iterable)) self.assertTrue(issubclass(models.ModelBase, Iterable))
def test_modelbase_set(self): def test_modelbase_set(self):
self.mb['world'] = 'hello' self.mb['world'] = 'hello'