summaryrefslogtreecommitdiff
path: root/scalarstring.py
diff options
context:
space:
mode:
authorAnthon van der Neut <anthon@mnt.org>2023-05-01 19:13:50 +0200
committerAnthon van der Neut <anthon@mnt.org>2023-05-01 19:13:50 +0200
commit8b731994b1543d7886af85f926d9eea5a22d0732 (patch)
tree3553d4cbc80b541484d7a3f39e00cdcfd8f9d030 /scalarstring.py
parent45111ba0b67e8619265d89f3202635e62c13cde6 (diff)
downloadruamel.yaml-8b731994b1543d7886af85f926d9eea5a22d0732.tar.gz
retrofitted 0.18 changes
Diffstat (limited to 'scalarstring.py')
-rw-r--r--scalarstring.py42
1 files changed, 15 insertions, 27 deletions
diff --git a/scalarstring.py b/scalarstring.py
index 7538ab7..30f4fde 100644
--- a/scalarstring.py
+++ b/scalarstring.py
@@ -2,8 +2,8 @@
from ruamel.yaml.anchor import Anchor
-if False: # MYPY
- from typing import Text, Any, Dict, List # NOQA
+from typing import Text, Any, Dict, List # NOQA
+from ruamel.yaml.compat import SupportsIndex
__all__ = [
'ScalarString',
@@ -21,35 +21,30 @@ __all__ = [
class ScalarString(str):
__slots__ = Anchor.attrib
- def __new__(cls, *args, **kw):
- # type: (Any, Any) -> Any
+ def __new__(cls, *args: Any, **kw: Any) -> Any:
anchor = kw.pop('anchor', None)
ret_val = str.__new__(cls, *args, **kw)
if anchor is not None:
ret_val.yaml_set_anchor(anchor, always_dump=True)
return ret_val
- def replace(self, old, new, maxreplace=-1):
- # type: (Any, Any, int) -> Any
+ def replace(self, old: Any, new: Any, maxreplace: SupportsIndex = -1) -> Any:
return type(self)((str.replace(self, old, new, maxreplace)))
@property
- def anchor(self):
- # type: () -> Any
+ def anchor(self) -> Any:
if not hasattr(self, Anchor.attrib):
setattr(self, Anchor.attrib, Anchor())
return getattr(self, Anchor.attrib)
- def yaml_anchor(self, any=False):
- # type: (bool) -> Any
+ def yaml_anchor(self, any: bool = False) -> Any:
if not hasattr(self, Anchor.attrib):
return None
if any or self.anchor.always_dump:
return self.anchor
return None
- def yaml_set_anchor(self, value, always_dump=False):
- # type: (Any, bool) -> None
+ def yaml_set_anchor(self, value: Any, always_dump: bool = False) -> None:
self.anchor.value = value
self.anchor.always_dump = always_dump
@@ -59,8 +54,7 @@ class LiteralScalarString(ScalarString):
style = '|'
- def __new__(cls, value, anchor=None):
- # type: (Text, Any) -> Any
+ def __new__(cls, value: Text, anchor: Any = None) -> Any:
return ScalarString.__new__(cls, value, anchor=anchor)
@@ -72,8 +66,7 @@ class FoldedScalarString(ScalarString):
style = '>'
- def __new__(cls, value, anchor=None):
- # type: (Text, Any) -> Any
+ def __new__(cls, value: Text, anchor: Any = None) -> Any:
return ScalarString.__new__(cls, value, anchor=anchor)
@@ -82,8 +75,7 @@ class SingleQuotedScalarString(ScalarString):
style = "'"
- def __new__(cls, value, anchor=None):
- # type: (Text, Any) -> Any
+ def __new__(cls, value: Text, anchor: Any = None) -> Any:
return ScalarString.__new__(cls, value, anchor=anchor)
@@ -92,8 +84,7 @@ class DoubleQuotedScalarString(ScalarString):
style = '"'
- def __new__(cls, value, anchor=None):
- # type: (Text, Any) -> Any
+ def __new__(cls, value: Text, anchor: Any = None) -> Any:
return ScalarString.__new__(cls, value, anchor=anchor)
@@ -102,18 +93,15 @@ class PlainScalarString(ScalarString):
style = ''
- def __new__(cls, value, anchor=None):
- # type: (Text, Any) -> Any
+ def __new__(cls, value: Text, anchor: Any = None) -> Any:
return ScalarString.__new__(cls, value, anchor=anchor)
-def preserve_literal(s):
- # type: (Text) -> Text
+def preserve_literal(s: Text) -> Text:
return LiteralScalarString(s.replace('\r\n', '\n').replace('\r', '\n'))
-def walk_tree(base, map=None):
- # type: (Any, Any) -> None
+def walk_tree(base: Any, map: Any = None) -> None:
"""
the routine here walks over a simple yaml tree (recursing in
dict values and list items) and converts strings that
@@ -133,7 +121,7 @@ def walk_tree(base, map=None):
if isinstance(base, MutableMapping):
for k in base:
- v = base[k] # type: Text
+ v: Text = base[k]
if isinstance(v, str):
for ch in map:
if ch in v: