state.py 824 B

12345678910111213141516171819202122232425262728
  1. from __future__ import annotations
  2. from contextlib import contextmanager
  3. from typing import Final, Iterator
  4. # These are global mutable state. Don't add anything here unless there's a very
  5. # good reason.
  6. class StrictOptionalState:
  7. # Wrap this in a class since it's faster that using a module-level attribute.
  8. def __init__(self, strict_optional: bool) -> None:
  9. # Value varies by file being processed
  10. self.strict_optional = strict_optional
  11. @contextmanager
  12. def strict_optional_set(self, value: bool) -> Iterator[None]:
  13. saved = self.strict_optional
  14. self.strict_optional = value
  15. try:
  16. yield
  17. finally:
  18. self.strict_optional = saved
  19. state: Final = StrictOptionalState(strict_optional=False)
  20. find_occurrences: tuple[str, str] | None = None