You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Filtering AstropyDeprecationWarning with PYTHONWARNINGS env. #9832
So apparently python only allows built-in category names from the environment variable.
However, AstropyDeprecationWarning is not a subclass of the built-in DeprecationWarning. Thus it seems like there is no way to filter on the category from the environment variable.
Maybe this is because I do not know how to use PYTHONWARNINGS environment -- any suggestions?
Hi, @rainwoodman ! To better assist you, we would need to understand your use case a little more. Are you trying to filter it out inside your package's conftest.py (or some other file inside your package) or as part of a non-package script?
That same place also gives a work-around:
"The (currently only and accepted) answer proposes as a workaround to set the PYTHONPATH variable, as the contents of those apparently are already in sys.path at the time warnings._processoptions is called, unlike site-packages. So manually adding site-packages to the PYTHONPATH variable does circumvent this problem (tested in Python 3.7.1)."
I'm not sure, though, why we are not subclassing from DeprecationWarning - we do subclass our AstropyPendingDeprecationWarning properly. Maybe @astrofrog knows?
Thanks for the link and the PYTHONPATH workaround.
More about the use case: I am running an MPI application on 2048 ranks -- the warning (due to importing astropy/halotools) buries the useful logs coming out from the application. Thus I am adding the warnings filter as an environment variable in the job script as a non-invasive patchwork till a new release of halotools arrives.
It would still be curious to know why not subclassing from DeprecationWarning. Could it be because certain versions of python suppresses DeprecationWarning by default? I had the impression they have eliminated this suppression in recent releases.
I'm not sure, though, why we are not subclassing from DeprecationWarning - we do subclass our AstropyPendingDeprecationWarning properly. Maybe @astrofrog knows?
Historically deprecation warnings were hidden by default by Python, but we wanted to make sure users would see astropy deprecation warnings by default, hence why we don't inherit from DeprecationWarning. I think for the pending deprecation warnings we were ok with those being hidden by default. See #1871 for more information.
In particular it looks like we might want to consider inheriting from (or using directly) FutureWarning since that is to be used for warnings that should always be seen by end users.
Turns out @Juanlu001 had already opened an issue about PEP 565 which got closed: #8465 - if anyone feels strongly about this feel free to comment there?
Activity
pllim commentedon Jan 3, 2020
Hi, @rainwoodman ! To better assist you, we would need to understand your use case a little more. Are you trying to filter it out inside your package's
conftest.py(or some other file inside your package) or as part of a non-package script?saimn commentedon Jan 4, 2020
It should be possible to use something like
python -W ignore::astropy.utils.exceptions.AstropyDeprecationWarningor maybepython -W ignore::AstropyDeprecationWarning:stropy.utils.exceptions:but there is a Python bug that prevents this :https://bugs.python.org/issue22543
https://stackoverflow.com/questions/42495641/filtering-based-on-custom-warning-categories
It seems that the import fails because it is done before site-packages are enabled.
mhvk commentedon Jan 4, 2020
That same place also gives a work-around:
"The (currently only and accepted) answer proposes as a workaround to set the PYTHONPATH variable, as the contents of those apparently are already in sys.path at the time warnings._processoptions is called, unlike site-packages. So manually adding site-packages to the PYTHONPATH variable does circumvent this problem (tested in Python 3.7.1)."
I'm not sure, though, why we are not subclassing from
DeprecationWarning- we do subclass ourAstropyPendingDeprecationWarningproperly. Maybe @astrofrog knows?rainwoodman commentedon Jan 5, 2020
Thanks for the link and the PYTHONPATH workaround.
More about the use case: I am running an MPI application on 2048 ranks -- the warning (due to importing astropy/halotools) buries the useful logs coming out from the application. Thus I am adding the warnings filter as an environment variable in the job script as a non-invasive patchwork till a new release of halotools arrives.
It would still be curious to know why not subclassing from DeprecationWarning. Could it be because certain versions of python suppresses DeprecationWarning by default? I had the impression they have eliminated this suppression in recent releases.
astrofrog commentedon Jan 5, 2020
Historically deprecation warnings were hidden by default by Python, but we wanted to make sure users would see astropy deprecation warnings by default, hence why we don't inherit from
DeprecationWarning. I think for the pending deprecation warnings we were ok with those being hidden by default. See #1871 for more information.pllim commentedon Jan 5, 2020
@rainwoodman , if the workaround works for you until the next release of
halotools, maybe we can close this?astrofrog commentedon Jan 5, 2020
Note that we could always revise things once we support Python 3.7+, since things changed a bit there:
https://docs.python.org/3/whatsnew/3.7.html#whatsnew37-pep565
In particular it looks like we might want to consider inheriting from (or using directly)
FutureWarningsince that is to be used for warnings that should always be seen by end users.pllim commentedon Jan 5, 2020
@astrofrog , your suggestion at #9832 (comment) should be its own issue, no?
astrofrog commentedon Jan 5, 2020
@pllim - indeed, will do
mhvk commentedon Jan 5, 2020
👍 to following PEP 565!
rainwoodman commentedon Jan 6, 2020
Yes the work around works for me. I will close this issue. Thanks!
astrofrog commentedon Jan 6, 2020
Turns out @Juanlu001 had already opened an issue about PEP 565 which got closed: #8465 - if anyone feels strongly about this feel free to comment there?
pllim commentedon Jan 7, 2020
@astrofrog , #8465 mentioned specifically
python < 3.7, but what you said above was forpython >= 3.7. I am now confused.astrofrog commentedon Jan 7, 2020
I'm confused too now so I need to go back and check PEP 565 :)