Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Type annotations: pathlib.Path vs importlib.resources.abc.Traversable

When loading file resources that are bundled inside my distribution package, I use importlib.resources.files. When loading files from disk, I use pathlib.Path.

Sometimes I want to write a function that will take either:

from importlib.resources.abc import Traversable
from pathlib import Path

def process_file(file_to_process: Traversable | Path) -> None: ...

It feels cumbersome to annotate all file-processing functions with Traversable | Path. I could define and use my own union type, but that feels like something that Python might already have built-in that I’m missing. I only require the basic subset of both types: open / close / read / write, etc, and nothing like touching permissions or using OS-specific details.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

What is the correct type to use for a file resource that can be loaded from a distribution package via importlib or from a drive via pathlib?

I’m currently using Python 3.11 but will upgrade over time, so all 3.11+ answers are welcome.

>Solution :

I believe you can just use Traversable in all cases. It looks like Traversable is a protocol, and since pathlib.Path implements the necessary methods, objects of type pathlib.Path should also be Traversable. And in fact:

>>> from pathlib import Path
>>> from importlib.resources.abc import Traversable
>>> p = Path('.')
>>> isinstance(p, Path)
True
>>> isinstance(p, Traversable)
True

So you should be able to write:

def process_file(file_to_process: Traversable) -> None: ...

I’ve checked, and mypy seems happy with the above annotation when calling process_file(Path('example.txt')).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading