Skip to content

Commit f5e73a8

Browse files
committed
Fix PyYAML loader deprecation
1 parent 26a27ee commit f5e73a8

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

CHANGES.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
Release Notes
22
-------------
33

4+
**1.8.0 (unreleased)**
5+
6+
* Handle PyYaml deprecation
7+
8+
* Thanks to `@jurisbu <https://github.com/jurisbu>`_ for the PR
9+
410
**1.7.1 (2018-01-15)**
511

612
* Encode variables as UTF-8.

README.rst

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,26 @@ languages. To install YAML support:
6363
6464
$ pip install pytest-variables[yaml]
6565
66+
YAML Loader
67+
^^^^^^^^^^^
68+
69+
You can specify which loader to use by setting ``yaml_loader`` in ``pytest.ini`` (or similar file)
70+
to one of the following:
71+
72+
* BaseLoader
73+
* SafeLoader
74+
* FullLoader (default)
75+
* UnsafeLoader
76+
77+
.. code-block:: ini
78+
79+
[pytest]
80+
yaml_loader = BaseLoader
81+
82+
**Note** that loader is case-sensitive.
83+
84+
To learn more about the loader, see `here <https://github.com/yaml/pyyaml/wiki/PyYAML-yaml.load(input)-Deprecation>`_
85+
6686
Contributing
6787
------------
6888

pytest_variables/plugin.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@
1313
from pytest_variables import errors
1414

1515

16-
def default(module, path):
16+
def default(module, path, loader):
1717
with io.open(path, "r", encoding="utf8") as f:
18-
return module.load(f)
18+
try:
19+
return module.load(f, Loader=getattr(module, loader))
20+
except (AttributeError, TypeError): # Module is not yaml or no loader
21+
return module.load(f)
1922

2023

2124
parser_table = {
@@ -26,7 +29,7 @@ def default(module, path):
2629
}
2730

2831

29-
def import_parser(path, import_type, parser_func):
32+
def import_parser(path, import_type, parser_func, loader=None):
3033
try:
3134
__import__(import_type)
3235
mod = sys.modules[import_type]
@@ -35,10 +38,15 @@ def import_parser(path, import_type, parser_func):
3538
"{0} import error, please make sure that {0} is "
3639
"installed".format(import_type)
3740
)
38-
return parser_func(mod, path)
41+
return parser_func(mod, path, loader)
3942

4043

4144
def pytest_addoption(parser):
45+
parser.addini(
46+
"yaml_loader",
47+
default="FullLoader",
48+
help="Which loader to use when parsing yaml",
49+
)
4250
group = parser.getgroup("debugconfig")
4351
group.addoption(
4452
"--variables",
@@ -73,10 +81,12 @@ def _merge(a, b, path=None):
7381
def pytest_configure(config):
7482
config._variables = {}
7583
paths = config.getoption("variables")
84+
loader = config.getini("yaml_loader")
7685
for path in paths:
7786
ext = os.path.splitext(path)[1][1:].lower() or "json"
7887
try:
79-
variables = import_parser(path, *parser_table[ext])
88+
import_type, parser_func = parser_table[ext]
89+
variables = import_parser(path, import_type, parser_func, loader)
8090
except KeyError:
8191
warnings.warn(
8292
UserWarning(

0 commit comments

Comments
 (0)