You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

45 lines
1.3 KiB

  1. from __future__ import annotations
  2. """
  3. You cannot subclass bool, and this is necessary for round-tripping anchored
  4. bool values (and also if you want to preserve the original way of writing)
  5. bool.__bases__ is type 'int', so that is what is used as the basis for ScalarBoolean as well.
  6. You can use these in an if statement, but not when testing equivalence
  7. """
  8. from ruamel.yaml.anchor import Anchor
  9. if False: # MYPY
  10. from typing import Text, Any, Dict, List # NOQA
  11. __all__ = ['ScalarBoolean']
  12. class ScalarBoolean(int):
  13. def __new__(cls: Any, *args: Any, **kw: Any) -> Any:
  14. anchor = kw.pop('anchor', None)
  15. b = int.__new__(cls, *args, **kw)
  16. if anchor is not None:
  17. b.yaml_set_anchor(anchor, always_dump=True)
  18. return b
  19. @property
  20. def anchor(self) -> Any:
  21. if not hasattr(self, Anchor.attrib):
  22. setattr(self, Anchor.attrib, Anchor())
  23. return getattr(self, Anchor.attrib)
  24. def yaml_anchor(self, any: bool = False) -> Any:
  25. if not hasattr(self, Anchor.attrib):
  26. return None
  27. if any or self.anchor.always_dump:
  28. return self.anchor
  29. return None
  30. def yaml_set_anchor(self, value: Any, always_dump: bool = False) -> None:
  31. self.anchor.value = value
  32. self.anchor.always_dump = always_dump