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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit('0') prints NULL factor argument and NULL unit argument #240

Open
schlunma opened this issue May 6, 2022 · 3 comments
Open

Unit('0') prints NULL factor argument and NULL unit argument #240

schlunma opened this issue May 6, 2022 · 3 comments
Labels
New: Issue Highlight a new community raised "generic" issue Type: Bug

Comments

@schlunma
Copy link

schlunma commented May 6, 2022

馃悰 Bug Report

While reading lots of large netCDF files with iris, I noticed that my entire stderr is cluttered with the following prints:

ut_scale(): NULL factor argument                                                                                                                                                                                                                                             
ut_are_convertible(): NULL unit argument

These come from the use of cf_units.Unit('0') for variables with the unit '0'.

How to Reproduce

Steps to reproduce the behaviour:

Using

from cf_units import Unit
Unit('0')

correctly raises an error, but also prints the messages mentioned above:

ut_scale(): NULL factor argument
ut_are_convertible(): NULL unit argument
Traceback (most recent call last):
  File "/home/b/b309141/tmp/iris_warning.py", line 3, in <module>
    Unit('0')
  File "/work/bd0854/b309141/mambaforge/envs/esm/lib/python3.10/site-packages/cf_units/__init__.py", line 839, in __init__
    raise value_error from None
ValueError: [UT_SUCCESS] Failed to parse unit "0"

These warnings cannot be suppressed using python -W ignore or by using warnings.filterwarnings.

Expected Behaviour

These warnings should not be printed or be issued using the warnings module so they can be handled properly.

Environment

  • OS & Version: Red Hat Enterprise Linux 8.4
  • cf-units Version: 3.0.1
@schlunma schlunma added New: Issue Highlight a new community raised "generic" issue Type: Bug labels May 6, 2022
@bouweandela
Copy link
Member

bouweandela commented May 9, 2022

After looking around in the code a bit, I found that you can use the undocumented function suppress_errors to stop the warnings from being printed:

from cf_units import Unit, suppress_errors
with suppress_errors():
  Unit('0')

@rcomer
Copy link
Member

rcomer commented May 9, 2022

Looks like there are a few undocumented yet public functions in there (e.g. is_vertical, is_time). Maybe we just need to add a new section to the docs?

@bouweandela
Copy link
Member

The current default behaviour of printing to stderr if something goes wrong seems odd for a library, because there is no standard way in which users of the library can do something with that information. I agree with @schlunma that it would be better to use the warnigns module for that (or a logger).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
New: Issue Highlight a new community raised "generic" issue Type: Bug
Projects
Status: No status
Development

No branches or pull requests

3 participants