diff options
author | Anthon van der Neut <anthon@mnt.org> | 2018-07-28 23:53:41 +0200 |
---|---|---|
committer | Anthon van der Neut <anthon@mnt.org> | 2018-07-28 23:53:41 +0200 |
commit | f9b50b606fe3f12b09dd36cb999238bf440d0cd1 (patch) | |
tree | 04a14e70240d7677ac84d95cd38f3cded4729091 | |
parent | 9f9266b31445296439cbcc9c0e3f01b4fb15ed8a (diff) | |
download | ruamel.yaml-f9b50b606fe3f12b09dd36cb999238bf440d0cd1.tar.gz |
Resolving issue #210 deprecation warning in python 3.7
*When this change indeed resolves your problem, please **Close** this issue*.
*(You can do so usingthe WorkFlow pull-down (close to the top right of this page)*
-rw-r--r-- | _test/roundtrip.py | 106 | ||||
-rw-r--r-- | _test/test_add_xxx.py | 46 | ||||
-rw-r--r-- | _test/test_anchor.py | 6 | ||||
-rw-r--r-- | _test/test_api_change.py | 25 | ||||
-rw-r--r-- | _test/test_class_register.py | 57 | ||||
-rw-r--r-- | _test/test_collections.py | 4 | ||||
-rw-r--r-- | _test/test_comments.py | 9 | ||||
-rw-r--r-- | _test/test_copy.py | 1 | ||||
-rw-r--r-- | _test/test_datetime.py | 1 | ||||
-rw-r--r-- | _test/test_documents.py | 3 | ||||
-rw-r--r-- | _test/test_float.py | 2 | ||||
-rw-r--r-- | _test/test_flowsequencekey.py | 1 | ||||
-rw-r--r-- | _test/test_indentation.py | 7 | ||||
-rw-r--r-- | _test/test_issues.py | 2 | ||||
-rw-r--r-- | _test/test_json_numbers.py | 2 | ||||
-rw-r--r-- | _test/test_none.py | 6 | ||||
-rw-r--r-- | _test/test_numpy.py | 3 | ||||
-rw-r--r-- | _test/test_string.py | 3 | ||||
-rw-r--r-- | _test/test_tag.py | 28 | ||||
-rw-r--r-- | _test/test_version.py | 5 | ||||
-rw-r--r-- | _test/test_yamlfile.py | 9 | ||||
-rw-r--r-- | _test/test_yamlobject.py | 26 | ||||
-rw-r--r-- | _test/test_z_data.py | 7 | ||||
-rw-r--r-- | comments.py | 6 | ||||
-rw-r--r-- | compat.py | 2 | ||||
-rw-r--r-- | constructor.py | 15 | ||||
-rw-r--r-- | tox.ini | 25 |
27 files changed, 244 insertions, 163 deletions
diff --git a/_test/roundtrip.py b/_test/roundtrip.py index 0037437..3a842a9 100644 --- a/_test/roundtrip.py +++ b/_test/roundtrip.py @@ -1,3 +1,4 @@ +# coding: utf-8 from __future__ import print_function @@ -7,9 +8,6 @@ helper routines for testing round trip of commented YAML data import sys import textwrap -import ruamel.yaml -from ruamel.yaml.compat import StringIO, BytesIO # NOQA - def dedent(data): try: @@ -25,6 +23,7 @@ def dedent(data): def round_trip_load(inp, preserve_quotes=None, version=None): + import ruamel.yaml # NOQA dinp = dedent(inp) return ruamel.yaml.load( dinp, @@ -35,6 +34,7 @@ def round_trip_load(inp, preserve_quotes=None, version=None): def round_trip_load_all(inp, preserve_quotes=None, version=None): + import ruamel.yaml # NOQA dinp = dedent(inp) return ruamel.yaml.load_all( dinp, @@ -47,6 +47,7 @@ def round_trip_load_all(inp, preserve_quotes=None, version=None): def round_trip_dump(data, stream=None, indent=None, block_seq_indent=None, top_level_colon_align=None, prefix_colon=None, explicit_start=None, explicit_end=None, version=None): + import ruamel.yaml # NOQA return ruamel.yaml.round_trip_dump(data, stream=stream, indent=indent, block_seq_indent=block_seq_indent, top_level_colon_align=top_level_colon_align, @@ -111,52 +112,59 @@ def round_trip(inp, outp=None, extra=None, intermediate=None, indent=None, return data -class YAML(ruamel.yaml.YAML): - """auto dedent string parameters on load""" - def load(self, stream): - if isinstance(stream, str): - if stream and stream[0] == '\n': - stream = stream[1:] - stream = textwrap.dedent(stream) - return ruamel.yaml.YAML.load(self, stream) - - def load_all(self, stream): - if isinstance(stream, str): +def YAML(**kw): + import ruamel.yaml # NOQA + + class MyYAML(ruamel.yaml.YAML): + """auto dedent string parameters on load""" + def load(self, stream): + if isinstance(stream, str): + if stream and stream[0] == '\n': + stream = stream[1:] + stream = textwrap.dedent(stream) + return ruamel.yaml.YAML.load(self, stream) + + def load_all(self, stream): + if isinstance(stream, str): + if stream and stream[0] == '\n': + stream = stream[1:] + stream = textwrap.dedent(stream) + for d in ruamel.yaml.YAML.load_all(self, stream): + yield d + + def dump(self, data, **kw): + from ruamel.yaml.compat import StringIO, BytesIO # NOQA + assert ('stream' in kw) ^ ('compare' in kw) + if 'stream' in kw: + return ruamel.yaml.YAML.dump(data, **kw) + lkw = kw.copy() + expected = textwrap.dedent(lkw.pop('compare')) + unordered_lines = lkw.pop('unordered_lines', False) + if expected and expected[0] == '\n': + expected = expected[1:] + lkw['stream'] = st = StringIO() + ruamel.yaml.YAML.dump(self, data, **lkw) + res = st.getvalue() + print(res) + if unordered_lines: + res = sorted(res.splitlines()) + expected = sorted(expected.splitlines()) + assert res == expected + + def round_trip(self, stream, **kw): + from ruamel.yaml.compat import StringIO, BytesIO # NOQA + assert isinstance(stream, ruamel.yaml.compat.text_type) + lkw = kw.copy() if stream and stream[0] == '\n': stream = stream[1:] stream = textwrap.dedent(stream) - for d in ruamel.yaml.YAML.load_all(self, stream): - yield d - - def dump(self, data, **kw): - assert ('stream' in kw) ^ ('compare' in kw) - if 'stream' in kw: - return ruamel.yaml.YAML.dump(data, **kw) - lkw = kw.copy() - expected = textwrap.dedent(lkw.pop('compare')) - unordered_lines = lkw.pop('unordered_lines', False) - if expected and expected[0] == '\n': - expected = expected[1:] - lkw['stream'] = st = StringIO() - ruamel.yaml.YAML.dump(self, data, **lkw) - res = st.getvalue() - print(res) - if unordered_lines: - res = sorted(res.splitlines()) - expected = sorted(expected.splitlines()) - assert res == expected - - def round_trip(self, stream, **kw): - assert isinstance(stream, ruamel.yaml.compat.text_type) - lkw = kw.copy() - if stream and stream[0] == '\n': - stream = stream[1:] - stream = textwrap.dedent(stream) - data = ruamel.yaml.YAML.load(self, stream) - outp = lkw.pop('outp', stream) - lkw['stream'] = st = StringIO() - ruamel.yaml.YAML.dump(self, data, **lkw) - res = st.getvalue() - if res != outp: - diff(outp, res, "input string") - assert res == outp + data = ruamel.yaml.YAML.load(self, stream) + outp = lkw.pop('outp', stream) + lkw['stream'] = st = StringIO() + ruamel.yaml.YAML.dump(self, data, **lkw) + res = st.getvalue() + if res != outp: + diff(outp, res, "input string") + assert res == outp + + return MyYAML(**kw) diff --git a/_test/test_add_xxx.py b/_test/test_add_xxx.py index 5b064f7..f2e976f 100644 --- a/_test/test_add_xxx.py +++ b/_test/test_add_xxx.py @@ -2,9 +2,8 @@ import re import pytest # NOQA -import ruamel.yaml -from roundtrip import round_trip, dedent, round_trip_load, round_trip_dump # NOQA +from roundtrip import dedent # from PyYAML docs @@ -27,18 +26,21 @@ def dice_representer(dumper, data): def test_dice_constructor(): + import ruamel.yaml # NOQA ruamel.yaml.add_constructor(u'!dice', dice_constructor) data = ruamel.yaml.load('initial hit points: !dice 8d4', Loader=ruamel.yaml.Loader) assert str(data) == "{'initial hit points': Dice(8,4)}" def test_dice_constructor_with_loader(): + import ruamel.yaml # NOQA ruamel.yaml.add_constructor(u'!dice', dice_constructor, Loader=ruamel.yaml.Loader) data = ruamel.yaml.load('initial hit points: !dice 8d4', Loader=ruamel.yaml.Loader) assert str(data) == "{'initial hit points': Dice(8,4)}" def test_dice_representer(): + import ruamel.yaml # NOQA ruamel.yaml.add_representer(Dice, dice_representer) # ruamel.yaml 0.15.8+ no longer forces quotes tagged scalars assert ruamel.yaml.dump(dict(gold=Dice(10, 6)), default_flow_style=False) == \ @@ -46,6 +48,7 @@ def test_dice_representer(): def test_dice_implicit_resolver(): + import ruamel.yaml # NOQA pattern = re.compile(r'^\d+d\d+$') ruamel.yaml.add_implicit_resolver(u'!dice', pattern) assert ruamel.yaml.dump(dict(treasure=Dice(10, 20)), default_flow_style=False) == \ @@ -74,6 +77,7 @@ class YAMLObj1(object): @classmethod def from_yaml(cls, loader, suffix, node): + import ruamel.yaml # NOQA obj1 = Obj1(suffix) if isinstance(node, ruamel.yaml.MappingNode): obj1.add_node(loader.construct_mapping(node)) @@ -87,6 +91,7 @@ class YAMLObj1(object): def test_yaml_obj(): + import ruamel.yaml # NOQA ruamel.yaml.add_representer(Obj1, YAMLObj1.to_yaml) ruamel.yaml.add_multi_constructor(YAMLObj1.yaml_tag, YAMLObj1.from_yaml) x = ruamel.yaml.load('!obj:x.2\na: 1', Loader=ruamel.yaml.Loader) @@ -95,6 +100,7 @@ def test_yaml_obj(): def test_yaml_obj_with_loader_and_dumper(): + import ruamel.yaml # NOQA ruamel.yaml.add_representer(Obj1, YAMLObj1.to_yaml, Dumper=ruamel.yaml.Dumper) ruamel.yaml.add_multi_constructor(YAMLObj1.yaml_tag, YAMLObj1.from_yaml, Loader=ruamel.yaml.Loader) @@ -109,28 +115,30 @@ def test_yaml_obj_with_loader_and_dumper(): # Issue 127 reported by Tommy Wang -class Ref(ruamel.yaml.YAMLObject): - yaml_constructor = ruamel.yaml.RoundTripConstructor - yaml_representer = ruamel.yaml.RoundTripRepresenter - yaml_tag = u'!Ref' - def __init__(self, logical_id): - self.logical_id = logical_id +def test_issue_127(): + import ruamel.yaml # NOQA - @classmethod - def from_yaml(cls, loader, node): - return cls(loader.construct_scalar(node)) + class Ref(ruamel.yaml.YAMLObject): + yaml_constructor = ruamel.yaml.RoundTripConstructor + yaml_representer = ruamel.yaml.RoundTripRepresenter + yaml_tag = u'!Ref' - @classmethod - def to_yaml(cls, dumper, data): - if isinstance(data.logical_id, ruamel.yaml.scalarstring.ScalarString): - style = data.logical_id.style # ruamel.yaml>0.15.8 - else: - style = None - return dumper.represent_scalar(cls.yaml_tag, data.logical_id, style=style) + def __init__(self, logical_id): + self.logical_id = logical_id + @classmethod + def from_yaml(cls, loader, node): + return cls(loader.construct_scalar(node)) + + @classmethod + def to_yaml(cls, dumper, data): + if isinstance(data.logical_id, ruamel.yaml.scalarstring.ScalarString): + style = data.logical_id.style # ruamel.yaml>0.15.8 + else: + style = None + return dumper.represent_scalar(cls.yaml_tag, data.logical_id, style=style) -def test_issue_127(): document = dedent('''\ AList: - !Ref One diff --git a/_test/test_anchor.py b/_test/test_anchor.py index 8b83c3e..ae6469a 100644 --- a/_test/test_anchor.py +++ b/_test/test_anchor.py @@ -9,9 +9,6 @@ from textwrap import dedent import platform from roundtrip import round_trip, dedent, round_trip_load, round_trip_dump # NOQA -from ruamel.yaml.compat import PY3 -from ruamel.yaml.error import ReusedAnchorWarning -from ruamel.yaml import version_info def load(s): @@ -233,6 +230,7 @@ class TestAnchorsAliases: # this is an error in PyYAML def test_reused_anchor(self): + from ruamel.yaml.error import ReusedAnchorWarning yaml = ''' - &a x: 1 @@ -354,6 +352,7 @@ class TestMergeKeysValues: def test_len_items_delete(self): from ruamel.yaml import safe_load + from ruamel.yaml.compat import PY3 d = safe_load(self.yaml_str) data = round_trip_load(self.yaml_str) x = data[2].items() @@ -375,6 +374,7 @@ class TestMergeKeysValues: class TestDuplicateKeyThroughAnchor: def test_duplicate_key_00(self): + from ruamel.yaml import version_info from ruamel.yaml import safe_load, round_trip_load from ruamel.yaml.constructor import DuplicateKeyFutureWarning, DuplicateKeyError s = dedent("""\ diff --git a/_test/test_api_change.py b/_test/test_api_change.py index 65789f4..59473fe 100644 --- a/_test/test_api_change.py +++ b/_test/test_api_change.py @@ -9,31 +9,35 @@ testing of anchors and the aliases referring to them import sys import textwrap import pytest -from ruamel.yaml import YAML, safe_load -from ruamel.yaml.constructor import DuplicateKeyError, DuplicateKeyFutureWarning from ruamel.std.pathlib import Path class TestNewAPI: def test_duplicate_keys_00(self): + from ruamel.yaml import YAML from ruamel.yaml.constructor import DuplicateKeyError yaml = YAML() with pytest.raises(DuplicateKeyError): yaml.load('{a: 1, a: 2}') def test_duplicate_keys_01(self): + from ruamel.yaml import YAML + from ruamel.yaml.constructor import DuplicateKeyError yaml = YAML(typ='safe', pure=True) with pytest.raises(DuplicateKeyError): yaml.load('{a: 1, a: 2}') # @pytest.mark.xfail(strict=True) def test_duplicate_keys_02(self): + from ruamel.yaml import YAML + from ruamel.yaml.constructor import DuplicateKeyError yaml = YAML(typ='safe') with pytest.raises(DuplicateKeyError): yaml.load('{a: 1, a: 2}') def test_issue_135(self): # reported by Andrzej Ostrowski + from ruamel.yaml import YAML data = {'a': 1, 'b': 2} yaml = YAML(typ='safe') # originally on 2.7: with pytest.raises(TypeError): @@ -41,6 +45,7 @@ class TestNewAPI: def test_issue_135_temporary_workaround(self): # never raised error + from ruamel.yaml import YAML data = {'a': 1, 'b': 2} yaml = YAML(typ='safe', pure=True) yaml.dump(data, sys.stdout) @@ -48,6 +53,7 @@ class TestNewAPI: class TestWrite: def test_dump_path(self, tmpdir): + from ruamel.yaml import YAML fn = Path(str(tmpdir)) / 'test.yaml' yaml = YAML() data = yaml.map() @@ -57,6 +63,7 @@ class TestWrite: assert fn.read_text() == "a: 1\nb: 2\n" def test_dump_file(self, tmpdir): + from ruamel.yaml import YAML fn = Path(str(tmpdir)) / 'test.yaml' yaml = YAML() data = yaml.map() @@ -67,6 +74,7 @@ class TestWrite: assert fn.read_text() == "a: 1\nb: 2\n" def test_dump_missing_stream(self): + from ruamel.yaml import YAML yaml = YAML() data = yaml.map() data['a'] = 1 @@ -75,6 +83,7 @@ class TestWrite: yaml.dump(data) def test_dump_too_many_args(self, tmpdir): + from ruamel.yaml import YAML fn = Path(str(tmpdir)) / 'test.yaml' yaml = YAML() data = yaml.map() @@ -84,6 +93,8 @@ class TestWrite: yaml.dump(data, fn, True) def test_transform(self, tmpdir): + from ruamel.yaml import YAML + def tr(s): return s.replace(' ', ' ') @@ -96,6 +107,7 @@ class TestWrite: assert fn.read_text() == "a: 1\nb: 2\n" def test_print(self, capsys): + from ruamel.yaml import YAML yaml = YAML() data = yaml.map() data['a'] = 1 @@ -108,6 +120,7 @@ class TestWrite: class TestRead: def test_multi_load(self): # make sure reader, scanner, parser get reset + from ruamel.yaml import YAML yaml = YAML() yaml.load('a: 1') yaml.load('a: 1') # did not work in 0.15.4 @@ -116,6 +129,7 @@ class TestRead: class TestLoadAll: def test_multi_document_load(self, tmpdir): """this went wrong on 3.7 because of StopIteration, PR 37 and Issue 211""" + from ruamel.yaml import YAML fn = Path(str(tmpdir)) / 'test.yaml' fn.write_text(textwrap.dedent(u"""\ --- @@ -131,6 +145,8 @@ class TestLoadAll: class TestDuplSet: def test_dupl_set_00(self): # round-trip-loader should except + from ruamel.yaml import YAML + from ruamel.yaml.constructor import DuplicateKeyError yaml = YAML() with pytest.raises(DuplicateKeyError): yaml.load(textwrap.dedent("""\ @@ -146,6 +162,7 @@ class TestDumpLoadUnicode: # test triggered by SamH on stackoverflow (https://stackoverflow.com/q/45281596/1307905) # and answer by randomir (https://stackoverflow.com/a/45281922/1307905) def test_write_unicode(self, tmpdir): + from ruamel.yaml import YAML yaml = YAML() text_dict = {"text": u"HELLO_WORLD©"} file_name = str(tmpdir) + '/tstFile.yaml' @@ -153,6 +170,7 @@ class TestDumpLoadUnicode: assert open(file_name, 'rb').read().decode('utf-8') == u'text: HELLO_WORLD©\n' def test_read_unicode(self, tmpdir): + from ruamel.yaml import YAML yaml = YAML() file_name = str(tmpdir) + '/tstFile.yaml' with open(file_name, 'wb') as fp: @@ -164,6 +182,7 @@ class TestDumpLoadUnicode: class TestFlowStyle: def test_flow_style(self, capsys): # https://stackoverflow.com/questions/45791712/ + from ruamel.yaml import YAML yaml = YAML() yaml.default_flow_style = None data = yaml.map() @@ -179,5 +198,7 @@ class TestOldAPI: @pytest.mark.xfail(strict=True) def test_duplicate_keys_02(self): # Issue 165 unicode keys in error/warning + from ruamel.yaml import safe_load + from ruamel.yaml.constructor import DuplicateKeyFutureWarning with pytest.warns(DuplicateKeyFutureWarning): safe_load('type: Doméstica\ntype: International') diff --git a/_test/test_class_register.py b/_test/test_class_register.py index cb3d6f0..3da9fb7 100644 --- a/_test/test_class_register.py +++ b/_test/test_class_register.py @@ -5,7 +5,6 @@ testing of YAML.register_class and @yaml_object """ from roundtrip import YAML -from ruamel.yaml import yaml_object class User0(object): @@ -91,36 +90,18 @@ class TestRegisterClass(object): yaml.dump(d, compare=ys) -yml = YAML() - - -@yaml_object(yml) -class User2(object): - def __init__(self, name, age): - self.name = name - self.age = age - - -@yaml_object(yml) -class User3(object): - yaml_tag = u'!USER' - - def __init__(self, name, age): - self.name = name - self.age = age - - @classmethod - def to_yaml(cls, representer, node): - return representer.represent_scalar(cls.yaml_tag, - u'{.name}-{.age}'.format(node, node)) +class TestDecorator(object): - @classmethod - def from_yaml(cls, constructor, node): - return cls(*node.value.split('-')) + def test_decorator_implicit(self): + from ruamel.yaml import yaml_object + yml = YAML() + @yaml_object(yml) + class User2(object): + def __init__(self, name, age): + self.name = name + self.age = age -class TestDecorator(object): - def test_decorator_implicit(self): ys = ''' - !User2 name: Anthon @@ -130,6 +111,26 @@ class TestDecorator(object): yml.dump(d, compare=ys, unordered_lines=True) def test_decorator_explicit(self): + from ruamel.yaml import yaml_object + yml = YAML() + + @yaml_object(yml) + class User3(object): + yaml_tag = u'!USER' + + def __init__(self, name, age): + self.name = name + self.age = age + + @classmethod + def to_yaml(cls, representer, node): + return representer.represent_scalar(cls.yaml_tag, + u'{.name}-{.age}'.format(node, node)) + + @classmethod + def from_yaml(cls, constructor, node): + return cls(*node.value.split('-')) + ys = ''' - !USER Anthon-18 ''' diff --git a/_test/test_collections.py b/_test/test_collections.py index 638dde5..e70dbd2 100644 --- a/_test/test_collections.py +++ b/_test/test_collections.py @@ -8,8 +8,6 @@ This is now so integrated in Python that it can be mapped to !!omap """ import pytest # NOQA -import ruamel.yaml # NOQA -from collections import OrderedDict from roundtrip import round_trip, dedent, round_trip_load, round_trip_dump # NOQA @@ -17,4 +15,6 @@ from roundtrip import round_trip, dedent, round_trip_load, round_trip_dump # NO class TestOrderedDict: def test_ordereddict(self): + from collections import OrderedDict + import ruamel.yaml # NOQA assert ruamel.yaml.dump(OrderedDict()) == '!!omap []\n' diff --git a/_test/test_comments.py b/_test/test_comments.py index 11e452f..921b193 100644 --- a/_test/test_comments.py +++ b/_test/test_comments.py @@ -11,7 +11,7 @@ roundtrip changes """ import pytest -import ruamel.yaml +import sys from roundtrip import round_trip, dedent, round_trip_load, round_trip_dump @@ -262,6 +262,7 @@ class TestComments: """) def test_dump_utf8(self): + import ruamel.yaml # NOQA x = dedent("""\ ab: - x # comment @@ -275,6 +276,7 @@ class TestComments: assert y == x def test_dump_unicode_utf8(self): + import ruamel.yaml # NOQA x = dedent(u"""\ ab: - x # comment @@ -695,8 +697,8 @@ class TestEmptyLines: class TestUnicodeComments: - @pytest.mark.skipif(ruamel.yaml.compat.UNICODE_SIZE < 4, - reason="unicode not wide enough") + + @pytest.mark.skipif(sys.version_info < (2, 7), reason="wide unicode") def test_issue_55(self): # reported by Haraguroicha Hsu round_trip("""\ name: TEST @@ -821,6 +823,7 @@ a: | class TestBlockScalarWithComments: # issue 99 reported by Colm O'Connor def test_scalar_with_comments(self): + import ruamel.yaml # NOQA for x in ['', '\n', '\n# Another comment\n', '\n\n', '\n\n# abc\n#xyz\n', '\n\n# abc\n#xyz\n', '# abc\n\n#xyz\n', '\n\n # abc\n #xyz\n']: diff --git a/_test/test_copy.py b/_test/test_copy.py index 37f978d..9a6deb0 100644 --- a/_test/test_copy.py +++ b/_test/test_copy.py @@ -7,7 +7,6 @@ Testing copy and deepcopy, instigated by Issue 84 (Peter Amstutz) import copy import pytest # NOQA -import ruamel.yaml # NOQA from roundtrip import dedent, round_trip_load, round_trip_dump diff --git a/_test/test_datetime.py b/_test/test_datetime.py index a33d368..b48d529 100644 --- a/_test/test_datetime.py +++ b/_test/test_datetime.py @@ -21,7 +21,6 @@ Please note that a fraction can only be included if not equal to 0 import copy import pytest # NOQA -import ruamel.yaml # NOQA from roundtrip import round_trip, dedent, round_trip_load, round_trip_dump # NOQA diff --git a/_test/test_documents.py b/_test/test_documents.py index 7cc1083..6da293a 100644 --- a/_test/test_documents.py +++ b/_test/test_documents.py @@ -2,7 +2,6 @@ import pytest # NOQA -from ruamel import yaml from roundtrip import round_trip, round_trip_load_all @@ -16,6 +15,7 @@ class TestDocument: """, explicit_start=True, explicit_end=True) def test_multi_doc_begin_end(self): + from ruamel import yaml docs = list(round_trip_load_all("""\ --- - a @@ -58,6 +58,7 @@ class TestDocument: assert docs == [['a'], ['b']] def test_multi_doc_ends_only_1_1(self): + from ruamel import yaml # this is not ok in 1.1 with pytest.raises(yaml.parser.ParserError): docs = list(round_trip_load_all("""\ diff --git a/_test/test_float.py b/_test/test_float.py index dc39ce9..2bfe098 100644 --- a/_test/test_float.py +++ b/_test/test_float.py @@ -5,7 +5,6 @@ from __future__ import print_function, absolute_import, division, unicode_litera import pytest # NOQA from roundtrip import round_trip, dedent, round_trip_load, round_trip_dump # NOQA -from ruamel.yaml.error import MantissaNoDotYAML1_1Warning # http://yaml.org/type/int.html is where underscores in integers are defined @@ -146,6 +145,7 @@ class TestFloat: assert 3.0517578122e-56 < d < 3.0517578124e-56 def test_yaml_1_1_no_dot(self): + from ruamel.yaml.error import MantissaNoDotYAML1_1Warning with pytest.warns(MantissaNoDotYAML1_1Warning): round_trip_load("""\ %YAML 1.1 diff --git a/_test/test_flowsequencekey.py b/_test/test_flowsequencekey.py index 6f6d7db..8490430 100644 --- a/_test/test_flowsequencekey.py +++ b/_test/test_flowsequencekey.py @@ -6,7 +6,6 @@ test flow style sequences as keys roundtrip """ # import pytest -# import ruamel.yaml from roundtrip import round_trip # , dedent, round_trip_load, round_trip_dump diff --git a/_test/test_indentation.py b/_test/test_indentation.py index 014f89b..528e3b6 100644 --- a/_test/test_indentation.py +++ b/_test/test_indentation.py @@ -7,13 +7,11 @@ from __future__ import unicode_literals import pytest # NOQA -import ruamel.yaml -from ruamel.yaml.util import load_yaml_guess_indent - from roundtrip import round_trip, round_trip_load, round_trip_dump, dedent, YAML def rt(s): + import ruamel.yaml return ruamel.yaml.dump( ruamel.yaml.load(s, Loader=ruamel.yaml.RoundTripLoader), Dumper=ruamel.yaml.RoundTripDumper, @@ -55,6 +53,7 @@ class TestIndent: # first test by explicitly setting flow style def test_added_inline_list(self): + import ruamel.yaml s1 = dedent(""" a: - b @@ -72,6 +71,7 @@ class TestIndent: # ############ flow mappings def test_roundtrip_flow_mapping(self): + import ruamel.yaml s = dedent("""\ - {a: 1, b: hallo} - {j: fka, k: 42} @@ -209,6 +209,7 @@ class TestYpkgIndent: def guess(s): + from ruamel.yaml.util import load_yaml_guess_indent x, y, z = load_yaml_guess_indent(dedent(s)) return y, z diff --git a/_test/test_issues.py b/_test/test_issues.py index 3260fbf..0e01875 100644 --- a/_test/test_issues.py +++ b/_test/test_issues.py @@ -5,13 +5,13 @@ from __future__ import absolute_import, print_function, unicode_literals import pytest # NOQA -import ruamel.yaml from roundtrip import round_trip, round_trip_load, round_trip_dump, dedent # NOQA class TestIssue61: def test_issue_61(self): + import ruamel.yaml s = dedent(""" def1: &ANCHOR1 key1: value1 diff --git a/_test/test_json_numbers.py b/_test/test_json_numbers.py index b41c350..a580277 100644 --- a/_test/test_json_numbers.py +++ b/_test/test_json_numbers.py @@ -4,11 +4,11 @@ from __future__ import print_function import pytest # NOQA -import ruamel.yaml import json def load(s, typ=float): + import ruamel.yaml x = '{"low": %s }' % (s) print('input: [%s]' % (s), repr(x)) # just to check it is loadable json diff --git a/_test/test_none.py b/_test/test_none.py index ad51109..681f1e0 100644 --- a/_test/test_none.py +++ b/_test/test_none.py @@ -2,11 +2,11 @@ import pytest # NOQA -import ruamel.yaml # NOQA class TestNone: def test_dump00(self): + import ruamel.yaml # NOQA data = None s = ruamel.yaml.round_trip_dump(data) assert s == 'null\n...\n' @@ -14,6 +14,7 @@ class TestNone: assert d == data def test_dump01(self): + import ruamel.yaml # NOQA data = None s = ruamel.yaml.round_trip_dump(data, explicit_end=True) assert s == 'null\n...\n' @@ -21,6 +22,7 @@ class TestNone: assert d == data def test_dump02(self): + import ruamel.yaml # NOQA data = None s = ruamel.yaml.round_trip_dump(data, explicit_end=False) assert s == 'null\n...\n' @@ -28,6 +30,7 @@ class TestNone: assert d == data def test_dump03(self): + import ruamel.yaml # NOQA data = None s = ruamel.yaml.round_trip_dump(data, explicit_start=True) assert s == '---\n...\n' @@ -35,6 +38,7 @@ class TestNone: assert d == data def test_dump04(self): + import ruamel.yaml # NOQA data = None s = ruamel.yaml.round_trip_dump(data, explicit_start=True, explicit_end=False) assert s == '---\n...\n' diff --git a/_test/test_numpy.py b/_test/test_numpy.py index 6a096af..e3a7718 100644 --- a/_test/test_numpy.py +++ b/_test/test_numpy.py @@ -7,10 +7,9 @@ try: except: # NOQA numpy = None -import ruamel.yaml - def Xtest_numpy(): + import ruamel.yaml if numpy is None: return data = numpy.arange(10) diff --git a/_test/test_string.py b/_test/test_string.py index 6b3070f..f095095 100644 --- a/_test/test_string.py +++ b/_test/test_string.py @@ -17,7 +17,6 @@ and the chomping modifiers: import pytest import platform -import ruamel # from ruamel.yaml.compat import ordereddict from roundtrip import round_trip, dedent, round_trip_load, round_trip_dump # NOQA @@ -130,6 +129,7 @@ class TestQuotedScalarString: class TestReplace: """inspired by issue 110 from sandres23""" def test_replace_preserved_scalar_string(self): + import ruamel s = dedent("""\ foo: | foo @@ -148,6 +148,7 @@ class TestReplace: """) def test_replace_double_quoted_scalar_string(self): + import ruamel s = dedent("""\ foo: "foo foo bar foo" """) diff --git a/_test/test_tag.py b/_test/test_tag.py index 1aba5ab..c4fabc0 100644 --- a/_test/test_tag.py +++ b/_test/test_tag.py @@ -2,24 +2,25 @@ import pytest # NOQA -from ruamel import yaml from roundtrip import round_trip, round_trip_load -class XXX(yaml.comments.CommentedMap): - @staticmethod - def yaml_dump(dumper, data): - return dumper.represent_mapping(u'!xxx', data) +def register_xxx(**kw): + from ruamel import yaml - @classmethod - def yaml_load(cls, constructor, node): - data = cls() - yield data - constructor.construct_mapping(node, data) + class XXX(yaml.comments.CommentedMap): + @staticmethod + def yaml_dump(dumper, data): + return dumper.represent_mapping(u'!xxx', data) + @classmethod + def yaml_load(cls, constructor, node): + data = cls() + yield data + constructor.construct_mapping(node, data) -yaml.add_constructor(u'!xxx', XXX.yaml_load, constructor=yaml.RoundTripConstructor) -yaml.add_representer(XXX, XXX.yaml_dump, representer=yaml.RoundTripRepresenter) + yaml.add_constructor(u'!xxx', XXX.yaml_load, constructor=yaml.RoundTripConstructor) + yaml.add_representer(XXX, XXX.yaml_dump, representer=yaml.RoundTripRepresenter) class TestIndentFailures: @@ -66,6 +67,7 @@ class TestIndentFailures: class TestRoundTripCustom: def test_X1(self): + register_xxx() round_trip("""\ !xxx name: Anthon @@ -75,6 +77,7 @@ class TestRoundTripCustom: @pytest.mark.xfail(strict=True) def test_X_pre_tag_comment(self): + register_xxx() round_trip("""\ - # hello @@ -86,6 +89,7 @@ class TestRoundTripCustom: @pytest.mark.xfail(strict=True) def test_X_post_tag_comment(self): + register_xxx() round_trip("""\ - !xxx # hello diff --git a/_test/test_version.py b/_test/test_version.py index 4aad308..0855a5c 100644 --- a/_test/test_version.py +++ b/_test/test_version.py @@ -2,11 +2,11 @@ import pytest # NOQA -import ruamel.yaml from roundtrip import dedent, round_trip, round_trip_load def load(s, version=None): + import ruamel.yaml # NOQA return ruamel.yaml.round_trip_load(dedent(s), version) @@ -111,6 +111,7 @@ class TestVersions: class TestIssue62: # bitbucket issue 62, issue_62 def test_00(self): + import ruamel.yaml # NOQA s = dedent("""\ {}# Outside flow collection: - ::vector @@ -126,6 +127,7 @@ class TestIssue62: round_trip(s.format(''), preserve_quotes=True) def test_00_single_comment(self): + import ruamel.yaml # NOQA s = dedent("""\ {}# Outside flow collection: - ::vector @@ -141,6 +143,7 @@ class TestIssue62: # round_trip(s.format('%YAML 1.2\n---\n'), preserve_quotes=True, version=(1, 2)) def test_01(self): + import ruamel.yaml # NOQA s = dedent("""\ {}[random plain value that contains a ? character] """) diff --git a/_test/test_yamlfile.py b/_test/test_yamlfile.py index e7e31ca..d1ba63c 100644 --- a/_test/test_yamlfile.py +++ b/_test/test_yamlfile.py @@ -9,8 +9,6 @@ import sys import pytest # NOQA import platform -import ruamel.yaml -from ruamel.yaml.compat import ordereddict from roundtrip import round_trip, dedent, round_trip_load, round_trip_dump # NOQA @@ -23,6 +21,8 @@ class TestYAML: def test_omap_out(self): # ordereddict mapped to !!omap + from ruamel.yaml.compat import ordereddict + import ruamel.yaml # NOQA x = ordereddict([('a', 1), ('b', 2)]) res = ruamel.yaml.dump(x, default_flow_style=False) assert res == dedent(""" @@ -43,6 +43,7 @@ class TestYAML: @pytest.mark.skipif(sys.version_info < (2, 7), reason="collections not available") def test_dump_collections_ordereddict(self): from collections import OrderedDict + import ruamel.yaml # NOQA # OrderedDict mapped to !!omap x = OrderedDict([('a', 1), ('b', 2)]) res = ruamel.yaml.dump(x, @@ -59,6 +60,7 @@ class TestYAML: reason="ruamel.yaml not available") def test_dump_ruamel_ordereddict(self): from ruamel.ordereddict import ordereddict + import ruamel.yaml # NOQA # OrderedDict mapped to !!omap x = ordereddict([('a', 1), ('b', 2)]) res = ruamel.yaml.dump(x, @@ -83,7 +85,7 @@ class TestYAML: def test_set_out(self): # preferable would be the shorter format without the ': null' - # from ruamel.yaml.compat import ordereddict + import ruamel.yaml # NOQA x = set(['a', 'b', 'c']) res = ruamel.yaml.dump(x, default_flow_style=False) assert res == dedent(""" @@ -194,6 +196,7 @@ class TestYAML: assert d['c'][1].split('line.')[1] == '' def test_load_all_perserve_quotes(self): + import ruamel.yaml # NOQA s = dedent("""\ a: 'hello' --- diff --git a/_test/test_yamlobject.py b/_test/test_yamlobject.py index a5e06d8..ee39c89 100644 --- a/_test/test_yamlobject.py +++ b/_test/test_yamlobject.py @@ -1,26 +1,30 @@ # coding: utf-8 import pytest # NOQA -import ruamel.yaml from roundtrip import round_trip, dedent, round_trip_load, round_trip_dump # NOQA -class Monster(ruamel.yaml.YAMLObject): - yaml_tag = u'!Monster' +def register_monster(): + import ruamel.yaml # NOQA - def __init__(self, name, hp, ac, attacks): - self.name = name - self.hp = hp - self.ac = ac - self.attacks = attacks + class Monster(ruamel.yaml.YAMLObject): + yaml_tag = u'!Monster' - def __repr__(self): - return "%s(name=%r, hp=%r, ac=%r, attacks=%r)" % ( - self.__class__.__name__, self.name, self.hp, self.ac, self.attacks) + def __init__(self, name, hp, ac, attacks): + self.name = name + self.hp = hp + self.ac = ac + self.attacks = attacks + + def __repr__(self): + return "%s(name=%r, hp=%r, ac=%r, attacks=%r)" % ( + self.__class__.__name__, self.name, self.hp, self.ac, self.attacks) def test_monster(): + import ruamel.yaml # NOQA + register_monster() data = ruamel.yaml.load(dedent("""\ --- !Monster name: Cave spider diff --git a/_test/test_z_data.py b/_test/test_z_data.py index d85ba28..9ea921b 100644 --- a/_test/test_z_data.py +++ b/_test/test_z_data.py @@ -9,16 +9,14 @@ import platform # NOQA sys.path.insert(0, os.path.dirname(__file__) + '/lib') -import ruamel.yaml # NOQA -import test_appliance # NOQA import warnings # NOQA -warnings.simplefilter('ignore', ruamel.yaml.error.UnsafeLoaderWarning) args = [] def test_data(): + import test_appliance # NOQA collections = [] import test_yaml collections.append(test_yaml) @@ -30,6 +28,9 @@ def test_data(): def test_data_ext(): collections = [] + import ruamel.yaml # NOQA + import test_appliance # NOQA + warnings.simplefilter('ignore', ruamel.yaml.error.UnsafeLoaderWarning) if ruamel.yaml.__with_libyaml__: import test_yaml_ext collections.append(test_yaml_ext) diff --git a/comments.py b/comments.py index afbcbf0..98c80c7 100644 --- a/comments.py +++ b/comments.py @@ -11,11 +11,15 @@ a separate base import sys import copy -from collections import MutableSet, Sized, Set from ruamel.yaml.compat import ordereddict, PY2, string_types from ruamel.yaml.scalarstring import ScalarString +if PY2: + from collections import MutableSet, Sized, Set +else: + from collections.abc import MutableSet, Sized, Set + if False: # MYPY from typing import Any, Dict, Optional, List, Union # NOQA @@ -83,6 +83,7 @@ if PY3: BytesIO = io.BytesIO # have unlimited precision no_limit_int = int + from collections.abc import Hashable # NOQA else: string_types = basestring # NOQA @@ -99,6 +100,7 @@ else: BytesIO = cStringIO.StringIO # have unlimited precision no_limit_int = long # NOQA not available on Python 3 + from collections import Hashable # NOQA if False: # MYPY # StreamType = Union[BinaryIO, IO[str], IO[unicode], StringIO] diff --git a/constructor.py b/constructor.py index e5126f4..c752b73 100644 --- a/constructor.py +++ b/constructor.py @@ -2,7 +2,6 @@ from __future__ import print_function, absolute_import, division -import collections import datetime import base64 import binascii @@ -16,7 +15,7 @@ from ruamel.yaml.error import (MarkedYAMLError, MarkedYAMLFutureWarning, from ruamel.yaml.nodes import * # NOQA from ruamel.yaml.nodes import (SequenceNode, MappingNode, ScalarNode) from ruamel.yaml.compat import (utf8, builtins_module, to_str, PY2, PY3, # NOQA - ordereddict, text_type, nprint, version_tnf) + ordereddict, text_type, nprint, version_tnf, Hashable) from ruamel.yaml.comments import * # NOQA from ruamel.yaml.comments import (CommentedMap, CommentedOrderedMap, CommentedSet, CommentedKeySeq, CommentedSeq, TaggedScalar) @@ -217,7 +216,7 @@ class BaseConstructor(object): # keys can be list -> deep key = self.construct_object(key_node, deep=True) # lists are not hashable, but tuples are - if not isinstance(key, collections.Hashable): + if not isinstance(key, Hashable): if isinstance(key, list): key = tuple(key) if PY2: @@ -229,7 +228,7 @@ class BaseConstructor(object): "found unacceptable key (%s)" % exc, key_node.start_mark) else: - if not isinstance(key, collections.Hashable): + if not isinstance(key, Hashable): raise ConstructorError( "while constructing a mapping", node.start_mark, "found unhashable key", key_node.start_mark) @@ -1240,7 +1239,7 @@ class RoundTripConstructor(SafeConstructor): # keys can be list -> deep key = self.construct_object(key_node, deep=True) # lists are not hashable, but tuples are - if not isinstance(key, collections.Hashable): + if not isinstance(key, Hashable): if isinstance(key, list): key_a = CommentedKeySeq(key) if key_node.flow_style is True: @@ -1258,7 +1257,7 @@ class RoundTripConstructor(SafeConstructor): "found unacceptable key (%s)" % exc, key_node.start_mark) else: - if not isinstance(key, collections.Hashable): + if not isinstance(key, Hashable): raise ConstructorError( "while constructing a mapping", node.start_mark, "found unhashable key", key_node.start_mark) @@ -1307,7 +1306,7 @@ class RoundTripConstructor(SafeConstructor): # keys can be list -> deep key = self.construct_object(key_node, deep=True) # lists are not hashable, but tuples are - if not isinstance(key, collections.Hashable): + if not isinstance(key, Hashable): if isinstance(key, list): key = tuple(key) if PY2: @@ -1319,7 +1318,7 @@ class RoundTripConstructor(SafeConstructor): "found unacceptable key (%s)" % exc, key_node.start_mark) else: - if not isinstance(key, collections.Hashable): + if not isinstance(key, Hashable): raise ConstructorError( "while constructing a mapping", node.start_mark, "found unhashable key", key_node.start_mark) @@ -1,11 +1,10 @@ [tox] -# toworkdir = /data1/DATA/tox/ruamel.yaml -envlist = cs,py36,py27,py35,py34,pypy,py27m +toworkdir = /data1/DATA/tox/ruamel.yaml +envlist = cs,py37,py27,py36,py35,py34,pypy,py27m [testenv] commands = - python -c "import sys, sysconfig; print('%s ucs-%s' % (sys.version.replace('\n', ' '), sysconfig.get_config_var('Py_UNICODE_SIZE')))" - /bin/bash -c 'pytest _test/test_*.py' + /bin/bash -c 'pytest -ra _test/test_*.py' deps = pytest flake8==2.5.5 @@ -19,6 +18,19 @@ basepython = python3.6 commands = flake8 --exclude .tox,jabsy,jinja2,base,cmd,convert{posargs} +# deprecation warning +[testenv:dw] +basepython = python3.7 +commands = + /bin/bash -c 'pytest _test/test_a*.py _test/test_z*.py' + +# deprecation warning fail +[testenv:dwf] +basepython = python3.7 +commands = + /bin/sed 's/collections.abc/collections/' -i .tox/dwf/lib/python3.7/site-packages/ruamel/yaml/comments.py + /bin/bash -c 'pytest --maxfail=2 _test/test_[a-vz]*.py' + [testenv:pep8] basepython = python3.6 commands = @@ -28,3 +40,8 @@ commands = show-source = True max-line-length = 95 exclude = _test/lib,.hg,.git,.tox,dist,.cache,__pycache__,ruamel.zip2tar.egg-info + +[pytest] +filterwarnings = + error::DeprecationWarning + error::PendingDeprecationWarning |