state.py 853 B

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