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 quiet flag for warnings #261

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Add quiet flag for warnings #261

wants to merge 3 commits into from

Conversation

nvaytet
Copy link
Member

@nvaytet nvaytet commented Jan 30, 2025

Using snx.load(filename, quiet=True) suppressed all warnings raised when loading files.

Fixes #260

group = group[root]
return group[select]
with warnings.catch_warnings():
warnings.simplefilter("ignore" if quiet else "default")
Copy link
Member

Choose a reason for hiding this comment

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

I think this resets user-defined filters. You need to not call simplefilter if not quiet. But I may be wrong. Did you check?

Copy link
Member

Choose a reason for hiding this comment

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

There are some issues with this approach:

  • It affects all warnings, even those that may not be related to file structure but, e.g., arise from dependencies.
  • It works by changing a global variable. So if this code is used in a multi-threaded context, e.g., in Sciline with Dask, the filter also affects warnings that don't arise from ScippNexus.

Those issues can be somewhat mitigated by filtering on the module where the warning is raised. But you can still get race conditions with multi-threading.

If you want to go with this approach, please add a clear docstring explaining the caveats.

Copy link
Member

Choose a reason for hiding this comment

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

Would it make sense to consider using logging instead of warnings?

Copy link
Member

Choose a reason for hiding this comment

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

Not if you want to treat warnings as errors in some contexts.

Copy link
Member Author

Choose a reason for hiding this comment

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

There are some issues with this approach

Yes, you make some good points. Do you have a suggestion for an alternative? If not, I guess filtering on the module would already be a big improvement.

Copy link
Member

Choose a reason for hiding this comment

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

Have we considered putting the filtering code into userland, i.e., not change ScippNexus?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's an option, I just thought having a flag would be the most user friendly mechanism.

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.

Add a quiet argument on load to suppress warnings?
3 participants