from typing import Optional
import pathlib


def _get_reach_dir() -> Optional[pathlib.Path]:
  """Get the reach directory using .reach files."""
  test_directories = []
  cwd: pathlib.Path = pathlib.Path(".").absolute()
  test_directories.append(cwd)
  test_directories.extend(cwd.parents)
  test_directories.extend(pathlib.Path(__file__).parents)
  for test_dir in test_directories:
    test_file: pathlib.Path = test_dir / ".reach"
    if test_file.is_file():
      return test_dir
  return None