question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

warning control "once"

See original GitHub issue

While ignoring warnings works well, I can’t make the warning appear only once (as they actually should do as default behaviour)

I have a (potentially corrupt) fits file that outputs tons of warnings like: WARNING: The following header keyword is invalid or follows an unrecognized non-standard convention: ??????????????????????????????????????????????????????????????????????????????? [astropy.io.fits.card] , even if I explicitly set the warning filter to “once”, they keep coming…

Is this wanted behaviour? If so, why, and can we not change it, or at least allow control over it?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:14 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
Laubeeecommented, Oct 21, 2020

Sure thing. The fits is available in our public archive

then just:

from astropy.io import fits
h = fits.getheader("INPE_20120103_113000_58.fit.gz")

and you should see all the warnings.

Then using the linked example about warning control, suppressing it with ignore works, but “once” does the same as “default”…

warnings.filterwarnings('once', category=AstropyUserWarning, append=True)

the python docs state:

default: print the first occurrence of matching warnings for each location (module + line number) where the warning is issued

as far as I can tell all those warnings come from the exact same module+line number so IMO only one warning should be printed with default settings…?

Tested on Win10 with astropy4.0, python 3.7.2

0reactions
saimncommented, Feb 20, 2021

Finally had a more detailed look, and that’s Python’s warnings behavior:

In [1]: import warnings

In [2]: warnings.warn('hello')
<ipython-input-2-bba56b013a54>:1: UserWarning: hello
  warnings.warn('hello')

In [3]: warnings.warn('hello')
<ipython-input-3-bba56b013a54>:1: UserWarning: hello
  warnings.warn('hello')

In [4]: for i in range(5):
   ...:     warnings.warn('hello')
   ...: 
<ipython-input-4-3e5aa4640a88>:2: UserWarning: hello
  warnings.warn('hello')

In [5]: for i in range(5):
   ...:     with warnings.catch_warnings():
   ...:         pass
   ...:     warnings.warn('hello')
   ...: 
<ipython-input-5-4df48fc8ac58>:4: UserWarning: hello
  warnings.warn('hello')
<ipython-input-5-4df48fc8ac58>:4: UserWarning: hello
  warnings.warn('hello')
<ipython-input-5-4df48fc8ac58>:4: UserWarning: hello
  warnings.warn('hello')
<ipython-input-5-4df48fc8ac58>:4: UserWarning: hello
  warnings.warn('hello')
<ipython-input-5-4df48fc8ac58>:4: UserWarning: hello
  warnings.warn('hello')

There is some buffering when calling warnings.warn in a loop, but this is not the case if you call warnings.catch_warnings in the loop iterations. So this is not an Astropy issue, closing this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

warnings — Warning control — Python 3.11.1 documentation
There are two stages in warning control: first, each time a warning is issued, a determination is made whether a message should be...
Read more >
Python Warning control - Stack Overflow
You can e.g. call warnings.filterwarnings("once") somewhere before the warnings are raised. However, there is no way to show the same warning ...
Read more >
Warning Control Statements :: Error Handling (Programming)
Use query to determine the current state of all warnings. It reports that you have set all warnings to off with the exception...
Read more >
3.20 warnings -- Warning control
There are two stages in warning control: first, each time a warning is issued, a determination is made whether a message should be...
Read more >
Warning Options (Using the GNU Compiler Collection (GCC))
The warning message for each controllable warning includes the option that controls the warning. That option can then be used with -Werror= and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found