From ea3d878ef8635120354cd59a1f31b97b05a3e09b Mon Sep 17 00:00:00 2001 From: Anthon van der Neut Date: Mon, 31 May 2021 08:36:28 +0200 Subject: fix 385 (missing attribute) and mypy issues --- CHANGES | 7 +++++ README.rst | 13 +++++++--- __init__.py | 4 +-- _doc/_static/pypi.svg | 2 +- comments.py | 25 +++++++++++------- compat.py | 41 ++++++++++++++--------------- composer.py | 1 + constructor.py | 18 ++++++------- emitter.py | 12 ++++----- events.py | 8 ++++-- loader.py | 1 + main.py | 28 +++++++++++--------- nodes.py | 2 +- parser.py | 22 ++++++++++------ reader.py | 4 +-- representer.py | 7 ++--- resolver.py | 2 +- scanner.py | 71 +++++++++++++++++++++++++++++++++++---------------- timestamp.py | 1 + tokens.py | 27 ++++++++++++-------- util.py | 2 +- 21 files changed, 185 insertions(+), 113 deletions(-) diff --git a/CHANGES b/CHANGES index 8b5c36f..a2a1828 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,10 @@ +[0, 17, 6]: 2021-05-31 + - merged type annotations update provided by + `Jochen Sprickerhof `__ + - fix for issue 385: deprecated round_trip_loader function not working + (reported by `Mike Gouline `__) + - wasted a few hours getting rid of mypy warnings/errors + [0, 17, 5]: 2021-05-30 - fix for issue 384 !!set with aliased entry resulting in broken YAML on rt reported by `William Kimball `__) diff --git a/README.rst b/README.rst index 2d83774..5c5b263 100644 --- a/README.rst +++ b/README.rst @@ -4,8 +4,8 @@ ruamel.yaml ``ruamel.yaml`` is a YAML 1.2 loader/dumper package for Python. -:version: 0.17.5 -:updated: 2021-05-30 +:version: 0.17.6 +:updated: 2021-05-31 :documentation: http://yaml.readthedocs.io :repository: https://sourceforge.net/projects/ruamel-yaml/ :pypi: https://pypi.org/project/ruamel.yaml/ @@ -65,7 +65,14 @@ ChangeLog .. should insert NEXT: at the beginning of line for next key (with empty line) -0.17.5 (2021-05-30): +0.17.6 (2021-05-31): + - merged type annotations update provided by + `Jochen Sprickerhof `__ + - fix for issue 385: deprecated round_trip_loader function not working + (reported by `Mike Gouline `__) + - wasted a few hours getting rid of mypy warnings/errors + +0.17.5: - fix for issue 384 !!set with aliased entry resulting in broken YAML on rt reported by `William Kimball `__) diff --git a/__init__.py b/__init__.py index ef4c8b4..b6ba4c0 100644 --- a/__init__.py +++ b/__init__.py @@ -5,8 +5,8 @@ if False: # MYPY _package_data = dict( full_package_name='ruamel.yaml', - version_info=(0, 17, 5), - __version__='0.17.5', + version_info=(0, 17, 6), + __version__='0.17.6', author='Anthon van der Neut', author_email='a.van.der.neut@ruamel.eu', description='ruamel.yaml is a YAML parser/emitter that supports roundtrip preservation of comments, seq/map flow style, and map key order', # NOQA diff --git a/_doc/_static/pypi.svg b/_doc/_static/pypi.svg index 820debd..75b8410 100644 --- a/_doc/_static/pypi.svg +++ b/_doc/_static/pypi.svg @@ -1 +1 @@ - pypipypi0.17.50.17.5 + pypipypi0.17.60.17.6 diff --git a/comments.py b/comments.py index 252a450..3df5f34 100644 --- a/comments.py +++ b/comments.py @@ -47,14 +47,17 @@ C_BLANK_LINE_PRESERVE_SPACE = 0b100 class IDX: # temporary auto increment, so rearranging is easier def __init__(self): + # type: () -> None self._idx = 0 def __call__(self): + # type: () -> Any x = self._idx self._idx += 1 return x def __str__(self): + # type: () -> Any return str(self._idx) @@ -90,8 +93,8 @@ class Comment: attrib = comment_attrib def __init__(self, old=True): - # type: (Bool) -> None - self._pre = None if old else [] + # type: (bool) -> None + self._pre = None if old else [] # type: ignore self.comment = None # [post, [pre]] # map key (mapping/omap/dict) or index (sequence/list) to a list of # dict: post_key, pre_key, post_value, pre_value @@ -103,7 +106,7 @@ class Comment: def __str__(self): # type: () -> str if bool(self._post): - end = ',\n end=' + str(self._end) + end = ',\n end=' + str(self._post) else: end = "" return 'Comment(comment={0},\n items={1}{2})'.format(self.comment, self._items, end) @@ -115,9 +118,9 @@ class Comment: else: end = "" try: - ln = max([len(str(k)) for k in self._items]) + 1 + ln = max([len(str(k)) for k in self._items]) + 1 # type: ignore except ValueError: - ln = '' + ln = '' # type: ignore it = ' '.join( ['{:{}} {}\n'.format(str(k) + ':', ln, v) for k, v in self._items.items()] ) @@ -126,6 +129,7 @@ class Comment: return 'Comment(\n start={},\n items={{{}}}{})'.format(self.comment, it, end) def __repr__(self): + # type: () -> str if self._pre is None: return self._old__repr__() if bool(self._post): @@ -133,9 +137,9 @@ class Comment: else: end = "" try: - ln = max([len(str(k)) for k in self._items]) + 1 + ln = max([len(str(k)) for k in self._items]) + 1 # type: ignore except ValueError: - ln = '' + ln = '' # type: ignore it = ' '.join( ['{:{}} {}\n'.format(str(k) + ':', ln, v) for k, v in self._items.items()] ) @@ -169,12 +173,14 @@ class Comment: self._pre = value def get(self, item, pos): + # type: (Any, Any) -> Any x = self._items.get(item) if x is None or len(x) < pos: return None return x[pos] # can be None def set(self, item, pos, value): + # type: (Any, Any, Any) -> Any x = self._items.get(item) if x is None: self._items[item] = x = [None] * (pos + 1) @@ -185,6 +191,7 @@ class Comment: x[pos] = value def __contains__(self, x): + # type: (Any) -> Any # test if a substring is in any of the attached comments if self.comment: if self.comment[0] and x in self.comment[0].value: @@ -287,7 +294,7 @@ class LineCol: def __repr__(self): # type: () -> str - return _F('LineCol({line}, {col})', line=self.line, col=self.col) + return _F('LineCol({line}, {col})', line=self.line, col=self.col) # type: ignore class Tag: @@ -351,7 +358,7 @@ class CommentedBase: from .error import CommentMark from .tokens import CommentToken - pre_comments = self._yaml_clear_pre_comment() + pre_comments = self._yaml_clear_pre_comment() # type: ignore if comment[-1] == '\n': comment = comment[:-1] # strip final newline if there start_mark = CommentMark(indent) diff --git a/compat.py b/compat.py index e88b433..87d5e35 100644 --- a/compat.py +++ b/compat.py @@ -19,30 +19,29 @@ if False: # MYPY _DEFAULT_YAML_VERSION = (1, 2) try: - from ruamel.ordereddict import ordereddict -except: # NOQA - try: - from collections import OrderedDict - except ImportError: - from ordereddict import OrderedDict # type: ignore + from collections import OrderedDict +except ImportError: + from ordereddict import OrderedDict # type: ignore + # to get the right name import ... as ordereddict doesn't do that - class ordereddict(OrderedDict): # type: ignore - if not hasattr(OrderedDict, 'insert'): - def insert(self, pos, key, value): - # type: (int, Any, Any) -> None - if pos >= len(self): +class ordereddict(OrderedDict): # type: ignore + if not hasattr(OrderedDict, 'insert'): + + def insert(self, pos, key, value): + # type: (int, Any, Any) -> None + if pos >= len(self): + self[key] = value + return + od = ordereddict() + od.update(self) + for k in od: + del self[k] + for index, old_key in enumerate(od): + if pos == index: self[key] = value - return - od = ordereddict() - od.update(self) - for k in od: - del self[k] - for index, old_key in enumerate(od): - if pos == index: - self[key] = value - self[old_key] = od[old_key] + self[old_key] = od[old_key] PY2 = sys.version_info[0] == 2 @@ -54,6 +53,7 @@ PY3 = sys.version_info[0] == 3 # assert _F('abc {ft!r}', ft=ft) == 'abc %r' % ft # 'abc %r' % ft -> _F('abc {ft!r}' -> f'abc {ft!r}' def _F(s, *superfluous, **kw): + # type: (Any, Any, Any) -> Any if superfluous: raise TypeError return s.format(**kw) @@ -163,6 +163,7 @@ class Nprint: self._count = None def fp(self, mode='a'): + # type: (str) -> Any out = sys.stdout if self._file_name is None else open(self._file_name, mode) return out diff --git a/composer.py b/composer.py index 1d7fc65..bad132a 100644 --- a/composer.py +++ b/composer.py @@ -105,6 +105,7 @@ class Composer: return node def return_alias(self, a): + # type: (Any) -> Any return a def compose_node(self, parent, index): diff --git a/constructor.py b/constructor.py index 0a5db9b..53a3d41 100644 --- a/constructor.py +++ b/constructor.py @@ -545,10 +545,7 @@ class SafeConstructor(BaseConstructor): node.start_mark, ) try: - if hasattr(base64, 'decodebytes'): - return base64.decodebytes(value) - else: - return base64.decodestring(value) + return base64.decodebytes(value) except binascii.Error as exc: raise ConstructorError( None, @@ -804,10 +801,7 @@ class Constructor(SafeConstructor): node.start_mark, ) try: - if hasattr(base64, 'decodebytes'): - return base64.decodebytes(value) - else: - return base64.decodestring(value) + return base64.decodebytes(value) except binascii.Error as exc: raise ConstructorError( None, @@ -1068,12 +1062,14 @@ class RoundTripConstructor(SafeConstructor): """ def comment(self, idx): + # type: (Any) -> Any assert self.loader.comment_handling is not None x = self.scanner.comments[idx] x.set_assigned() return x def comments(self, list_of_comments, idx=None): + # type: (Any, Optional[Any]) -> Any # hand in the comment and optional pre, eol, post segment if list_of_comments is None: return [] @@ -1103,7 +1099,8 @@ class RoundTripConstructor(SafeConstructor): # NEWCMNT if node.comment is not None and node.comment[1]: # nprintf('>>>>nc1', node.comment) - lss.comment = self.comment(node.comment[1][0]) # EOL comment after | + # EOL comment after | + lss.comment = self.comment(node.comment[1][0]) # type: ignore return lss if node.style == '>' and isinstance(node.value, str): fold_positions = [] # type: List[int] @@ -1121,7 +1118,8 @@ class RoundTripConstructor(SafeConstructor): # NEWCMNT if node.comment is not None and node.comment[1]: # nprintf('>>>>nc2', node.comment) - lss.comment = self.comment(node.comment[1][0]) # EOL comment after > + # EOL comment after > + lss.comment = self.comment(node.comment[1][0]) # type: ignore if fold_positions: fss.fold_pos = fold_positions # type: ignore return fss diff --git a/emitter.py b/emitter.py index 635bb22..a17e0e5 100644 --- a/emitter.py +++ b/emitter.py @@ -904,7 +904,7 @@ class Emitter: and self.event.comment[0].column >= self.indent ): # comment following a folded scalar must dedent (issue 376) - self.event.comment[0].column = self.indent - 1 + self.event.comment[0].column = self.indent - 1 # type: ignore elif self.style == '|': # self.write_literal(self.analysis.scalar, self.event.comment) try: @@ -918,7 +918,7 @@ class Emitter: and self.event.comment[0].column >= self.indent ): # comment following a literal scalar must dedent (issue 376) - self.event.comment[0].column = self.indent - 1 + self.event.comment[0].column = self.indent - 1 # type: ignore else: self.write_plain(self.analysis.scalar, split) self.analysis = None @@ -1271,7 +1271,7 @@ class Emitter: data = ' ' * (indent - self.column) self.column = indent if self.encoding: - data = data.encode(self.encoding) + data = data.encode(self.encoding) # type: ignore self.stream.write(data) def write_line_break(self, data=None): @@ -1629,7 +1629,7 @@ class Emitter: data = ' ' self.column += len(data) if self.encoding: - data = data.encode(self.encoding) + data = data.encode(self.encoding) # type: ignore self.stream.write(data) self.whitespace = False self.indention = False @@ -1650,7 +1650,7 @@ class Emitter: data = text[start:end] self.column += len(data) if self.encoding: - data = data.encode(self.encoding) + data = data.encode(self.encoding) # type: ignore self.stream.write(data) start = end elif breaks: @@ -1671,7 +1671,7 @@ class Emitter: data = text[start:end] self.column += len(data) if self.encoding: - data = data.encode(self.encoding) + data = data.encode(self.encoding) # type: ignore try: self.stream.write(data) except: # NOQA diff --git a/events.py b/events.py index 7a02a29..2a895ff 100644 --- a/events.py +++ b/events.py @@ -32,7 +32,10 @@ class Event: if True: arguments = [] if hasattr(self, 'value'): - arguments.append(repr(self.value)) + # if you use repr(getattr(self, 'value')) then flake8 complains about + # abuse of getattr with a constant. When you change to self.value + # then mypy throws an error + arguments.append(repr(self.value)) # type: ignore for key in ['anchor', 'tag', 'implicit', 'flow_style', 'style']: v = getattr(self, key, None) if v is not None: @@ -48,7 +51,7 @@ class Event: self.end_mark.column, ) ) - arguments = ', '.join(arguments) + arguments = ', '.join(arguments) # type: ignore else: attributes = [ key @@ -150,6 +153,7 @@ class AliasEvent(NodeEvent): __slots__ = 'style' def __init__(self, anchor, start_mark=None, end_mark=None, style=None, comment=None): + # type: (Any, Any, Any, Any, Any) -> None NodeEvent.__init__(self, anchor, start_mark, end_mark, comment) self.style = style diff --git a/loader.py b/loader.py index b04ef18..fbfe4da 100644 --- a/loader.py +++ b/loader.py @@ -63,6 +63,7 @@ class RoundTripLoader( def __init__(self, stream, version=None, preserve_quotes=None): # type: (StreamTextType, Optional[VersionType], Optional[bool]) -> None # self.reader = Reader.__init__(self, stream) + self.comment_handling = None # issue 385 Reader.__init__(self, stream, loader=self) RoundTripScanner.__init__(self, loader=self) RoundTripParser.__init__(self, loader=self) diff --git a/main.py b/main.py index 397f5dd..8a90342 100644 --- a/main.py +++ b/main.py @@ -444,7 +444,7 @@ class YAML: pass def load_all(self, stream): # *, skip=None): - # type: (Union[Path, StreamTextType], Any) -> Any + # type: (Union[Path, StreamTextType]) -> Any if not hasattr(stream, 'read') and hasattr(stream, 'open'): # pathlib.Path() instance with stream.open('r') as fp: @@ -516,6 +516,7 @@ class YAML: return self.constructor, self.parser def emit(self, events, stream): + # type: (Any, Any) -> None """ Emit YAML parsing events into a stream. If stream is None, return the produced string instead. @@ -573,7 +574,7 @@ class YAML: return self.dump_all([data], stream, transform=transform) def dump_all(self, documents, stream, *, transform=None): - # type: (Any, Union[Path, StreamType], Any, Any) -> Any + # type: (Any, Union[Path, StreamType], Any) -> Any if self._context_manager: raise NotImplementedError self._output = stream @@ -585,7 +586,7 @@ class YAML: self._context_manager = None def Xdump_all(self, documents, stream, *, transform=None): - # type: (Any, Union[Path, StreamType], Any, Any) -> Any + # type: (Any, Any, Any) -> Any """ Serialize a sequence of Python objects into a YAML stream. """ @@ -980,6 +981,7 @@ def yaml_object(yml): ######################################################################################## def warn_deprecation(fun, method, arg=''): + # type: (Any, Any, str) -> None from ruamel.yaml.compat import _F warnings.warn( @@ -992,6 +994,8 @@ def warn_deprecation(fun, method, arg=''): PendingDeprecationWarning, # this will show when testing with pytest/tox stacklevel=3, ) + + ######################################################################################## @@ -1053,7 +1057,7 @@ def compose_all(stream, Loader=Loader): def load(stream, Loader=None, version=None, preserve_quotes=None): - # type: (StreamTextType, Any, Optional[VersionType], Any) -> Any + # type: (Any, Any, Any, Any) -> Any """ Parse the first YAML document in a stream and produce the corresponding Python object. @@ -1062,7 +1066,7 @@ def load(stream, Loader=None, version=None, preserve_quotes=None): if Loader is None: warnings.warn(UnsafeLoaderWarning.text, UnsafeLoaderWarning, stacklevel=2) Loader = UnsafeLoader - loader = Loader(stream, version, preserve_quotes=preserve_quotes) + loader = Loader(stream, version, preserve_quotes=preserve_quotes) # type: Any try: return loader._constructor.get_single_data() finally: @@ -1078,7 +1082,7 @@ def load(stream, Loader=None, version=None, preserve_quotes=None): def load_all(stream, Loader=None, version=None, preserve_quotes=None): - # type: (Optional[StreamTextType], Any, Optional[VersionType], Optional[bool]) -> Any # NOQA + # type: (Any, Any, Any, Any) -> Any # NOQA """ Parse all YAML documents in a stream and produce corresponding Python objects. @@ -1087,7 +1091,7 @@ def load_all(stream, Loader=None, version=None, preserve_quotes=None): if Loader is None: warnings.warn(UnsafeLoaderWarning.text, UnsafeLoaderWarning, stacklevel=2) Loader = UnsafeLoader - loader = Loader(stream, version, preserve_quotes=preserve_quotes) + loader = Loader(stream, version, preserve_quotes=preserve_quotes) # type: Any try: while loader._constructor.check_data(): yield loader._constructor.get_data() @@ -1277,7 +1281,7 @@ def dump_all( top_level_colon_align=None, prefix_colon=None, ): - # type: (Any, Optional[StreamType], Any, Any, Any, Optional[bool], Optional[int], Optional[int], Optional[bool], Any, Any, Optional[bool], Optional[bool], Any, Any, Any, Any, Any) -> Optional[str] # NOQA + # type: (Any, Optional[StreamType], Any, Any, Any, Optional[bool], Optional[int], Optional[int], Optional[bool], Any, Any, Optional[bool], Optional[bool], Any, Any, Any, Any, Any) -> Any # NOQA """ Serialize a sequence of Python objects into a YAML stream. If stream is None, return the produced string instead. @@ -1348,7 +1352,7 @@ def dump( tags=None, block_seq_indent=None, ): - # type: (Any, Optional[StreamType], Any, Any, Any, Optional[bool], Optional[int], Optional[int], Optional[bool], Any, Any, Optional[bool], Optional[bool], Optional[VersionType], Any, Any) -> Optional[str] # NOQA + # type: (Any, Optional[StreamType], Any, Any, Any, Optional[bool], Optional[int], Optional[int], Optional[bool], Any, Any, Optional[bool], Optional[bool], Optional[VersionType], Any, Any) -> Optional[Any] # NOQA """ Serialize a Python object into a YAML stream. If stream is None, return the produced string instead. @@ -1378,7 +1382,7 @@ def dump( def safe_dump_all(documents, stream=None, **kwds): - # type: (Any, Optional[StreamType], Any) -> Optional[str] + # type: (Any, Optional[StreamType], Any) -> Optional[Any] """ Serialize a sequence of Python objects into a YAML stream. Produce only basic YAML tags. @@ -1389,7 +1393,7 @@ def safe_dump_all(documents, stream=None, **kwds): def safe_dump(data, stream=None, **kwds): - # type: (Any, Optional[StreamType], Any) -> Optional[str] + # type: (Any, Optional[StreamType], Any) -> Optional[Any] """ Serialize a Python object into a YAML stream. Produce only basic YAML tags. @@ -1419,7 +1423,7 @@ def round_trip_dump( top_level_colon_align=None, prefix_colon=None, ): - # type: (Any, Optional[StreamType], Any, Any, Any, Optional[bool], Optional[int], Optional[int], Optional[bool], Any, Any, Optional[bool], Optional[bool], Optional[VersionType], Any, Any, Any, Any) -> Optional[str] # NOQA + # type: (Any, Optional[StreamType], Any, Any, Any, Optional[bool], Optional[int], Optional[int], Optional[bool], Any, Any, Optional[bool], Optional[bool], Optional[VersionType], Any, Any, Any, Any) -> Optional[Any] # NOQA allow_unicode = True if allow_unicode is None else allow_unicode warn_deprecation('round_trip_dump', 'dump') return dump_all( diff --git a/nodes.py b/nodes.py index 0efe3ff..c76bb4e 100644 --- a/nodes.py +++ b/nodes.py @@ -21,7 +21,7 @@ class Node: self.anchor = anchor def __repr__(self): - # type: () -> str + # type: () -> Any value = self.value # if isinstance(value, list): # if len(value) == 0: diff --git a/parser.py b/parser.py index db225a1..28ca33c 100644 --- a/parser.py +++ b/parser.py @@ -83,12 +83,13 @@ from ruamel.yaml.comments import C_PRE, C_POST, C_SPLIT_ON_FIRST_BLANK from ruamel.yaml.compat import _F, nprint, nprintf # NOQA if False: # MYPY - from typing import Any, Dict, Optional, List # NOQA + from typing import Any, Dict, Optional, List, Optional # NOQA __all__ = ['Parser', 'RoundTripParser', 'ParserError'] def xprintf(*args, **kw): + # type: (Any, Any) -> Any return nprintf(*args, **kw) pass @@ -574,8 +575,8 @@ class Parser: c = token.comment start_mark = token.start_mark else: - start_mark = self.last_event.end_mark - c = self.distribute_comment(token.comment, start_mark.line) + start_mark = self.last_event.end_mark # type: ignore + c = self.distribute_comment(token.comment, start_mark.line) # type: ignore event = SequenceEndEvent(start_mark, start_mark, comment=c) self.state = self.states.pop() return event @@ -803,6 +804,7 @@ class Parser: return ScalarEvent(None, None, (True, False), "", mark, mark, comment=comment) def move_token_comment(self, token, nt=None, empty=False): + # type: (Any, Optional[Any], Optional[bool]) -> Any pass @@ -830,25 +832,29 @@ class RoundTripParser(Parser): return handle + suffix def move_token_comment(self, token, nt=None, empty=False): + # type: (Any, Optional[Any], Optional[bool]) -> Any token.move_old_comment(self.scanner.peek_token() if nt is None else nt, empty=empty) class RoundTripParserSC(RoundTripParser): """roundtrip is a safe loader, that wants to see the unmangled tag""" + # some of the differences are based on the superclass testing # if self.loader.comment_handling is not None def move_token_comment(self, token, nt=None, empty=False): + # type: (Any, Any, Any, Optional[bool]) -> None token.move_new_comment(self.scanner.peek_token() if nt is None else nt, empty=empty) def distribute_comment(self, comment, line): + # type: (Any, Any) -> Any # ToDo, look at indentation of the comment to determine attachment if comment is None: return None if not comment[0]: return None if comment[0][0] != line + 1: - nprintf('>>>dcxxx', comment, line, typ) + nprintf('>>>dcxxx', comment, line) assert comment[0][0] == line + 1 # if comment[0] - line > 1: # return @@ -861,17 +867,17 @@ class RoundTripParserSC(RoundTripParser): comment[0] = None return c # nprintf('>>>dcb', comment[0]) - for cmntidx in comment[0]: + for _idx, cmntidx in enumerate(comment[0]): # nprintf('>>>dcb', cmntidx) if isinstance(self.scanner.comments[cmntidx], BlankLineComment): break else: return None # no space found - if idx == 0: + if _idx == 0: return None # first line was blank # nprintf('>>>dcc', idx) if typ == C_SPLIT_ON_FIRST_BLANK: - c = [None, None, comment[0][:idx]] - comment[0] = comment[0][idx:] + c = [None, None, comment[0][:_idx]] + comment[0] = comment[0][_idx:] return c raise NotImplementedError # reserved diff --git a/reader.py b/reader.py index ff62135..a78a878 100644 --- a/reader.py +++ b/reader.py @@ -42,7 +42,7 @@ class ReaderError(YAMLError): self.reason = reason def __str__(self): - # type: () -> str + # type: () -> Any if isinstance(self.character, bytes): return _F( "'{self_encoding!s}' codec can't decode byte #x{ord_self_character:02x}: " @@ -212,7 +212,7 @@ class Reader: @classmethod def _get_non_printable_ascii(cls, data): # type: ignore # type: (Text, bytes) -> Optional[Tuple[int, Text]] - ascii_bytes = data.encode('ascii') + ascii_bytes = data.encode('ascii') # type: ignore non_printables = ascii_bytes.translate(None, cls._printable_ascii) # type: ignore if not non_printables: return None diff --git a/representer.py b/representer.py index 4b78c65..d419f50 100644 --- a/representer.py +++ b/representer.py @@ -253,7 +253,8 @@ class SafeRepresenter(BaseRepresenter): if hasattr(base64, 'encodebytes'): data = base64.encodebytes(data).decode('ascii') else: - data = base64.encodestring(data).decode('ascii') + # check py2 only? + data = base64.encodestring(data).decode('ascii') # type: ignore return self.represent_scalar('tag:yaml.org,2002:binary', data, style='|') def represent_bool(self, data, anchor=None): @@ -439,8 +440,8 @@ class Representer(SafeRepresenter): # !!python/object/apply node. cls = type(data) - if cls in copyreg.dispatch_table: - reduce = copyreg.dispatch_table[cls](data) + if cls in copyreg.dispatch_table: # type: ignore + reduce = copyreg.dispatch_table[cls](data) # type: ignore elif hasattr(data, '__reduce_ex__'): reduce = data.__reduce_ex__(2) elif hasattr(data, '__reduce__'): diff --git a/resolver.py b/resolver.py index a6c97df..969b9a9 100644 --- a/resolver.py +++ b/resolver.py @@ -250,7 +250,7 @@ class BaseResolver: self.resolver_prefix_paths.pop() def check_resolver_prefix(self, depth, path, kind, current_node, current_index): - # type: (int, Text, Any, Any, Any) -> bool + # type: (int, Any, Any, Any, Any) -> bool node_check, index_check = path[depth - 1] if isinstance(node_check, str): if current_node.tag != node_check: diff --git a/scanner.py b/scanner.py index 0a516f7..67fc3b3 100644 --- a/scanner.py +++ b/scanner.py @@ -46,6 +46,7 @@ _SPACE_TAB = ' \t' def xprintf(*args, **kw): + # type: (Any, Any) -> Any return nprintf(*args, **kw) pass @@ -1249,7 +1250,7 @@ class Scanner: line = end_mark.line - len(trailing) for x in trailing: assert x[-1] == '\n' - self.comments.add_blank_line(x, 0, line) + self.comments.add_blank_line(x, 0, line) # type: ignore line += 1 comment = self.scan_to_next_token() while comment: @@ -1620,7 +1621,7 @@ class Scanner: line = start_mark.line + 1 for ch in spaces: if ch == '\n': - self.comments.add_blank_line('\n', 0, line) + self.comments.add_blank_line('\n', 0, line) # type: ignore line += 1 return token @@ -2016,6 +2017,7 @@ class CommentBase: __slots__ = ('value', 'line', 'column', 'used', 'function', 'fline', 'ufun', 'uline') def __init__(self, value, line, column): + # type: (Any, Any, Any) -> None self.value = value self.line = line self.column = column @@ -2027,24 +2029,29 @@ class CommentBase: self.uline = None def set_used(self, v='+'): + # type: (Any) -> None self.used = v info = inspect.getframeinfo(inspect.stack()[1][0]) - self.ufun = info.function - self.uline = info.lineno + self.ufun = info.function # type: ignore + self.uline = info.lineno # type: ignore def set_assigned(self): + # type: () -> None self.used = '|' def __str__(self): - return _F('{value}', value=self.value) + # type: () -> str + return _F('{value}', value=self.value) # type: ignore def __repr__(self): - return _F('{value!r}', value=self.value) + # type: () -> str + return _F('{value!r}', value=self.value) # type: ignore def info(self): - return _F( + # type: () -> str + return _F( # type: ignore '{name}{used} {line:2}:{column:<2} "{value:40s} {function}:{fline} {ufun}:{uline}', - name=self.name, + name=self.name, # type: ignore line=self.line, column=self.column, value=self.value + '"', @@ -2060,6 +2067,7 @@ class EOLComment(CommentBase): name = 'EOLC' def __init__(self, value, line, column): + # type: (Any, Any, Any) -> None super().__init__(value, line, column) @@ -2067,6 +2075,7 @@ class FullLineComment(CommentBase): name = 'FULL' def __init__(self, value, line, column): + # type: (Any, Any, Any) -> None super().__init__(value, line, column) @@ -2074,15 +2083,18 @@ class BlankLineComment(CommentBase): name = 'BLNK' def __init__(self, value, line, column): + # type: (Any, Any, Any) -> None super().__init__(value, line, column) class ScannedComments: def __init__(self): - self.comments = {} - self.unused = [] + # type: (Any) -> None + self.comments = {} # type: ignore + self.unused = [] # type: ignore def add_eol_comment(self, comment, column, line): + # type: (Any, Any, Any) -> Any # info = inspect.getframeinfo(inspect.stack()[1][0]) if comment.count('\n') == 1: assert comment[-1] == '\n' @@ -2093,6 +2105,7 @@ class ScannedComments: return retval def add_blank_line(self, comment, column, line): + # type: (Any, Any, Any) -> Any # info = inspect.getframeinfo(inspect.stack()[1][0]) assert comment.count('\n') == 1 and comment[-1] == '\n' assert line not in self.comments @@ -2101,6 +2114,7 @@ class ScannedComments: return retval def add_full_line_comment(self, comment, column, line): + # type: (Any, Any, Any) -> Any # info = inspect.getframeinfo(inspect.stack()[1][0]) assert comment.count('\n') == 1 and comment[-1] == '\n' # if comment.startswith('# C12'): @@ -2111,9 +2125,11 @@ class ScannedComments: return retval def __getitem__(self, idx): + # type: (Any) -> Any return self.comments[idx] def __str__(self): + # type: () -> Any return ( 'ParsedComments:\n ' + '\n '.join( @@ -2126,10 +2142,12 @@ class ScannedComments: ) def last(self): + # type: () -> str lineno, x = list(self.comments.items())[-1] - return _F('{lineno:2} {x}\n', lineno=lineno, x=x.info()) + return _F('{lineno:2} {x}\n', lineno=lineno, x=x.info()) # type: ignore def any_unprocessed(self): + # type: () -> bool # ToDo: might want to differentiate based on lineno return len(self.unused) > 0 # for lno, comment in reversed(self.comments.items()): @@ -2138,6 +2156,7 @@ class ScannedComments: # return False def unprocessed(self, use=False): + # type: (Any) -> Any while len(self.unused) > 0: first = self.unused.pop(0) if use else self.unused[0] info = inspect.getframeinfo(inspect.stack()[1][0]) @@ -2147,6 +2166,7 @@ class ScannedComments: self.comments[first].set_used() def assign_pre(self, token): + # type: (Any) -> Any token_line = token.start_mark.line info = inspect.getframeinfo(inspect.stack()[1][0]) xprintf('assign_pre', token_line, self.unused, info.function, info.lineno) @@ -2160,6 +2180,7 @@ class ScannedComments: return gobbled def assign_eol(self, tokens): + # type: (Any) -> Any try: comment_line = self.unused[0] except IndexError: @@ -2215,6 +2236,7 @@ class ScannedComments: sys.exit(0) def assign_post(self, token): + # type: (Any) -> Any token_line = token.start_mark.line info = inspect.getframeinfo(inspect.stack()[1][0]) xprintf('assign_post', token_line, self.unused, info.function, info.lineno) @@ -2228,6 +2250,7 @@ class ScannedComments: return gobbled def str_unprocessed(self): + # type: () -> Any return ''.join( ( _F(' {ind:2} {x}\n', ind=ind, x=x.info()) @@ -2239,6 +2262,7 @@ class ScannedComments: class RoundTripScannerSC(Scanner): # RoundTripScanner Split Comments def __init__(self, *arg, **kw): + # type: (Any, Any) -> None super().__init__(*arg, **kw) assert self.loader is not None # comments isinitialised on .need_more_tokens and persist on @@ -2252,15 +2276,16 @@ class RoundTripScannerSC(Scanner): # RoundTripScanner Split Comments self.fetch_more_tokens() if len(self.tokens) > 0: if isinstance(self.tokens[0], BlockEndToken): - self.comments.assign_post(self.tokens[0]) + self.comments.assign_post(self.tokens[0]) # type: ignore else: - self.comments.assign_pre(self.tokens[0]) + self.comments.assign_pre(self.tokens[0]) # type: ignore self.tokens_taken += 1 return self.tokens.pop(0) def need_more_tokens(self): + # type: () -> bool if self.comments is None: - self.loader.parsed_comments = self.comments = ScannedComments() + self.loader.parsed_comments = self.comments = ScannedComments() # type: ignore if self.done: return False if len(self.tokens) == 0: @@ -2279,12 +2304,13 @@ class RoundTripScannerSC(Scanner): # RoundTripScanner Split Comments for t in self.tokens: xprintf(t) # xprintf(self.comments.last()) - xprintf(self.comments.str_unprocessed()) - self.comments.assign_pre(self.tokens[0]) - self.comments.assign_eol(self.tokens) + xprintf(self.comments.str_unprocessed()) # type: ignore + self.comments.assign_pre(self.tokens[0]) # type: ignore + self.comments.assign_eol(self.tokens) # type: ignore return False def scan_to_next_token(self): + # type: () -> None srp = self.reader.peek srf = self.reader.forward if self.reader.index == 0 and srp() == '\uFEFF': @@ -2312,11 +2338,11 @@ class RoundTripScannerSC(Scanner): # RoundTripScanner Split Comments srf() # we have a comment if start_mark.column == 0: - self.comments.add_full_line_comment( + self.comments.add_full_line_comment( # type: ignore comment, comment_start_mark.column, comment_start_mark.line ) else: - self.comments.add_eol_comment( + self.comments.add_eol_comment( # type: ignore comment, comment_start_mark.column, comment_start_mark.line ) comment = "" @@ -2348,6 +2374,7 @@ class RoundTripScannerSC(Scanner): # RoundTripScanner Split Comments return None def scan_empty_or_full_line_comments(self): + # type: () -> None blmark = self.reader.get_mark() assert blmark.column == 0 blanks = "" @@ -2367,7 +2394,7 @@ class RoundTripScannerSC(Scanner): # RoundTripScanner Split Comments comment = None else: blanks += '\n' - self.comments.add_blank_line(blanks, blmark.column, blmark.line) + self.comments.add_blank_line(blanks, blmark.column, blmark.line) # type: ignore # NOQA blanks = "" blmark = self.reader.get_mark() ch = self.reader.peek() @@ -2402,7 +2429,7 @@ class RoundTripScannerSC(Scanner): # RoundTripScanner Split Comments while srp() not in _THE_END: comment += srp() srf() - comment += '\n' + comment += '\n' # type: ignore ch = srp() if ch not in _THE_END: raise ScannerError( @@ -2412,6 +2439,6 @@ class RoundTripScannerSC(Scanner): # RoundTripScanner Split Comments self.reader.get_mark(), ) if comment is not None: - self.comments.add_eol_comment(comment, mark.column, mark.line) + self.comments.add_eol_comment(comment, mark.column, mark.line) # type: ignore self.scan_line_break() return None diff --git a/timestamp.py b/timestamp.py index 8f00db2..e338c30 100644 --- a/timestamp.py +++ b/timestamp.py @@ -37,6 +37,7 @@ class TimeStamp(datetime.datetime): tzinfo=True, fold=None, ): + # type: (Any, Any, Any, Any, Any, Any, Any, Any, Any) -> Any if year is None: year = self.year if month is None: diff --git a/tokens.py b/tokens.py index 202cbe2..13b25a8 100644 --- a/tokens.py +++ b/tokens.py @@ -42,10 +42,12 @@ class Token: @property def column(self): + # type: () -> int return self.start_mark.column @column.setter def column(self, pos): + # type: (Any) -> None self.start_mark.column = pos # old style ( <= 0.17) is a TWO element list with first being the EOL @@ -83,34 +85,37 @@ class Token: # new style def add_comment_pre(self, comment): + # type: (Any) -> None if not hasattr(self, '_comment'): - self._comment = [[], None, None] + self._comment = [[], None, None] # type: ignore else: assert len(self._comment) == 3 if self._comment[0] is None: - self._comment[0] = [] - self._comment[0].append(comment) + self._comment[0] = [] # type: ignore + self._comment[0].append(comment) # type: ignore def add_comment_eol(self, comment, comment_type): + # type: (Any, Any) -> None if not hasattr(self, '_comment'): self._comment = [None, None, None] else: assert len(self._comment) == 3 assert self._comment[1] is None if self.comment[1] is None: - self._comment[1] = [] - self._comment[1].extend([None] * (comment_type + 1 - len(self.comment[1]))) + self._comment[1] = [] # type: ignore + self._comment[1].extend([None] * (comment_type + 1 - len(self.comment[1]))) # type: ignore # NOQA # nprintf('commy', self.comment, comment_type) - self._comment[1][comment_type] = comment + self._comment[1][comment_type] = comment # type: ignore def add_comment_post(self, comment): + # type: (Any) -> None if not hasattr(self, '_comment'): - self._comment = [None, None, []] + self._comment = [None, None, []] # type: ignore else: assert len(self._comment) == 3 if self._comment[2] is None: - self._comment[2] = [] - self._comment[2].append(comment) + self._comment[2] = [] # type: ignore + self._comment[2].append(comment) # type: ignore # def get_comment(self): # # type: () -> Any @@ -350,17 +355,19 @@ class CommentToken(Token): if start_mark is None: assert column is not None self._column = column - Token.__init__(self, start_mark, None) + Token.__init__(self, start_mark, None) # type: ignore self._value = value @property def value(self): + # type: () -> str if isinstance(self._value, str): return self._value return "".join(self._value) @value.setter def value(self, val): + # type: (Any) -> None self._value = val def reset(self): diff --git a/util.py b/util.py index 869aaef..8c7af47 100644 --- a/util.py +++ b/util.py @@ -115,7 +115,7 @@ def load_yaml_guess_indent(stream, **kw): if indent is None and map_indent is not None: indent = map_indent yaml = YAML() - return yaml.load(yaml_str, **kw), indent, block_seq_indent + return yaml.load(yaml_str, **kw), indent, block_seq_indent # type: ignore def configobj_walker(cfg): -- cgit v1.2.1