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 python readers #699

Merged
merged 9 commits into from
Nov 8, 2024

Conversation

Victor-Schwan
Copy link
Contributor

@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
Contributor 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
Contributor 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
python/podio/root_io.py Outdated Show resolved Hide resolved
python/podio/sio_io.py Outdated Show resolved Hide resolved
@tmadlener tmadlener merged commit d1bb7b0 into AIDASoft:master Nov 8, 2024
18 checks passed
@tmadlener tmadlener changed the title add compatibility with pathlib.Path to root_io add compatibility with pathlib.Path to python readers Nov 8, 2024
@Victor-Schwan Victor-Schwan deleted the pr_path_compati branch November 15, 2024 09:32
jmcarcell pushed a commit to jmcarcell/podio that referenced this pull request Nov 18, 2024
* add compatibility with pathlib.Path to root_io

* outsource TypeError handling to os.fspath

* add pathlib compatibility in sio_io as well

* sio_io: only store first path in list returned by convert_to_str_paths

* make ruff import sorting happy

* outsource convert_to_str_paths

* Fix imports to be absolute

* Add module docstring to fix pylint complaints
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