| 28 | |
| 29 | |
| 30 | class _Option: |
| 31 | __slots__ = ("name", "typespec", "value", "_default", "choices", "help") |
| 32 | |
| 33 | def __init__( |
| 34 | self, |
| 35 | name: str, |
| 36 | typespec: type | object, # object for Optional[x], which is not a type. |
| 37 | default: Any, |
| 38 | help: str, |
| 39 | choices: Sequence[str] | None, |
| 40 | ) -> None: |
| 41 | typecheck.check_option_type(name, default, typespec) |
| 42 | self.name = name |
| 43 | self.typespec = typespec |
| 44 | self._default = default |
| 45 | self.value = unset |
| 46 | self.help = textwrap.dedent(help).strip().replace("\n", " ") |
| 47 | self.choices = choices |
| 48 | |
| 49 | def __repr__(self): |
| 50 | return f"{self.current()} [{self.typespec}]" |
| 51 | |
| 52 | @property |
| 53 | def default(self): |
| 54 | return copy.deepcopy(self._default) |
| 55 | |
| 56 | def current(self) -> Any: |
| 57 | if self.value is unset: |
| 58 | v = self.default |
| 59 | else: |
| 60 | v = self.value |
| 61 | return copy.deepcopy(v) |
| 62 | |
| 63 | def set(self, value: Any) -> None: |
| 64 | typecheck.check_option_type(self.name, value, self.typespec) |
| 65 | self.value = value |
| 66 | |
| 67 | def reset(self) -> None: |
| 68 | self.value = unset |
| 69 | |
| 70 | def has_changed(self) -> bool: |
| 71 | return self.current() != self.default |
| 72 | |
| 73 | def __eq__(self, other) -> bool: |
| 74 | for i in self.__slots__: |
| 75 | if getattr(self, i) != getattr(other, i): |
| 76 | return False |
| 77 | return True |
| 78 | |
| 79 | def __deepcopy__(self, _): |
| 80 | o = _Option(self.name, self.typespec, self.default, self.help, self.choices) |
| 81 | if self.has_changed(): |
| 82 | o.value = self.current() |
| 83 | return o |
| 84 | |
| 85 | |
| 86 | @dataclass |
no outgoing calls
no test coverage detected
searching dependent graphs…