From 8b731994b1543d7886af85f926d9eea5a22d0732 Mon Sep 17 00:00:00 2001 From: Anthon van der Neut Date: Mon, 1 May 2023 19:13:50 +0200 Subject: retrofitted 0.18 changes --- error.py | 131 +++++++++++++++++++++++---------------------------------------- 1 file changed, 48 insertions(+), 83 deletions(-) (limited to 'error.py') diff --git a/error.py b/error.py index 30b114a..ccdbf28 100644 --- a/error.py +++ b/error.py @@ -3,10 +3,7 @@ import warnings import textwrap -from ruamel.yaml.compat import _F - -if False: # MYPY - from typing import Any, Dict, Optional, List, Text # NOQA +from typing import Any, Dict, Optional, List, Text # NOQA __all__ = [ @@ -25,33 +22,24 @@ __all__ = [ class StreamMark: __slots__ = 'name', 'index', 'line', 'column' - def __init__(self, name, index, line, column): - # type: (Any, int, int, int) -> None + def __init__(self, name: Any, index: int, line: int, column: int) -> None: self.name = name self.index = index self.line = line self.column = column - def __str__(self): - # type: () -> Any - where = _F( - ' in "{sname!s}", line {sline1:d}, column {scolumn1:d}', - sname=self.name, - sline1=self.line + 1, - scolumn1=self.column + 1, - ) + def __str__(self) -> Any: + where = f' in "{self.name!s}", line {self.line + 1:d}, column {self.column + 1:d}' return where - def __eq__(self, other): - # type: (Any) -> bool + def __eq__(self, other: Any) -> bool: if self.line != other.line or self.column != other.column: return False if self.name != other.name or self.index != other.index: return False return True - def __ne__(self, other): - # type: (Any) -> bool + def __ne__(self, other: Any) -> bool: return not self.__eq__(other) @@ -62,14 +50,14 @@ class FileMark(StreamMark): class StringMark(StreamMark): __slots__ = 'name', 'index', 'line', 'column', 'buffer', 'pointer' - def __init__(self, name, index, line, column, buffer, pointer): - # type: (Any, int, int, int, Any, Any) -> None + def __init__( + self, name: Any, index: int, line: int, column: int, buffer: Any, pointer: Any + ) -> None: StreamMark.__init__(self, name, index, line, column) self.buffer = buffer self.pointer = pointer - def get_snippet(self, indent=4, max_length=75): - # type: (int, int) -> Any + def get_snippet(self, indent: int = 4, max_length: int = 75) -> Any: if self.buffer is None: # always False return None head = "" @@ -90,7 +78,7 @@ class StringMark(StreamMark): break snippet = self.buffer[start:end] caret = '^' - caret = '^ (line: {})'.format(self.line + 1) + caret = f'^ (line: {self.line + 1})' return ( ' ' * indent + head @@ -101,28 +89,16 @@ class StringMark(StreamMark): + caret ) - def __str__(self): - # type: () -> Any + def __str__(self) -> Any: snippet = self.get_snippet() - where = _F( - ' in "{sname!s}", line {sline1:d}, column {scolumn1:d}', - sname=self.name, - sline1=self.line + 1, - scolumn1=self.column + 1, - ) + where = f' in "{self.name!s}", line {self.line + 1:d}, column {self.column + 1:d}' if snippet is not None: where += ':\n' + snippet return where - def __repr__(self): - # type: () -> Any + def __repr__(self) -> Any: snippet = self.get_snippet() - where = _F( - ' in "{sname!s}", line {sline1:d}, column {scolumn1:d}', - sname=self.name, - sline1=self.line + 1, - scolumn1=self.column + 1, - ) + where = f' in "{self.name!s}", line {self.line + 1:d}, column {self.column + 1:d}' if snippet is not None: where += ':\n' + snippet return where @@ -131,8 +107,7 @@ class StringMark(StreamMark): class CommentMark: __slots__ = ('column',) - def __init__(self, column): - # type: (Any) -> None + def __init__(self, column: Any) -> None: self.column = column @@ -143,14 +118,13 @@ class YAMLError(Exception): class MarkedYAMLError(YAMLError): def __init__( self, - context=None, - context_mark=None, - problem=None, - problem_mark=None, - note=None, - warn=None, - ): - # type: (Any, Any, Any, Any, Any, Any) -> None + context: Any = None, + context_mark: Any = None, + problem: Any = None, + problem_mark: Any = None, + note: Any = None, + warn: Any = None, + ) -> None: self.context = context self.context_mark = context_mark self.problem = problem @@ -158,9 +132,8 @@ class MarkedYAMLError(YAMLError): self.note = note # warn is ignored - def __str__(self): - # type: () -> Any - lines = [] # type: List[str] + def __str__(self) -> Any: + lines: List[str] = [] if self.context is not None: lines.append(self.context) if self.context_mark is not None and ( @@ -192,14 +165,13 @@ class YAMLWarning(Warning): class MarkedYAMLWarning(YAMLWarning): def __init__( self, - context=None, - context_mark=None, - problem=None, - problem_mark=None, - note=None, - warn=None, - ): - # type: (Any, Any, Any, Any, Any, Any) -> None + context: Any = None, + context_mark: Any = None, + problem: Any = None, + problem_mark: Any = None, + note: Any = None, + warn: Any = None, + ) -> None: self.context = context self.context_mark = context_mark self.problem = problem @@ -207,9 +179,8 @@ class MarkedYAMLWarning(YAMLWarning): self.note = note self.warn = warn - def __str__(self): - # type: () -> Any - lines = [] # type: List[str] + def __str__(self) -> Any: + lines: List[str] = [] if self.context is not None: lines.append(self.context) if self.context_mark is not None and ( @@ -254,30 +225,26 @@ warnings.simplefilter('once', UnsafeLoaderWarning) class MantissaNoDotYAML1_1Warning(YAMLWarning): - def __init__(self, node, flt_str): - # type: (Any, Any) -> None + def __init__(self, node: Any, flt_str: Any) -> None: self.node = node self.flt = flt_str - def __str__(self): - # type: () -> Any + def __str__(self) -> Any: line = self.node.start_mark.line col = self.node.start_mark.column - return """ + return f""" In YAML 1.1 floating point values should have a dot ('.') in their mantissa. See the Floating-Point Language-Independent Type for YAML™ Version 1.1 specification ( http://yaml.org/type/float.html ). This dot is not required for JSON nor for YAML 1.2 -Correct your float: "{}" on line: {}, column: {} +Correct your float: "{self.flt}" on line: {line}, column: {col} or alternatively include the following in your code: import warnings warnings.simplefilter('ignore', ruamel.yaml.error.MantissaNoDotYAML1_1Warning) -""".format( - self.flt, line, col - ) +""" warnings.simplefilter('once', MantissaNoDotYAML1_1Warning) @@ -290,14 +257,13 @@ class YAMLFutureWarning(Warning): class MarkedYAMLFutureWarning(YAMLFutureWarning): def __init__( self, - context=None, - context_mark=None, - problem=None, - problem_mark=None, - note=None, - warn=None, - ): - # type: (Any, Any, Any, Any, Any, Any) -> None + context: Any = None, + context_mark: Any = None, + problem: Any = None, + problem_mark: Any = None, + note: Any = None, + warn: Any = None, + ) -> None: self.context = context self.context_mark = context_mark self.problem = problem @@ -305,9 +271,8 @@ class MarkedYAMLFutureWarning(YAMLFutureWarning): self.note = note self.warn = warn - def __str__(self): - # type: () -> Any - lines = [] # type: List[str] + def __str__(self) -> Any: + lines: List[str] = [] if self.context is not None: lines.append(self.context) -- cgit v1.2.1