PaNOSC-ViNYL / SimEx

Start-to-end photon experiment simulation platform
https://simex.readthedocs.io/
GNU General Public License v3.0
26 stars 25 forks source link

Importing IOUtilities unintentionally causes a warnings.simplefilter("all") globally. #249

Open godot11 opened 1 year ago

godot11 commented 1 year ago

In SimEx.Utilities.IOUtilities, there's an import

from wpg.converters.genesis_v2 import read_genesis_file as genesis2

that silently causes ALL warnings ever, globally, to be supressed after this line. Probably there's a warnings.simplefilter("ignore") line in WPG somewhere. In fact, it's more severe: anything that imports wpg or (probably) any part of it is affected.

This sounds like a serious unintended side effect. It will happen to anything that imports SimEx.Utilities.IOUtilities, and anything that imports the code that imports it, and so on.

If WPG is not under our control, a possible fix would be to add warnings.simplefilter("ignore") after every place in SimEx WPG is imported. This may also cause troubles however, as it also overrides any intentional warnings filter applied beforehand.

MRE of bug and fix:

import warnings
warnings.warn("First warning before import")

import wpg
# from wpg.converters.genesis_v2 import read_genesis_file as genesis2  # causes the same behaviour
# from SimEx.Utilities.IOUtilities import loadPDB  # causes the same behaviour

warnings.warn("Second warning after import")

warnings.simplefilter("default")
warnings.warn("Third warning after filter reset")

This will print

pdb_wtf.py:2: UserWarning: First warning before import
  warnings.warn("First warning before import")
initializing ocelot...
pdb_wtf.py:9: UserWarning: Third warning after filter reset
  warnings.warn("Third warning after filter reset")