Concrete implementation for leaf nodes.
| 365 | |
| 366 | |
| 367 | class Leaf(Base): |
| 368 | """Concrete implementation for leaf nodes.""" |
| 369 | |
| 370 | # Default values for instance variables |
| 371 | value: str |
| 372 | fixers_applied: list[Any] |
| 373 | bracket_depth: int |
| 374 | # Changed later in brackets.py |
| 375 | opening_bracket: Optional["Leaf"] = None |
| 376 | used_names: set[str] | None |
| 377 | _prefix = "" # Whitespace and comments preceding this token in the input |
| 378 | lineno: int = 0 # Line where this token starts in the input |
| 379 | column: int = 0 # Column where this token starts in the input |
| 380 | # If not None, this Leaf is created by converting a block of fmt off/skip |
| 381 | # code, and `fmt_pass_converted_first_leaf` points to the first Leaf in the |
| 382 | # converted code. |
| 383 | fmt_pass_converted_first_leaf: Optional["Leaf"] = None |
| 384 | |
| 385 | def __init__( |
| 386 | self, |
| 387 | type: int, |
| 388 | value: str, |
| 389 | context: Context | None = None, |
| 390 | prefix: str | None = None, |
| 391 | fixers_applied: list[Any] = [], |
| 392 | opening_bracket: Optional["Leaf"] = None, |
| 393 | fmt_pass_converted_first_leaf: Optional["Leaf"] = None, |
| 394 | ) -> None: |
| 395 | """ |
| 396 | Initializer. |
| 397 | |
| 398 | Takes a type constant (a token number < 256), a string value, and an |
| 399 | optional context keyword argument. |
| 400 | """ |
| 401 | |
| 402 | assert 0 <= type < 256, type |
| 403 | if context is not None: |
| 404 | self._prefix, (self.lineno, self.column) = context |
| 405 | self.type = type |
| 406 | self.value = value |
| 407 | if prefix is not None: |
| 408 | self._prefix = prefix |
| 409 | self.fixers_applied: list[Any] | None = fixers_applied[:] |
| 410 | self.children = [] |
| 411 | self.opening_bracket = opening_bracket |
| 412 | self.fmt_pass_converted_first_leaf = fmt_pass_converted_first_leaf |
| 413 | |
| 414 | def __repr__(self) -> str: |
| 415 | """Return a canonical string representation.""" |
| 416 | from .pgen2.token import tok_name |
| 417 | |
| 418 | assert self.type is not None |
| 419 | return ( |
| 420 | f"{self.__class__.__name__}({tok_name.get(self.type, self.type)}," |
| 421 | f" {self.value!r})" |
| 422 | ) |
| 423 | |
| 424 | def __str__(self) -> str: |
no outgoing calls
no test coverage detected