StringTransformer instances have a call signature that mirrors that of the Transformer type. Raises: CannotTransform(...) if the concrete StringTransformer class is unable to transform @line.
(
self, line: Line, _features: Collection[Feature], _mode: Mode
)
| 276 | """ |
| 277 | |
| 278 | def __call__( |
| 279 | self, line: Line, _features: Collection[Feature], _mode: Mode |
| 280 | ) -> Iterator[Line]: |
| 281 | """ |
| 282 | StringTransformer instances have a call signature that mirrors that of |
| 283 | the Transformer type. |
| 284 | |
| 285 | Raises: |
| 286 | CannotTransform(...) if the concrete StringTransformer class is unable |
| 287 | to transform @line. |
| 288 | """ |
| 289 | # Optimization to avoid calling `self.do_match(...)` when the line does |
| 290 | # not contain any string. |
| 291 | if not any(leaf.type == token.STRING for leaf in line.leaves): |
| 292 | raise CannotTransform("There are no strings in this line.") |
| 293 | |
| 294 | match_result = self.do_match(line) |
| 295 | |
| 296 | if isinstance(match_result, Err): |
| 297 | cant_transform = match_result.err() |
| 298 | raise CannotTransform( |
| 299 | f"The string transformer {self.__class__.__name__} does not recognize" |
| 300 | " this line as one that it can transform." |
| 301 | ) from cant_transform |
| 302 | |
| 303 | string_indices = match_result.ok() |
| 304 | |
| 305 | for line_result in self.do_transform(line, string_indices): |
| 306 | if isinstance(line_result, Err): |
| 307 | cant_transform = line_result.err() |
| 308 | raise CannotTransform( |
| 309 | "StringTransformer failed while attempting to transform string." |
| 310 | ) from cant_transform |
| 311 | line = line_result.ok() |
| 312 | yield line |
| 313 | |
| 314 | |
| 315 | @dataclass |
nothing calls this directly
no test coverage detected