Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add compatibility with pathlib.Path to root_io #699

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

Victor-Schwan
Copy link

@Victor-Schwan Victor-Schwan commented Oct 23, 2024

BEGINRELEASENOTES

  • add compatibility with pathlib.Path objects in root_io and sio_io

ENDRELEASENOTES

Copy link
Collaborator

@tmadlener tmadlener left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose you stumbled over this when trying to instantiate a Reader with a pathlib.Path object and it failed?

The same issue will be present in sio_io, so it should also be fixed there.

Comment on lines 25 to 34
if isinstance(filenames, list):
str_filenames = []
for f in filenames:
if isinstance(f, (str, Path)):
str_filenames.append(str(f))
else:
raise TypeError(f"Invalid filename type: {f} (type: {type(f)})")
return str_filenames

raise TypeError(f"Invalid filenames argument: {filenames} (type: {type(filenames)})")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we not simply do

return [str(e) for e in filenames]

and the conversion will raise the TypeError for us in case it is not possible to do it?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. Do you have any objections to using os.fspath instead of str? It is better for catching wrong types, e.g. if None is passed

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If reader.openFiles swallows a list of os.fspath, I have no objections.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

os.fspath() only returns str or bytes. I am unsure what happens if a bytes path is given to reader.openFiles. fspath is just better at throwing TypeError if the argument is not pathlike compared to str(). https://docs.python.org/3/library/os.html#os.fspath

python/podio/sio_io.py Outdated Show resolved Hide resolved
python/podio/sio_io.py Outdated Show resolved Hide resolved
python/podio/root_io.py Outdated Show resolved Hide resolved
python/podio/root_io.py Outdated Show resolved Hide resolved
@@ -3,6 +3,8 @@

from ROOT import gSystem

from utils import convert_to_str_paths
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
from utils import convert_to_str_paths
from podio.utils import convert_to_str_paths

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should also go to where the rest of podio modules are imported

@@ -3,6 +3,8 @@

from ROOT import gSystem

from utils import convert_to_str_paths
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
from utils import convert_to_str_paths
from podio.utils import convert_to_str_paths

Also these should be grouped with the rest of the podio imports

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants