Skip to content

Fire can't find custom __str__ of ~datasets.Dataset from HuggingFace #595

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

Open
nerdai opened this issue Apr 7, 2025 · 1 comment
Open

Comments

@nerdai
Copy link

nerdai commented Apr 7, 2025

Hi,

I am using fire on a main function that returns a ~datasets.Dataset object. When running from cli, I get the help screen for ~datasets.Dataset rather than the __str__ implementation (likely inherited from a parent class -- haven't looked).

Reproducible example

from datasets import Dataset
import fire

def main():
    # Create a simple dataset
    dataset = Dataset.from_dict({"a": [1, 2, 3], "b": [4, 5, 6]})
    return dataset

if __name__ == "__main__":
    fire.Fire(main)
    
    # This works but I feel shouldn't be necessary:
    # def custom_serializer(obj: object) -> str:
    #     if hasattr(obj, "__str__"):
    #         return str(obj)
    #     else:
    #         return ""
    # fire.Fire(main, serialize=custom_serializer)
@dbieber
Copy link
Collaborator

dbieber commented May 21, 2025

Thanks for reporting. Agreed it would be better behavior for it to call __str__ than to show help.

The reason this happens is: HasCustomStr(dataset) == False

def HasCustomStr(component):

I think (haven't double checked) this is because Dataset overrides __repr__ but not __str__.

Related: #446

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

No branches or pull requests

2 participants