summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile2
-rw-r--r--compat.py5
-rw-r--r--configobjwalker.py4
-rw-r--r--constructor.py37
-rw-r--r--main.py36
-rw-r--r--reader.py4
-rw-r--r--resolver.py1
-rw-r--r--util.py3
8 files changed, 68 insertions, 24 deletions
diff --git a/Makefile b/Makefile
index 770f63e..27b7895 100644
--- a/Makefile
+++ b/Makefile
@@ -28,7 +28,7 @@ mypy:
cd ..; mypy --strict --no-warn-unused-ignores yaml/*.py
mypy2:
- cd ..; mypy --py2 --strict --no-strict-boolean --no-warn-unused-ignores yaml/*.py
+ cd ../.. ; mypy --py2 --strict --no-strict-boolean --no-warn-unused-ignores ruamel/yaml/*.py
#tstvenv: testvenv testsetup testtest
#
diff --git a/compat.py b/compat.py
index d9fc76c..d3529d4 100644
--- a/compat.py
+++ b/compat.py
@@ -8,7 +8,7 @@ import sys
import os
import types
-from typing import Any, Dict, Optional, List, Union # NOQA
+from typing import Any, Dict, Optional, List, Union, BinaryIO, IO, Text # NOQA
try:
@@ -94,6 +94,9 @@ else:
import cStringIO
BytesIO = cStringIO.StringIO
+StreamType = Union[BinaryIO, IO[str], StringIO]
+StreamTextType = Union[Text, StreamType]
+
if PY3:
builtins_module = 'builtins'
else:
diff --git a/configobjwalker.py b/configobjwalker.py
index a6eb54c..f1005a0 100644
--- a/configobjwalker.py
+++ b/configobjwalker.py
@@ -1,9 +1,13 @@
# coding: utf-8
import warnings
+
+from typing import Any # NOQA
+
from ruamel.yaml.util import configobj_walker as new_configobj_walker
def configobj_walker(cfg):
+ # type: (Any) -> Any
warnings.warn("configobj_walker has moved to ruamel.yaml.util, please update your code")
return new_configobj_walker(cfg)
diff --git a/constructor.py b/constructor.py
index d425bae..2bcb3bd 100644
--- a/constructor.py
+++ b/constructor.py
@@ -10,9 +10,10 @@ import re
import sys
import types
-from typing import Any, Dict # NOQA
+from typing import Any, Dict, List # NOQA
-from ruamel.yaml.error import * # NOQA
+
+from ruamel.yaml.error import (MarkedYAMLError)
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
@@ -39,10 +40,10 @@ class BaseConstructor(object):
yaml_multi_constructors = {} # type: Dict[Any, Any]
def __init__(self, preserve_quotes=None):
- # type: (Any) -> None
- self.constructed_objects = {}
- self.recursive_objects = {}
- self.state_generators = []
+ # type: (bool) -> None
+ self.constructed_objects = {} # type: Dict[Any, Any]
+ self.recursive_objects = {} # type: Dict[Any, Any]
+ self.state_generators = [] # type: List[Any]
self.deep_construct = False
self._preserve_quotes = preserve_quotes
@@ -430,7 +431,8 @@ class SafeConstructor(BaseConstructor):
delta = None
if values['tz_sign']:
tz_hour = int(values['tz_hour'])
- tz_minute = int(values['tz_minute'] or 0)
+ minutes = values['tz_minute']
+ tz_minute = int(minutes) if minutes else 0
delta = datetime.timedelta(hours=tz_hour, minutes=tz_minute)
if values['tz_sign'] == '-':
delta = -delta
@@ -471,7 +473,7 @@ class SafeConstructor(BaseConstructor):
def construct_yaml_pairs(self, node):
# type: (Any) -> Any
# Note: the same code as `construct_yaml_omap`.
- pairs = []
+ pairs = [] # type: List[Any]
yield pairs
if not isinstance(node, SequenceNode):
raise ConstructorError(
@@ -900,7 +902,7 @@ class RoundTripConstructor(SafeConstructor):
except UnicodeEncodeError:
return value
- def construct_sequence(self, node, seqtyp, deep=False):
+ def construct_rt_sequence(self, node, seqtyp, deep=False):
# type: (Any, Any, bool) -> Any
if not isinstance(node, SequenceNode):
raise ConstructorError(
@@ -933,6 +935,7 @@ class RoundTripConstructor(SafeConstructor):
"""
def constructed(value_node):
+ # type: (Any) -> Any
# If the contents of a merge are defined within the
# merge marker, then they won't have been constructed
# yet. But if they were already constructed, we need to use
@@ -1014,11 +1017,12 @@ class RoundTripConstructor(SafeConstructor):
# lists are not hashable, but tuples are
if not isinstance(key, collections.Hashable):
if isinstance(key, list):
- key = CommentedKeySeq(key)
+ key_a = CommentedKeySeq(key)
if key_node.flow_style is True:
- key.fa.set_flow_style()
+ key_a.fa.set_flow_style()
elif key_node.flow_style is False:
- key.fa.set_block_style()
+ key_a.fa.set_block_style()
+ key = key_a
# key = tuple(key)
if PY2:
try:
@@ -1110,7 +1114,7 @@ class RoundTripConstructor(SafeConstructor):
if node.comment:
data._yaml_add_comment(node.comment)
yield data
- data.extend(self.construct_sequence(node, data))
+ data.extend(self.construct_rt_sequence(node, data))
def construct_yaml_map(self, node):
# type: (Any) -> Any
@@ -1192,8 +1196,8 @@ class RoundTripConstructor(SafeConstructor):
utf8(node.tag),
node.start_mark)
- def construct_yaml_timestamp(self, node):
- # type: (Any) -> Any
+ def construct_yaml_timestamp(self, node, values=None):
+ # type: (Any, Any) -> Any
match = self.timestamp_regexp.match(node.value)
values = match.groupdict()
if not values['hour']:
@@ -1220,7 +1224,8 @@ class RoundTripConstructor(SafeConstructor):
delta = None
if values['tz_sign']:
tz_hour = int(values['tz_hour'])
- tz_minute = int(values['tz_minute'] or 0)
+ minutes = values['tz_minute']
+ tz_minute = int(minutes) if minutes else 0
delta = datetime.timedelta(hours=tz_hour, minutes=tz_minute)
if values['tz_sign'] == '-':
delta = -delta
diff --git a/main.py b/main.py
index 0946899..f4768f5 100644
--- a/main.py
+++ b/main.py
@@ -14,14 +14,15 @@ from ruamel.yaml.nodes import * # NOQA
from ruamel.yaml.loader import BaseLoader, SafeLoader, Loader, RoundTripLoader # NOQA
from ruamel.yaml.dumper import BaseDumper, SafeDumper, Dumper, RoundTripDumper # NOQA
from ruamel.yaml.compat import StringIO, BytesIO, with_metaclass, PY3
+from ruamel.yaml.compat import StreamType, StreamTextType # NOQA
# import io
VersionType = Union[List[int], str, Tuple[int, int]]
-StreamType = Union[BinaryIO, IO[str], StringIO]
def scan(stream, Loader=Loader):
+ # type: (StreamTextType, Any) -> Any
"""
Scan a YAML stream and produce scanning tokens.
"""
@@ -34,6 +35,7 @@ def scan(stream, Loader=Loader):
def parse(stream, Loader=Loader):
+ # type: (StreamTextType, Any) -> Any
"""
Parse a YAML stream and produce parsing events.
"""
@@ -46,6 +48,7 @@ def parse(stream, Loader=Loader):
def compose(stream, Loader=Loader):
+ # type: (StreamTextType, Any) -> Any
"""
Parse the first YAML document in a stream
and produce the corresponding representation tree.
@@ -58,6 +61,7 @@ def compose(stream, Loader=Loader):
def compose_all(stream, Loader=Loader):
+ # type: (StreamTextType, Any) -> Any
"""
Parse all YAML documents in a stream
and produce corresponding representation trees.
@@ -71,7 +75,7 @@ def compose_all(stream, Loader=Loader):
def load(stream, Loader=None, version=None, preserve_quotes=None):
- # type: (StreamType, Any, VersionType, Any) -> Any
+ # type: (StreamTextType, Any, VersionType, Any) -> Any
"""
Parse the first YAML document in a stream
and produce the corresponding Python object.
@@ -89,6 +93,7 @@ def load(stream, Loader=None, version=None, preserve_quotes=None):
def load_all(stream, Loader=None, version=None, preserve_quotes=None):
+ # type: (StreamTextType, Any, VersionType, bool) -> Any
"""
Parse all YAML documents in a stream
and produce corresponding Python objects.
@@ -107,6 +112,7 @@ def load_all(stream, Loader=None, version=None, preserve_quotes=None):
def safe_load(stream, version=None):
+ # type: (StreamTextType, VersionType) -> Any
"""
Parse the first YAML document in a stream
and produce the corresponding Python object.
@@ -116,6 +122,7 @@ def safe_load(stream, version=None):
def safe_load_all(stream, version=None):
+ # type: (StreamTextType, VersionType) -> Any
"""
Parse all YAML documents in a stream
and produce corresponding Python objects.
@@ -125,7 +132,7 @@ def safe_load_all(stream, version=None):
def round_trip_load(stream, version=None, preserve_quotes=None):
- # type: (Any, Any, bool) -> Any
+ # type: (StreamTextType, VersionType, bool) -> Any
"""
Parse the first YAML document in a stream
and produce the corresponding Python object.
@@ -135,6 +142,7 @@ def round_trip_load(stream, version=None, preserve_quotes=None):
def round_trip_load_all(stream, version=None, preserve_quotes=None):
+ # type: (StreamTextType, VersionType, bool) -> Any
"""
Parse all YAML documents in a stream
and produce corresponding Python objects.
@@ -146,6 +154,7 @@ def round_trip_load_all(stream, version=None, preserve_quotes=None):
def emit(events, stream=None, Dumper=Dumper,
canonical=None, indent=None, width=None,
allow_unicode=None, line_break=None):
+ # type: (Any, StreamType, Any, bool, Union[int, None], int, bool, Any) -> Any
"""
Emit YAML parsing events into a stream.
If stream is None, return the produced string instead.
@@ -172,6 +181,7 @@ def serialize_all(nodes, stream=None, Dumper=Dumper,
allow_unicode=None, line_break=None,
encoding=enc, explicit_start=None, explicit_end=None,
version=None, tags=None):
+ # type: (Any, StreamType, Any, Any, Union[None, int], Union[None, int], bool, Any, Any, Union[None, bool], Union[None, bool], VersionType, Any) -> Any # NOQA
"""
Serialize a sequence of representation trees into a YAML stream.
If stream is None, return the produced string instead.
@@ -199,6 +209,7 @@ def serialize_all(nodes, stream=None, Dumper=Dumper,
def serialize(node, stream=None, Dumper=Dumper, **kwds):
+ # type: (Any, StreamType, Any, Any) -> Any
"""
Serialize a representation tree into a YAML stream.
If stream is None, return the produced string instead.
@@ -213,6 +224,7 @@ def dump_all(documents, stream=None, Dumper=Dumper,
encoding=enc, explicit_start=None, explicit_end=None,
version=None, tags=None, block_seq_indent=None,
top_level_colon_align=None, prefix_colon=None):
+ # type: (Any, StreamType, Any, Any, Any, bool, Union[None, int], Union[None, int], bool, Any, Any, Union[None, bool], Union[None, bool], Any, Any, Any, Any, Any) -> Union[None, str] # NOQA
"""
Serialize a sequence of Python objects into a YAML stream.
If stream is None, return the produced string instead.
@@ -244,6 +256,7 @@ def dump_all(documents, stream=None, Dumper=Dumper,
dumper.dispose()
if getvalue:
return getvalue()
+ return None
def dump(data, stream=None, Dumper=Dumper,
@@ -252,6 +265,7 @@ def dump(data, stream=None, Dumper=Dumper,
allow_unicode=None, line_break=None,
encoding=enc, explicit_start=None, explicit_end=None,
version=None, tags=None, block_seq_indent=None):
+ # type: (Any, StreamType, Any, Any, Any, bool, Union[None, int], Union[None, int], bool, Any, Any, Union[None, bool], Union[None, bool], VersionType, Any, Any) -> Union[None, str] # NOQA
"""
Serialize a Python object into a YAML stream.
If stream is None, return the produced string instead.
@@ -272,6 +286,7 @@ def dump(data, stream=None, Dumper=Dumper,
def safe_dump_all(documents, stream=None, **kwds):
+ # type: (Any, StreamType, Any) -> Union[None, str]
"""
Serialize a sequence of Python objects into a YAML stream.
Produce only basic YAML tags.
@@ -281,6 +296,7 @@ def safe_dump_all(documents, stream=None, **kwds):
def safe_dump(data, stream=None, **kwds):
+ # type: (Any, StreamType, Any) -> Union[None, str]
"""
Serialize a Python object into a YAML stream.
Produce only basic YAML tags.
@@ -296,6 +312,7 @@ def round_trip_dump(data, stream=None, Dumper=RoundTripDumper,
encoding=enc, explicit_start=None, explicit_end=None,
version=None, tags=None, block_seq_indent=None,
top_level_colon_align=None, prefix_colon=None):
+ # type: (Any, StreamType, Any, Any, Any, bool, Union[None, int], Union[None, int], bool, Any, Any, Union[None, bool], Union[None, bool], VersionType, Any, Any, Any, Any) -> Union[None, str] # NOQA
allow_unicode = True if allow_unicode is None else allow_unicode
return dump_all([data], stream, Dumper=Dumper,
default_style=default_style,
@@ -312,6 +329,7 @@ def round_trip_dump(data, stream=None, Dumper=RoundTripDumper,
def add_implicit_resolver(tag, regexp, first=None,
Loader=Loader, Dumper=Dumper):
+ # type: (Any, Any, Any, Any, Any) -> None
"""
Add an implicit scalar detector.
If an implicit scalar value matches the given regexp,
@@ -323,6 +341,7 @@ def add_implicit_resolver(tag, regexp, first=None,
def add_path_resolver(tag, path, kind=None, Loader=Loader, Dumper=Dumper):
+ # type: (Any, Any, Any, Any, Any) -> None
"""
Add a path based resolver for the given tag.
A path is a list of keys that forms a path
@@ -334,6 +353,7 @@ def add_path_resolver(tag, path, kind=None, Loader=Loader, Dumper=Dumper):
def add_constructor(tag, constructor, Loader=Loader):
+ # type: (Any, Any, Any) -> None
"""
Add a constructor for the given tag.
Constructor is a function that accepts a Loader instance
@@ -343,6 +363,7 @@ def add_constructor(tag, constructor, Loader=Loader):
def add_multi_constructor(tag_prefix, multi_constructor, Loader=Loader):
+ # type: (Any, Any, Any) -> None
"""
Add a multi-constructor for the given tag prefix.
Multi-constructor is called for a node if its tag starts with tag_prefix.
@@ -353,6 +374,7 @@ def add_multi_constructor(tag_prefix, multi_constructor, Loader=Loader):
def add_representer(data_type, representer, Dumper=Dumper):
+ # type: (Any, Any, Any) -> None
"""
Add a representer for the given type.
Representer is a function accepting a Dumper instance
@@ -363,6 +385,7 @@ def add_representer(data_type, representer, Dumper=Dumper):
def add_multi_representer(data_type, multi_representer, Dumper=Dumper):
+ # type: (Any, Any, Any) -> None
"""
Add a representer for the given type.
Multi-representer is a function accepting a Dumper instance
@@ -377,10 +400,11 @@ class YAMLObjectMetaclass(type):
The metaclass for YAMLObject.
"""
def __init__(cls, name, bases, kwds):
+ # type: (Any, Any, Any) -> None
super(YAMLObjectMetaclass, cls).__init__(name, bases, kwds)
if 'yaml_tag' in kwds and kwds['yaml_tag'] is not None:
- cls.yaml_loader.add_constructor(cls.yaml_tag, cls.from_yaml)
- cls.yaml_dumper.add_representer(cls, cls.to_yaml)
+ cls.yaml_loader.add_constructor(cls.yaml_tag, cls.from_yaml) # type: ignore
+ cls.yaml_dumper.add_representer(cls, cls.to_yaml) # type: ignore
class YAMLObject(with_metaclass(YAMLObjectMetaclass)): # type: ignore
@@ -398,6 +422,7 @@ class YAMLObject(with_metaclass(YAMLObjectMetaclass)): # type: ignore
@classmethod
def from_yaml(cls, loader, node):
+ # type: (Any, Any) -> Any
"""
Convert a representation node to a Python object.
"""
@@ -405,6 +430,7 @@ class YAMLObject(with_metaclass(YAMLObjectMetaclass)): # type: ignore
@classmethod
def to_yaml(cls, dumper, data):
+ # type: (Any, Any) -> Any
"""
Convert a Python object to a representation node.
"""
diff --git a/reader.py b/reader.py
index 63307b7..a7f0c37 100644
--- a/reader.py
+++ b/reader.py
@@ -23,6 +23,8 @@ from __future__ import absolute_import
import codecs
import re
+from typing import Any, Dict, Optional, List # NOQA
+
from ruamel.yaml.error import YAMLError, FileMark, StringMark
from ruamel.yaml.compat import text_type, binary_type, PY3
@@ -32,6 +34,7 @@ __all__ = ['Reader', 'ReaderError']
class ReaderError(YAMLError):
def __init__(self, name, position, character, encoding, reason):
+ # type: (Any, Any, Any, Any, Any) -> None
self.name = name
self.character = character
self.position = position
@@ -39,6 +42,7 @@ class ReaderError(YAMLError):
self.reason = reason
def __str__(self):
+ # type () -> str
if isinstance(self.character, binary_type):
return "'%s' codec can't decode byte #x%02x: %s\n" \
" in \"%s\", position %d" \
diff --git a/resolver.py b/resolver.py
index 9902a29..38546ac 100644
--- a/resolver.py
+++ b/resolver.py
@@ -101,6 +101,7 @@ class BaseResolver(object):
yaml_path_resolvers = {} # type: Dict[Any, Any]
def __init__(self):
+ # type: () -> None
self._loader_version = None
self.resolver_exact_paths = []
self.resolver_prefix_paths = []
diff --git a/util.py b/util.py
index 40aed9e..bb061ce 100644
--- a/util.py
+++ b/util.py
@@ -9,6 +9,7 @@ from __future__ import absolute_import, print_function
from typing import Any, Dict, Optional, List # NOQA
from .compat import text_type, binary_type
+from .compat import StreamTextType, StringIO # NOQA
# originally as comment
@@ -17,7 +18,7 @@ from .compat import text_type, binary_type
# that check this routines output against a known piece of your YAML
# before upgrades to this code break your round-tripped YAML
def load_yaml_guess_indent(stream, **kw):
- # type: (Any, Any) -> Any
+ # type: (StreamTextType, Any) -> Any
"""guess the indent and block sequence indent of yaml stream/string
returns round_trip_loaded stream, indent level, block sequence indent