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

Refactor string_to_dict to return None if there is no match instead of raising ValueError #7435

Merged

Conversation

ringohoffman
Copy link
Contributor

Making this change, as encouraged here:

instead of having the pattern of using try-except to handle when there is no match, we can instead check if the return value is None; we can also assert that the return value should not be None if we know that should be true

…f raising ValueError

instead of having the pattern of using try-except to handle when there is no match, we can instead check if the return value is None; we can also assert that the return value should not be None if we know that should be true
if repo_split not in repo_splits:
repo_splits.append(split)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This looked like a mistake

elif value.get("bytes") is not None or value.get("path") is not None:
# store the video bytes, and path is used to infer the video format using the file extension
return {"bytes": value.get("bytes"), "path": value.get("path")}
elif isinstance(value, dict):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added this indentation because pyright isn't able to narrow the negative of the VideoReader case bc of the VideoReader is not None.

Comment on lines -144 to +161
path, bytes_ = value["path"], value["bytes"]
if isinstance(value, str):
path, bytes_ = value, None
else:
path, bytes_ = value["path"], value["bytes"]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The docstring said that value could just be the str path, but that case was not handled.

I took the liberty of creating the Example TypedDict to try to improve the typing in this class.

@ringohoffman
Copy link
Contributor Author

cc: @lhoestq

@ringohoffman
Copy link
Contributor Author

I am going to rebase #7434 onto this branch. Then we can merge this one first if you approve, and then #7434.

@ringohoffman
Copy link
Contributor Author

@lhoestq any thoughts here?

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
@ringohoffman
Copy link
Contributor Author

ringohoffman commented Mar 7, 2025

It looks like I was unsafely asserting that source_url_fields is not None in image.py, video.py and audio.py (which did not correspond to the except ValueError like was there previously). I've changed it to handle source_url_fields is None.

@ringohoffman
Copy link
Contributor Author

Can we re-run CI on this one?

@ringohoffman
Copy link
Contributor Author

Sweet! These failures are looking spurious due to connectivity issues. Can the failing run be retried?

@ringohoffman
Copy link
Contributor Author

@lhoestq Sorry to double ping, but can this PR be reviewed? I think it is ready!

Copy link
Member

@lhoestq lhoestq left a comment

Choose a reason for hiding this comment

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

great ! lgtm :)

@lhoestq lhoestq merged commit 67ffdfb into huggingface:main Mar 12, 2025
13 of 14 checks passed
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.

None yet

3 participants