Skip to content

Commit

Permalink
Remove deprecation warnings for pyyaml and collections (#309)
Browse files Browse the repository at this point in the history
* Remove deprecation warnings for pyyaml and collections

* Add our own warning about python 2

* Add link to Pytest deprecation
  • Loading branch information
michaelboulton authored and benhowes committed Mar 17, 2019
1 parent f96baf4 commit 2cc9cfd
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 13 deletions.
2 changes: 1 addition & 1 deletion tavern/_plugins/mqtt/tavernhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ def get_expected_from_request(stage, test_block_config, session):

schema_path = join(abspath(dirname(__file__)), "schema.yaml")
with open(schema_path, "r") as schema_file:
schema = yaml.load(schema_file)
schema = yaml.load(schema_file, Loader=yaml.SafeLoader)
7 changes: 6 additions & 1 deletion tavern/response/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import logging
from collections import Mapping

try:
from collections.abc import Mapping
except ImportError:
from collections import Mapping

from abc import abstractmethod
import warnings

Expand Down
4 changes: 3 additions & 1 deletion tavern/schemas/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ def _load_base_schema(self, schema_filename):
return self._loaded[schema_filename]
except KeyError:
with open(schema_filename, "r") as sfile:
self._loaded[schema_filename] = yaml.load(sfile.read())
self._loaded[schema_filename] = yaml.load(
sfile.read(), Loader=yaml.SafeLoader
)

logger.debug("Loaded schema from %s", schema_filename)

Expand Down
12 changes: 12 additions & 0 deletions tavern/testutils/pytesthook/item.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import logging
import sys
import warnings

import attr
import pytest
Expand Down Expand Up @@ -29,13 +31,23 @@ class YamlItem(pytest.Item):
spec (dict): The whole dictionary of the test
"""

py2_warned = False

def __init__(self, name, parent, spec, path):
super(YamlItem, self).__init__(name, parent)
self.path = path
self.spec = spec

self.global_cfg = {}

if sys.version_info < (3, 0, 0):
if not YamlItem.py2_warned:
warnings.warn(
"Tavern will drop support for Python 2 in a future release, please switch to using Python 3 (see https://docs.pytest.org/en/latest/py27-py34-deprecation.html)",
FutureWarning,
)
YamlItem.py2_warned = True

def initialise_fixture_attrs(self):
# pylint: disable=protected-access,attribute-defined-outside-init
self.funcargs = {}
Expand Down
12 changes: 6 additions & 6 deletions tavern/util/dict_util.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import collections
import warnings
import logging
from builtins import str as ustr

try:
from collections.abc import Mapping
except ImportError:
from collections import Mapping

from future.utils import raise_from
from box import Box

Expand Down Expand Up @@ -102,11 +106,7 @@ def deep_dict_merge(initial_dct, merge_dct):
dct = initial_dct.copy()

for k in merge_dct:
if (
k in dct
and isinstance(dct[k], dict)
and isinstance(merge_dct[k], collections.Mapping)
):
if k in dct and isinstance(dct[k], dict) and isinstance(merge_dct[k], Mapping):
dct[k] = deep_dict_merge(dct[k], merge_dct[k])
else:
dct[k] = merge_dct[k]
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
def run_all():
current_dir = os.path.dirname(os.path.abspath(__file__))
with open(os.path.join(current_dir, "logging.yaml"), "r") as spec_file:
settings = yaml.load(spec_file)
settings = yaml.load(spec_file, Loader=yaml.SafeLoader)
logging.config.dictConfig(settings)
8 changes: 6 additions & 2 deletions tests/unit/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,9 @@ def test_validate_schema_correct(self, nested_response):
"""
)

validate_pykwalify(nested_response, yaml.load(correct_schema))
validate_pykwalify(
nested_response, yaml.load(correct_schema, Loader=yaml.SafeLoader)
)

def test_validate_schema_incorrect(self, nested_response):
correct_schema = dedent(
Expand All @@ -229,4 +231,6 @@ def test_validate_schema_incorrect(self, nested_response):
)

with pytest.raises(exceptions.BadSchemaError):
validate_pykwalify(nested_response, yaml.load(correct_schema))
validate_pykwalify(
nested_response, yaml.load(correct_schema, Loader=yaml.SafeLoader)
)
2 changes: 1 addition & 1 deletion tests/unit/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def fix_test_dict():
"""
)

as_dict = yaml.load(text)
as_dict = yaml.load(text, Loader=yaml.SafeLoader)

return as_dict

Expand Down

0 comments on commit 2cc9cfd

Please sign in to comment.