def _open_file(filepath: Path, encoding: str) -> Iterator[TextIO]:
    """
    Open a file and return a file object, optionally handling bz2 or gz files.

    Uses a wrapper context manager to yield the file object to ensure the file
    will always get closed when the caller is finished with it.

    Parameters
    ----------
    filepath
        Path to file.
    encoding
        The file's character encoding.

    Returns
    -------
    file
        The file handle.
    """
    if filepath.suffix == ".bz2":
        with bz2.open(filepath, mode="rt", encoding=encoding) as file:
            yield file
    elif filepath.suffix == ".gz":
        with gzip.open(filepath, mode="rt", encoding=encoding) as file:
            yield file
    else:
        with filepath.open(mode="rt", encoding=encoding) as file:
            yield file

    Open a file and return a file object, optionally handling bz2 or gz files.

    Uses a wrapper context manager to yield the file object to ensure the file
    will always get closed when the caller is finished with it.

    Parameters
    ----------
    filepath
        Path to file.
    encoding
        The file's character encoding.

    Returns
    -------
    file
        The file handle.
    
osmnx/_osm_xml.py